Skip to content

Commit 856b5d2

Browse files
committed
Enable growable shared array buffers
Currently this is still behind a flag so we never turn it on by default. Fixes: #24287
1 parent d3432e7 commit 856b5d2

File tree

9 files changed

+46
-9
lines changed

9 files changed

+46
-9
lines changed

.circleci/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ commands:
320320
CHROME_FLAGS_BASE: "--no-first-run -start-maximized --no-sandbox --use-gl=swiftshader --user-data-dir=/tmp/chrome-emscripten-profile --enable-experimental-web-platform-features --enable-features=JavaScriptSourcePhaseImports"
321321
# Increase the window size to avoid flaky sdl tests see #24236.
322322
CHROME_FLAGS_HEADLESS: "--headless=new --window-size=1024,768 --remote-debugging-port=1234"
323-
CHROME_FLAGS_WASM: "--enable-experimental-webassembly-features --js-flags=\"--experimental-wasm-stack-switching --experimental-wasm-type-reflection\""
323+
CHROME_FLAGS_WASM: "--enable-experimental-webassembly-features --js-flags=\"--experimental-wasm-stack-switching --experimental-wasm-type-reflection --experimental-wasm-rab-integration\""
324324
CHROME_FLAGS_NOCACHE: "--disk-cache-dir=/dev/null --disk-cache-size=1 --media-cache-size=1 --disable-application-cache --incognito"
325325
# The runners lack sound hardware so fallback to a dummy device (and
326326
# bypass the user gesture so audio tests work without interaction)
@@ -820,6 +820,7 @@ jobs:
820820
other.test_gen_struct_info
821821
other.test_native_call_before_init
822822
other.test_node_unhandled_rejection
823+
other.test_pthread_growth_growable_arraybuffers
823824
core2.test_hello_world
824825
core2.test_esm_integration*
825826
core0.test_esm_integration*
@@ -1023,6 +1024,7 @@ jobs:
10231024
skip:browser.test_webgl_offscreen_canvas_in_pthread
10241025
skip:browser.test_webgl_offscreen_canvas_in_mainthread_after_pthread
10251026
skip:browser.test_glut_glutget
1027+
skip:browser.test_*_growable_arraybuffers
10261028
"
10271029
test-browser-firefox-wasm64:
10281030
executor: focal

src/lib/libcore.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ addToLibrary({
182182
try {
183183
// round size grow request up to wasm page size (fixed 64KB per spec)
184184
wasmMemory.grow({{{ toIndexType('pages') }}}); // .grow() takes a delta compared to the previous size
185+
#if !GROWABLE_ARRAYBUFFERS
185186
updateMemoryViews();
187+
#endif
186188
#if MEMORYPROFILER
187189
if (typeof emscriptenMemoryProfiler != 'undefined') {
188190
emscriptenMemoryProfiler.onMemoryResize(oldHeapSize, wasmMemory.buffer.byteLength);
@@ -320,6 +322,7 @@ addToLibrary({
320322
#endif // ALLOW_MEMORY_GROWTH
321323
},
322324

325+
#if !GROWABLE_ARRAYBUFFERS
323326
// Called after wasm grows memory. At that time we need to update the views.
324327
// Without this notification, we'd need to check the buffer in JS every time
325328
// we return from any wasm, which adds overhead. See
@@ -330,6 +333,7 @@ addToLibrary({
330333
#endif
331334
updateMemoryViews();
332335
},
336+
#endif
333337

334338
_emscripten_system: (command) => {
335339
#if ENVIRONMENT_MAY_BE_NODE

src/runtime_common.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "runtime_safe_heap.js"
1313
#endif
1414

15-
#if SHARED_MEMORY && ALLOW_MEMORY_GROWTH
15+
#if SHARED_MEMORY && ALLOW_MEMORY_GROWTH && !GROWABLE_ARRAYBUFFERS
1616
#include "growableHeap.js"
1717
#endif
1818

@@ -132,7 +132,11 @@ var runtimeExited = false;
132132
}}}
133133

134134
function updateMemoryViews() {
135+
#if GROWABLE_ARRAYBUFFERS
136+
var b = wasmMemory.toResizableBuffer();
137+
#else
135138
var b = wasmMemory.buffer;
139+
#endif
136140
{{{ maybeExportHeap('HEAP8') }}}HEAP8 = new Int8Array(b);
137141
{{{ maybeExportHeap('HEAP16') }}}HEAP16 = new Int16Array(b);
138142
{{{ maybeExportHeap('HEAPU8') }}}HEAPU8 = new Uint8Array(b);

src/settings.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,11 @@ var WASM_ESM_INTEGRATION = false;
21842184
// [link]
21852185
var JS_BASE64_API = false;
21862186

2187+
// Enable support for GrowableSharedArrayBuffer.
2188+
// This features is only available behind a flag in recent versions of
2189+
// node/chrome.
2190+
var GROWABLE_ARRAYBUFFERS = false;
2191+
21872192
// Experimental support for WebAssembly js-types proposal.
21882193
// It's currently only available under a flag in certain browsers,
21892194
// so we disable it by default to save on code size.

test/test_browser.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4739,17 +4739,21 @@ def test_minimal_runtime_hello_thread(self, opts):
47394739
# Tests memory growth in pthreads mode, but still on the main thread.
47404740
@parameterized({
47414741
'': ([], 1),
4742+
'growable_arraybuffers': (['-sGROWABLE_ARRAYBUFFERS'], 1),
47424743
'proxy': (['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'], 2),
47434744
})
47444745
@no_2gb('uses INITIAL_MEMORY')
47454746
@no_4gb('uses INITIAL_MEMORY')
47464747
def test_pthread_growth_mainthread(self, cflags, pthread_pool_size):
47474748
self.set_setting('PTHREAD_POOL_SIZE', pthread_pool_size)
4748-
self.btest_exit('pthread/test_pthread_memory_growth_mainthread.c', cflags=['-Wno-pthreads-mem-growth', '-pthread', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=32MB', '-sMAXIMUM_MEMORY=256MB'] + cflags)
4749+
if '-sGROWABLE_ARRAYBUFFERS' not in cflags:
4750+
self.cflags.append('-Wno-pthreads-mem-growth')
4751+
self.btest_exit('pthread/test_pthread_memory_growth_mainthread.c', cflags=['-pthread', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=32MB', '-sMAXIMUM_MEMORY=256MB'] + cflags)
47494752

47504753
# Tests memory growth in a pthread.
47514754
@parameterized({
47524755
'': ([],),
4756+
'growable_arraybuffers': (['-sGROWABLE_ARRAYBUFFERS'],),
47534757
'assert': (['-sASSERTIONS'],),
47544758
'proxy': (['-sPROXY_TO_PTHREAD'], 2),
47554759
'minimal': (['-sMINIMAL_RUNTIME', '-sMODULARIZE', '-sEXPORT_NAME=MyModule'],),
@@ -4758,7 +4762,9 @@ def test_pthread_growth_mainthread(self, cflags, pthread_pool_size):
47584762
@no_4gb('uses INITIAL_MEMORY')
47594763
def test_pthread_growth(self, cflags, pthread_pool_size = 1):
47604764
self.set_setting('PTHREAD_POOL_SIZE', pthread_pool_size)
4761-
self.btest_exit('pthread/test_pthread_memory_growth.c', cflags=['-Wno-pthreads-mem-growth', '-pthread', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=32MB', '-sMAXIMUM_MEMORY=256MB'] + cflags)
4765+
if '-sGROWABLE_ARRAYBUFFERS' not in cflags:
4766+
self.cflags.append('-Wno-pthreads-mem-growth')
4767+
self.btest_exit('pthread/test_pthread_memory_growth.c', cflags=['-pthread', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=32MB', '-sMAXIMUM_MEMORY=256MB'] + cflags)
47624768

47634769
# Tests that time in a pthread is relative to the main thread, so measurements
47644770
# on different threads are still monotonic, as if checking a single central

test/test_other.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14285,23 +14285,35 @@ def test_pthread_kill(self):
1428514285
@node_pthreads
1428614286
@parameterized({
1428714287
'': ([], 1),
14288+
'growable_arraybuffers': (['-sGROWABLE_ARRAYBUFFERS', '-Wno-experimental'], 1),
1428814289
'proxy': (['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'], 2),
1428914290
})
1429014291
def test_pthread_growth_mainthread(self, cflags, pthread_pool_size):
14292+
if '-sGROWABLE_ARRAYBUFFERS' in cflags:
14293+
self.node_args.append('--experimental-wasm-rab-integration')
14294+
self.require_node_canary()
14295+
else:
14296+
self.cflags.append('-Wno-pthreads-mem-growth')
1429114297
self.set_setting('PTHREAD_POOL_SIZE', pthread_pool_size)
14292-
self.do_runf('pthread/test_pthread_memory_growth_mainthread.c', cflags=['-Wno-pthreads-mem-growth', '-pthread', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=32MB', '-sMAXIMUM_MEMORY=256MB'] + cflags)
14298+
self.do_runf('pthread/test_pthread_memory_growth_mainthread.c', cflags=['-pthread', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=32MB', '-sMAXIMUM_MEMORY=256MB'] + cflags)
1429314299

1429414300
# Tests memory growth in a pthread.
1429514301
@node_pthreads
1429614302
@parameterized({
1429714303
'': ([],),
14304+
'growable_arraybuffers': (['-sGROWABLE_ARRAYBUFFERS', '-Wno-experimental'],),
1429814305
'assert': (['-sASSERTIONS'],),
1429914306
'proxy': (['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'], 2),
1430014307
'minimal': (['-sMINIMAL_RUNTIME', '-sMODULARIZE', '-sEXPORT_NAME=MyModule'],),
1430114308
})
1430214309
def test_pthread_growth(self, cflags, pthread_pool_size = 1):
1430314310
self.set_setting('PTHREAD_POOL_SIZE', pthread_pool_size)
14304-
self.do_runf('pthread/test_pthread_memory_growth.c', cflags=['-Wno-pthreads-mem-growth', '-pthread', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=32MB', '-sMAXIMUM_MEMORY=256MB'] + cflags)
14311+
if '-sGROWABLE_ARRAYBUFFERS' in cflags:
14312+
self.node_args.append('--experimental-wasm-rab-integration')
14313+
self.require_node_canary()
14314+
else:
14315+
self.cflags.append('-Wno-pthreads-mem-growth')
14316+
self.do_runf('pthread/test_pthread_memory_growth.c', cflags=['-pthread', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=32MB', '-sMAXIMUM_MEMORY=256MB'] + cflags)
1430514317

1430614318
@node_pthreads
1430714319
def test_emscripten_set_interval(self):

tools/building.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ def little_endian_heap(js_file):
10591059

10601060

10611061
def apply_wasm_memory_growth(js_file):
1062+
assert not settings.GROWABLE_ARRAYBUFFERS
10621063
logger.debug('supporting wasm memory growth with pthreads')
10631064
return acorn_optimizer(js_file, ['growableHeap'])
10641065

tools/link.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def setup_pthreads():
509509
diagnostics.warning('experimental', '-sMAIN_MODULE + pthreads is experimental')
510510
elif settings.LINKABLE:
511511
diagnostics.warning('experimental', '-sLINKABLE + pthreads is experimental')
512-
if settings.ALLOW_MEMORY_GROWTH:
512+
if settings.ALLOW_MEMORY_GROWTH and not settings.GROWABLE_ARRAYBUFFERS:
513513
diagnostics.warning('pthreads-mem-growth', '-pthread + ALLOW_MEMORY_GROWTH may run non-wasm code slowly, see https://github.com/WebAssembly/design/issues/1271')
514514

515515
default_setting('DEFAULT_PTHREAD_STACK_SIZE', settings.STACK_SIZE)
@@ -795,6 +795,9 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
795795
if settings.JS_BASE64_API:
796796
diagnostics.warning('experimental', '-sJS_BASE64_API is still experimental and not yet supported in browsers')
797797

798+
if settings.GROWABLE_ARRAYBUFFERS:
799+
diagnostics.warning('experimental', '-sGROWABLE_ARRAYBUFFERS is still experimental and not yet supported in browsers')
800+
798801
if settings.SOURCE_PHASE_IMPORTS:
799802
if not settings.EXPORT_ES6:
800803
exit_with_error('SOURCE_PHASE_IMPORTS requires EXPORT_ES6')
@@ -2330,7 +2333,7 @@ def phase_binaryen(target, options, wasm_target):
23302333
# unsigning pass.
23312334
# we also must do this after the asan or safe_heap instrumentation, as they
23322335
# wouldn't be able to recognize patterns produced by the growth pass.
2333-
if settings.SHARED_MEMORY and settings.ALLOW_MEMORY_GROWTH:
2336+
if settings.SHARED_MEMORY and settings.ALLOW_MEMORY_GROWTH and not settings.GROWABLE_ARRAYBUFFERS:
23342337
with ToolchainProfiler.profile_block('apply_wasm_memory_growth'):
23352338
final_js = building.apply_wasm_memory_growth(final_js)
23362339

tools/system_libs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ def vary_on(cls):
22312231
def get_default_variation(cls, **kwargs):
22322232
return super().get_default_variation(
22332233
is_mem_grow=settings.ALLOW_MEMORY_GROWTH,
2234-
is_pure=settings.PURE_WASI,
2234+
is_pure=settings.PURE_WASI or settings.GROWABLE_ARRAYBUFFERS,
22352235
nocatch=settings.DISABLE_EXCEPTION_CATCHING and not settings.WASM_EXCEPTIONS,
22362236
**kwargs,
22372237
)

0 commit comments

Comments
 (0)