From 3aeb9f0d791292d2e55baf0e0e08cdb6d7103dbc Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 14 Jun 2021 13:31:47 -0700 Subject: [PATCH] Improve dynamic linking browser tests. NFC - Remove unnecessary use of `EXPORT_ALL` - Use C over C++ where there is no actual C++ in use. - Use `MAIN_MODULE=2` where possible (smaller, faster builds) - Re-enable disabled test. --- .circleci/config.yml | 8 ++- tests/{browser_main.cpp => browser_main.c} | 3 +- .../{browser_module.cpp => browser_module.c} | 5 -- tests/test_browser.py | 68 ++++++++++--------- 4 files changed, 43 insertions(+), 41 deletions(-) rename tests/{browser_main.cpp => browser_main.c} (96%) rename tests/{browser_module.cpp => browser_module.c} (94%) diff --git a/.circleci/config.yml b/.circleci/config.yml index 536fb4c35b963..535ab7e933453 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -196,6 +196,8 @@ commands: # browser.test_webgl_offscreen_canvas_in_mainthread_after_pthread # are crashing Firefox (bugzil.la/1281796). The former case is # further blocked by issue #6897. + # browser.test_sdl2_misc_main_module: Requires PIC version of libSDL + # which is not included in emsdk (not compatible with FROZEN_CACHE). name: run tests environment: GALLIUM_DRIVER: softpipe # TODO: use the default llvmpipe when it supports more extensions @@ -212,7 +214,7 @@ commands: echo "-----" echo "Running browser tests" echo "-----" - tests/runner browser skip:browser.test_sdl2_mouse skip:browser.test_html5_webgl_create_context skip:browser.test_webgl_offscreen_canvas_in_pthread skip:browser.test_webgl_offscreen_canvas_in_mainthread_after_pthread skip:browser.test_glut_glutget + tests/runner browser skip:browser.test_sdl2_mouse skip:browser.test_html5_webgl_create_context skip:browser.test_webgl_offscreen_canvas_in_pthread skip:browser.test_webgl_offscreen_canvas_in_mainthread_after_pthread skip:browser.test_glut_glutget skip:browser.test_sdl2_misc_main_module # posix and emrun suites are disabled because firefox errors on # "Firefox is already running, but is not responding." # TODO: find out a way to shut down and restart firefox @@ -249,7 +251,9 @@ commands: command: | export EMTEST_BROWSER="/usr/bin/google-chrome $CHROME_FLAGS_BASE $CHROME_FLAGS_HEADLESS $CHROME_FLAGS_WASM $CHROME_FLAGS_NOCACHE" # skip test_zzz_zzz_4gb_fail as it OOMs on the current bot - tests/runner browser posixtest_browser.test_pthread_create_1_1 skip:browser.test_zzz_zzz_4gb_fail + # browser.test_sdl2_misc_main_module: Requires PIC version of libSDL + # which is not included in emsdk (not compatible with FROZEN_CACHE). + tests/runner browser posixtest_browser.test_pthread_create_1_1 skip:browser.test_zzz_zzz_4gb_fail skip:browser.test_sdl2_misc_main_module tests/runner emrun test-sockets-chrome: description: "Runs emscripten sockets tests under chrome" diff --git a/tests/browser_main.cpp b/tests/browser_main.c similarity index 96% rename from tests/browser_main.cpp rename to tests/browser_main.c index 9b71fdc86edb9..8e852749671e1 100644 --- a/tests/browser_main.cpp +++ b/tests/browser_main.c @@ -37,7 +37,8 @@ void next(const char *x) { assert(twofunc() == 7); onefunc(); int result = twofunc(); - exit(result); + assert(result == 8); + exit(0); } int main() { diff --git a/tests/browser_module.cpp b/tests/browser_module.c similarity index 94% rename from tests/browser_module.cpp rename to tests/browser_module.c index f5fa013320bba..9320efc4f57c1 100644 --- a/tests/browser_module.cpp +++ b/tests/browser_module.c @@ -5,8 +5,6 @@ int state = 0; -extern "C" { - void one() { state++; } @@ -14,6 +12,3 @@ void one() { int two() { return state; } - -} - diff --git a/tests/test_browser.py b/tests/test_browser.py index c00eede827688..0b99c52b58de0 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -2221,50 +2221,52 @@ def test_openal_capture_sanity(self): def test_runtimelink(self): create_file('header.h', r''' - struct point - { + struct point { int x, y; }; ''') - create_file('supp.cpp', r''' + create_file('supp.c', r''' #include #include "header.h" extern void mainFunc(int x); extern int mainInt; - void suppFunc(struct point &p) { - printf("supp: %d,%d\n", p.x, p.y); - mainFunc(p.x + p.y); + void suppFunc(struct point *p) { + printf("supp: %d,%d\n", p->x, p->y); + mainFunc(p->x + p->y); printf("supp see: %d\n", mainInt); } int suppInt = 76; ''') - create_file('main.cpp', r''' + create_file('main.c', r''' #include + #include #include "header.h" - extern void suppFunc(struct point &p); + extern void suppFunc(struct point *p); extern int suppInt; void mainFunc(int x) { printf("main: %d\n", x); + assert(x == 56); } int mainInt = 543; int main( int argc, const char *argv[] ) { struct point p = { 54, 2 }; - suppFunc(p); + suppFunc(&p); printf("main see: %d\nok.\n", suppInt); - return suppInt; + assert(suppInt == 76); + return 0; } ''') - self.run_process([EMCC, 'supp.cpp', '-o', 'supp.wasm', '-s', 'SIDE_MODULE', '-O2', '-s', 'EXPORT_ALL']) - self.btest_exit('main.cpp', args=['-DBROWSER=1', '-s', 'MAIN_MODULE', '-O2', 'supp.wasm', '-s', 'EXPORT_ALL'], assert_returncode=76) + self.run_process([EMCC, 'supp.c', '-o', 'supp.wasm', '-s', 'SIDE_MODULE', '-O2']) + self.btest_exit('main.c', args=['-s', 'MAIN_MODULE=2', '-O2', 'supp.wasm']) def test_pre_run_deps(self): # Adding a dependency in preRun will delay run @@ -2458,8 +2460,8 @@ def test_emscripten_async_wget2_data(self): time.sleep(10) def test_emscripten_async_wget_side_module(self): - self.run_process([EMCC, test_file('browser_module.cpp'), '-o', 'lib.wasm', '-O2', '-s', 'SIDE_MODULE', '-s', 'EXPORTED_FUNCTIONS=_one,_two']) - self.btest_exit('browser_main.cpp', args=['-O2', '-s', 'MAIN_MODULE'], assert_returncode=8) + self.run_process([EMCC, test_file('browser_module.c'), '-o', 'lib.wasm', '-O2', '-s', 'SIDE_MODULE']) + self.btest_exit('browser_main.c', args=['-O2', '-s', 'MAIN_MODULE=2']) @parameterized({ 'non-lz4': ([],), @@ -2472,7 +2474,7 @@ def test_preload_module(self, args): return 42; } ''') - self.run_process([EMCC, 'library.c', '-s', 'SIDE_MODULE', '-O2', '-o', 'library.so', '-s', 'EXPORT_ALL']) + self.run_process([EMCC, 'library.c', '-s', 'SIDE_MODULE', '-O2', '-o', 'library.so']) create_file('main.c', r''' #include #include @@ -2498,7 +2500,7 @@ def test_preload_module(self, args): ''') self.btest_exit( 'main.c', - args=['-s', 'MAIN_MODULE', '--preload-file', '.@/', '-O2', '--use-preload-plugins', '-s', 'EXPORT_ALL'] + args) + args=['-s', 'MAIN_MODULE=2', '--preload-file', '.@/', '-O2', '--use-preload-plugins'] + args) def test_mmap_file(self): create_file('data.dat', 'data from the file ' + ('.' * 9000)) @@ -3215,7 +3217,6 @@ def test_sdl2_custom_cursor(self): def test_sdl2_misc(self): self.btest_exit('sdl2_misc.c', args=['-s', 'USE_SDL=2']) - @disabled('https://github.com/emscripten-core/emscripten/issues/13101') def test_sdl2_misc_main_module(self): self.btest_exit('sdl2_misc.c', args=['-s', 'USE_SDL=2', '-s', 'MAIN_MODULE']) @@ -3479,7 +3480,7 @@ def test_webidl(self): @requires_sync_compilation def test_dynamic_link(self): - create_file('main.cpp', r''' + create_file('main.c', r''' #include #include #include @@ -3499,11 +3500,10 @@ def test_dynamic_link(self): }); puts(ret); EM_ASM({ assert(Module.printed === 'hello through side', ['expected', Module.printed]); }); - REPORT_RESULT(2); return 0; } ''') - create_file('side.cpp', r''' + create_file('side.c', r''' #include #include char *side(const char *data); @@ -3513,18 +3513,18 @@ def test_dynamic_link(self): return ret; } ''') - self.run_process([EMCC, 'side.cpp', '-s', 'SIDE_MODULE', '-O2', '-o', 'side.wasm', '-s', 'EXPORT_ALL']) - self.btest(self.in_dir('main.cpp'), '2', args=['-s', 'MAIN_MODULE', '-O2', '-s', 'EXPORT_ALL', 'side.wasm']) + self.run_process([EMCC, 'side.c', '-s', 'SIDE_MODULE', '-O2', '-o', 'side.wasm']) + self.btest_exit(self.in_dir('main.c'), args=['-s', 'MAIN_MODULE=2', '-O2', 'side.wasm']) print('wasm in worker (we can read binary data synchronously there)') - self.run_process([EMCC, 'side.cpp', '-s', 'SIDE_MODULE', '-O2', '-o', 'side.wasm', '-s', 'EXPORT_ALL']) - self.btest(self.in_dir('main.cpp'), '2', args=['-s', 'MAIN_MODULE', '-O2', '--proxy-to-worker', '-s', 'EXPORT_ALL', 'side.wasm']) + self.run_process([EMCC, 'side.c', '-s', 'SIDE_MODULE', '-O2', '-o', 'side.wasm']) + self.btest_exit(self.in_dir('main.c'), args=['-s', 'MAIN_MODULE=2', '-O2', '--proxy-to-worker', 'side.wasm']) print('wasm (will auto-preload since no sync binary reading)') # same wasm side module works - self.btest(self.in_dir('main.cpp'), '2', args=['-s', 'MAIN_MODULE', '-O2', '-s', 'EXPORT_ALL', 'side.wasm']) + self.btest_exit(self.in_dir('main.c'), args=['-s', 'MAIN_MODULE=2', '-O2', '-s', 'EXPORT_ALL', 'side.wasm']) # verify that dynamic linking works in all kinds of in-browser environments. # don't mix different kinds in a single test. @@ -3573,7 +3573,7 @@ def do_run(src, expected_output, emcc_args=[]): @requires_graphics_hardware @requires_sync_compilation def test_dynamic_link_glemu(self): - create_file('main.cpp', r''' + create_file('main.c', r''' #include #include #include @@ -3586,7 +3586,7 @@ def test_dynamic_link_glemu(self): return 0; } ''') - create_file('side.cpp', r''' + create_file('side.c', r''' #include "SDL/SDL.h" #include "SDL/SDL_opengl.h" const char *side() { @@ -3595,17 +3595,20 @@ def test_dynamic_link_glemu(self): return (const char *)glGetString(GL_EXTENSIONS); } ''') - self.run_process([EMCC, 'side.cpp', '-s', 'SIDE_MODULE', '-O2', '-o', 'side.wasm', '-lSDL', '-s', 'EXPORT_ALL']) + self.run_process([EMCC, 'side.c', '-s', 'SIDE_MODULE', '-O2', '-o', 'side.wasm', '-lSDL']) - self.btest(self.in_dir('main.cpp'), '1', args=['-s', 'MAIN_MODULE', '-O2', '-s', 'LEGACY_GL_EMULATION', '-lSDL', '-lGL', '-s', 'EXPORT_ALL', 'side.wasm']) + self.btest(self.in_dir('main.c'), '1', args=['-s', 'MAIN_MODULE=2', '-O2', '-s', 'LEGACY_GL_EMULATION', '-lSDL', '-lGL', 'side.wasm']) def test_dynamic_link_many(self): # test asynchronously loading two side modules during startup create_file('main.c', r''' + #include int side1(); int side2(); int main() { - return side1() + side2(); + assert(side1() == 1); + assert(side2() == 2); + return 0; } ''') create_file('side1.c', r''' @@ -3616,8 +3619,7 @@ def test_dynamic_link_many(self): ''') self.run_process([EMCC, 'side1.c', '-s', 'SIDE_MODULE', '-o', 'side1.wasm']) self.run_process([EMCC, 'side2.c', '-s', 'SIDE_MODULE', '-o', 'side2.wasm']) - self.btest_exit(self.in_dir('main.c'), assert_returncode=3, - args=['-s', 'MAIN_MODULE', 'side1.wasm', 'side2.wasm']) + self.btest_exit(self.in_dir('main.c'), args=['-s', 'MAIN_MODULE=2', 'side1.wasm', 'side2.wasm']) def test_dynamic_link_pthread_many(self): # Test asynchronously loading two side modules during startup @@ -3661,7 +3663,7 @@ def test_dynamic_link_pthread_many(self): self.run_process([EMCC, 'side1.cpp', '-Wno-experimental', '-pthread', '-s', 'SIDE_MODULE', '-o', 'side1.wasm']) self.run_process([EMCC, 'side2.cpp', '-Wno-experimental', '-pthread', '-s', 'SIDE_MODULE', '-o', 'side2.wasm']) self.btest(self.in_dir('main.cpp'), '1', - args=['-Wno-experimental', '-pthread', '-s', 'MAIN_MODULE', 'side1.wasm', 'side2.wasm']) + args=['-Wno-experimental', '-pthread', '-s', 'MAIN_MODULE=2', 'side1.wasm', 'side2.wasm']) def test_memory_growth_during_startup(self): create_file('data.dat', 'X' * (30 * 1024 * 1024))