Skip to content

Commit

Permalink
[wasm64] Fix wasm64 memory read in Fetch.js
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
sbc100 committed Feb 2, 2024
1 parent 380a9dd commit 1b5f4a8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
function saveResponseAndStatus() {
var ptr = 0;
var ptrLen = 0;
if (xhr.response && fetchAttrLoadToMemory && HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.data }}} >> 2] === 0) {
if (xhr.response && fetchAttrLoadToMemory && {{{ makeGetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, '*') }}} === 0) {
ptrLen = xhr.response.byteLength;
}
if (ptrLen > 0) {
Expand Down
4 changes: 2 additions & 2 deletions test/fetch/test_fetch_sync_xhr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ int result = -1;
int main() {
// If an exception is thrown from the user callback, it bubbles up to
// self.onerror but is otherwise completely swallowed by xhr.send.
EM_ASM({self.onerror = function() {
out('Got error');
EM_ASM({self.onerror = (e) => {
out('Got error', e);
HEAP32[$0 >> 2] = 2;
};}, &result);
emscripten_fetch_attr_t attr;
Expand Down
10 changes: 10 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5908,6 +5908,16 @@ def setUp(self):
self.require_wasm64()


class browser64_2gb(browser):
def setUp(self):
super().setUp()
self.set_setting('MEMORY64')
self.set_setting('INITIAL_MEMORY', '2200gb')
self.set_setting('GLOBAL_BASE', '2gb')
self.emcc_args.append('-Wno-experimental')
self.require_wasm64()


class browser_2gb(browser):
def setUp(self):
super().setUp()
Expand Down

0 comments on commit 1b5f4a8

Please sign in to comment.