Skip to content

Commit cf17cbb

Browse files
ccawley2011lephilousophe
authored andcommitted
Emscripten WIP
1 parent 883d8c6 commit cf17cbb

File tree

4 files changed

+351
-3
lines changed

4 files changed

+351
-3
lines changed

toolchains/emscripten/Dockerfile.m4

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ helpers_package(libmad, --with-pic --enable-fpm=no)
4343

4444
RUN emcc -s USE_OGG=1 -E - < /dev/null
4545

46-
RUN emcc -s USE_VORBIS=1 -E - < /dev/null
46+
# Emscripten has an official port for vorbis, but it doesn't properly link vorbisfile https://github.com/emscripten-core/emscripten/pull/14005
47+
# RUN emcc -s USE_VORBIS=1 -E - < /dev/null
48+
helpers_package(libvorbis, , CFLAGS="-fPIC -s USE_OGG=1")
4749

48-
helpers_package(libtheora, --disable-asm, CFLAGS="-fPIC -s USE_OGG=1 -s USE_VORBIS=1")
50+
# helpers_package(libtheora, --disable-asm, CFLAGS="-fPIC -s USE_OGG=1 -s USE_VORBIS=1")
51+
helpers_package(libtheora, --disable-asm, CFLAGS="-fPIC -s USE_OGG=1")
4952

5053
# TODO: flac
5154

toolchains/emscripten/packages/toolchain/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /bin/sh
22

3-
EMSDK_VERSION=3.1.1
3+
EMSDK_VERSION=3.1.6
44

55
PACKAGE_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
66
HELPERS_DIR=$PACKAGE_DIR/../..
Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
From af4322618a2a6fd0474ff4d52e2e4f39b796c154 Mon Sep 17 00:00:00 2001
2+
From: kamenokonokotan <kamenokonokotan@gmail.com>
3+
Date: Thu, 6 Jan 2022 14:55:48 +0900
4+
Subject: [PATCH 1/9] import mutable globals used in Asyncify pass
5+
6+
---
7+
emcc.py | 9 +++++++++
8+
emscripten.py | 1 +
9+
src/library.js | 6 ++++++
10+
3 files changed, 16 insertions(+)
11+
12+
diff --git a/emcc.py b/emcc.py
13+
index fa3076eb8e0..2e3c816e7ef 100755
14+
--- a/emcc.py
15+
+++ b/emcc.py
16+
@@ -559,6 +559,8 @@ def get_binaryen_passes():
17+
passes += ['--fpcast-emu']
18+
if settings.ASYNCIFY:
19+
passes += ['--asyncify']
20+
+ if settings.MAIN_MODULE or settings.SIDE_MODULE:
21+
+ passes += ['--pass-arg=asyncify-side-module']
22+
if settings.ASSERTIONS:
23+
passes += ['--pass-arg=asyncify-asserts']
24+
if settings.ASYNCIFY_ADVISE:
25+
@@ -1771,6 +1773,13 @@ def phase_linker_setup(options, state, newargs, user_settings):
26+
'__heap_base',
27+
'__stack_pointer',
28+
]
29+
+
30+
+ if settings.ASYNCIFY:
31+
+ settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += [
32+
+ '__asyncify_state',
33+
+ '__asyncify_data'
34+
+ ]
35+
+
36+
# Unconditional dependency in library_dylink.js
37+
settings.REQUIRED_EXPORTS += ['setThrew']
38+
39+
diff --git a/emscripten.py b/emscripten.py
40+
index b19c0f97eb9..beabb6fca3b 100644
41+
--- a/emscripten.py
42+
+++ b/emscripten.py
43+
@@ -344,6 +344,7 @@ def emscript(in_wasm, out_wasm, outfile_js, memfile):
44+
45+
if settings.ASYNCIFY:
46+
exports += ['asyncify_start_unwind', 'asyncify_stop_unwind', 'asyncify_start_rewind', 'asyncify_stop_rewind']
47+
+ metadata['globalImports'] += ['__asyncify_state', '__asyncify_data']
48+
49+
report_missing_symbols(forwarded_json['libraryFunctions'])
50+
51+
diff --git a/src/library.js b/src/library.js
52+
index b1947fc69d7..dcb54aae883 100644
53+
--- a/src/library.js
54+
+++ b/src/library.js
55+
@@ -3619,6 +3619,12 @@ LibraryManager.library = {
56+
__c_longjmp: "new WebAssembly.Tag({'parameters': ['{{{ POINTER_TYPE }}}']})",
57+
__c_longjmp_import: true,
58+
#endif
59+
+#if ASYNCIFY
60+
+ __asyncify_state: "new WebAssembly.Global({'value': 'i32', 'mutable': true}, 0)",
61+
+ __asyncify_state__import: true,
62+
+ __asyncify_data: "new WebAssembly.Global({'value': 'i32', 'mutable': true}, 0)",
63+
+ __asyncify_data__import: true,
64+
+#endif
65+
#endif
66+
};
67+
68+
69+
From aeb0ce56b8a778b75182d2b3791ffa63b2080f44 Mon Sep 17 00:00:00 2001
70+
From: nokotan <kamenokonokotan@gmail.com>
71+
Date: Sun, 23 Jan 2022 21:54:03 +0900
72+
Subject: [PATCH 2/9] move globals metadata modification
73+
74+
---
75+
emscripten.py | 4 +++-
76+
1 file changed, 3 insertions(+), 1 deletion(-)
77+
78+
diff --git a/emscripten.py b/emscripten.py
79+
index beabb6fca3b..271213ad728 100644
80+
--- a/emscripten.py
81+
+++ b/emscripten.py
82+
@@ -324,6 +324,9 @@ def emscript(in_wasm, out_wasm, outfile_js, memfile):
83+
if settings.INITIAL_TABLE == -1:
84+
settings.INITIAL_TABLE = dylink_sec.table_size + 1
85+
86+
+ if settings.ASYNCIFY:
87+
+ metadata['globalImports'] += ['__asyncify_state', '__asyncify_data']
88+
+
89+
glue, forwarded_data = compile_settings()
90+
if DEBUG:
91+
logger.debug(' emscript: glue took %s seconds' % (time.time() - t))
92+
@@ -344,7 +347,6 @@ def emscript(in_wasm, out_wasm, outfile_js, memfile):
93+
94+
if settings.ASYNCIFY:
95+
exports += ['asyncify_start_unwind', 'asyncify_stop_unwind', 'asyncify_start_rewind', 'asyncify_stop_rewind']
96+
- metadata['globalImports'] += ['__asyncify_state', '__asyncify_data']
97+
98+
report_missing_symbols(forwarded_json['libraryFunctions'])
99+
100+
101+
From 1010d46660efb3322580ee6628412f5a78a7c6b8 Mon Sep 17 00:00:00 2001
102+
From: kamenokonokotan <kamenokonokotan@gmail.com>
103+
Date: Tue, 25 Jan 2022 22:00:21 +0900
104+
Subject: [PATCH 3/9] Remove redundant spaces
105+
106+
---
107+
emcc.py | 2 +-
108+
1 file changed, 1 insertion(+), 1 deletion(-)
109+
110+
diff --git a/emcc.py b/emcc.py
111+
index 2e3c816e7ef..45b1eba4319 100755
112+
--- a/emcc.py
113+
+++ b/emcc.py
114+
@@ -1779,7 +1779,7 @@ def phase_linker_setup(options, state, newargs, user_settings):
115+
'__asyncify_state',
116+
'__asyncify_data'
117+
]
118+
-
119+
+
120+
# Unconditional dependency in library_dylink.js
121+
settings.REQUIRED_EXPORTS += ['setThrew']
122+
123+
124+
From fe54d139855728e6c52956c05b231b4ae19c4f67 Mon Sep 17 00:00:00 2001
125+
From: kamenokonokotan <kamenokonokotan@gmail.com>
126+
Date: Tue, 25 Jan 2022 22:10:36 +0900
127+
Subject: [PATCH 4/9] Add test_asyncify_side_module
128+
129+
---
130+
tests/test_core.py | 28 ++++++++++++++++++++++++++++
131+
1 file changed, 28 insertions(+)
132+
133+
diff --git a/tests/test_core.py b/tests/test_core.py
134+
index ca8ad60f9f8..f6033f83fe7 100644
135+
--- a/tests/test_core.py
136+
+++ b/tests/test_core.py
137+
@@ -7870,6 +7870,34 @@ def test_asyncify_indirect_lists(self, args, should_pass):
138+
if should_pass:
139+
raise
140+
141+
+ @needs_dylink
142+
+ @no_memory64('TODO: asyncify for wasm64')
143+
+ def test_asyncify_side_module(self):
144+
+ self.set_setting('ASYNCIFY')
145+
+ self.emcc_args += ['-sASYNCIFY_IMPORTS=["_Z8my_sleepi"]']
146+
+ self.dylink_test(r'''
147+
+ #include <stdio.h>
148+
+ #include "header.h"
149+
+
150+
+ int main() {
151+
+ my_sleep(1);
152+
+ return 0;
153+
+ }
154+
+ ''', r'''
155+
+ #include <emscripten.h>
156+
+ #include <stdio.h>
157+
+ #include "header.h"
158+
+
159+
+ void my_sleep(int milli_seconds) {
160+
+ // put variable onto stack
161+
+ volatile int value = 42;
162+
+ printf("%d ", value);
163+
+ emscripten_sleep(milli_seconds);
164+
+ // variable on stack in side module function should be restored.
165+
+ printf("%d\n", value);
166+
+ }
167+
+ ''', '42 42', header='void my_sleep(int);')
168+
+
169+
@no_asan('asyncify stack operations confuse asan')
170+
@no_memory64('TODO: asyncify for wasm64')
171+
def test_emscripten_scan_registers(self):
172+
173+
From afba627a0c6a1666b8d20aa2cbb08657b6694692 Mon Sep 17 00:00:00 2001
174+
From: kamenokonokotan <kamenokonokotan@gmail.com>
175+
Date: Wed, 26 Jan 2022 01:29:14 +0900
176+
Subject: [PATCH 5/9] flake8, add EXIT_RUNTIME
177+
178+
---
179+
tests/test_core.py | 3 ++-
180+
1 file changed, 2 insertions(+), 1 deletion(-)
181+
182+
diff --git a/tests/test_core.py b/tests/test_core.py
183+
index f6033f83fe7..d7f5cead083 100644
184+
--- a/tests/test_core.py
185+
+++ b/tests/test_core.py
186+
@@ -7874,13 +7874,14 @@ def test_asyncify_indirect_lists(self, args, should_pass):
187+
@no_memory64('TODO: asyncify for wasm64')
188+
def test_asyncify_side_module(self):
189+
self.set_setting('ASYNCIFY')
190+
+ self.set_setting('EXIT_RUNTIME', 1)
191+
self.emcc_args += ['-sASYNCIFY_IMPORTS=["_Z8my_sleepi"]']
192+
self.dylink_test(r'''
193+
#include <stdio.h>
194+
#include "header.h"
195+
196+
int main() {
197+
- my_sleep(1);
198+
+ my_sleep(1);
199+
return 0;
200+
}
201+
''', r'''
202+
203+
From 1af80c0d84b794bb5fc807f788524066c06422a0 Mon Sep 17 00:00:00 2001
204+
From: kamenokonokotan <kamenokonokotan@gmail.com>
205+
Date: Sun, 30 Jan 2022 02:39:01 +0900
206+
Subject: [PATCH 6/9] add instrumentWasmExports
207+
208+
---
209+
src/library_dylink.js | 3 +++
210+
1 file changed, 3 insertions(+)
211+
212+
diff --git a/src/library_dylink.js b/src/library_dylink.js
213+
index 8ed16752912..9d1c69ec768 100644
214+
--- a/src/library_dylink.js
215+
+++ b/src/library_dylink.js
216+
@@ -570,6 +570,9 @@ var LibraryDylink = {
217+
// add new entries to functionsInTableMap
218+
updateTableMap(tableBase, metadata.tableSize);
219+
moduleExports = relocateExports(instance.exports, memoryBase);
220+
+#if ASYNCIFY
221+
+ moduleExports = Asyncify.instrumentWasmExports(moduleExports);
222+
+#endif
223+
if (!flags.allowUndefined) {
224+
reportUndefinedSymbols();
225+
}
226+
227+
From d5a90f3deec595fd6f0de0d16caf6183b20ad6c3 Mon Sep 17 00:00:00 2001
228+
From: kamenokonokotan <kamenokonokotan@gmail.com>
229+
Date: Sun, 30 Jan 2022 02:39:56 +0900
230+
Subject: [PATCH 7/9] add searched symbols in getDataRewindFunc
231+
232+
---
233+
src/library_async.js | 5 +++++
234+
1 file changed, 5 insertions(+)
235+
236+
diff --git a/src/library_async.js b/src/library_async.js
237+
index da09a1ae2d8..0ceb072dd37 100644
238+
--- a/src/library_async.js
239+
+++ b/src/library_async.js
240+
@@ -205,6 +205,11 @@ mergeInto(LibraryManager.library, {
241+
var id = {{{ makeGetValue('ptr', C_STRUCTS.asyncify_data_s.rewind_id, 'i32') }}};
242+
var name = Asyncify.callStackIdToName[id];
243+
var func = Module['asm'][name];
244+
+#if RELOCATABLE
245+
+ if (!func) {
246+
+ func = Module[asmjsMangle(name)];
247+
+ }
248+
+#endif
249+
return func;
250+
},
251+
252+
253+
From 23f45459f660f307012c725449eec1240c389a4c Mon Sep 17 00:00:00 2001
254+
From: =?UTF-8?q?Christian=20K=C3=BCndig?= <christian@kuendig.info>
255+
Date: Fri, 18 Feb 2022 18:15:53 +0100
256+
Subject: [PATCH 8/9] Fixing dlsym for emscripten-core/emscripten#15893
257+
258+
---
259+
src/library_dylink.js | 6 ++++++
260+
1 file changed, 6 insertions(+)
261+
262+
diff --git a/src/library_dylink.js b/src/library_dylink.js
263+
index 9d1c69ec768..e2ed388a1fc 100644
264+
--- a/src/library_dylink.js
265+
+++ b/src/library_dylink.js
266+
@@ -962,6 +962,12 @@ var LibraryDylink = {
267+
#if DYLINK_DEBUG
268+
err('dlsym: ' + symbol + ' getting table slot for: ' + result);
269+
#endif
270+
+
271+
+#if ASYNCIFY
272+
+ if(symbol in GOT && GOT[symbol].value != 0) {
273+
+ return GOT[symbol].value
274+
+ }
275+
+#endif
276+
// Insert the function into the wasm table. If its a direct wasm function
277+
// the second argument will not be needed. If its a JS function we rely
278+
// on the `sig` attribute being set based on the `<func>__sig` specified
279+
280+
From de6021f93f6be8c76b5d546ab85b50de8dd63cae Mon Sep 17 00:00:00 2001
281+
From: kamenokonokotan <kamenokonokotan@gmail.com>
282+
Date: Sun, 6 Mar 2022 00:05:00 +0900
283+
Subject: [PATCH 9/9] Add test case test_asyncify_dlfcn
284+
285+
---
286+
tests/test_core.py | 33 +++++++++++++++++++++++++++++++++
287+
1 file changed, 33 insertions(+)
288+
289+
diff --git a/tests/test_core.py b/tests/test_core.py
290+
index d7f5cead083..e21072d143e 100644
291+
--- a/tests/test_core.py
292+
+++ b/tests/test_core.py
293+
@@ -7899,6 +7899,39 @@ def test_asyncify_side_module(self):
294+
}
295+
''', '42 42', header='void my_sleep(int);')
296+
297+
+ @needs_dylink
298+
+ @no_memory64('TODO: asyncify for wasm64')
299+
+ def test_asyncify_dlfcn(self):
300+
+ self.set_setting('ASYNCIFY')
301+
+ self.set_setting('EXIT_RUNTIME', 1)
302+
+ self.emcc_args += ['-sASYNCIFY_IGNORE_INDIRECT=0']
303+
+ self.dylink_test(r'''
304+
+ #include <iostream>
305+
+ #include <dlfcn.h>
306+
+
307+
+ typedef int (*func_t)();
308+
+
309+
+ int main(int argc, char **argv)
310+
+ {
311+
+ void *_dlHandle = dlopen("liblib.so", RTLD_NOW | RTLD_LOCAL);
312+
+ func_t my_func = (func_t)dlsym(_dlHandle, "side_module_run");
313+
+ printf("%d\n", my_func());
314+
+ return 0;
315+
+ }
316+
+ ''', r'''
317+
+ #include <iostream>
318+
+ #include <emscripten/emscripten.h>
319+
+
320+
+ extern "C"
321+
+ {
322+
+ int side_module_run()
323+
+ {
324+
+ emscripten_sleep(1000);
325+
+ return 42;
326+
+ }
327+
+ }
328+
+ ''', '42', need_reverse=False)
329+
+
330+
@no_asan('asyncify stack operations confuse asan')
331+
@no_memory64('TODO: asyncify for wasm64')
332+
def test_emscripten_scan_registers(self):
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--- a/tools/ports/sdl2.py 2022-03-18 17:48:16.000000000 +0100
2+
+++ b/tools/ports/sdl2.py 2022-03-18 17:49:49.000000000 +0100
3+
@@ -5,8 +5,8 @@
4+
5+
import os
6+
7+
-TAG = 'release-2.0.20'
8+
-HASH = '67e1abe1183b04836b35d724fd495c83c9559b4530d4a5c9bcc89648af0ac7cc51c02f7055a1664fe5f5f90953d22a6c431fa8bc5cdd77c94a97f107c47e2d62'
9+
+TAG = 'a485ffc3c8d040fc87893b61cd8cddda4248243f'
10+
+HASH = 'c9014265ad3215edc08414dec1034c10f07f9092ce52b8cb0d0122d470b4565fc09ce9f9e8629417d553d3571384133ba30ee4ba018fb01bccb5b7a0d9b61617'
11+
SUBDIR = 'SDL-' + TAG
12+
13+

0 commit comments

Comments
 (0)