Bug 1310239: Only allow numbers as valid global imports; r=luke

MozReview-Commit-ID: DrlZbrpvV9S

--HG--
extra : rebase_source : 814cf87d395ab5342e22675a6c83b302762b32ce
extra : histedit_source : cc8e50d9e5e3219426b0176515e4b257ad8a4dc2
This commit is contained in:
Benjamin Bouvier 2016-10-14 17:25:47 +02:00
parent e8b2ba94d1
commit 44ca6b4157
3 changed files with 64 additions and 13 deletions

View File

@ -270,6 +270,8 @@ GetImports(JSContext* cx,
MOZ_ASSERT(!global.isMutable());
switch (global.type()) {
case ValType::I32: {
if (!v.isNumber())
return ThrowBadImportField(cx, "a number");
int32_t i32;
if (!ToInt32(cx, v, &i32))
return false;
@ -292,6 +294,8 @@ GetImports(JSContext* cx,
val = Val(RawF32::fromBits(bits));
break;
}
if (!v.isNumber())
return ThrowBadImportField(cx, "a number");
double d;
if (!ToNumber(cx, v, &d))
return false;
@ -306,6 +310,8 @@ GetImports(JSContext* cx,
val = Val(RawF64::fromBits(bits));
break;
}
if (!v.isNumber())
return ThrowBadImportField(cx, "a number");
double d;
if (!ToNumber(cx, v, &d))
return false;

View File

@ -51,10 +51,11 @@ wasmFailValidateText(`(module (global i32 (i32.const 1337)) (func (set_global 0
// Big module with many variables: test that setting one doesn't overwrite the
// other ones.
function get_set(i, type) { return `
(func $get_${i} (result ${type}) (get_global ${i}))
(func $set_${i} (param ${type}) (set_global ${i} (get_local 0)))
`
function get_set(i, type) {
return `
(func $get_${i} (result ${type}) (get_global ${i}))
(func $set_${i} (param ${type}) (set_global ${i} (get_local 0)))
`;
}
var module = wasmEvalText(`(module
@ -123,6 +124,51 @@ module = wasmEvalText(`(module
assertEq(module.getter(), 42);
assertEq(module.value, 42);
// Can only import numbers (no implicit coercions).
module = new WebAssembly.Module(wasmTextToBinary(`(module
(global (import "globs" "i32") i32)
(global (import "globs" "f32") f32)
(global (import "globs" "f64") f32)
)`));
const assertLinkFails = (m, imp, err) => {
assertErrorMessage(() => new WebAssembly.Instance(m, imp), TypeError, err);
}
var imp = {
globs: {
i32: 0,
f32: Infinity,
f64: NaN
}
};
let i = new WebAssembly.Instance(module, imp);
for (let v of [
null,
{},
"42",
/not a number/,
false,
undefined,
Symbol(),
{ valueOf() { return 42; } }
]) {
imp.globs.i32 = v;
assertLinkFails(module, imp, /not a number/);
imp.globs.i32 = 0;
imp.globs.f32 = v;
assertLinkFails(module, imp, /not a number/);
imp.globs.f32 = Math.fround(13.37);
imp.globs.f64 = v;
assertLinkFails(module, imp, /not a number/);
imp.globs.f64 = 13.37;
}
// Imported globals and locally defined globals use the same index space.
module = wasmEvalText(`(module
(import "globals" "x" (global i32))

View File

@ -77,15 +77,14 @@
(assert_return (invoke "get-x") (i32.const 666))
(assert_return (invoke "get-y") (i32.const 666))
;; TODO; Tests not adapted for a JS host!
;;(assert_unlinkable
;; (module (import "spectest" "unknown" (global i32)))
;; "unknown import"
;;)
;;(assert_unlinkable
;; (module (import "spectest" "print" (global i32)))
;; "type mismatch"
;;)
(assert_unlinkable
(module (import "spectest" "unknown" (global i32)))
"unknown import"
)
(assert_unlinkable
(module (import "spectest" "print" (global i32)))
"type mismatch"
)
;; TODO;
;;(module (import "spectest" "global" (global i64)))