Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1318)

Unified Diff: src/wasm/encoder.cc

Issue 1900153002: [wasm] Enforce strict ordering of WASM module sections. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/wasm/module-decoder.cc » ('j') | src/wasm/module-decoder.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/wasm/encoder.cc
diff --git a/src/wasm/encoder.cc b/src/wasm/encoder.cc
index a95171d0ae54b9ea6a7ac5ee8f70180252815036..3d35128a45b53e522cc57ac41eefabe8c70c5a02 100644
--- a/src/wasm/encoder.cc
+++ b/src/wasm/encoder.cc
@@ -54,7 +54,7 @@ void EmitUint32(byte** b, uint32_t x) {
// We generate a large varint which we then fixup later when the size is known.
//
// TODO(jfb) Not strictly necessary since sizes are calculated ahead of time.
-const size_t padded_varint = 5;
+const size_t kPaddedVarintSize = 5;
void EmitVarInt(byte** b, size_t val) {
while (true) {
@@ -81,12 +81,12 @@ size_t SizeOfVarInt(size_t value) {
void FixupSection(byte* start, byte* end) {
// Same as EmitVarInt, but fixed-width with zeroes in the MSBs.
- size_t val = end - start - padded_varint;
+ size_t val = end - start - kPaddedVarintSize;
TRACE(" fixup %u\n", (unsigned)val);
- for (size_t pos = 0; pos != padded_varint; ++pos) {
+ for (size_t pos = 0; pos != kPaddedVarintSize; ++pos) {
size_t next = val >> 7;
byte out = static_cast<byte>(val & 0x7f);
- if (pos != padded_varint - 1) {
+ if (pos != kPaddedVarintSize - 1) {
*(start++) = 0x80 | out;
val = next;
} else {
@@ -98,15 +98,18 @@ void FixupSection(byte* start, byte* end) {
// Returns the start of the section, where the section VarInt size is.
byte* EmitSection(WasmSection::Code code, byte** b) {
+ // Emit a placeholder for the length.
byte* start = *b;
- const char* name = WasmSection::getName(code);
- size_t length = WasmSection::getNameLength(code);
- TRACE("emit section: %s\n", name);
- for (size_t padding = 0; padding != padded_varint; ++padding) {
+ for (size_t padding = 0; padding != kPaddedVarintSize; ++padding) {
EmitUint8(b, 0xff); // Will get fixed up later.
}
+ // Emit the section name.
+ const char* name = WasmSection::getName(code);
+ TRACE("emit section: %s\n", name);
+ size_t length = WasmSection::getNameLength(code);
EmitVarInt(b, length); // Section name string size.
for (size_t i = 0; i != length; ++i) EmitUint8(b, name[i]);
+
return start;
}
} // namespace
@@ -551,7 +554,7 @@ struct Sizes {
}
void AddSection(WasmSection::Code code, size_t other_size) {
- Add(padded_varint + SizeOfVarInt(WasmSection::getNameLength(code)) +
+ Add(kPaddedVarintSize + SizeOfVarInt(WasmSection::getNameLength(code)) +
WasmSection::getNameLength(code),
0);
if (other_size) Add(SizeOfVarInt(other_size), 0);
@@ -563,11 +566,6 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
sizes.Add(2 * sizeof(uint32_t), 0); // header
- sizes.AddSection(WasmSection::Code::Memory, 0);
- sizes.Add(kDeclMemorySize, 0);
- TRACE("Size after memory: %u, %u\n", (unsigned)sizes.header_size,
- (unsigned)sizes.body_size);
-
if (globals_.size() > 0) {
sizes.AddSection(WasmSection::Code::Globals, globals_.size());
/* These globals never have names, so are always 3 bytes. */
@@ -596,6 +594,21 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
(unsigned)sizes.body_size);
}
+ if (indirect_functions_.size() > 0) {
+ sizes.AddSection(WasmSection::Code::FunctionTable,
+ indirect_functions_.size());
+ for (auto function_index : indirect_functions_) {
+ sizes.Add(SizeOfVarInt(function_index), 0);
+ }
+ TRACE("Size after indirect functions: %u, %u\n",
+ (unsigned)sizes.header_size, (unsigned)sizes.body_size);
+ }
+
+ sizes.AddSection(WasmSection::Code::Memory, 0);
+ sizes.Add(kDeclMemorySize, 0);
+ TRACE("Size after memory: %u, %u\n", (unsigned)sizes.header_size,
+ (unsigned)sizes.body_size);
+
if (start_function_index_ >= 0) {
sizes.AddSection(WasmSection::Code::StartFunction, 0);
sizes.Add(SizeOfVarInt(start_function_index_), 0);
@@ -612,16 +625,6 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
(unsigned)sizes.body_size);
}
- if (indirect_functions_.size() > 0) {
- sizes.AddSection(WasmSection::Code::FunctionTable,
- indirect_functions_.size());
- for (auto function_index : indirect_functions_) {
- sizes.Add(SizeOfVarInt(function_index), 0);
- }
- TRACE("Size after indirect functions: %u, %u\n",
- (unsigned)sizes.header_size, (unsigned)sizes.body_size);
- }
-
if (sizes.body_size > 0) {
sizes.AddSection(WasmSection::Code::End, 0);
TRACE("Size after end: %u, %u\n", (unsigned)sizes.header_size,
@@ -638,16 +641,6 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
EmitUint32(&header, kWasmMagic);
EmitUint32(&header, kWasmVersion);
- // -- emit memory declaration ------------------------------------------------
- {
- byte* section = EmitSection(WasmSection::Code::Memory, &header);
- EmitVarInt(&header, 16); // min memory size
- EmitVarInt(&header, 16); // max memory size
- EmitUint8(&header, 0); // memory export
- static_assert(kDeclMemorySize == 3, "memory size must match emit above");
- FixupSection(section, header);
- }
-
// -- emit globals -----------------------------------------------------------
if (globals_.size() > 0) {
byte* section = EmitSection(WasmSection::Code::Globals, &header);
@@ -691,6 +684,27 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
FixupSection(section, header);
}
+ // -- emit function table ----------------------------------------------------
+ if (indirect_functions_.size() > 0) {
+ byte* section = EmitSection(WasmSection::Code::FunctionTable, &header);
+ EmitVarInt(&header, indirect_functions_.size());
+
+ for (auto index : indirect_functions_) {
+ EmitVarInt(&header, index);
+ }
+ FixupSection(section, header);
+ }
+
+ // -- emit memory declaration ------------------------------------------------
+ {
+ byte* section = EmitSection(WasmSection::Code::Memory, &header);
+ EmitVarInt(&header, 16); // min memory size
+ EmitVarInt(&header, 16); // max memory size
+ EmitUint8(&header, 0); // memory export
+ static_assert(kDeclMemorySize == 3, "memory size must match emit above");
+ FixupSection(section, header);
+ }
+
// -- emit start function index ----------------------------------------------
if (start_function_index_ >= 0) {
byte* section = EmitSection(WasmSection::Code::StartFunction, &header);
@@ -709,17 +723,6 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
FixupSection(section, header);
}
- // -- emit function table ----------------------------------------------------
- if (indirect_functions_.size() > 0) {
- byte* section = EmitSection(WasmSection::Code::FunctionTable, &header);
- EmitVarInt(&header, indirect_functions_.size());
-
- for (auto index : indirect_functions_) {
- EmitVarInt(&header, index);
- }
- FixupSection(section, header);
- }
-
if (sizes.body_size > 0) {
byte* section = EmitSection(WasmSection::Code::End, &header);
FixupSection(section, header);
« no previous file with comments | « no previous file | src/wasm/module-decoder.cc » ('j') | src/wasm/module-decoder.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698