Skip to content

Commit e95d24c

Browse files
committed
[GR-14706] Sulong: check target triple of bitcode modules.
PullRequest: graal/8385
2 parents 5a1d413 + f24e516 commit e95d24c

File tree

37 files changed

+312
-133
lines changed

37 files changed

+312
-133
lines changed

sulong/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Version 21.1.0
2+
3+
New features:
4+
5+
* Added a sanity check to verify that loaded bitcode files have been compiled correctly.
6+
If not, a _mismatching target triple_ error is reported. To make this error non-fatal,
7+
set `--llvm.verifyBitcode=false`. To silence the message, set `--log.llvm.BitcodeVerifier.level=OFF`.
8+
19
# Version 21.0.0
210

311
Fixes:

sulong/mx.sulong/mx_sulong.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def extract_bitcode(args=None, out=None):
256256
return mx.run_java(mx.get_runtime_jvm_args(["com.oracle.truffle.llvm.tools"]) + ["com.oracle.truffle.llvm.tools.ExtractBitcode"] + args, out=out)
257257

258258

259-
@mx.command(_suite.name, "lli-dis")
259+
@mx.command(_suite.name, "llvm-dis")
260260
def llvm_dis(args=None, out=None):
261261
"""Disassemble (embedded) LLVM bitcode to LLVM assembly"""
262262
parser = ArgumentParser(prog='mx llvm-dis', description='Disassemble (embedded) LLVM bitcode to LLVM assembly.')

sulong/mx.sulong/suite.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@
777777
],
778778
"buildDependencies" : [
779779
"SULONG_HOME",
780+
"LINUX_AMD64_SUPPORT",
780781
],
781782
"testProject" : True,
782783
"defaultBuild" : False,
@@ -863,6 +864,9 @@
863864
"dependencies" : [
864865
"SULONG_TEST",
865866
],
867+
"buildDependencies" : [
868+
"LINUX_AMD64_SUPPORT",
869+
],
866870
"testProject" : True,
867871
"defaultBuild" : False,
868872
},
@@ -886,6 +890,9 @@
886890
"dependencies" : [
887891
"SULONG_TEST",
888892
],
893+
"buildDependencies" : [
894+
"LINUX_AMD64_SUPPORT",
895+
],
889896
"testProject" : True,
890897
"defaultBuild" : False,
891898
},
@@ -903,6 +910,9 @@
903910
"dependencies" : [
904911
"SULONG_TEST",
905912
],
913+
"buildDependencies" : [
914+
"LINUX_AMD64_SUPPORT",
915+
],
906916
"testProject" : True,
907917
"defaultBuild" : False,
908918
},
@@ -922,7 +932,6 @@
922932
"SULONG_TEST",
923933
],
924934
"buildDependencies" : [
925-
# "AMD64_SUPPORT" currently not working on darwin GR-21946,
926935
"LINUX_AMD64_SUPPORT",
927936
],
928937
"testProject" : True,

sulong/projects/com.oracle.truffle.llvm.parser/src/com/oracle/truffle/llvm/parser/LLVMParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.oracle.truffle.llvm.parser.model.symbols.constants.CastConstant;
3939
import com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalAlias;
4040
import com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalVariable;
41+
import com.oracle.truffle.llvm.parser.model.target.TargetTriple;
4142
import com.oracle.truffle.llvm.runtime.LLVMAlias;
4243
import com.oracle.truffle.llvm.runtime.LLVMFunction;
4344
import com.oracle.truffle.llvm.runtime.LLVMFunctionCode.Function;
@@ -71,7 +72,7 @@ public LLVMParserResult parse(ModelModule module, DataLayout targetDataLayout) {
7172
defineFunctions(module, definedFunctions, externalFunctions, targetDataLayout);
7273
defineAliases(module.getAliases());
7374

74-
return new LLVMParserResult(runtime, definedFunctions, externalFunctions, definedGlobals, externalGlobals, targetDataLayout);
75+
return new LLVMParserResult(runtime, definedFunctions, externalFunctions, definedGlobals, externalGlobals, targetDataLayout, module.getTargetInformation(TargetTriple.class));
7576
}
7677

7778
private void defineGlobals(List<GlobalVariable> globals, List<GlobalVariable> definedGlobals, List<GlobalVariable> externalGlobals) {

sulong/projects/com.oracle.truffle.llvm.parser/src/com/oracle/truffle/llvm/parser/LLVMParserResult.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020, Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2021, Oracle and/or its affiliates.
33
*
44
* All rights reserved.
55
*
@@ -31,6 +31,7 @@
3131

3232
import com.oracle.truffle.llvm.parser.model.functions.FunctionSymbol;
3333
import com.oracle.truffle.llvm.parser.model.symbols.globals.GlobalVariable;
34+
import com.oracle.truffle.llvm.parser.model.target.TargetTriple;
3435
import com.oracle.truffle.llvm.runtime.datalayout.DataLayout;
3536

3637
import java.util.List;
@@ -44,19 +45,22 @@ public final class LLVMParserResult {
4445
private final List<GlobalVariable> externalGlobals;
4546
private final DataLayout dataLayout;
4647
private final int symbolTableSize;
48+
private final TargetTriple targetTriple;
4749

4850
LLVMParserResult(LLVMParserRuntime runtime,
4951
List<FunctionSymbol> definedFunctions,
5052
List<FunctionSymbol> externalFunctions,
5153
List<GlobalVariable> definedGlobals,
5254
List<GlobalVariable> externalGlobals,
53-
DataLayout dataLayout) {
55+
DataLayout dataLayout,
56+
TargetTriple targetTriple) {
5457
this.runtime = runtime;
5558
this.definedFunctions = definedFunctions;
5659
this.externalFunctions = externalFunctions;
5760
this.definedGlobals = definedGlobals;
5861
this.externalGlobals = externalGlobals;
5962
this.dataLayout = dataLayout;
63+
this.targetTriple = targetTriple;
6064
this.symbolTableSize = definedFunctions.size() + externalFunctions.size() + definedGlobals.size() + externalGlobals.size();
6165
}
6266

@@ -92,4 +96,8 @@ public String toString() {
9296
public int getSymbolTableSize() {
9397
return symbolTableSize;
9498
}
99+
100+
public TargetTriple getTargetTriple() {
101+
return targetTriple;
102+
}
95103
}

sulong/projects/com.oracle.truffle.llvm.parser/src/com/oracle/truffle/llvm/parser/model/ModelModule.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
2+
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
33
*
44
* All rights reserved.
55
*
@@ -153,4 +153,13 @@ public void addSourceFileReference(LLVMSourceFileReference sourceFile) {
153153
public ArrayList<LLVMSourceFileReference> getSourceFileReferences() {
154154
return sourceFiles;
155155
}
156+
157+
public <TI extends TargetInformation> TI getTargetInformation(Class<TI> targetInfoClazz) {
158+
for (TargetInformation info : targetInfo) {
159+
if (targetInfoClazz.isInstance(info)) {
160+
return targetInfoClazz.cast(info);
161+
}
162+
}
163+
return null;
164+
}
156165
}

sulong/projects/com.oracle.truffle.llvm.parser/src/com/oracle/truffle/llvm/parser/model/target/TargetTriple.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
33
*
44
* All rights reserved.
55
*
@@ -37,7 +37,8 @@ public TargetTriple(String triple) {
3737
this.triple = triple;
3838
}
3939

40-
public String getTriple() {
40+
@Override
41+
public String toString() {
4142
return triple;
4243
}
4344
}

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/LLVMContext.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import com.oracle.truffle.api.source.Source;
6060
import com.oracle.truffle.llvm.api.Toolchain;
6161
import com.oracle.truffle.llvm.runtime.LLVMArgumentBuffer.LLVMArgumentArray;
62-
import com.oracle.truffle.llvm.runtime.datalayout.DataLayout;
6362
import com.oracle.truffle.llvm.runtime.debug.LLVMSourceContext;
6463
import com.oracle.truffle.llvm.runtime.except.LLVMIllegalSymbolIndexException;
6564
import com.oracle.truffle.llvm.runtime.except.LLVMLinkerException;
@@ -151,8 +150,6 @@ public final class LLVMContext {
151150
protected boolean initialized;
152151
protected boolean cleanupNecessary;
153152
private boolean initializeContextCalled;
154-
private DataLayout libsulongDatalayout;
155-
private Boolean datalayoutInitialised;
156153
private final LLVMLanguage language;
157154

158155
private LLVMTracerInstrument tracer; // effectively final after initialization
@@ -176,8 +173,6 @@ synchronized LLVMFunctionDescriptor create(LLVMFunction functionDetail, LLVMFunc
176173
@SuppressWarnings({"unchecked", "rawtypes"})
177174
LLVMContext(LLVMLanguage language, Env env, Toolchain toolchain) {
178175
this.language = language;
179-
this.libsulongDatalayout = null;
180-
this.datalayoutInitialised = false;
181176
this.env = env;
182177
this.initialized = false;
183178
this.cleanupNecessary = false;
@@ -205,7 +200,7 @@ synchronized LLVMFunctionDescriptor create(LLVMFunction functionDetail, LLVMFunc
205200

206201
addLibraryPaths(SulongEngineOption.getPolyglotOptionSearchPaths(env));
207202

208-
pThreadContext = new LLVMPThreadContext(getEnv(), getLanguage(), getLibsulongDataLayout());
203+
pThreadContext = new LLVMPThreadContext(getEnv(), getLanguage(), language.getDefaultDataLayout());
209204

210205
symbolAssumptions = new Assumption[10][];
211206
// These two fields contain the same value, but have different CompilationFinal annotations:
@@ -420,20 +415,6 @@ private static LLVMManagedPointer toManagedPointer(Object value) {
420415
return LLVMManagedPointer.create(value);
421416
}
422417

423-
public void addLibsulongDataLayout(DataLayout datalayout) {
424-
// Libsulong datalayout can only be set once.
425-
if (!datalayoutInitialised) {
426-
this.libsulongDatalayout = datalayout;
427-
datalayoutInitialised = true;
428-
} else {
429-
throw new NullPointerException("The default datalayout cannot be overrwitten");
430-
}
431-
}
432-
433-
public DataLayout getLibsulongDataLayout() {
434-
return libsulongDatalayout;
435-
}
436-
437418
void finalizeContext(LLVMFunction sulongDisposeContext) {
438419
// join all created pthread - threads
439420
pThreadContext.joinAllThreads();
@@ -565,6 +546,10 @@ public boolean isInternalLibraryFile(TruffleFile file) {
565546
return file.normalize().startsWith(internalLibraryPathFile);
566547
}
567548

549+
public boolean isInternalLibraryPath(Path path) {
550+
return path.normalize().startsWith(internalLibraryPath);
551+
}
552+
568553
public TruffleFile getOrAddTruffleFile(TruffleFile file) {
569554
synchronized (truffleFilesLock) {
570555
int index = truffleFiles.indexOf(file);

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/LLVMLanguage.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.truffle.llvm.runtime.config.Configuration;
5252
import com.oracle.truffle.llvm.runtime.config.Configurations;
5353
import com.oracle.truffle.llvm.runtime.config.LLVMCapability;
54+
import com.oracle.truffle.llvm.runtime.datalayout.DataLayout;
5455
import com.oracle.truffle.llvm.runtime.debug.LLDBSupport;
5556
import com.oracle.truffle.llvm.runtime.debug.debugexpr.nodes.DebugExprExecutableNode;
5657
import com.oracle.truffle.llvm.runtime.debug.debugexpr.parser.DebugExprException;
@@ -62,6 +63,7 @@
6263
import com.oracle.truffle.llvm.runtime.memory.LLVMMemoryOpNode;
6364
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMStatementNode;
6465
import com.oracle.truffle.llvm.runtime.pointer.LLVMPointer;
66+
import com.oracle.truffle.llvm.runtime.target.TargetTriple;
6567
import com.oracle.truffle.llvm.toolchain.config.LLVMConfig;
6668
import org.graalvm.collections.EconomicMap;
6769
import org.graalvm.options.OptionDescriptors;
@@ -144,6 +146,9 @@ private <U extends ContextExtension> ContextExtensionKey<U> cast(Class<U> target
144146

145147
private final ConcurrentHashMap<Class<?>, RootCallTarget> cachedCallTargets = new ConcurrentHashMap<>();
146148

149+
private DataLayout defaultDataLayout;
150+
private TargetTriple defaultTargetTriple;
151+
147152
@CompilationFinal private LLVMFunctionCode sulongInitContextCode;
148153
@CompilationFinal private LLVMFunction sulongDisposeContext;
149154
@CompilationFinal private LLVMFunctionCode startFunctionCode;
@@ -442,6 +447,29 @@ public CallTarget getFreeGlobalBlocks() {
442447
return freeGlobalBlocks;
443448
}
444449

450+
public synchronized void setDefaultBitcode(DataLayout datalayout, TargetTriple targetTriple) {
451+
// Libsulong datalayout can only be set once.
452+
if (defaultDataLayout == null) {
453+
this.defaultDataLayout = datalayout;
454+
} else {
455+
throw new NullPointerException("The default datalayout cannot be overwritten");
456+
}
457+
// Libsulong targettriple can only be set once.
458+
if (defaultTargetTriple == null) {
459+
this.defaultTargetTriple = targetTriple;
460+
} else {
461+
throw new NullPointerException("The default targetTriple cannot be overwritten");
462+
}
463+
}
464+
465+
public DataLayout getDefaultDataLayout() {
466+
return defaultDataLayout;
467+
}
468+
469+
public TargetTriple getDefaultTargetTriple() {
470+
return defaultTargetTriple;
471+
}
472+
445473
public AtomicInteger getRawRunnerID() {
446474
return nextID;
447475
}

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/debug/debugexpr/nodes/DebugExprSizeofNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020, Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2021, Oracle and/or its affiliates.
33
*
44
* All rights reserved.
55
*
@@ -43,7 +43,7 @@ public abstract class DebugExprSizeofNode extends LLVMExpressionNode {
4343
private final long typeSize;
4444

4545
public static DebugExprSizeofNode create(DebugExprType type) throws TypeOverflowException {
46-
DataLayout datalayout = LLVMLanguage.getContext().getLibsulongDataLayout();
46+
DataLayout datalayout = LLVMLanguage.getLanguage().getDefaultDataLayout();
4747
return DebugExprSizeofNodeGen.create(type.getLLVMRuntimeType().getSize(datalayout));
4848
}
4949

0 commit comments

Comments
 (0)