Skip to content

Commit 7fcebb0

Browse files
authored
Avoid reference to SharedArrayBuffer in UTF8ArrayToString (#22844)
This allows program built with `SHARED_MEMORY` enabled to run in a single threaded context where `SharedArrayBuffer` is not defined (i.e. when crossOriginIsolated is false). Fixes: #22837
1 parent 8bbe1f8 commit 7fcebb0

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/parseTools.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,9 +1042,11 @@ function getUnsharedTextDecoderView(heap, start, end) {
10421042
// then unconditionally do a .slice() for smallest code size.
10431043
if (SHRINK_LEVEL == 2 || heap == 'HEAPU8') return shared;
10441044

1045-
// Otherwise, generate a runtime type check: must do a .slice() if looking at a SAB,
1046-
// or can use .subarray() otherwise.
1047-
return `${heap}.buffer instanceof SharedArrayBuffer ? ${shared} : ${unshared}`;
1045+
// Otherwise, generate a runtime type check: must do a .slice() if looking at
1046+
// a SAB, or can use .subarray() otherwise. Note: We compare with
1047+
// `ArrayBuffer` here to avoid referencing `SharedArrayBuffer` which could be
1048+
// undefined.
1049+
return `${heap}.buffer instanceof ArrayBuffer ? ${unshared} : ${shared}`;
10481050
}
10491051

10501052
function getEntryFunction() {

test/test_other.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12193,14 +12193,26 @@ def test_pthread_relocatable(self):
1219312193
@node_pthreads
1219412194
def test_pthread_unavailable(self):
1219512195
# Run a simple hello world program that uses pthreads
12196-
self.emcc_args += ['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME']
12196+
self.emcc_args += ['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME', '-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$stringToNewUTF8,$UTF8ToString']
1219712197
self.do_run_in_out_file_test('hello_world.c')
1219812198

1219912199
# Now run the same program but with SharedArrayBuffer undefined, it should run
1220012200
# fine and then fail on the first call to pthread_create.
12201-
create_file('pre.js', 'SharedArrayBuffer = undefined\n')
12202-
expected = 'pthread_create: environment does not support SharedArrayBuffer, pthreads are not available'
12203-
self.do_runf('hello_world.c', expected, assert_returncode=NON_ZERO, emcc_args=['--pre-js=pre.js'])
12201+
#
12202+
# We specifically test that we can call UTF8ToString, which in older emscripten
12203+
# versions had an instanceof check against SharedArrayBuffer which would cause
12204+
# a crash when SharedArrayBuffer was undefined.
12205+
create_file('pre.js', '''
12206+
SharedArrayBuffer = undefined;
12207+
Module.onRuntimeInitialized = () => {
12208+
var addr = stringToNewUTF8("hello world string, longer than 16 chars");
12209+
assert(addr);
12210+
var str = UTF8ToString(addr);
12211+
console.log("got: " + str);
12212+
assert(str == "hello world string, longer than 16 chars");
12213+
};''')
12214+
expected = ['got: hello world string, longer than 16 chars', 'pthread_create: environment does not support SharedArrayBuffer, pthreads are not available']
12215+
self.do_runf('hello_world.c', expected, assert_all=True, assert_returncode=NON_ZERO, emcc_args=['--pre-js=pre.js'])
1220412216

1220512217
def test_stdin_preprocess(self):
1220612218
create_file('temp.h', '#include <string>')

0 commit comments

Comments
 (0)