Skip to content

Improve dynamic linking browser tests. NFC #14448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion tests/browser_main.cpp → tests/browser_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 0 additions & 5 deletions tests/browser_module.cpp → tests/browser_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@

int state = 0;

extern "C" {

void one() {
state++;
}

int two() {
return state;
}

}

68 changes: 35 additions & 33 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
#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 <stdio.h>
#include <assert.h>
#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
Expand Down Expand Up @@ -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': ([],),
Expand All @@ -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 <dlfcn.h>
#include <stdio.h>
Expand All @@ -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))
Expand Down Expand Up @@ -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'])

Expand Down Expand Up @@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -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 <stdlib.h>
#include <string.h>
char *side(const char *data);
Expand All @@ -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.
Expand Down Expand Up @@ -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 <stdio.h>
#include <string.h>
#include <assert.h>
Expand All @@ -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() {
Expand All @@ -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 <assert.h>
int side1();
int side2();
int main() {
return side1() + side2();
assert(side1() == 1);
assert(side2() == 2);
return 0;
}
''')
create_file('side1.c', r'''
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down