| 1 |
"use strict"; |
| 2 |
|
| 3 |
/* Copyright 2021 Mozilla Foundation |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
if (!wasmIsSupported()) { |
| 19 |
quit(); |
| 20 |
} |
| 21 |
|
| 22 |
function partialOobWriteMayWritePartialData() { |
| 23 |
let arm_native = getBuildConfiguration("arm") && !getBuildConfiguration("arm-simulator"); |
| 24 |
let arm64_native = getBuildConfiguration("arm64") && !getBuildConfiguration("arm64-simulator"); |
| 25 |
return arm_native || arm64_native; |
| 26 |
} |
| 27 |
|
| 28 |
let native_arm = getBuildConfiguration("arm") && !getBuildConfiguration("arm-simulator"); |
| 29 |
let native_arm64 = getBuildConfiguration("arm64") && !getBuildConfiguration("arm64-simulator"); |
| 30 |
|
| 31 |
function bytes(type, bytes) { |
| 32 |
var typedBuffer = new Uint8Array(bytes); |
| 33 |
return wasmGlobalFromArrayBuffer(type, typedBuffer.buffer); |
| 34 |
} |
| 35 |
function value(type, value) { |
| 36 |
return new WebAssembly.Global({ |
| 37 |
value: type, |
| 38 |
mutable: false, |
| 39 |
}, value); |
| 40 |
} |
| 41 |
|
| 42 |
function i8x16(elements) { |
| 43 |
let typedBuffer = new Uint8Array(elements); |
| 44 |
return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); |
| 45 |
} |
| 46 |
function i16x8(elements) { |
| 47 |
let typedBuffer = new Uint16Array(elements); |
| 48 |
return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); |
| 49 |
} |
| 50 |
function i32x4(elements) { |
| 51 |
let typedBuffer = new Uint32Array(elements); |
| 52 |
return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); |
| 53 |
} |
| 54 |
function i64x2(elements) { |
| 55 |
let typedBuffer = new BigUint64Array(elements); |
| 56 |
return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); |
| 57 |
} |
| 58 |
function f32x4(elements) { |
| 59 |
let typedBuffer = new Float32Array(elements); |
| 60 |
return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); |
| 61 |
} |
| 62 |
function f64x2(elements) { |
| 63 |
let typedBuffer = new Float64Array(elements); |
| 64 |
return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer); |
| 65 |
} |
| 66 |
|
| 67 |
function either(...arr) { |
| 68 |
return new EitherVariants(arr); |
| 69 |
} |
| 70 |
|
| 71 |
class F32x4Pattern { |
| 72 |
constructor(x, y, z, w) { |
| 73 |
this.x = x; |
| 74 |
this.y = y; |
| 75 |
this.z = z; |
| 76 |
this.w = w; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
class F64x2Pattern { |
| 81 |
constructor(x, y) { |
| 82 |
this.x = x; |
| 83 |
this.y = y; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
class RefWithType { |
| 88 |
constructor(type) { |
| 89 |
this.type = type; |
| 90 |
} |
| 91 |
|
| 92 |
formatExpected() { |
| 93 |
return `RefWithType(${this.type})`; |
| 94 |
} |
| 95 |
|
| 96 |
test(refGlobal) { |
| 97 |
try { |
| 98 |
new WebAssembly.Global({value: this.type}, refGlobal.value); |
| 99 |
return true; |
| 100 |
} catch (err) { |
| 101 |
assertEq(err instanceof TypeError, true, `wrong type of error when creating global: ${err}`); |
| 102 |
assertEq(!!err.message.match(/can only pass/), true, `wrong type of error when creating global: ${err}`); |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// ref.extern values created by spec tests will be JS objects of the form |
| 109 |
// { [externsym]: <number> }. Other externref values are possible to observe |
| 110 |
// if extern.convert_any is used. |
| 111 |
let externsym = Symbol("externref"); |
| 112 |
function externref(s) { |
| 113 |
return { [externsym]: s }; |
| 114 |
} |
| 115 |
function is_externref(x) { |
| 116 |
return (x !== null && externsym in x) ? 1 : 0; |
| 117 |
} |
| 118 |
function is_funcref(x) { |
| 119 |
return typeof x === "function" ? 1 : 0; |
| 120 |
} |
| 121 |
function eq_externref(x, y) { |
| 122 |
return x === y ? 1 : 0; |
| 123 |
} |
| 124 |
function eq_funcref(x, y) { |
| 125 |
return x === y ? 1 : 0; |
| 126 |
} |
| 127 |
|
| 128 |
class ExternRefResult { |
| 129 |
constructor(n) { |
| 130 |
this.n = n; |
| 131 |
} |
| 132 |
|
| 133 |
formatExpected() { |
| 134 |
return `ref.extern ${this.n}`; |
| 135 |
} |
| 136 |
|
| 137 |
test(global) { |
| 138 |
// the global's value can either be an externref or just a plain old JS number |
| 139 |
let result = global.value; |
| 140 |
if (typeof global.value === "object" && externsym in global.value) { |
| 141 |
result = global.value[externsym]; |
| 142 |
} |
| 143 |
return result === this.n; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// ref.host values created by spectests will be whatever the JS API does to |
| 148 |
// convert the given value to anyref. It should implicitly be like any.convert_extern. |
| 149 |
function hostref(v) { |
| 150 |
const { internalizeNum } = new WebAssembly.Instance( |
| 151 |
new WebAssembly.Module(wasmTextToBinary(`(module |
| 152 |
(func (import "test" "coerce") (param i32) (result anyref)) |
| 153 |
(func (export "internalizeNum") (param i32) (result anyref) |
| 154 |
(call 0 (local.get 0)) |
| 155 |
) |
| 156 |
)`)), |
| 157 |
{ "test": { "coerce": x => x } }, |
| 158 |
).exports; |
| 159 |
return internalizeNum(v); |
| 160 |
} |
| 161 |
|
| 162 |
class HostRefResult { |
| 163 |
constructor(n) { |
| 164 |
this.n = n; |
| 165 |
} |
| 166 |
|
| 167 |
formatExpected() { |
| 168 |
return `ref.host ${this.n}`; |
| 169 |
} |
| 170 |
|
| 171 |
test(externrefGlobal) { |
| 172 |
assertEq(externsym in externrefGlobal.value, true, `HostRefResult only works with externref inputs`); |
| 173 |
return externrefGlobal.value[externsym] === this.n; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
let spectest = { |
| 178 |
externref: externref, |
| 179 |
is_externref: is_externref, |
| 180 |
is_funcref: is_funcref, |
| 181 |
eq_externref: eq_externref, |
| 182 |
eq_funcref: eq_funcref, |
| 183 |
print: console.log.bind(console), |
| 184 |
print_i32: console.log.bind(console), |
| 185 |
print_i32_f32: console.log.bind(console), |
| 186 |
print_f64_f64: console.log.bind(console), |
| 187 |
print_f32: console.log.bind(console), |
| 188 |
print_f64: console.log.bind(console), |
| 189 |
global_i32: 666, |
| 190 |
global_i64: 666n, |
| 191 |
global_f32: 666, |
| 192 |
global_f64: 666, |
| 193 |
table: new WebAssembly.Table({ |
| 194 |
initial: 10, |
| 195 |
maximum: 20, |
| 196 |
element: "anyfunc", |
| 197 |
}), |
| 198 |
memory: new WebAssembly.Memory({ initial: 1, maximum: 2 }), |
| 199 |
}; |
| 200 |
|
| 201 |
let linkage = { |
| 202 |
spectest, |
| 203 |
}; |
| 204 |
|
| 205 |
function module(source) { |
| 206 |
let bytecode = wasmTextToBinary(source); |
| 207 |
let module = new WebAssembly.Module(bytecode); |
| 208 |
return module; |
| 209 |
} |
| 210 |
|
| 211 |
function instantiate(source) { |
| 212 |
let bytecode = wasmTextToBinary(source); |
| 213 |
let module = new WebAssembly.Module(bytecode); |
| 214 |
let instance = new WebAssembly.Instance(module, linkage); |
| 215 |
return instance.exports; |
| 216 |
} |
| 217 |
|
| 218 |
function instantiateFromModule(module) { |
| 219 |
let instance = new WebAssembly.Instance(module, linkage); |
| 220 |
return instance.exports; |
| 221 |
} |
| 222 |
|
| 223 |
function register(instance, name) { |
| 224 |
linkage[name] = instance; |
| 225 |
} |
| 226 |
|
| 227 |
function invoke(instance, field, params) { |
| 228 |
let func = instance[field]; |
| 229 |
assertEq(func instanceof Function, true, "expected a function"); |
| 230 |
return wasmLosslessInvoke(func, ...params); |
| 231 |
} |
| 232 |
|
| 233 |
function get(instance, field) { |
| 234 |
let global = instance[field]; |
| 235 |
assertEq( |
| 236 |
global instanceof WebAssembly.Global, |
| 237 |
true, |
| 238 |
"expected a WebAssembly.Global", |
| 239 |
); |
| 240 |
return global; |
| 241 |
} |
| 242 |
|
| 243 |
function assert_trap(thunk, message) { |
| 244 |
try { |
| 245 |
thunk(); |
| 246 |
throw new Error(`got no error`); |
| 247 |
} catch (err) { |
| 248 |
if (err instanceof WebAssembly.RuntimeError) { |
| 249 |
return; |
| 250 |
} |
| 251 |
err.message = `expected trap (${message}): ${err.message}`; |
| 252 |
throw err; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
let StackOverflow; |
| 257 |
try { |
| 258 |
(function f() { |
| 259 |
1 + f(); |
| 260 |
})(); |
| 261 |
} catch (e) { |
| 262 |
StackOverflow = e.constructor; |
| 263 |
} |
| 264 |
function assert_exhaustion(thunk, message) { |
| 265 |
try { |
| 266 |
thunk(); |
| 267 |
throw new Error(`got no error`); |
| 268 |
} catch (err) { |
| 269 |
if (err instanceof StackOverflow) { |
| 270 |
return; |
| 271 |
} |
| 272 |
err.message = `expected exhaustion (${message}): ${err.message}`; |
| 273 |
throw err; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
function assert_invalid(thunk, message) { |
| 278 |
try { |
| 279 |
thunk(); |
| 280 |
throw new Error(`got no error`); |
| 281 |
} catch (err) { |
| 282 |
if (err instanceof WebAssembly.LinkError || err instanceof WebAssembly.CompileError) { |
| 283 |
return; |
| 284 |
} |
| 285 |
err.message = `expected invalid module (${message}): ${err.message}`; |
| 286 |
throw err; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
function assert_unlinkable(thunk, message) { |
| 291 |
try { |
| 292 |
thunk(); |
| 293 |
throw new Error(`got no error`); |
| 294 |
} catch (err) { |
| 295 |
if (err instanceof WebAssembly.LinkError || err instanceof WebAssembly.CompileError) { |
| 296 |
return; |
| 297 |
} |
| 298 |
err.message = `expected an unlinkable module (${message}): ${err.message}`; |
| 299 |
throw err; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
function assert_malformed(thunk, message) { |
| 304 |
try { |
| 305 |
thunk(); |
| 306 |
throw new Error(`got no error`); |
| 307 |
} catch (err) { |
| 308 |
if ( |
| 309 |
err instanceof TypeError || |
| 310 |
err instanceof SyntaxError || |
| 311 |
err instanceof WebAssembly.CompileError || |
| 312 |
err instanceof WebAssembly.LinkError |
| 313 |
) { |
| 314 |
return; |
| 315 |
} |
| 316 |
err.message = `expected a malformed module (${message}): ${err.message}`; |
| 317 |
throw err; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
function assert_exception(thunk) { |
| 322 |
let thrown = false; |
| 323 |
try { |
| 324 |
thunk(); |
| 325 |
} catch (err) { |
| 326 |
thrown = true; |
| 327 |
} |
| 328 |
assertEq(thrown, true, "expected an exception to be thrown"); |
| 329 |
} |
| 330 |
|
| 331 |
function assert_return(thunk, expected) { |
| 332 |
let results = thunk(); |
| 333 |
|
| 334 |
if (results === undefined) { |
| 335 |
results = []; |
| 336 |
} else if (!Array.isArray(results)) { |
| 337 |
results = [results]; |
| 338 |
} |
| 339 |
if (!Array.isArray(expected)) { |
| 340 |
expected = [expected]; |
| 341 |
} |
| 342 |
|
| 343 |
if (!compareResults(results, expected)) { |
| 344 |
let got = results.map((x) => formatResult(x)).join(", "); |
| 345 |
let wanted = expected.map((x) => formatExpected(x)).join(", "); |
| 346 |
assertEq( |
| 347 |
`[${got}]`, |
| 348 |
`[${wanted}]`, |
| 349 |
); |
| 350 |
assertEq(true, false, `${got} !== ${wanted}`); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
function formatResult(result) { |
| 355 |
if (typeof (result) === "object") { |
| 356 |
return wasmGlobalToString(result); |
| 357 |
} else { |
| 358 |
return `${result}`; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
function formatExpected(expected) { |
| 363 |
if ( |
| 364 |
expected === `f32_canonical_nan` || |
| 365 |
expected === `f32_arithmetic_nan` || |
| 366 |
expected === `f64_canonical_nan` || |
| 367 |
expected === `f64_arithmetic_nan` |
| 368 |
) { |
| 369 |
return expected; |
| 370 |
} else if (expected instanceof F32x4Pattern) { |
| 371 |
return `f32x4(${formatExpected(expected.x)}, ${ |
| 372 |
formatExpected(expected.y) |
| 373 |
}, ${formatExpected(expected.z)}, ${formatExpected(expected.w)})`; |
| 374 |
} else if (expected instanceof F64x2Pattern) { |
| 375 |
return `f64x2(${formatExpected(expected.x)}, ${ |
| 376 |
formatExpected(expected.y) |
| 377 |
})`; |
| 378 |
} else if (expected instanceof EitherVariants) { |
| 379 |
return expected.formatExpected(); |
| 380 |
} else if (expected instanceof RefWithType) { |
| 381 |
return expected.formatExpected(); |
| 382 |
} else if (expected instanceof ExternRefResult) { |
| 383 |
return expected.formatExpected(); |
| 384 |
} else if (expected instanceof HostRefResult) { |
| 385 |
return expected.formatExpected(); |
| 386 |
} else if (typeof (expected) === "object") { |
| 387 |
return wasmGlobalToString(expected); |
| 388 |
} else { |
| 389 |
throw new Error("unknown expected result"); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
class EitherVariants { |
| 394 |
constructor(arr) { |
| 395 |
this.arr = arr; |
| 396 |
} |
| 397 |
matches(v) { |
| 398 |
return this.arr.some((e) => compareResult(v, e)); |
| 399 |
} |
| 400 |
formatExpected() { |
| 401 |
return `either(${this.arr.map(formatExpected).join(", ")})`; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
function compareResults(results, expected) { |
| 406 |
if (results.length !== expected.length) { |
| 407 |
return false; |
| 408 |
} |
| 409 |
for (let i in results) { |
| 410 |
if (expected[i] instanceof EitherVariants) { |
| 411 |
return expected[i].matches(results[i]); |
| 412 |
} |
| 413 |
if (!compareResult(results[i], expected[i])) { |
| 414 |
return false; |
| 415 |
} |
| 416 |
} |
| 417 |
return true; |
| 418 |
} |
| 419 |
|
| 420 |
function compareResult(result, expected) { |
| 421 |
if ( |
| 422 |
expected === `canonical_nan` || |
| 423 |
expected === `arithmetic_nan` |
| 424 |
) { |
| 425 |
return wasmGlobalIsNaN(result, expected); |
| 426 |
} else if (expected === null) { |
| 427 |
return result.value === null; |
| 428 |
} else if (expected instanceof F32x4Pattern) { |
| 429 |
return compareResult( |
| 430 |
wasmGlobalExtractLane(result, "f32x4", 0), |
| 431 |
expected.x, |
| 432 |
) && |
| 433 |
compareResult(wasmGlobalExtractLane(result, "f32x4", 1), expected.y) && |
| 434 |
compareResult(wasmGlobalExtractLane(result, "f32x4", 2), expected.z) && |
| 435 |
compareResult(wasmGlobalExtractLane(result, "f32x4", 3), expected.w); |
| 436 |
} else if (expected instanceof F64x2Pattern) { |
| 437 |
return compareResult( |
| 438 |
wasmGlobalExtractLane(result, "f64x2", 0), |
| 439 |
expected.x, |
| 440 |
) && |
| 441 |
compareResult(wasmGlobalExtractLane(result, "f64x2", 1), expected.y); |
| 442 |
} else if (expected instanceof RefWithType) { |
| 443 |
return expected.test(result); |
| 444 |
} else if (expected instanceof ExternRefResult) { |
| 445 |
return expected.test(result); |
| 446 |
} else if (expected instanceof HostRefResult) { |
| 447 |
return expected.test(result); |
| 448 |
} else if (typeof (expected) === "object") { |
| 449 |
return wasmGlobalsEqual(result, expected); |
| 450 |
} else { |
| 451 |
throw new Error("unknown expected result"); |
| 452 |
} |
| 453 |
} |