[wasm] Implement WebAssembly.validate()
R=bradnelson@chromium.org,mtrofin@chromium.org BUG=chromium:575167 Review-Url: https://codereview.chromium.org/2384513002 Cr-Commit-Position: refs/heads/master@{#39885}
This commit is contained in:
parent
5bb978461f
commit
424cd4cf84
@ -197,6 +197,21 @@ static i::MaybeHandle<i::JSObject> CreateModuleObject(
|
||||
i::wasm::ModuleOrigin::kWasmOrigin);
|
||||
}
|
||||
|
||||
static bool ValidateModule(v8::Isolate* isolate,
|
||||
const v8::Local<v8::Value> source,
|
||||
ErrorThrower* thrower) {
|
||||
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
||||
i::MaybeHandle<i::JSObject> nothing;
|
||||
|
||||
RawBuffer buffer = GetRawBufferSource(source, thrower);
|
||||
if (buffer.start == nullptr) return false;
|
||||
|
||||
DCHECK(source->IsArrayBuffer() || source->IsTypedArray());
|
||||
return i::wasm::ValidateModuleBytes(i_isolate, buffer.start, buffer.end,
|
||||
thrower,
|
||||
i::wasm::ModuleOrigin::kWasmOrigin);
|
||||
}
|
||||
|
||||
bool BrandCheck(Isolate* isolate, i::Handle<i::Object> value,
|
||||
i::Handle<i::Symbol> sym, const char* msg) {
|
||||
if (value->IsJSObject()) {
|
||||
@ -235,6 +250,25 @@ void WebAssemblyCompile(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
return_value.Set(resolver->GetPromise());
|
||||
}
|
||||
|
||||
void WebAssemblyValidate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
v8::Isolate* isolate = args.GetIsolate();
|
||||
HandleScope scope(isolate);
|
||||
ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate),
|
||||
"WebAssembly.validate()");
|
||||
|
||||
if (args.Length() < 1) {
|
||||
thrower.TypeError("Argument 0 must be a buffer source");
|
||||
return;
|
||||
}
|
||||
|
||||
v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
|
||||
if (ValidateModule(isolate, args[0], &thrower)) {
|
||||
return_value.Set(v8::True(isolate));
|
||||
} else {
|
||||
return_value.Set(v8::False(isolate));
|
||||
}
|
||||
}
|
||||
|
||||
void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
v8::Isolate* isolate = args.GetIsolate();
|
||||
HandleScope scope(isolate);
|
||||
@ -578,6 +612,9 @@ void WasmJs::InstallWasmConstructors(Isolate* isolate,
|
||||
// Setup compile
|
||||
InstallFunc(isolate, wasm_object, "compile", WebAssemblyCompile);
|
||||
|
||||
// Setup compile
|
||||
InstallFunc(isolate, wasm_object, "validate", WebAssemblyValidate);
|
||||
|
||||
// Setup Module
|
||||
Handle<JSFunction> module_constructor =
|
||||
InstallFunc(isolate, wasm_object, "Module", WebAssemblyModule);
|
||||
|
@ -1813,6 +1813,19 @@ MaybeHandle<JSObject> CreateModuleObjectFromBytes(Isolate* isolate,
|
||||
origin);
|
||||
}
|
||||
|
||||
bool ValidateModuleBytes(Isolate* isolate, const byte* start, const byte* end,
|
||||
ErrorThrower* thrower, ModuleOrigin origin) {
|
||||
Zone zone(isolate->allocator());
|
||||
ModuleResult result =
|
||||
DecodeWasmModule(isolate, &zone, start, end, false, origin);
|
||||
if (result.ok()) {
|
||||
DCHECK_NOT_NULL(result.val);
|
||||
delete result.val;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
MaybeHandle<JSArrayBuffer> GetInstanceMemory(Isolate* isolate,
|
||||
Handle<JSObject> instance) {
|
||||
Object* mem = instance->GetInternalField(kWasmMemArrayBuffer);
|
||||
|
@ -398,6 +398,11 @@ V8_EXPORT_PRIVATE MaybeHandle<JSObject> CreateModuleObjectFromBytes(
|
||||
Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower,
|
||||
ModuleOrigin origin);
|
||||
|
||||
V8_EXPORT_PRIVATE bool ValidateModuleBytes(Isolate* isolate, const byte* start,
|
||||
const byte* end,
|
||||
ErrorThrower* thrower,
|
||||
ModuleOrigin origin);
|
||||
|
||||
// Get the number of imported functions for a WASM instance.
|
||||
uint32_t GetNumImportedFunctions(Handle<JSObject> wasm_object);
|
||||
|
||||
|
@ -62,6 +62,10 @@ CheckInstance(new WebAssembly.Instance(module));
|
||||
let promise = WebAssembly.compile(buffer);
|
||||
promise.then(module => CheckInstance(new WebAssembly.Instance(module)));
|
||||
|
||||
// Check that validate works correctly for a module.
|
||||
assertTrue(WebAssembly.validate(buffer));
|
||||
assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
|
||||
|
||||
// Negative tests.
|
||||
(function InvalidModules() {
|
||||
print("InvalidModules...");
|
||||
@ -69,7 +73,7 @@ promise.then(module => CheckInstance(new WebAssembly.Instance(module)));
|
||||
let len = invalid_cases.length;
|
||||
for (var i = 0; i < len; ++i) {
|
||||
try {
|
||||
let instance = new WebAssembly.Instance(1);
|
||||
let instance = new WebAssembly.Instance(invalid_cases[i]);
|
||||
assertUnreachable("should not be able to instantiate invalid modules.");
|
||||
} catch (e) {
|
||||
assertContains("Argument 0", e.toString());
|
||||
|
@ -15,3 +15,4 @@ assertEquals('object', typeof WebAssembly);
|
||||
assertEquals('function', typeof WebAssembly.Module);
|
||||
assertEquals('function', typeof WebAssembly.Instance);
|
||||
assertEquals('function', typeof WebAssembly.compile);
|
||||
assertEquals('function', typeof WebAssembly.validate);
|
||||
|
Loading…
Reference in New Issue
Block a user