Commit 4feecc66 authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[wasm] Clean up {ExtractUtf8StringFromModuleBytes}

Change a {MaybeHandle} to {Handle}. We do utf8-validation during decode
time (and verify this via DCHECK in {ExtractUtf8StringFromModuleBytes}),
so the only case where it could happen that we return an empty handle
there would be in an out-of-memory situation, and this is not handled
correctly anyway, so it is better to just crash in that case (via
{ToHandleChecked}).

R=ahaas@chromium.org

Bug: chromium:1036737
Change-Id: I6a0d94d920ab5dd7deecfa3e3033bdb5d266ffa7
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1993288Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65679}
parent 921d5ca1
...@@ -2730,8 +2730,7 @@ Handle<Script> CreateWasmScript(Isolate* isolate, ...@@ -2730,8 +2730,7 @@ Handle<Script> CreateWasmScript(Isolate* isolate,
.ToHandleChecked(); .ToHandleChecked();
Handle<String> module_name = Handle<String> module_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes( WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, wire_bytes.module_bytes(), name) isolate, wire_bytes.module_bytes(), name);
.ToHandleChecked();
name_str = isolate->factory() name_str = isolate->factory()
->NewConsString(module_name, name_hash) ->NewConsString(module_name, name_hash)
.ToHandleChecked(); .ToHandleChecked();
......
...@@ -776,25 +776,13 @@ void InstanceBuilder::SanitizeImports() { ...@@ -776,25 +776,13 @@ void InstanceBuilder::SanitizeImports() {
for (size_t index = 0; index < module_->import_table.size(); ++index) { for (size_t index = 0; index < module_->import_table.size(); ++index) {
const WasmImport& import = module_->import_table[index]; const WasmImport& import = module_->import_table[index];
Handle<String> module_name; Handle<String> module_name =
MaybeHandle<String> maybe_module_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(isolate_, wire_bytes, WasmModuleObject::ExtractUtf8StringFromModuleBytes(isolate_, wire_bytes,
import.module_name); import.module_name);
if (!maybe_module_name.ToHandle(&module_name)) {
thrower_->LinkError("Could not resolve module name for import %zu",
index);
return;
}
Handle<String> import_name; Handle<String> import_name =
MaybeHandle<String> maybe_import_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(isolate_, wire_bytes, WasmModuleObject::ExtractUtf8StringFromModuleBytes(isolate_, wire_bytes,
import.field_name); import.field_name);
if (!maybe_import_name.ToHandle(&import_name)) {
thrower_->LinkError("Could not resolve import name for import %zu",
index);
return;
}
int int_index = static_cast<int>(index); int int_index = static_cast<int>(index);
MaybeHandle<Object> result = MaybeHandle<Object> result =
...@@ -1493,8 +1481,7 @@ void InstanceBuilder::ProcessExports(Handle<WasmInstanceObject> instance) { ...@@ -1493,8 +1481,7 @@ void InstanceBuilder::ProcessExports(Handle<WasmInstanceObject> instance) {
// Process each export in the export table. // Process each export in the export table.
for (const WasmExport& exp : module_->export_table) { for (const WasmExport& exp : module_->export_table) {
Handle<String> name = WasmModuleObject::ExtractUtf8StringFromModuleBytes( Handle<String> name = WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate_, module_object_, exp.name) isolate_, module_object_, exp.name);
.ToHandleChecked();
Handle<JSObject> export_to = exports_object; Handle<JSObject> export_to = exports_object;
switch (exp.kind) { switch (exp.kind) {
case kExternalFunction: { case kExternalFunction: {
......
...@@ -399,18 +399,16 @@ Handle<JSArray> GetImports(Isolate* isolate, ...@@ -399,18 +399,16 @@ Handle<JSArray> GetImports(Isolate* isolate,
UNREACHABLE(); UNREACHABLE();
} }
MaybeHandle<String> import_module = Handle<String> import_module =
WasmModuleObject::ExtractUtf8StringFromModuleBytes( WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, import.module_name); isolate, module_object, import.module_name);
MaybeHandle<String> import_name = Handle<String> import_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes( WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, import.field_name); isolate, module_object, import.field_name);
JSObject::AddProperty(isolate, entry, module_string, JSObject::AddProperty(isolate, entry, module_string, import_module, NONE);
import_module.ToHandleChecked(), NONE); JSObject::AddProperty(isolate, entry, name_string, import_name, NONE);
JSObject::AddProperty(isolate, entry, name_string,
import_name.ToHandleChecked(), NONE);
JSObject::AddProperty(isolate, entry, kind_string, import_kind, NONE); JSObject::AddProperty(isolate, entry, kind_string, import_kind, NONE);
if (!type_value.is_null()) { if (!type_value.is_null()) {
JSObject::AddProperty(isolate, entry, type_string, type_value, NONE); JSObject::AddProperty(isolate, entry, type_string, type_value, NONE);
...@@ -501,12 +499,11 @@ Handle<JSArray> GetExports(Isolate* isolate, ...@@ -501,12 +499,11 @@ Handle<JSArray> GetExports(Isolate* isolate,
Handle<JSObject> entry = factory->NewJSObject(object_function); Handle<JSObject> entry = factory->NewJSObject(object_function);
MaybeHandle<String> export_name = Handle<String> export_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes( WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, exp.name); isolate, module_object, exp.name);
JSObject::AddProperty(isolate, entry, name_string, JSObject::AddProperty(isolate, entry, name_string, export_name, NONE);
export_name.ToHandleChecked(), NONE);
JSObject::AddProperty(isolate, entry, kind_string, export_kind, NONE); JSObject::AddProperty(isolate, entry, kind_string, export_kind, NONE);
if (!type_value.is_null()) { if (!type_value.is_null()) {
JSObject::AddProperty(isolate, entry, type_string, type_value, NONE); JSObject::AddProperty(isolate, entry, type_string, type_value, NONE);
...@@ -532,11 +529,11 @@ Handle<JSArray> GetCustomSections(Isolate* isolate, ...@@ -532,11 +529,11 @@ Handle<JSArray> GetCustomSections(Isolate* isolate,
// Gather matching sections. // Gather matching sections.
for (auto& section : custom_sections) { for (auto& section : custom_sections) {
MaybeHandle<String> section_name = Handle<String> section_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes( WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, section.name); isolate, module_object, section.name);
if (!name->Equals(*section_name.ToHandleChecked())) continue; if (!name->Equals(*section_name)) continue;
// Make a copy of the payload data in the section. // Make a copy of the payload data in the section.
size_t size = section.payload.length(); size_t size = section.payload.length();
...@@ -583,8 +580,7 @@ Handle<FixedArray> DecodeLocalNames(Isolate* isolate, ...@@ -583,8 +580,7 @@ Handle<FixedArray> DecodeLocalNames(Isolate* isolate,
for (LocalName& name : func.names) { for (LocalName& name : func.names) {
Handle<String> name_str = Handle<String> name_str =
WasmModuleObject::ExtractUtf8StringFromModuleBytes( WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, name.name) isolate, module_object, name.name);
.ToHandleChecked();
func_locals_names->set(name.local_index, *name_str); func_locals_names->set(name.local_index, *name_str);
} }
} }
......
...@@ -324,7 +324,7 @@ int WasmModuleObject::GetSourcePosition(Handle<WasmModuleObject> module_object, ...@@ -324,7 +324,7 @@ int WasmModuleObject::GetSourcePosition(Handle<WasmModuleObject> module_object,
return offset_table->get_int(kOTESize * left + idx); return offset_table->get_int(kOTESize * left + idx);
} }
MaybeHandle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes( Handle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
Isolate* isolate, Handle<WasmModuleObject> module_object, Isolate* isolate, Handle<WasmModuleObject> module_object,
wasm::WireBytesRef ref) { wasm::WireBytesRef ref) {
// TODO(wasm): cache strings from modules if it's a performance win. // TODO(wasm): cache strings from modules if it's a performance win.
...@@ -333,15 +333,16 @@ MaybeHandle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes( ...@@ -333,15 +333,16 @@ MaybeHandle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
return ExtractUtf8StringFromModuleBytes(isolate, wire_bytes, ref); return ExtractUtf8StringFromModuleBytes(isolate, wire_bytes, ref);
} }
MaybeHandle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes( Handle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
Isolate* isolate, Vector<const uint8_t> wire_bytes, Isolate* isolate, Vector<const uint8_t> wire_bytes,
wasm::WireBytesRef ref) { wasm::WireBytesRef ref) {
Vector<const uint8_t> name_vec = wire_bytes + ref.offset(); Vector<const uint8_t> name_vec =
name_vec.Truncate(ref.length()); wire_bytes.SubVector(ref.offset(), ref.end_offset());
// UTF8 validation happens at decode time. // UTF8 validation happens at decode time.
DCHECK(unibrow::Utf8::ValidateEncoding(name_vec.begin(), name_vec.length())); DCHECK(unibrow::Utf8::ValidateEncoding(name_vec.begin(), name_vec.length()));
return isolate->factory()->NewStringFromUtf8( return isolate->factory()
Vector<const char>::cast(name_vec)); ->NewStringFromUtf8(Vector<const char>::cast(name_vec))
.ToHandleChecked();
} }
MaybeHandle<String> WasmModuleObject::GetModuleNameOrNull( MaybeHandle<String> WasmModuleObject::GetModuleNameOrNull(
......
...@@ -187,13 +187,10 @@ class WasmModuleObject : public JSObject { ...@@ -187,13 +187,10 @@ class WasmModuleObject : public JSObject {
bool is_at_number_conversion); bool is_at_number_conversion);
// Extract a portion of the wire bytes as UTF-8 string. // Extract a portion of the wire bytes as UTF-8 string.
// Returns a null handle if the respective bytes do not form a valid UTF-8 static Handle<String> ExtractUtf8StringFromModuleBytes(
// string. Isolate*, Handle<WasmModuleObject>, wasm::WireBytesRef);
static MaybeHandle<String> ExtractUtf8StringFromModuleBytes( static Handle<String> ExtractUtf8StringFromModuleBytes(
Isolate* isolate, Handle<WasmModuleObject>, wasm::WireBytesRef ref); Isolate*, Vector<const uint8_t> wire_byte, wasm::WireBytesRef);
static MaybeHandle<String> ExtractUtf8StringFromModuleBytes(
Isolate* isolate, Vector<const uint8_t> wire_byte,
wasm::WireBytesRef ref);
OBJECT_CONSTRUCTORS(WasmModuleObject, JSObject); OBJECT_CONSTRUCTORS(WasmModuleObject, JSObject);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment