Commit 043f4cd5 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Make more central data structure move-only

This makes all data structures containing non-trivially-copyable fields
move-only, to prevent security and performance bugs.

Drive-by: Fix smaller performance bugs found by this refactoring.

R=titzer@chromium.org

Change-Id: I6802ac3591534c2ab5cacb2ca42b737f3b7fa801
Reviewed-on: https://chromium-review.googlesource.com/576170Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46747}
parent e825c431
...@@ -1791,7 +1791,7 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table, ...@@ -1791,7 +1791,7 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table,
// compilation). // compilation).
std::unordered_map<uint32_t, uint32_t> num_table_exports; std::unordered_map<uint32_t, uint32_t> num_table_exports;
if (compile_lazy(module_)) { if (compile_lazy(module_)) {
for (auto table_init : module_->table_inits) { for (auto& table_init : module_->table_inits) {
for (uint32_t func_index : table_init.entries) { for (uint32_t func_index : table_init.entries) {
Code* code = Code* code =
Code::cast(code_table->get(static_cast<int>(func_index))); Code::cast(code_table->get(static_cast<int>(func_index)));
...@@ -1806,7 +1806,7 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table, ...@@ -1806,7 +1806,7 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table,
// TODO(titzer): this does redundant work if there are multiple tables, // TODO(titzer): this does redundant work if there are multiple tables,
// since initializations are not sorted by table index. // since initializations are not sorted by table index.
for (auto table_init : module_->table_inits) { for (auto& table_init : module_->table_inits) {
uint32_t base = EvalUint32InitExpr(table_init.offset); uint32_t base = EvalUint32InitExpr(table_init.offset);
uint32_t num_entries = static_cast<uint32_t>(table_init.entries.size()); uint32_t num_entries = static_cast<uint32_t>(table_init.entries.size());
DCHECK(in_bounds(base, num_entries, DCHECK(in_bounds(base, num_entries,
......
...@@ -660,8 +660,7 @@ class ModuleDecoder : public Decoder { ...@@ -660,8 +660,7 @@ class ModuleDecoder : public Decoder {
WasmInitExpr offset = consume_init_expr(module_.get(), kWasmI32); WasmInitExpr offset = consume_init_expr(module_.get(), kWasmI32);
uint32_t num_elem = uint32_t num_elem =
consume_count("number of elements", kV8MaxWasmTableEntries); consume_count("number of elements", kV8MaxWasmTableEntries);
std::vector<uint32_t> vector; module_->table_inits.emplace_back(table_index, offset);
module_->table_inits.push_back({table_index, offset, vector});
WasmTableInit* init = &module_->table_inits.back(); WasmTableInit* init = &module_->table_inits.back();
for (uint32_t j = 0; j < num_elem; j++) { for (uint32_t j = 0; j < num_elem; j++) {
WasmFunction* func = nullptr; WasmFunction* func = nullptr;
......
...@@ -169,7 +169,7 @@ class InterpreterHandle { ...@@ -169,7 +169,7 @@ class InterpreterHandle {
SeqOneByteString* bytes_str = SeqOneByteString* bytes_str =
debug_info->wasm_instance()->compiled_module()->module_bytes(); debug_info->wasm_instance()->compiled_module()->module_bytes();
Vector<const byte> bytes(bytes_str->GetChars(), bytes_str->length()); Vector<const byte> bytes(bytes_str->GetChars(), bytes_str->length());
return ModuleBytesEnv(instance->module, instance, bytes); return {instance->module, instance, bytes};
} }
WasmInterpreter* interpreter() { return &interpreter_; } WasmInterpreter* interpreter() { return &interpreter_; }
......
...@@ -34,6 +34,15 @@ class CallDescriptor; ...@@ -34,6 +34,15 @@ class CallDescriptor;
namespace wasm { namespace wasm {
class ErrorThrower; class ErrorThrower;
// Use this in the private section to mark a struct move-only.
#define WASM_MOVE_ONLY_STRUCT(name) \
public: \
name() = default; \
name(name&&) = default; \
\
private: \
DISALLOW_COPY_AND_ASSIGN(name)
enum WasmExternalKind { enum WasmExternalKind {
kExternalFunction = 0, kExternalFunction = 0,
kExternalTable = 1, kExternalTable = 1,
...@@ -142,13 +151,22 @@ struct WasmIndirectFunctionTable { ...@@ -142,13 +151,22 @@ struct WasmIndirectFunctionTable {
bool imported = false; // true if imported. bool imported = false; // true if imported.
bool exported = false; // true if exported. bool exported = false; // true if exported.
SignatureMap map; // canonicalizing map for sig indexes. SignatureMap map; // canonicalizing map for sig indexes.
private:
WASM_MOVE_ONLY_STRUCT(WasmIndirectFunctionTable);
}; };
// Static representation of how to initialize a table. // Static representation of how to initialize a table.
struct WasmTableInit { struct WasmTableInit {
WasmTableInit(uint32_t table_index, WasmInitExpr offset)
: table_index(table_index), offset(offset) {}
uint32_t table_index; uint32_t table_index;
WasmInitExpr offset; WasmInitExpr offset;
std::vector<uint32_t> entries; std::vector<uint32_t> entries;
private:
WASM_MOVE_ONLY_STRUCT(WasmTableInit);
}; };
// Static representation of a wasm import. // Static representation of a wasm import.
...@@ -210,6 +228,8 @@ struct V8_EXPORT_PRIVATE WasmModule { ...@@ -210,6 +228,8 @@ struct V8_EXPORT_PRIVATE WasmModule {
private: private:
// TODO(kschimpf) - Encapsulate more fields. // TODO(kschimpf) - Encapsulate more fields.
ModuleOrigin origin_ = kWasmOrigin; // origin of the module ModuleOrigin origin_ = kWasmOrigin; // origin of the module
DISALLOW_COPY_AND_ASSIGN(WasmModule);
}; };
typedef Managed<WasmModule> WasmModuleWrapper; typedef Managed<WasmModule> WasmModuleWrapper;
...@@ -251,6 +271,9 @@ struct WasmInstance { ...@@ -251,6 +271,9 @@ struct WasmInstance {
code = handle(*code, isolate); code = handle(*code, isolate);
} }
} }
private:
WASM_MOVE_ONLY_STRUCT(WasmInstance);
}; };
// Interface to the storage (wire bytes) of a wasm module. // Interface to the storage (wire bytes) of a wasm module.
...@@ -369,6 +392,9 @@ struct V8_EXPORT_PRIVATE ModuleEnv { ...@@ -369,6 +392,9 @@ struct V8_EXPORT_PRIVATE ModuleEnv {
DCHECK_NOT_NULL(instance); DCHECK_NOT_NULL(instance);
return instance->function_code[index]; return instance->function_code[index];
} }
private:
WASM_MOVE_ONLY_STRUCT(ModuleEnv);
}; };
// A ModuleEnv together with ModuleWireBytes. // A ModuleEnv together with ModuleWireBytes.
...@@ -546,6 +572,9 @@ void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj); ...@@ -546,6 +572,9 @@ void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj);
void ValidateOrphanedInstance(Isolate* isolate, void ValidateOrphanedInstance(Isolate* isolate,
Handle<WasmInstanceObject> instance); Handle<WasmInstanceObject> instance);
} // namespace testing } // namespace testing
#undef WASM_MOVE_ONLY_STRUCT
} // namespace wasm } // namespace wasm
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
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