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,
.ToHandleChecked();
Handle<String> module_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, wire_bytes.module_bytes(), name)
.ToHandleChecked();
isolate, wire_bytes.module_bytes(), name);
name_str = isolate->factory()
->NewConsString(module_name, name_hash)
.ToHandleChecked();
......
......@@ -776,25 +776,13 @@ void InstanceBuilder::SanitizeImports() {
for (size_t index = 0; index < module_->import_table.size(); ++index) {
const WasmImport& import = module_->import_table[index];
Handle<String> module_name;
MaybeHandle<String> maybe_module_name =
Handle<String> module_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(isolate_, wire_bytes,
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;
MaybeHandle<String> maybe_import_name =
Handle<String> import_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(isolate_, wire_bytes,
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);
MaybeHandle<Object> result =
......@@ -1493,8 +1481,7 @@ void InstanceBuilder::ProcessExports(Handle<WasmInstanceObject> instance) {
// Process each export in the export table.
for (const WasmExport& exp : module_->export_table) {
Handle<String> name = WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate_, module_object_, exp.name)
.ToHandleChecked();
isolate_, module_object_, exp.name);
Handle<JSObject> export_to = exports_object;
switch (exp.kind) {
case kExternalFunction: {
......
......@@ -399,18 +399,16 @@ Handle<JSArray> GetImports(Isolate* isolate,
UNREACHABLE();
}
MaybeHandle<String> import_module =
Handle<String> import_module =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, import.module_name);
MaybeHandle<String> import_name =
Handle<String> import_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, import.field_name);
JSObject::AddProperty(isolate, entry, module_string,
import_module.ToHandleChecked(), NONE);
JSObject::AddProperty(isolate, entry, name_string,
import_name.ToHandleChecked(), NONE);
JSObject::AddProperty(isolate, entry, module_string, import_module, NONE);
JSObject::AddProperty(isolate, entry, name_string, import_name, NONE);
JSObject::AddProperty(isolate, entry, kind_string, import_kind, NONE);
if (!type_value.is_null()) {
JSObject::AddProperty(isolate, entry, type_string, type_value, NONE);
......@@ -501,12 +499,11 @@ Handle<JSArray> GetExports(Isolate* isolate,
Handle<JSObject> entry = factory->NewJSObject(object_function);
MaybeHandle<String> export_name =
Handle<String> export_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, exp.name);
JSObject::AddProperty(isolate, entry, name_string,
export_name.ToHandleChecked(), NONE);
JSObject::AddProperty(isolate, entry, name_string, export_name, NONE);
JSObject::AddProperty(isolate, entry, kind_string, export_kind, NONE);
if (!type_value.is_null()) {
JSObject::AddProperty(isolate, entry, type_string, type_value, NONE);
......@@ -532,11 +529,11 @@ Handle<JSArray> GetCustomSections(Isolate* isolate,
// Gather matching sections.
for (auto& section : custom_sections) {
MaybeHandle<String> section_name =
Handle<String> section_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(
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.
size_t size = section.payload.length();
......@@ -583,8 +580,7 @@ Handle<FixedArray> DecodeLocalNames(Isolate* isolate,
for (LocalName& name : func.names) {
Handle<String> name_str =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, module_object, name.name)
.ToHandleChecked();
isolate, module_object, name.name);
func_locals_names->set(name.local_index, *name_str);
}
}
......
......@@ -324,7 +324,7 @@ int WasmModuleObject::GetSourcePosition(Handle<WasmModuleObject> module_object,
return offset_table->get_int(kOTESize * left + idx);
}
MaybeHandle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
Handle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
Isolate* isolate, Handle<WasmModuleObject> module_object,
wasm::WireBytesRef ref) {
// TODO(wasm): cache strings from modules if it's a performance win.
......@@ -333,15 +333,16 @@ MaybeHandle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
return ExtractUtf8StringFromModuleBytes(isolate, wire_bytes, ref);
}
MaybeHandle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
Handle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
Isolate* isolate, Vector<const uint8_t> wire_bytes,
wasm::WireBytesRef ref) {
Vector<const uint8_t> name_vec = wire_bytes + ref.offset();
name_vec.Truncate(ref.length());
Vector<const uint8_t> name_vec =
wire_bytes.SubVector(ref.offset(), ref.end_offset());
// UTF8 validation happens at decode time.
DCHECK(unibrow::Utf8::ValidateEncoding(name_vec.begin(), name_vec.length()));
return isolate->factory()->NewStringFromUtf8(
Vector<const char>::cast(name_vec));
return isolate->factory()
->NewStringFromUtf8(Vector<const char>::cast(name_vec))
.ToHandleChecked();
}
MaybeHandle<String> WasmModuleObject::GetModuleNameOrNull(
......
......@@ -187,13 +187,10 @@ class WasmModuleObject : public JSObject {
bool is_at_number_conversion);
// 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
// string.
static MaybeHandle<String> ExtractUtf8StringFromModuleBytes(
Isolate* isolate, Handle<WasmModuleObject>, wasm::WireBytesRef ref);
static MaybeHandle<String> ExtractUtf8StringFromModuleBytes(
Isolate* isolate, Vector<const uint8_t> wire_byte,
wasm::WireBytesRef ref);
static Handle<String> ExtractUtf8StringFromModuleBytes(
Isolate*, Handle<WasmModuleObject>, wasm::WireBytesRef);
static Handle<String> ExtractUtf8StringFromModuleBytes(
Isolate*, Vector<const uint8_t> wire_byte, wasm::WireBytesRef);
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