Commit a8327749 authored by Ivica Bogosavljevic's avatar Ivica Bogosavljevic Committed by Commit Bot

[wasm] Force proper code alignment in wasm serializer

Test mjsunit/wasm/compiled-module-serialization fails on those
architectures that do not support missaligned memory access.
We fix this by adding padding between code header and code start
in NativeModule serializer/deserializer so the code start is
properly aligned.

TEST=mjsunit/wasm/compiled-module-serializationx

Change-Id: I4f35b78a1190194088795b6f09becc3ad4251fdb
Reviewed-on: https://chromium-review.googlesource.com/1044186
Commit-Queue: Ivica Bogosavljevic <ivica.bogosavljevic@mips.com>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53117}
parent 0b4b14bc
...@@ -65,6 +65,18 @@ class Writer { ...@@ -65,6 +65,18 @@ class Writer {
} }
} }
void Align(size_t alignment) {
size_t num_written_bytes = bytes_written();
if (num_written_bytes % alignment) {
size_t padding = alignment - num_written_bytes % alignment;
pos_ = pos_ + padding;
if (FLAG_wasm_trace_serialization) {
OFStream os(stdout);
os << "wrote padding, sized: " << padding << std::endl;
}
}
}
private: private:
byte* const start_; byte* const start_;
byte* const end_; byte* const end_;
...@@ -108,6 +120,18 @@ class Reader { ...@@ -108,6 +120,18 @@ class Reader {
} }
} }
void Align(size_t alignment) {
size_t num_read_bytes = bytes_read();
if (num_read_bytes % alignment) {
size_t padding = alignment - num_read_bytes % alignment;
pos_ = pos_ + padding;
if (FLAG_wasm_trace_serialization) {
OFStream os(stdout);
os << "read padding, sized: " << padding << std::endl;
}
}
}
void Skip(size_t size) { pos_ += size; } void Skip(size_t size) { pos_ += size; }
private: private:
...@@ -261,6 +285,9 @@ size_t NativeModuleSerializer::Measure() const { ...@@ -261,6 +285,9 @@ size_t NativeModuleSerializer::Measure() const {
uint32_t total_fns = native_module_->function_count(); uint32_t total_fns = native_module_->function_count();
for (uint32_t i = first_wasm_fn; i < total_fns; ++i) { for (uint32_t i = first_wasm_fn; i < total_fns; ++i) {
size += kCodeHeaderSize; size += kCodeHeaderSize;
// Code start needs to be kPointerSize aligned
// TODO(all) Remove padding after code reallocation is removed
size = RoundUp(size, kPointerSize);
size += MeasureCode(native_module_->code(i)); size += MeasureCode(native_module_->code(i));
} }
return size; return size;
...@@ -303,6 +330,12 @@ void NativeModuleSerializer::WriteCode(const WasmCode* code, Writer* writer) { ...@@ -303,6 +330,12 @@ void NativeModuleSerializer::WriteCode(const WasmCode* code, Writer* writer) {
writer->Write(code->source_positions().size()); writer->Write(code->source_positions().size());
writer->Write(code->protected_instructions().size()); writer->Write(code->protected_instructions().size());
writer->Write(code->tier()); writer->Write(code->tier());
// Code start needs to be kPointerSize aligned, because
// code is reallocated after loaded into the read buffer,
// and code reallocation needs to be done on properly
// aligned code.
// TODO(all) Remove padding after code reallocation is removed
writer->Align(kPointerSize);
// Get a pointer to the code buffer, which we have to relocate. // Get a pointer to the code buffer, which we have to relocate.
byte* serialized_code_start = writer->current_buffer().start(); byte* serialized_code_start = writer->current_buffer().start();
// Now write the code, reloc info, source positions, and protected code. // Now write the code, reloc info, source positions, and protected code.
...@@ -483,6 +516,12 @@ bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) { ...@@ -483,6 +516,12 @@ bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) {
size_t source_position_size = reader->Read<size_t>(); size_t source_position_size = reader->Read<size_t>();
size_t protected_instructions_size = reader->Read<size_t>(); size_t protected_instructions_size = reader->Read<size_t>();
WasmCode::Tier tier = reader->Read<WasmCode::Tier>(); WasmCode::Tier tier = reader->Read<WasmCode::Tier>();
// Code start needs to be kPointerSize aligned, because
// code is reallocated after loaded into the read buffer,
// and code reallocation needs to be done on properly
// aligned code.
// TODO(all) Remove padding after code reallocation is removed
reader->Align(kPointerSize);
Vector<const byte> code_buffer = {reader->current_location(), code_size}; Vector<const byte> code_buffer = {reader->current_location(), code_size};
reader->Skip(code_size); reader->Skip(code_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