Skip to content

Commit 50c7d06

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 50c7d06

File tree

10 files changed

+35
-7
lines changed

10 files changed

+35
-7
lines changed

.circleci/config.yml

Lines changed: 2 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*

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.

system/lib/standalone/standalone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ size_t emscripten_get_heap_max() {
124124
}
125125

126126
int emscripten_resize_heap(size_t size) {
127-
#if defined(EMSCRIPTEN_MEMORY_GROWTH)
127+
#if defined(EMSCRIPTEN_REPORT_MEMORY_GROWTH)
128128
size_t old_size = __builtin_wasm_memory_size(0) * WASM_PAGE_SIZE;
129129
assert(old_size < size);
130130
ssize_t diff = (size - old_size + WASM_PAGE_SIZE - 1) / WASM_PAGE_SIZE;

test/test_browser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4728,6 +4728,7 @@ def test_pthread_hello_thread(self, opts, modularize):
47284728
# Tests that a pthreads build of -sMINIMAL_RUNTIME works well in different build modes
47294729
@parameterized({
47304730
'': ([],),
4731+
'growable_arraybuffers': (['-sGROWABLE_ARRAYBUFFERS'],),
47314732
'modularize': (['-sMODULARIZE', '-sEXPORT_NAME=MyModule'],),
47324733
'O3': (['-O3'],),
47334734
'O3_modularize': (['-O3', '-sMODULARIZE', '-sEXPORT_NAME=MyModule'],),
@@ -4739,6 +4740,7 @@ def test_minimal_runtime_hello_thread(self, opts):
47394740
# Tests memory growth in pthreads mode, but still on the main thread.
47404741
@parameterized({
47414742
'': ([], 1),
4743+
'growable_arraybuffers': (['-sGROWABLE_ARRAYBUFFERS'], 1),
47424744
'proxy': (['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'], 2),
47434745
})
47444746
@no_2gb('uses INITIAL_MEMORY')

test/test_other.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14285,22 +14285,30 @@ def test_pthread_kill(self):
1428514285
@node_pthreads
1428614286
@parameterized({
1428714287
'': ([], 1),
14288+
'growable_arraybuffers': (['-sGROWABLE_ARRAYBUFFERS'], 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()
1429114295
self.set_setting('PTHREAD_POOL_SIZE', pthread_pool_size)
1429214296
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)
1429314297

1429414298
# Tests memory growth in a pthread.
1429514299
@node_pthreads
1429614300
@parameterized({
1429714301
'': ([],),
14302+
'growable_arraybuffers': (['-sGROWABLE_ARRAYBUFFERS'],),
1429814303
'assert': (['-sASSERTIONS'],),
1429914304
'proxy': (['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'], 2),
1430014305
'minimal': (['-sMINIMAL_RUNTIME', '-sMODULARIZE', '-sEXPORT_NAME=MyModule'],),
1430114306
})
1430214307
def test_pthread_growth(self, cflags, pthread_pool_size = 1):
1430314308
self.set_setting('PTHREAD_POOL_SIZE', pthread_pool_size)
14309+
if '-sGROWABLE_ARRAYBUFFERS' in cflags:
14310+
self.node_args.append('--experimental-wasm-rab-integration')
14311+
self.require_node_canary()
1430414312
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)
1430514313

1430614314
@node_pthreads

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,7 @@ def get_cflags(self):
22162216
cflags = super().get_cflags()
22172217
cflags += ['-DNDEBUG', '-DEMSCRIPTEN_STANDALONE_WASM']
22182218
if self.is_mem_grow:
2219-
cflags += ['-DEMSCRIPTEN_MEMORY_GROWTH']
2219+
cflags += ['-DEMSCRIPTEN_REPORT_MEMORY_GROWTH']
22202220
if self.is_pure:
22212221
cflags += ['-DEMSCRIPTEN_PURE_WASI']
22222222
if self.nocatch:
@@ -2230,7 +2230,7 @@ def vary_on(cls):
22302230
@classmethod
22312231
def get_default_variation(cls, **kwargs):
22322232
return super().get_default_variation(
2233-
is_mem_grow=settings.ALLOW_MEMORY_GROWTH,
2233+
is_mem_grow=settings.ALLOW_MEMORY_GROWTH and not settings.GROWABLE_ARRAYBUFFERS,
22342234
is_pure=settings.PURE_WASI,
22352235
nocatch=settings.DISABLE_EXCEPTION_CATCHING and not settings.WASM_EXCEPTIONS,
22362236
**kwargs,

0 commit comments

Comments
 (0)