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,
// compilation).
std::unordered_map<uint32_t, uint32_t> num_table_exports;
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) {
Code* code =
Code::cast(code_table->get(static_cast<int>(func_index)));
......@@ -1806,7 +1806,7 @@ void InstanceBuilder::LoadTableSegments(Handle<FixedArray> code_table,
// TODO(titzer): this does redundant work if there are multiple tables,
// 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 num_entries = static_cast<uint32_t>(table_init.entries.size());
DCHECK(in_bounds(base, num_entries,
......
......@@ -660,8 +660,7 @@ class ModuleDecoder : public Decoder {
WasmInitExpr offset = consume_init_expr(module_.get(), kWasmI32);
uint32_t num_elem =
consume_count("number of elements", kV8MaxWasmTableEntries);
std::vector<uint32_t> vector;
module_->table_inits.push_back({table_index, offset, vector});
module_->table_inits.emplace_back(table_index, offset);
WasmTableInit* init = &module_->table_inits.back();
for (uint32_t j = 0; j < num_elem; j++) {
WasmFunction* func = nullptr;
......
......@@ -169,7 +169,7 @@ class InterpreterHandle {
SeqOneByteString* bytes_str =
debug_info->wasm_instance()->compiled_module()->module_bytes();
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_; }
......
......@@ -34,6 +34,15 @@ class CallDescriptor;
namespace wasm {
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 {
kExternalFunction = 0,
kExternalTable = 1,
......@@ -142,13 +151,22 @@ struct WasmIndirectFunctionTable {
bool imported = false; // true if imported.
bool exported = false; // true if exported.
SignatureMap map; // canonicalizing map for sig indexes.
private:
WASM_MOVE_ONLY_STRUCT(WasmIndirectFunctionTable);
};
// Static representation of how to initialize a table.
struct WasmTableInit {
WasmTableInit(uint32_t table_index, WasmInitExpr offset)
: table_index(table_index), offset(offset) {}
uint32_t table_index;
WasmInitExpr offset;
std::vector<uint32_t> entries;
private:
WASM_MOVE_ONLY_STRUCT(WasmTableInit);
};
// Static representation of a wasm import.
......@@ -210,6 +228,8 @@ struct V8_EXPORT_PRIVATE WasmModule {
private:
// TODO(kschimpf) - Encapsulate more fields.
ModuleOrigin origin_ = kWasmOrigin; // origin of the module
DISALLOW_COPY_AND_ASSIGN(WasmModule);
};
typedef Managed<WasmModule> WasmModuleWrapper;
......@@ -251,6 +271,9 @@ struct WasmInstance {
code = handle(*code, isolate);
}
}
private:
WASM_MOVE_ONLY_STRUCT(WasmInstance);
};
// Interface to the storage (wire bytes) of a wasm module.
......@@ -369,6 +392,9 @@ struct V8_EXPORT_PRIVATE ModuleEnv {
DCHECK_NOT_NULL(instance);
return instance->function_code[index];
}
private:
WASM_MOVE_ONLY_STRUCT(ModuleEnv);
};
// A ModuleEnv together with ModuleWireBytes.
......@@ -546,6 +572,9 @@ void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj);
void ValidateOrphanedInstance(Isolate* isolate,
Handle<WasmInstanceObject> instance);
} // namespace testing
#undef WASM_MOVE_ONLY_STRUCT
} // namespace wasm
} // namespace internal
} // 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