Commit 615c215f authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Ensure constness of WasmModule after decoding

We pass the {WasmModule} by non-const pointer and by non-const
reference a lot. This violates the style guide, and adds the risk of
accidentally modifying it.
This CL makes the {WasmModule} const during compilation and
instantiation.

R=mstarzinger@chromium.org

Bug: v8:7754
Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: Ie1878322828b63284b341d97da595e8e91dd4f51
Reviewed-on: https://chromium-review.googlesource.com/1117194Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54078}
parent cf66f737
......@@ -9450,7 +9450,7 @@ int debug::WasmScript::NumFunctions() const {
DCHECK_EQ(i::Script::TYPE_WASM, script->type());
i::WasmModuleObject* module_object =
i::WasmModuleObject::cast(script->wasm_module_object());
i::wasm::WasmModule* module = module_object->module();
const i::wasm::WasmModule* module = module_object->module();
DCHECK_GE(i::kMaxInt, module->functions.size());
return static_cast<int>(module->functions.size());
}
......@@ -9461,7 +9461,7 @@ int debug::WasmScript::NumImportedFunctions() const {
DCHECK_EQ(i::Script::TYPE_WASM, script->type());
i::WasmModuleObject* module_object =
i::WasmModuleObject::cast(script->wasm_module_object());
i::wasm::WasmModule* module = module_object->module();
const i::wasm::WasmModule* module = module_object->module();
DCHECK_GE(i::kMaxInt, module->num_imported_functions);
return static_cast<int>(module->num_imported_functions);
}
......@@ -9473,10 +9473,10 @@ std::pair<int, int> debug::WasmScript::GetFunctionRange(
DCHECK_EQ(i::Script::TYPE_WASM, script->type());
i::WasmModuleObject* module_object =
i::WasmModuleObject::cast(script->wasm_module_object());
i::wasm::WasmModule* module = module_object->module();
const i::wasm::WasmModule* module = module_object->module();
DCHECK_LE(0, function_index);
DCHECK_GT(module->functions.size(), function_index);
i::wasm::WasmFunction& func = module->functions[function_index];
const i::wasm::WasmFunction& func = module->functions[function_index];
DCHECK_GE(i::kMaxInt, func.code.offset());
DCHECK_GE(i::kMaxInt, func.code.end_offset());
return std::make_pair(static_cast<int>(func.code.offset()),
......@@ -9489,10 +9489,10 @@ uint32_t debug::WasmScript::GetFunctionHash(int function_index) {
DCHECK_EQ(i::Script::TYPE_WASM, script->type());
i::WasmModuleObject* module_object =
i::WasmModuleObject::cast(script->wasm_module_object());
i::wasm::WasmModule* module = module_object->module();
const i::wasm::WasmModule* module = module_object->module();
DCHECK_LE(0, function_index);
DCHECK_GT(module->functions.size(), function_index);
i::wasm::WasmFunction& func = module->functions[function_index];
const i::wasm::WasmFunction& func = module->functions[function_index];
i::wasm::ModuleWireBytes wire_bytes(
module_object->native_module()->wire_bytes());
i::Vector<const i::byte> function_bytes = wire_bytes.GetFunctionBytes(&func);
......
......@@ -4721,7 +4721,7 @@ class WasmWrapperGraphBuilder : public WasmGraphBuilder {
} // namespace
MaybeHandle<Code> CompileJSToWasmWrapper(
Isolate* isolate, wasm::WasmModule* module, Address call_target,
Isolate* isolate, const wasm::WasmModule* module, Address call_target,
uint32_t index, wasm::UseTrapHandler use_trap_handler) {
const wasm::WasmFunction* func = &module->functions[index];
......
......@@ -116,7 +116,7 @@ MaybeHandle<Code> CompileWasmToJSWrapper(Isolate*, Handle<JSReceiver> target,
// Wraps a given wasm code object, producing a code object.
V8_EXPORT_PRIVATE MaybeHandle<Code> CompileJSToWasmWrapper(
Isolate*, wasm::WasmModule*, Address call_target, uint32_t index,
Isolate*, const wasm::WasmModule*, Address call_target, uint32_t index,
wasm::UseTrapHandler);
// Compiles a stub that redirects a call to a wasm function to the wasm
......
This diff is collapsed.
......@@ -48,9 +48,9 @@ std::unique_ptr<CompilationState, CompilationStateDeleter> NewCompilationState(
ModuleEnv* GetModuleEnv(CompilationState* compilation_state);
MaybeHandle<WasmModuleObject> CompileToModuleObject(
Isolate* isolate, ErrorThrower* thrower, std::shared_ptr<WasmModule> module,
const ModuleWireBytes& wire_bytes, Handle<Script> asm_js_script,
Vector<const byte> asm_js_offset_table_bytes);
Isolate* isolate, ErrorThrower* thrower,
std::shared_ptr<const WasmModule> module, const ModuleWireBytes& wire_bytes,
Handle<Script> asm_js_script, Vector<const byte> asm_js_offset_table_bytes);
MaybeHandle<WasmInstanceObject> InstantiateToInstanceObject(
Isolate* isolate, ErrorThrower* thrower,
......@@ -148,7 +148,7 @@ class AsyncCompileJob {
ModuleWireBytes wire_bytes_;
Handle<Context> context_;
std::unique_ptr<CompilationResultResolver> resolver_;
std::shared_ptr<WasmModule> module_;
std::shared_ptr<const WasmModule> module_;
std::vector<DeferredHandles*> deferred_handles_;
Handle<WasmModuleObject> module_object_;
......
......@@ -612,7 +612,7 @@ void WasmDebugInfo::RedirectToInterpreter(Handle<WasmDebugInfo> debug_info,
Handle<WasmInstanceObject> instance(debug_info->wasm_instance(), isolate);
wasm::NativeModule* native_module =
instance->module_object()->native_module();
wasm::WasmModule* module = instance->module();
const wasm::WasmModule* module = instance->module();
// We may modify js wrappers, as well as wasm functions. Hence the 2
// modification scopes.
......
......@@ -129,7 +129,7 @@ Handle<JSArray> GetImports(Isolate* isolate,
Handle<String> global_string = factory->InternalizeUtf8String("global");
// Create the result array.
WasmModule* module = module_object->module();
const WasmModule* module = module_object->module();
int num_imports = static_cast<int>(module->import_table.size());
Handle<JSArray> array_object = factory->NewJSArray(PACKED_ELEMENTS, 0, 0);
Handle<FixedArray> storage = factory->NewFixedArray(num_imports);
......@@ -141,7 +141,7 @@ Handle<JSArray> GetImports(Isolate* isolate,
// Populate the result array.
for (int index = 0; index < num_imports; ++index) {
WasmImport& import = module->import_table[index];
const WasmImport& import = module->import_table[index];
Handle<JSObject> entry = factory->NewJSObject(object_function);
......@@ -196,7 +196,7 @@ Handle<JSArray> GetExports(Isolate* isolate,
Handle<String> global_string = factory->InternalizeUtf8String("global");
// Create the result array.
WasmModule* module = module_object->module();
const WasmModule* module = module_object->module();
int num_exports = static_cast<int>(module->export_table.size());
Handle<JSArray> array_object = factory->NewJSArray(PACKED_ELEMENTS, 0, 0);
Handle<FixedArray> storage = factory->NewFixedArray(num_exports);
......@@ -208,7 +208,7 @@ Handle<JSArray> GetExports(Isolate* isolate,
// Populate the result array.
for (int index = 0; index < num_exports; ++index) {
WasmExport& exp = module->export_table[index];
const WasmExport& exp = module->export_table[index];
Handle<String> export_kind;
switch (exp.kind) {
......
......@@ -52,7 +52,7 @@ CAST_ACCESSOR(WasmTableObject)
ACCESSORS(WasmModuleObject, managed_native_module, Managed<wasm::NativeModule>,
kNativeModuleOffset)
ACCESSORS(WasmModuleObject, export_wrappers, FixedArray, kExportWrappersOffset)
ACCESSORS(WasmModuleObject, managed_module, Managed<wasm::WasmModule>,
ACCESSORS(WasmModuleObject, managed_module, Managed<const wasm::WasmModule>,
kManagedModuleOffset)
ACCESSORS(WasmModuleObject, script, Script, kScriptOffset)
ACCESSORS(WasmModuleObject, weak_instance_list, WeakArrayList,
......@@ -61,7 +61,7 @@ OPTIONAL_ACCESSORS(WasmModuleObject, asm_js_offset_table, ByteArray,
kAsmJsOffsetTableOffset)
OPTIONAL_ACCESSORS(WasmModuleObject, breakpoint_infos, FixedArray,
kBreakPointInfosOffset)
wasm::WasmModule* WasmModuleObject::module() const {
const wasm::WasmModule* WasmModuleObject::module() const {
return managed_module()->raw();
}
wasm::NativeModule* WasmModuleObject::native_module() {
......
......@@ -176,17 +176,17 @@ enum DispatchTableElements : int {
Handle<WasmModuleObject> WasmModuleObject::New(
Isolate* isolate, Handle<FixedArray> export_wrappers,
std::shared_ptr<wasm::WasmModule> shared_module, wasm::ModuleEnv& env,
std::shared_ptr<const wasm::WasmModule> shared_module, wasm::ModuleEnv& env,
std::unique_ptr<const uint8_t[]> wire_bytes, size_t wire_bytes_len,
Handle<Script> script, Handle<ByteArray> asm_js_offset_table) {
WasmModule* module = shared_module.get();
const WasmModule* module = shared_module.get();
DCHECK_EQ(module, env.module);
size_t module_size = EstimateWasmModuleSize(module);
// The {managed_module} will take shared ownership of the {WasmModule} object,
// and release it when the GC reclaim the managed.
Handle<Managed<WasmModule>> managed_module =
Managed<WasmModule>::FromSharedPtr(isolate, module_size,
std::move(shared_module));
Handle<Managed<const WasmModule>> managed_module =
Managed<const WasmModule>::FromSharedPtr(isolate, module_size,
std::move(shared_module));
// Now create the {WasmModuleObject}.
Handle<JSFunction> module_cons(
......@@ -228,7 +228,7 @@ bool WasmModuleObject::SetBreakPoint(Handle<WasmModuleObject> module_object,
// Find the function for this breakpoint.
int func_index = module_object->GetContainingFunction(*position);
if (func_index < 0) return false;
WasmFunction& func = module_object->module()->functions[func_index];
const WasmFunction& func = module_object->module()->functions[func_index];
int offset_in_func = *position - func.code.offset();
// According to the current design, we should only be called with valid
......@@ -372,7 +372,7 @@ void WasmModuleObject::SetBreakpointsOnNewInstance(
// Find the function for this breakpoint, and set the breakpoint.
int func_index = module_object->GetContainingFunction(position);
DCHECK_LE(0, func_index);
WasmFunction& func = module_object->module()->functions[func_index];
const WasmFunction& func = module_object->module()->functions[func_index];
int offset_in_func = position - func.code.offset();
WasmDebugInfo::SetBreakpoint(debug_info, func_index, offset_in_func);
}
......@@ -431,7 +431,8 @@ Handle<ByteArray> GetDecodedAsmJsOffsetTable(
module_object->set_asm_js_offset_table(*decoded_table);
int idx = 0;
std::vector<WasmFunction>& wasm_funs = module_object->module()->functions;
const std::vector<WasmFunction>& wasm_funs =
module_object->module()->functions;
for (int func = 0; func < num_functions; ++func) {
std::vector<wasm::AsmJsOffsetEntry>& func_asm_offsets =
asm_offsets.val[func];
......@@ -520,7 +521,7 @@ bool WasmModuleObject::GetPossibleBreakpoints(
std::vector<v8::debug::BreakLocation>* locations) {
DisallowHeapAllocation no_gc;
std::vector<WasmFunction>& functions = module()->functions;
const std::vector<WasmFunction>& functions = module()->functions;
if (start.GetLineNumber() < 0 || start.GetColumnNumber() < 0 ||
(!end.IsEmpty() &&
(end.GetLineNumber() < 0 || end.GetColumnNumber() < 0)))
......@@ -566,7 +567,7 @@ bool WasmModuleObject::GetPossibleBreakpoints(
for (uint32_t func_idx = start_func_index; func_idx <= end_func_index;
++func_idx) {
WasmFunction& func = functions[func_idx];
const WasmFunction& func = functions[func_idx];
if (func.code.length() == 0) continue;
wasm::BodyLocalDecls locals(&tmp);
......@@ -637,7 +638,7 @@ MaybeHandle<String> WasmModuleObject::ExtractUtf8StringFromModuleBytes(
MaybeHandle<String> WasmModuleObject::GetModuleNameOrNull(
Isolate* isolate, Handle<WasmModuleObject> module_object) {
WasmModule* module = module_object->module();
const WasmModule* module = module_object->module();
if (!module->name.is_set()) return {};
return ExtractUtf8StringFromModuleBytes(isolate, module_object, module->name);
}
......@@ -672,14 +673,14 @@ Vector<const uint8_t> WasmModuleObject::GetRawFunctionName(
}
int WasmModuleObject::GetFunctionOffset(uint32_t func_index) {
std::vector<WasmFunction>& functions = module()->functions;
const std::vector<WasmFunction>& functions = module()->functions;
if (static_cast<uint32_t>(func_index) >= functions.size()) return -1;
DCHECK_GE(kMaxInt, functions[func_index].code.offset());
return static_cast<int>(functions[func_index].code.offset());
}
int WasmModuleObject::GetContainingFunction(uint32_t byte_offset) {
std::vector<WasmFunction>& functions = module()->functions;
const std::vector<WasmFunction>& functions = module()->functions;
// Binary search for a function containing the given position.
int left = 0; // inclusive
......@@ -694,7 +695,7 @@ int WasmModuleObject::GetContainingFunction(uint32_t byte_offset) {
}
}
// If the found function does not contains the given position, return -1.
WasmFunction& func = functions[left];
const WasmFunction& func = functions[left];
if (byte_offset < func.code.offset() ||
byte_offset >= func.code.end_offset()) {
return -1;
......@@ -708,7 +709,7 @@ bool WasmModuleObject::GetPositionInfo(uint32_t position,
int func_index = GetContainingFunction(position);
if (func_index < 0) return false;
WasmFunction& function = module()->functions[func_index];
const WasmFunction& function = module()->functions[func_index];
info->line = func_index;
info->column = position - function.code.offset();
......@@ -1188,7 +1189,9 @@ void WasmInstanceObject::SetRawMemory(byte* mem_start, uint32_t mem_size) {
set_memory_mask(mem_mask64);
}
WasmModule* WasmInstanceObject::module() { return module_object()->module(); }
const WasmModule* WasmInstanceObject::module() {
return module_object()->module();
}
Handle<WasmDebugInfo> WasmInstanceObject::GetOrCreateDebugInfo(
Handle<WasmInstanceObject> instance) {
......
......@@ -107,8 +107,8 @@ class WasmModuleObject : public JSObject {
DECL_ACCESSORS(managed_native_module, Managed<wasm::NativeModule>)
inline wasm::NativeModule* native_module();
DECL_ACCESSORS(export_wrappers, FixedArray)
DECL_ACCESSORS(managed_module, Managed<wasm::WasmModule>)
inline wasm::WasmModule* module() const;
DECL_ACCESSORS(managed_module, Managed<const wasm::WasmModule>)
inline const wasm::WasmModule* module() const;
DECL_ACCESSORS(script, Script)
DECL_ACCESSORS(weak_instance_list, WeakArrayList)
DECL_OPTIONAL_ACCESSORS(asm_js_offset_table, ByteArray)
......@@ -136,7 +136,7 @@ class WasmModuleObject : public JSObject {
static Handle<WasmModuleObject> New(
Isolate* isolate, Handle<FixedArray> export_wrappers,
std::shared_ptr<wasm::WasmModule> module, wasm::ModuleEnv& env,
std::shared_ptr<const wasm::WasmModule> module, wasm::ModuleEnv& env,
std::unique_ptr<const uint8_t[]> wire_bytes, size_t wire_bytes_len,
Handle<Script> script, Handle<ByteArray> asm_js_offset_table);
......@@ -437,7 +437,7 @@ class WasmInstanceObject : public JSObject {
WASM_INSTANCE_OBJECT_FIELDS)
#undef WASM_INSTANCE_OBJECT_FIELDS
V8_EXPORT_PRIVATE wasm::WasmModule* module();
V8_EXPORT_PRIVATE const wasm::WasmModule* module();
static bool EnsureIndirectFunctionTableWithMinimumSize(
Handle<WasmInstanceObject> instance, uint32_t minimum_size);
......
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