Skip to content

Commit 76d313a

Browse files
committed
Avoid calling main method of GraalWasm C tests
Newer versions of Emscripten unconditionally call exit at the end of main. In our test harness, however, we want to run the test repeatedly to test compilation.
1 parent b59996c commit 76d313a

File tree

10 files changed

+52
-9
lines changed

10 files changed

+52
-9
lines changed

wasm/mx.wasm/mx_wasm.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,17 @@ def __str__(self):
445445
def benchmark_methods(self):
446446
return benchmark_methods
447447

448+
def test_methods(self, opts_path):
449+
if not os.path.isfile(opts_path):
450+
return []
451+
with open(opts_path) as opts_file:
452+
for line in opts_file:
453+
line = line.strip()
454+
if line.startswith("entry-point"):
455+
_, value = line.split("=", 1)
456+
return ['_' + value.strip()]
457+
return []
458+
448459
def build(self):
449460
source_dir = self.subject.getSourceDir()
450461
output_dir = self.subject.getOutputDir()
@@ -469,8 +480,6 @@ def build(self):
469480
if hasattr(self.project, "includeset"):
470481
include_flags = ["-I", os.path.join(_suite.dir, "includes", self.project.includeset)]
471482
emcc_flags = ["-s", "STANDALONE_WASM", "-s", "WASM_BIGINT"] + cc_flags
472-
if self.project.isBenchmarkProject():
473-
emcc_flags = emcc_flags + ["-s", "EXPORTED_FUNCTIONS=" + str(self.benchmark_methods()).replace("'", "\"") + ""]
474483
subdir_program_names = defaultdict(lambda: [])
475484
for root, filename in self.subject.getProgramSources():
476485
if filename.startswith("_"):
@@ -502,9 +511,13 @@ def build(self):
502511
# Step 1: build the .wasm binary.
503512
if mustRebuild:
504513
if filename.endswith(".c"):
514+
if self.project.isBenchmarkProject():
515+
emcc_export_flags = ["-s", "EXPORTED_FUNCTIONS=" + str(self.benchmark_methods()).replace("'", "\"") + ""]
516+
else:
517+
emcc_export_flags = ["-s", "EXPORTED_FUNCTIONS=" + str(self.test_methods(os.path.join(root, basename + ".opts"))).replace("'", "\"") + ""]
505518
# This generates both a js file and a wasm file.
506519
# See https://github.com/emscripten-core/emscripten/wiki/WebAssembly-Standalone
507-
build_cmd_line = [emcc_cmd] + emcc_flags + source_cc_flags + [source_path, "-o", output_js_path] + include_flags
520+
build_cmd_line = [emcc_cmd] + emcc_flags + emcc_export_flags + source_cc_flags + [source_path, "-o", output_js_path] + include_flags
508521
if mx.run(build_cmd_line, nonZeroIsFatal=False) != 0:
509522
mx.abort("Could not build the wasm-only output of " + filename + " with emcc.")
510523
elif filename.endswith(".wat"):

wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmFileSuite.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,21 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
216216
}
217217

218218
final WasmContext wasmContext = WasmContext.get(null);
219-
final Value mainFunction = findMain(moduleInstances);
220219
final List<WasmInstance> instanceList = moduleInstances.stream().map(i -> toWasmInstance(i)).toList();
221220

221+
final Value testFunction;
222+
final String entryPoint = testCase.options().getProperty("entry-point");
223+
if (entryPoint != null) {
224+
String testModuleName = testCase.name();
225+
Value testModule = context.getBindings(WasmLanguage.ID).getMember(testModuleName).getMember("exports");
226+
testFunction = testModule.getMember(entryPoint);
227+
if (testFunction == null) {
228+
throw new RuntimeException(String.format("Entry point %s not found in test module %s.", entryPoint, testCase.name()));
229+
}
230+
} else {
231+
testFunction = findMain(moduleInstances);
232+
}
233+
222234
resetStatus(System.out, phaseIcon, phaseLabel);
223235

224236
final String argString = testCase.options().getProperty("argument");
@@ -228,7 +240,7 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
228240
for (int i = 0; i != iterations; ++i) {
229241
try {
230242
testOut.reset();
231-
final Value result = arg == null ? mainFunction.execute() : mainFunction.execute(arg);
243+
final Value result = arg == null ? testFunction.execute() : testFunction.execute(arg);
232244
WasmCase.validateResult(testCase.data().resultValidator(), result, testOut);
233245
} catch (PolyglotException e) {
234246
// If no exception is expected and the program returns with success exit status,

wasm/src/org.graalvm.wasm.testcases/src/test/c/allocate-string.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#include <stdlib.h>
4343
#include <string.h>
4444

45-
int main() {
45+
int test() {
4646
char staticMemory[100];
4747
char *dynamicMemory;
4848

@@ -59,3 +59,7 @@ int main() {
5959
printf("2nd: %s\n", dynamicMemory);
6060
return 0;
6161
}
62+
63+
int main() {
64+
return test();
65+
}

wasm/src/org.graalvm.wasm.testcases/src/test/c/allocate-string.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ interpreter-iterations = 2
33
sync-noinline-iterations = 0
44
sync-inline-iterations = 0
55
async-iterations = 2000
6+
entry-point = test

wasm/src/org.graalvm.wasm.testcases/src/test/c/collatz.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ int collatz(int n) {
5656

5757
int number = 127;
5858

59-
int main() {
59+
int test() {
6060
printf("%d\n", collatz(number));
6161
return 0;
6262
}
6363

64+
int main() {
65+
return test();
66+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
entry-point = test

wasm/src/org.graalvm.wasm.testcases/src/test/c/floyd.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
#include <stdio.h>
4343

44-
int main() {
44+
int test() {
4545
int number = 1;
4646
int rows = 10;
4747
for (int i = 1; i <= rows; i++) {
@@ -53,3 +53,7 @@ int main() {
5353
}
5454
return 0;
5555
}
56+
57+
int main() {
58+
return test();
59+
}

wasm/src/org.graalvm.wasm.testcases/src/test/c/floyd.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ interpreter-iterations = 4
33
sync-noinline-iterations = 0
44
sync-inline-iterations = 0
55
async-iterations = 500
6+
entry-point = test

wasm/src/org.graalvm.wasm.testcases/src/test/c/hello.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
*/
4141
#include <stdio.h>
4242

43-
int main() {
43+
int test() {
4444
printf("Hello world!\n");
4545
return 0;
4646
}
4747

48+
int main() {
49+
return test();
50+
}

wasm/src/org.graalvm.wasm.testcases/src/test/c/hello.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ interpreter-iterations = 3
33
sync-noinline-iterations = 2
44
sync-inline-iterations = 2
55
async-iterations = 1250
6+
entry-point = test

0 commit comments

Comments
 (0)