Closed Bug 1920337 Opened 1 year ago Closed 1 year ago

The Wasm JS string builtin 'equals' does not accept 'null' arguments

Categories

(Core :: JavaScript: WebAssembly, defect, P3)

Firefox 130
defect

Tracking

()

RESOLVED FIXED
133 Branch
Tracking Status
firefox133 --- fixed

People

(Reporter: sjrdoeraene, Assigned: rhunt)

References

(Blocks 2 open bugs)

Details

Attachments

(2 files)

Attached file bug.zip

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0

Steps to reproduce:

Either unzip the attached bug.zip file to a directory, or recreate the files as follows:

index.html:

<html>
<head>
  <title>Bug</title>
</head>
<body>
  <script type=module src="./bug.js"></script>
</body>
</html>

bug.js

const options = {
  builtins: ["js-string"],
};
const instantiated = await WebAssembly.instantiateStreaming(
  fetch("./bug.wasm"), {}, options
);
const instance = instantiated.instance;
console.log(instance);
const { equalsBridge } = instance.exports;

function test(f) {
  try {
    console.log(f());
  } catch (e) {
    console.log(e);
  }
}

test(() => equalsBridge("foobar", "foobar")); // 1, OK
test(() => equalsBridge("foobar", "barfoo")); // 0, OK
test(() => equalsBridge("foobar", null));     // bad cast, should be 0
test(() => equalsBridge(null, "foobar"));     // bad cast, should be 0
test(() => equalsBridge(null, null));         // bad cast, should be 1

bug.wat

(module
  (type $equalsType (func (param externref) (param externref) (result i32)))
  (import "wasm:js-string" "equals" (func $equals (type $equalsType)))
  (func (export "equalsBridge") (param $a externref) (param $b externref) (result i32)
    local.get $a
    local.get $b
    call $equals))

and compile to bug.wasm.

Then

  1. In Firefox's about:config page, turn on the option javascript.options.wasm_js_string_builtins
  2. Start a local web server in the given directory (e.g., with npx http-server)
  3. Open http://127.0.0.1:8080/ (or another appropriate URL depending on the web server used)
  4. Open the console
  5. Observe results

Actual results:

The following output is logged:

1 bug.js:13:13
0 bug.js:13:13
RuntimeError: bad cast
    <anonymous> http://127.0.0.1:8080/bug.js:21
    test http://127.0.0.1:8080/bug.js:13
    <anonymous> http://127.0.0.1:8080/bug.js:21
bug.js:15:13
RuntimeError: bad cast
    <anonymous> http://127.0.0.1:8080/bug.js:22
    test http://127.0.0.1:8080/bug.js:13
    <anonymous> http://127.0.0.1:8080/bug.js:22
bug.js:15:13
RuntimeError: bad cast
    <anonymous> http://127.0.0.1:8080/bug.js:23
    test http://127.0.0.1:8080/bug.js:13
    <anonymous> http://127.0.0.1:8080/bug.js:23
bug.js:15:13

The 3 exceptions are incorrect. equals is supposed to accept null arguments.

Expected results:

1
0
0
0
1

Indeed, the spec of the JS string builtin for equals (https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md#wasmjs-string-equals) deliberately accepts null arguments.

(For the record, and as some validation that my interpretation of the spec is accurate, V8 behaves as expected.)

Summary: The Wasm JS string builtin 'equals' does not access 'null' arguments → The Wasm JS string builtin 'equals' does not accept 'null' arguments

The Bugbug bot thinks this bug should belong to the 'Core::JavaScript: WebAssembly' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → JavaScript: WebAssembly
Product: Firefox → Core
Severity: -- → S3
Priority: -- → P3
Assignee: nobody → rhunt
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Pushed by rhunt@eqrion.net: https://hg.mozilla.org/integration/autoland/rev/00a77e0dd0ed wasm: Fix null in js-string:equals. r=bvisness
Pushed by rhunt@eqrion.net: https://hg.mozilla.org/integration/autoland/rev/278b2ae92a12 wasm: Fix null in js-string:equals. r=bvisness
Status: ASSIGNED → RESOLVED
Closed: 1 year ago
Resolution: --- → FIXED
Target Milestone: --- → 133 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: