diff --git a/.circleci/config.yml b/.circleci/config.yml index 28781448a501f..05dd160abe7f4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1059,6 +1059,7 @@ jobs: other.test_min_node_version other.test_node_emscripten_num_logical_cores other.test_js_base64_api + other.test_memory_growth core2.test_hello_world core2.test_pthread_create core2.test_i64_invoke_bigint diff --git a/src/runtime_common.js b/src/runtime_common.js index 3d7ea7d8d75aa..cd45e7e3a2f18 100644 --- a/src/runtime_common.js +++ b/src/runtime_common.js @@ -109,9 +109,21 @@ var runtimeExited = false; }}} function updateMemoryViews() { +#if ALLOW_MEMORY_GROWTH + if (HEAP8 && wasmMemory.toResizableBuffer) { + return; + } #if GROWABLE_ARRAYBUFFERS var b = wasmMemory.toResizableBuffer(); #else + var b = wasmMemory.toResizableBuffer?.() ?? wasmMemory.buffer; +#endif +#else +#if ASSERTIONS + // When memory growth is disabled this function should be called + // exacltly once. + assert(!HEAP8, "updateMemoryViews"); +#endif var b = wasmMemory.buffer; #endif {{{ maybeExportHeap('HEAP8') }}}HEAP8 = new Int8Array(b); diff --git a/test/codesize/test_codesize_hello_O0.json b/test/codesize/test_codesize_hello_O0.json index 37e5e05139892..16d4417c9fd0b 100644 --- a/test/codesize/test_codesize_hello_O0.json +++ b/test/codesize/test_codesize_hello_O0.json @@ -1,10 +1,10 @@ { - "a.out.js": 23248, - "a.out.js.gz": 8396, + "a.out.js": 23290, + "a.out.js.gz": 8416, "a.out.nodebug.wasm": 15115, "a.out.nodebug.wasm.gz": 7464, - "total": 38363, - "total_gz": 15860, + "total": 38405, + "total_gz": 15880, "sent": [ "fd_write" ], diff --git a/test/codesize/test_codesize_mem_O3_grow.json b/test/codesize/test_codesize_mem_O3_grow.json index 7a4aeb02b1239..773d3ccaa79eb 100644 --- a/test/codesize/test_codesize_mem_O3_grow.json +++ b/test/codesize/test_codesize_mem_O3_grow.json @@ -1,10 +1,10 @@ { - "a.out.js": 4541, - "a.out.js.gz": 2193, + "a.out.js": 4602, + "a.out.js.gz": 2223, "a.out.nodebug.wasm": 5261, "a.out.nodebug.wasm.gz": 2419, - "total": 9802, - "total_gz": 4612, + "total": 9863, + "total_gz": 4642, "sent": [ "a (emscripten_resize_heap)" ], diff --git a/test/codesize/test_codesize_mem_O3_grow_standalone.json b/test/codesize/test_codesize_mem_O3_grow_standalone.json index 7d36e73aa01a5..5e5e0cd5b689e 100644 --- a/test/codesize/test_codesize_mem_O3_grow_standalone.json +++ b/test/codesize/test_codesize_mem_O3_grow_standalone.json @@ -1,10 +1,10 @@ { - "a.out.js": 4012, - "a.out.js.gz": 1933, + "a.out.js": 4067, + "a.out.js.gz": 1957, "a.out.nodebug.wasm": 5641, "a.out.nodebug.wasm.gz": 2659, - "total": 9653, - "total_gz": 4592, + "total": 9708, + "total_gz": 4616, "sent": [ "args_get", "args_sizes_get", diff --git a/test/codesize/test_codesize_minimal_O0.expected.js b/test/codesize/test_codesize_minimal_O0.expected.js index 448b167942cdf..decd94e3ca25d 100644 --- a/test/codesize/test_codesize_minimal_O0.expected.js +++ b/test/codesize/test_codesize_minimal_O0.expected.js @@ -465,6 +465,9 @@ var runtimeInitialized = false; function updateMemoryViews() { + // When memory growth is disabled this function should be called + // exacltly once. + assert(!HEAP8, "updateMemoryViews"); var b = wasmMemory.buffer; HEAP8 = new Int8Array(b); HEAP16 = new Int16Array(b); diff --git a/test/codesize/test_codesize_minimal_O0.json b/test/codesize/test_codesize_minimal_O0.json index a1cf25d9113fa..7281dddc7ba67 100644 --- a/test/codesize/test_codesize_minimal_O0.json +++ b/test/codesize/test_codesize_minimal_O0.json @@ -1,10 +1,10 @@ { - "a.out.js": 18596, - "a.out.js.gz": 6718, + "a.out.js": 18633, + "a.out.js.gz": 6733, "a.out.nodebug.wasm": 1015, "a.out.nodebug.wasm.gz": 602, - "total": 19611, - "total_gz": 7320, + "total": 19648, + "total_gz": 7335, "sent": [], "imports": [], "exports": [ diff --git a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json index ad0524a4dd8a1..21372c712a1e4 100644 --- a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json +++ b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json @@ -1,10 +1,10 @@ { - "a.out.js": 7357, - "a.out.js.gz": 3627, + "a.out.js": 7418, + "a.out.js.gz": 3658, "a.out.nodebug.wasm": 19064, "a.out.nodebug.wasm.gz": 8804, - "total": 26421, - "total_gz": 12431, + "total": 26482, + "total_gz": 12462, "sent": [ "a (memory)", "b (exit)", diff --git a/test/codesize/test_unoptimized_code_size.json b/test/codesize/test_unoptimized_code_size.json index f1b2ab32e7e61..74044f286bae7 100644 --- a/test/codesize/test_unoptimized_code_size.json +++ b/test/codesize/test_unoptimized_code_size.json @@ -1,16 +1,16 @@ { - "hello_world.js": 54902, - "hello_world.js.gz": 17283, + "hello_world.js": 55028, + "hello_world.js.gz": 17330, "hello_world.wasm": 15115, "hello_world.wasm.gz": 7464, "no_asserts.js": 25903, "no_asserts.js.gz": 8768, "no_asserts.wasm": 12229, "no_asserts.wasm.gz": 6004, - "strict.js": 52659, - "strict.js.gz": 16513, + "strict.js": 52785, + "strict.js.gz": 16561, "strict.wasm": 15115, - "strict.wasm.gz": 7457, - "total": 175923, - "total_gz": 63489 + "strict.wasm.gz": 7461, + "total": 176175, + "total_gz": 63588 } diff --git a/test/common.py b/test/common.py index 9f859979ad048..ab24068e5466b 100644 --- a/test/common.py +++ b/test/common.py @@ -332,6 +332,14 @@ def get_deno(): return get_engine(engine_is_deno) +def check_node_version(major, minor=0, revision=0): + nodejs = get_nodejs() + if not nodejs: + return False + version = shared.get_node_version(nodejs) + return version >= (major, minor, revision) + + def clean_js_output(output): """Cleanup the JS output prior to running verification steps on it. @@ -532,14 +540,10 @@ def require_wasm64(self): self.fail('either d8, node >= 24 or deno required to run wasm64 tests. Use EMTEST_SKIP_WASM64 to skip') def try_require_node_version(self, major, minor=0, revision=0): - nodejs = get_nodejs() - if not nodejs: - return False - version = shared.get_node_version(nodejs) - if version < (major, minor, revision): + if not self.check_node_version(major, minor, revision): return False - self.require_engine(nodejs) + self.require_engine(get_nodejs()) return True def require_wasm_legacy_eh(self): diff --git a/test/test_other.py b/test/test_other.py index a80e34bafa37e..68e405f9dd74d 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -42,6 +42,7 @@ TEST_ROOT, WEBIDL_BINDER, RunnerCore, + check_node_version, copy_asset, copytree, create_file, @@ -6426,28 +6427,36 @@ def test_file_packager_huge(self): self.assertContained(MESSAGE, err) self.clear() - @parameterized({ - '': (True,), - 'wasm2js': (False,), - }) - def test_massive_alloc(self, wasm): + @also_with_wasm2js + def test_memory_growth(self): create_file('main.c', r''' -#include +#include #include +#include +#include +#include int main() { - volatile long x = (long)malloc(1024 * 1024 * 1400); - return x == 0; // can't alloc it, but don't fail catastrophically, expect null + // Report whether we are running with `toResizableBuffer` enabled. + EM_ASM(out('resizable memory buffers:', HEAP8.buffer.resizable)); + + void* x = malloc(256 * 1024 * 1024); + assert(x != NULL); + // Have JS use some of the memory expanded range + char* str = (char*)x - 100; + strcpy(str, "Hello, world!"); + emscripten_out(str); + return 0; } - ''') - cmd = [EMCC, 'main.c', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=16MB'] - if not wasm: - cmd += ['-sWASM=0'] - self.run_process(cmd) - # just care about message regarding allocating over 1GB of memory - output = self.run_js('a.out.js') - if not wasm: - self.assertContained('Warning: Enlarging memory arrays, this is not fast! 16777216,1468137472\n', output) +''') + output = self.do_runf('main.c', 'Hello, world!\n', cflags=['-sALLOW_MEMORY_GROWTH']) + if self.is_wasm2js(): + self.assertContained('Warning: Enlarging memory arrays, this is not fast! 16908288,268566528\n', output) + + if check_node_version(26): + self.assertContained('resizable memory buffers: true\n', output) + else: + self.assertContained('resizable memory buffers: false\n', output) @also_with_wasm2js @parameterized({