Skip to content

Commit 1b5f4a8

Browse files
committed
[wasm64] Fix wasm64 memory read in Fetch.js
This bug only showed up under wasm64 when the address of the fetch object was between 2Gb and 4Gb. This causes the JS ">> 2" operation to generate a negative number becuase the high bit is set: ``` $ node > a = 2**31 + 10 2147483658 > a >> 2 -536870910 > ``` In `browser64_4gb` mode this bug resulted in a read from the first 4gb of memory somewhere, which results a in 0 whereas read from a negative address yields `undefined`.
1 parent 380a9dd commit 1b5f4a8

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/Fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
320320
function saveResponseAndStatus() {
321321
var ptr = 0;
322322
var ptrLen = 0;
323-
if (xhr.response && fetchAttrLoadToMemory && HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.data }}} >> 2] === 0) {
323+
if (xhr.response && fetchAttrLoadToMemory && {{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}} === 0) {
324324
ptrLen = xhr.response.byteLength;
325325
}
326326
if (ptrLen > 0) {

test/fetch/test_fetch_sync_xhr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ int result = -1;
1515
int main() {
1616
// If an exception is thrown from the user callback, it bubbles up to
1717
// self.onerror but is otherwise completely swallowed by xhr.send.
18-
EM_ASM({self.onerror = function() {
19-
out('Got error');
18+
EM_ASM({self.onerror = (e) => {
19+
out('Got error', e);
2020
HEAP32[$0 >> 2] = 2;
2121
};}, &result);
2222
emscripten_fetch_attr_t attr;

test/test_browser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5908,6 +5908,16 @@ def setUp(self):
59085908
self.require_wasm64()
59095909

59105910

5911+
class browser64_2gb(browser):
5912+
def setUp(self):
5913+
super().setUp()
5914+
self.set_setting('MEMORY64')
5915+
self.set_setting('INITIAL_MEMORY', '2200gb')
5916+
self.set_setting('GLOBAL_BASE', '2gb')
5917+
self.emcc_args.append('-Wno-experimental')
5918+
self.require_wasm64()
5919+
5920+
59115921
class browser_2gb(browser):
59125922
def setUp(self):
59135923
super().setUp()

0 commit comments

Comments
 (0)