| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/wasm/module-decoder.h" | 5 #include "src/wasm/module-decoder.h" |
| 6 | 6 |
| 7 #include "src/base/functional.h" | 7 #include "src/base/functional.h" |
| 8 #include "src/base/platform/platform.h" | 8 #include "src/base/platform/platform.h" |
| 9 #include "src/macro-assembler.h" | 9 #include "src/macro-assembler.h" |
| 10 #include "src/objects.h" | 10 #include "src/objects.h" |
| 11 #include "src/v8.h" | 11 #include "src/v8.h" |
| 12 | 12 |
| 13 #include "src/wasm/decoder.h" | 13 #include "src/wasm/decoder.h" |
| 14 | 14 |
| 15 namespace v8 { | 15 namespace v8 { |
| 16 namespace internal { | 16 namespace internal { |
| 17 namespace wasm { | 17 namespace wasm { |
| 18 | 18 |
| 19 #if DEBUG | 19 #if DEBUG |
| 20 #define TRACE(...) \ | 20 #define TRACE(...) \ |
| 21 do { \ | 21 do { \ |
| 22 if (FLAG_trace_wasm_decoder) PrintF(__VA_ARGS__); \ | 22 if (FLAG_trace_wasm_decoder) PrintF(__VA_ARGS__); \ |
| 23 } while (false) | 23 } while (false) |
| 24 #else | 24 #else |
| 25 #define TRACE(...) | 25 #define TRACE(...) |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 LocalType TypeOf(const WasmModule* module, const WasmInitExpr& expr) { |
| 31 switch (expr.kind) { |
| 32 case WasmInitExpr::kNone: |
| 33 return kAstStmt; |
| 34 case WasmInitExpr::kGlobalIndex: |
| 35 return expr.val.global_index < module->globals.size() |
| 36 ? module->globals[expr.val.global_index].type |
| 37 : kAstStmt; |
| 38 case WasmInitExpr::kI32Const: |
| 39 return kAstI32; |
| 40 case WasmInitExpr::kI64Const: |
| 41 return kAstI64; |
| 42 case WasmInitExpr::kF32Const: |
| 43 return kAstF32; |
| 44 case WasmInitExpr::kF64Const: |
| 45 return kAstF64; |
| 46 default: |
| 47 UNREACHABLE(); |
| 48 return kAstStmt; |
| 49 } |
| 50 } |
| 51 |
| 52 // An iterator over the sections in a WASM binary module. |
| 53 // Automatically skips all unknown sections. |
| 54 class WasmSectionIterator { |
| 55 public: |
| 56 explicit WasmSectionIterator(Decoder& decoder) |
| 57 : decoder_(decoder), |
| 58 section_code_(kUnknownSectionCode), |
| 59 section_start_(decoder.pc()), |
| 60 section_end_(decoder.pc()) { |
| 61 next(); |
| 62 } |
| 63 |
| 64 inline bool more() const { |
| 65 return section_code_ != kUnknownSectionCode && decoder_.more(); |
| 66 } |
| 67 |
| 68 inline WasmSectionCode section_code() const { return section_code_; } |
| 69 |
| 70 inline const byte* section_start() const { return section_start_; } |
| 71 |
| 72 inline uint32_t section_length() const { |
| 73 return static_cast<uint32_t>(section_end_ - section_start_); |
| 74 } |
| 75 |
| 76 inline const byte* section_end() const { return section_end_; } |
| 77 |
| 78 // Advances to the next section, checking that decoding the current section |
| 79 // stopped at {section_end_}. |
| 80 void advance() { |
| 81 if (decoder_.pc() != section_end_) { |
| 82 const char* msg = decoder_.pc() < section_end_ ? "shorter" : "longer"; |
| 83 decoder_.error(decoder_.pc(), decoder_.pc(), |
| 84 "section was %s than expected size " |
| 85 "(%u bytes expected, %zu decoded)", |
| 86 msg, section_length(), |
| 87 static_cast<size_t>(decoder_.pc() - section_start_)); |
| 88 } |
| 89 next(); |
| 90 } |
| 91 |
| 92 private: |
| 93 Decoder& decoder_; |
| 94 WasmSectionCode section_code_; |
| 95 const byte* section_start_; |
| 96 const byte* section_end_; |
| 97 |
| 98 // Reads the section code/name at the current position and sets up |
| 99 // the internal fields. |
| 100 void next() { |
| 101 while (true) { |
| 102 if (!decoder_.more()) { |
| 103 section_code_ = kUnknownSectionCode; |
| 104 return; |
| 105 } |
| 106 uint8_t section_code = decoder_.consume_u8("section code"); |
| 107 // Read and check the section size. |
| 108 uint32_t section_length = decoder_.consume_u32v("section length"); |
| 109 section_start_ = decoder_.pc(); |
| 110 if (decoder_.checkAvailable(section_length)) { |
| 111 // Get the limit of the section within the module. |
| 112 section_end_ = section_start_ + section_length; |
| 113 } else { |
| 114 // The section would extend beyond the end of the module. |
| 115 section_end_ = section_start_; |
| 116 } |
| 117 |
| 118 if (section_code == kUnknownSectionCode) { |
| 119 // Check for the known "names" section. |
| 120 uint32_t string_length = decoder_.consume_u32v("section name length"); |
| 121 const byte* section_name_start = decoder_.pc(); |
| 122 decoder_.consume_bytes(string_length, "section name"); |
| 123 if (decoder_.failed() || decoder_.pc() > section_end_) { |
| 124 TRACE("Section name of length %u couldn't be read\n", string_length); |
| 125 section_code_ = kUnknownSectionCode; |
| 126 return; |
| 127 } |
| 128 |
| 129 TRACE(" +%d section name : \"%.*s\"\n", |
| 130 static_cast<int>(section_name_start - decoder_.start()), |
| 131 string_length < 20 ? string_length : 20, section_name_start); |
| 132 |
| 133 if (string_length == kNameStringLength && |
| 134 strncmp(reinterpret_cast<const char*>(section_name_start), |
| 135 kNameString, kNameStringLength) == 0) { |
| 136 section_code = kNameSectionCode; |
| 137 } else { |
| 138 section_code = kUnknownSectionCode; |
| 139 } |
| 140 } else if (!IsValidSectionCode(section_code)) { |
| 141 decoder_.error(decoder_.pc(), decoder_.pc(), |
| 142 "unknown section code #0x%02x", section_code); |
| 143 section_code = kUnknownSectionCode; |
| 144 } |
| 145 section_code_ = static_cast<WasmSectionCode>(section_code); |
| 146 |
| 147 TRACE("Section: %s\n", SectionName(section_code_)); |
| 148 if (section_code_ == kUnknownSectionCode && |
| 149 section_end_ > decoder_.pc()) { |
| 150 // skip to the end of the unknown section. |
| 151 uint32_t remaining = |
| 152 static_cast<uint32_t>(section_end_ - decoder_.pc()); |
| 153 decoder_.consume_bytes(remaining, "section payload"); |
| 154 // fall through and continue to the next section. |
| 155 } else { |
| 156 return; |
| 157 } |
| 158 } |
| 159 } |
| 160 }; |
| 161 |
| 30 // The main logic for decoding the bytes of a module. | 162 // The main logic for decoding the bytes of a module. |
| 31 class ModuleDecoder : public Decoder { | 163 class ModuleDecoder : public Decoder { |
| 32 public: | 164 public: |
| 33 ModuleDecoder(Zone* zone, const byte* module_start, const byte* module_end, | 165 ModuleDecoder(Zone* zone, const byte* module_start, const byte* module_end, |
| 34 ModuleOrigin origin) | 166 ModuleOrigin origin) |
| 35 : Decoder(module_start, module_end), module_zone(zone), origin_(origin) { | 167 : Decoder(module_start, module_end), module_zone(zone), origin_(origin) { |
| 36 result_.start = start_; | 168 result_.start = start_; |
| 37 if (limit_ < start_) { | 169 if (limit_ < start_) { |
| 38 error(start_, "end is less than start"); | 170 error(start_, "end is less than start"); |
| 39 limit_ = start_; | 171 limit_ = start_; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 70 } | 202 } |
| 71 | 203 |
| 72 // Decodes an entire module. | 204 // Decodes an entire module. |
| 73 ModuleResult DecodeModule(WasmModule* module, bool verify_functions = true) { | 205 ModuleResult DecodeModule(WasmModule* module, bool verify_functions = true) { |
| 74 pc_ = start_; | 206 pc_ = start_; |
| 75 module->module_start = start_; | 207 module->module_start = start_; |
| 76 module->module_end = limit_; | 208 module->module_end = limit_; |
| 77 module->min_mem_pages = 0; | 209 module->min_mem_pages = 0; |
| 78 module->max_mem_pages = 0; | 210 module->max_mem_pages = 0; |
| 79 module->mem_export = false; | 211 module->mem_export = false; |
| 80 module->mem_external = false; | |
| 81 module->origin = origin_; | 212 module->origin = origin_; |
| 82 | 213 |
| 83 const byte* pos = pc_; | 214 const byte* pos = pc_; |
| 84 int current_order = 0; | |
| 85 uint32_t magic_word = consume_u32("wasm magic"); | 215 uint32_t magic_word = consume_u32("wasm magic"); |
| 86 #define BYTES(x) (x & 0xff), (x >> 8) & 0xff, (x >> 16) & 0xff, (x >> 24) & 0xff | 216 #define BYTES(x) (x & 0xff), (x >> 8) & 0xff, (x >> 16) & 0xff, (x >> 24) & 0xff |
| 87 if (magic_word != kWasmMagic) { | 217 if (magic_word != kWasmMagic) { |
| 88 error(pos, pos, | 218 error(pos, pos, |
| 89 "expected magic word %02x %02x %02x %02x, " | 219 "expected magic word %02x %02x %02x %02x, " |
| 90 "found %02x %02x %02x %02x", | 220 "found %02x %02x %02x %02x", |
| 91 BYTES(kWasmMagic), BYTES(magic_word)); | 221 BYTES(kWasmMagic), BYTES(magic_word)); |
| 92 goto done; | |
| 93 } | 222 } |
| 94 | 223 |
| 95 pos = pc_; | 224 pos = pc_; |
| 96 { | 225 { |
| 97 uint32_t magic_version = consume_u32("wasm version"); | 226 uint32_t magic_version = consume_u32("wasm version"); |
| 98 if (magic_version != kWasmVersion) { | 227 if (magic_version != kWasmVersion) { |
| 99 error(pos, pos, | 228 error(pos, pos, |
| 100 "expected version %02x %02x %02x %02x, " | 229 "expected version %02x %02x %02x %02x, " |
| 101 "found %02x %02x %02x %02x", | 230 "found %02x %02x %02x %02x", |
| 102 BYTES(kWasmVersion), BYTES(magic_version)); | 231 BYTES(kWasmVersion), BYTES(magic_version)); |
| 103 goto done; | 232 } |
| 104 } | 233 } |
| 105 } | 234 |
| 106 | 235 WasmSectionIterator section_iter(*this); |
| 107 // Decode the module sections. | 236 |
| 108 while (pc_ < limit_) { | 237 // ===== Type section ==================================================== |
| 109 TRACE("DecodeSection\n"); | 238 if (section_iter.section_code() == kTypeSectionCode) { |
| 110 pos = pc_; | 239 uint32_t signatures_count = consume_u32v("signatures count"); |
| 111 | 240 module->signatures.reserve(SafeReserve(signatures_count)); |
| 112 // Read the section name. | 241 for (uint32_t i = 0; ok() && i < signatures_count; ++i) { |
| 113 uint32_t string_length = consume_u32v("section name length"); | 242 TRACE("DecodeSignature[%d] module+%d\n", i, |
| 114 const byte* section_name_start = pc_; | 243 static_cast<int>(pc_ - start_)); |
| 115 consume_bytes(string_length); | 244 FunctionSig* s = consume_sig(); |
| 116 if (failed()) { | 245 module->signatures.push_back(s); |
| 117 TRACE("Section name of length %u couldn't be read\n", string_length); | 246 } |
| 118 break; | 247 section_iter.advance(); |
| 119 } | 248 } |
| 120 | 249 |
| 121 TRACE(" +%d section name : \"%.*s\"\n", | 250 // ===== Import section ================================================== |
| 122 static_cast<int>(section_name_start - start_), | 251 if (section_iter.section_code() == kImportSectionCode) { |
| 123 string_length < 20 ? string_length : 20, section_name_start); | 252 uint32_t import_table_count = consume_u32v("import table count"); |
| 124 | 253 module->import_table.reserve(SafeReserve(import_table_count)); |
| 125 WasmSection::Code section = | 254 for (uint32_t i = 0; ok() && i < import_table_count; ++i) { |
| 126 WasmSection::lookup(section_name_start, string_length); | 255 TRACE("DecodeImportTable[%d] module+%d\n", i, |
| 127 | 256 static_cast<int>(pc_ - start_)); |
| 128 // Read and check the section size. | 257 |
| 129 uint32_t section_length = consume_u32v("section length"); | 258 module->import_table.push_back({ |
| 130 if (!checkAvailable(section_length)) { | 259 0, // module_name_length |
| 131 // The section would extend beyond the end of the module. | 260 0, // module_name_offset |
| 132 break; | 261 0, // field_name_offset |
| 133 } | 262 0, // field_name_length |
| 134 const byte* section_start = pc_; | 263 kExternalFunction, // kind |
| 135 const byte* expected_section_end = pc_ + section_length; | 264 0 // index |
| 136 | 265 }); |
| 137 current_order = CheckSectionOrder(current_order, section); | 266 WasmImport* import = &module->import_table.back(); |
| 138 | 267 const byte* pos = pc_; |
| 139 switch (section) { | 268 import->module_name_offset = |
| 140 case WasmSection::Code::End: | 269 consume_string(&import->module_name_length, true); |
| 141 // Terminate section decoding. | 270 if (import->module_name_length == 0) { |
| 142 limit_ = pc_; | 271 error(pos, "import module name cannot be NULL"); |
| 143 break; | 272 } |
| 144 case WasmSection::Code::Memory: { | 273 import->field_name_offset = |
| 145 module->min_mem_pages = consume_u32v("min memory"); | 274 consume_string(&import->field_name_length, true); |
| 146 module->max_mem_pages = consume_u32v("max memory"); | 275 |
| 147 module->mem_export = consume_u8("export memory") != 0; | 276 import->kind = static_cast<WasmExternalKind>(consume_u8("import kind")); |
| 148 break; | 277 switch (import->kind) { |
| 149 } | 278 case kExternalFunction: { |
| 150 case WasmSection::Code::Signatures: { | 279 // ===== Imported function ======================================= |
| 151 uint32_t signatures_count = consume_u32v("signatures count"); | 280 import->index = static_cast<uint32_t>(module->functions.size()); |
| 152 module->signatures.reserve(SafeReserve(signatures_count)); | 281 module->num_imported_functions++; |
| 153 // Decode signatures. | 282 module->functions.push_back({nullptr, // sig |
| 154 for (uint32_t i = 0; ok() && i < signatures_count; ++i) { | 283 import->index, // func_index |
| 155 TRACE("DecodeSignature[%d] module+%d\n", i, | 284 0, // sig_index |
| 156 static_cast<int>(pc_ - start_)); | 285 0, // name_offset |
| 157 FunctionSig* s = consume_sig(); | 286 0, // name_length |
| 158 module->signatures.push_back(s); | 287 0, // code_start_offset |
| 159 } | 288 0, // code_end_offset |
| 160 break; | 289 true, // imported |
| 161 } | 290 false}); // exported |
| 162 case WasmSection::Code::FunctionSignatures: { | |
| 163 uint32_t functions_count = consume_u32v("functions count"); | |
| 164 module->functions.reserve(SafeReserve(functions_count)); | |
| 165 for (uint32_t i = 0; ok() && i < functions_count; ++i) { | |
| 166 module->functions.push_back({nullptr, // sig | |
| 167 i, // func_index | |
| 168 0, // sig_index | |
| 169 0, // name_offset | |
| 170 0, // name_length | |
| 171 0, // code_start_offset | |
| 172 0}); // code_end_offset | |
| 173 WasmFunction* function = &module->functions.back(); | 291 WasmFunction* function = &module->functions.back(); |
| 174 function->sig_index = consume_sig_index(module, &function->sig); | 292 function->sig_index = consume_sig_index(module, &function->sig); |
| 175 } | 293 break; |
| 176 break; | 294 } |
| 177 } | 295 case kExternalTable: { |
| 178 case WasmSection::Code::FunctionBodies: { | 296 // ===== Imported table ========================================== |
| 179 const byte* pos = pc_; | 297 import->index = |
| 180 uint32_t functions_count = consume_u32v("functions count"); | 298 static_cast<uint32_t>(module->function_tables.size()); |
| 181 if (functions_count != module->functions.size()) { | 299 module->function_tables.push_back( |
| 182 error(pos, pos, "function body count %u mismatch (%u expected)", | 300 {0, 0, std::vector<int32_t>(), true, false}); |
| 183 functions_count, | 301 expect_u8("element type", 0x20); |
| 184 static_cast<uint32_t>(module->functions.size())); | 302 WasmIndirectFunctionTable* table = &module->function_tables.back(); |
| 185 break; | 303 consume_resizable_limits("element count", "elements", kMaxUInt32, |
| 186 } | 304 &table->size, &table->max_size); |
| 187 for (uint32_t i = 0; ok() && i < functions_count; ++i) { | 305 break; |
| 188 WasmFunction* function = &module->functions[i]; | 306 } |
| 189 uint32_t size = consume_u32v("body size"); | 307 case kExternalMemory: { |
| 190 function->code_start_offset = pc_offset(); | 308 // ===== Imported memory ========================================= |
| 191 function->code_end_offset = pc_offset() + size; | 309 // import->index = |
| 192 | 310 // static_cast<uint32_t>(module->memories.size()); |
| 193 TRACE(" +%d %-20s: (%d bytes)\n", pc_offset(), "function body", | 311 // TODO(titzer): imported memories |
| 194 size); | 312 break; |
| 195 pc_ += size; | 313 } |
| 196 if (pc_ > limit_) { | 314 case kExternalGlobal: { |
| 197 error(pc_, "function body extends beyond end of file"); | 315 // ===== Imported global ========================================= |
| 198 } | 316 import->index = static_cast<uint32_t>(module->globals.size()); |
| 199 } | 317 module->globals.push_back( |
| 200 break; | 318 {kAstStmt, false, NO_INIT, 0, true, false}); |
| 201 } | |
| 202 case WasmSection::Code::Names: { | |
| 203 const byte* pos = pc_; | |
| 204 uint32_t functions_count = consume_u32v("functions count"); | |
| 205 if (functions_count != module->functions.size()) { | |
| 206 error(pos, pos, "function name count %u mismatch (%u expected)", | |
| 207 functions_count, | |
| 208 static_cast<uint32_t>(module->functions.size())); | |
| 209 break; | |
| 210 } | |
| 211 | |
| 212 for (uint32_t i = 0; ok() && i < functions_count; ++i) { | |
| 213 WasmFunction* function = &module->functions[i]; | |
| 214 function->name_offset = | |
| 215 consume_string(&function->name_length, false); | |
| 216 | |
| 217 uint32_t local_names_count = consume_u32v("local names count"); | |
| 218 for (uint32_t j = 0; ok() && j < local_names_count; j++) { | |
| 219 uint32_t unused = 0; | |
| 220 uint32_t offset = consume_string(&unused, false); | |
| 221 USE(unused); | |
| 222 USE(offset); | |
| 223 } | |
| 224 } | |
| 225 break; | |
| 226 } | |
| 227 case WasmSection::Code::Globals: { | |
| 228 uint32_t globals_count = consume_u32v("globals count"); | |
| 229 module->globals.reserve(SafeReserve(globals_count)); | |
| 230 // Decode globals. | |
| 231 for (uint32_t i = 0; ok() && i < globals_count; ++i) { | |
| 232 TRACE("DecodeGlobal[%d] module+%d\n", i, | |
| 233 static_cast<int>(pc_ - start_)); | |
| 234 // Add an uninitialized global and pass a pointer to it. | |
| 235 module->globals.push_back({0, 0, kAstStmt, 0, false}); | |
| 236 WasmGlobal* global = &module->globals.back(); | 319 WasmGlobal* global = &module->globals.back(); |
| 237 DecodeGlobalInModule(global); | 320 global->type = consume_value_type(); |
| 238 } | 321 global->mutability = consume_u8("mutability") != 0; |
| 239 break; | 322 break; |
| 240 } | 323 } |
| 241 case WasmSection::Code::DataSegments: { | 324 default: |
| 242 uint32_t data_segments_count = consume_u32v("data segments count"); | 325 error(pos, pos, "unknown import kind 0x%02x", import->kind); |
| 243 module->data_segments.reserve(SafeReserve(data_segments_count)); | 326 break; |
| 244 // Decode data segments. | 327 } |
| 245 for (uint32_t i = 0; ok() && i < data_segments_count; ++i) { | 328 } |
| 246 TRACE("DecodeDataSegment[%d] module+%d\n", i, | 329 section_iter.advance(); |
| 247 static_cast<int>(pc_ - start_)); | 330 } |
| 248 module->data_segments.push_back({0, // dest_addr | 331 |
| 249 0, // source_offset | 332 // ===== Function section ================================================ |
| 250 0, // source_size | 333 if (section_iter.section_code() == kFunctionSectionCode) { |
| 251 false}); // init | 334 uint32_t functions_count = consume_u32v("functions count"); |
| 252 WasmDataSegment* segment = &module->data_segments.back(); | 335 module->functions.reserve(SafeReserve(functions_count)); |
| 253 DecodeDataSegmentInModule(module, segment); | 336 module->num_declared_functions = functions_count; |
| 254 } | 337 for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
| 255 break; | 338 uint32_t func_index = static_cast<uint32_t>(module->functions.size()); |
| 256 } | 339 module->functions.push_back({nullptr, // sig |
| 257 case WasmSection::Code::FunctionTable: { | 340 func_index, // func_index |
| 258 // An indirect function table requires functions first. | 341 0, // sig_index |
| 259 CheckForFunctions(module, section); | 342 0, // name_offset |
| 260 // Assume only one table for now. | 343 0, // name_length |
| 261 static const uint32_t kSupportedTableCount = 1; | 344 0, // code_start_offset |
| 262 module->function_tables.reserve(SafeReserve(kSupportedTableCount)); | 345 0, // code_end_offset |
| 263 // Decode function table. | 346 false, // imported |
| 264 for (uint32_t i = 0; ok() && i < kSupportedTableCount; ++i) { | 347 false}); // exported |
| 265 TRACE("DecodeFunctionTable[%d] module+%d\n", i, | 348 WasmFunction* function = &module->functions.back(); |
| 266 static_cast<int>(pc_ - start_)); | 349 function->sig_index = consume_sig_index(module, &function->sig); |
| 267 module->function_tables.push_back({0, 0, std::vector<uint16_t>()}); | 350 } |
| 268 DecodeFunctionTableInModule(module, &module->function_tables[i]); | 351 section_iter.advance(); |
| 269 } | 352 } |
| 270 break; | 353 |
| 271 } | 354 // ===== Table section =================================================== |
| 272 case WasmSection::Code::StartFunction: { | 355 if (section_iter.section_code() == kTableSectionCode) { |
| 273 // Declares a start function for a module. | 356 const byte* pos = pc_; |
| 274 CheckForFunctions(module, section); | 357 uint32_t table_count = consume_u32v("table count"); |
| 275 if (module->start_function_index >= 0) { | 358 // Require at most one table for now. |
| 276 error("start function already declared"); | 359 if (table_count > 1) { |
| 277 break; | 360 error(pos, pos, "invalid table count %d, maximum 1", table_count); |
| 278 } | 361 } |
| 279 WasmFunction* func; | 362 |
| 280 const byte* pos = pc_; | 363 for (uint32_t i = 0; ok() && i < table_count; i++) { |
| 281 module->start_function_index = consume_func_index(module, &func); | 364 module->function_tables.push_back( |
| 282 if (func && func->sig->parameter_count() > 0) { | 365 {0, 0, std::vector<int32_t>(), false, false}); |
| 283 error(pos, "invalid start function: non-zero parameter count"); | 366 WasmIndirectFunctionTable* table = &module->function_tables.back(); |
| 284 break; | 367 expect_u8("table type", kWasmAnyFunctionTypeForm); |
| 285 } | 368 consume_resizable_limits("table elements", "elements", kMaxUInt32, |
| 286 break; | 369 &table->size, &table->max_size); |
| 287 } | 370 } |
| 288 case WasmSection::Code::ImportTable: { | 371 section_iter.advance(); |
| 289 uint32_t import_table_count = consume_u32v("import table count"); | 372 } |
| 290 module->import_table.reserve(SafeReserve(import_table_count)); | 373 |
| 291 // Decode import table. | 374 // ===== Memory section ================================================== |
| 292 for (uint32_t i = 0; ok() && i < import_table_count; ++i) { | 375 if (section_iter.section_code() == kMemorySectionCode) { |
| 293 TRACE("DecodeImportTable[%d] module+%d\n", i, | 376 const byte* pos = pc_; |
| 294 static_cast<int>(pc_ - start_)); | 377 uint32_t memory_count = consume_u32v("memory count"); |
| 295 | 378 // Require at most one memory for now. |
| 296 module->import_table.push_back({nullptr, // sig | 379 if (memory_count > 1) { |
| 297 0, // sig_index | 380 error(pos, pos, "invalid memory count %d, maximum 1", memory_count); |
| 298 0, // module_name_offset | 381 } |
| 299 0, // module_name_length | 382 |
| 300 0, // function_name_offset | 383 for (uint32_t i = 0; ok() && i < memory_count; i++) { |
| 301 0}); // function_name_length | 384 consume_resizable_limits("memory", "pages", WasmModule::kMaxLegalPages, |
| 302 WasmImport* import = &module->import_table.back(); | 385 &module->min_mem_pages, |
| 303 | 386 &module->max_mem_pages); |
| 304 import->sig_index = consume_sig_index(module, &import->sig); | 387 } |
| 305 const byte* pos = pc_; | 388 section_iter.advance(); |
| 306 import->module_name_offset = | 389 } |
| 307 consume_string(&import->module_name_length, true); | 390 |
| 308 if (import->module_name_length == 0) { | 391 // ===== Global section ================================================== |
| 309 error(pos, "import module name cannot be NULL"); | 392 if (section_iter.section_code() == kGlobalSectionCode) { |
| 310 } | 393 uint32_t globals_count = consume_u32v("globals count"); |
| 311 import->function_name_offset = | 394 module->globals.reserve(SafeReserve(globals_count)); |
| 312 consume_string(&import->function_name_length, true); | 395 for (uint32_t i = 0; ok() && i < globals_count; ++i) { |
| 313 } | 396 TRACE("DecodeGlobal[%d] module+%d\n", i, |
| 314 break; | 397 static_cast<int>(pc_ - start_)); |
| 315 } | 398 // Add an uninitialized global and pass a pointer to it. |
| 316 case WasmSection::Code::ExportTable: { | 399 module->globals.push_back({kAstStmt, false, NO_INIT, 0, false, false}); |
| 317 // Declares an export table. | 400 WasmGlobal* global = &module->globals.back(); |
| 318 CheckForFunctions(module, section); | 401 DecodeGlobalInModule(module, i, global); |
| 319 uint32_t export_table_count = consume_u32v("export table count"); | 402 } |
| 320 module->export_table.reserve(SafeReserve(export_table_count)); | 403 section_iter.advance(); |
| 321 // Decode export table. | 404 } |
| 322 for (uint32_t i = 0; ok() && i < export_table_count; ++i) { | 405 |
| 323 TRACE("DecodeExportTable[%d] module+%d\n", i, | 406 // ===== Export section ================================================== |
| 324 static_cast<int>(pc_ - start_)); | 407 if (section_iter.section_code() == kExportSectionCode) { |
| 325 | 408 uint32_t export_table_count = consume_u32v("export table count"); |
| 326 module->export_table.push_back({0, // func_index | 409 module->export_table.reserve(SafeReserve(export_table_count)); |
| 327 0, // name_offset | 410 for (uint32_t i = 0; ok() && i < export_table_count; ++i) { |
| 328 0}); // name_length | 411 TRACE("DecodeExportTable[%d] module+%d\n", i, |
| 329 WasmExport* exp = &module->export_table.back(); | 412 static_cast<int>(pc_ - start_)); |
| 330 | 413 |
| 331 WasmFunction* func; | 414 module->export_table.push_back({ |
| 332 exp->func_index = consume_func_index(module, &func); | 415 0, // name_length |
| 333 exp->name_offset = consume_string(&exp->name_length, true); | 416 0, // name_offset |
| 334 } | 417 kExternalFunction, // kind |
| 335 // Check for duplicate exports. | 418 0 // index |
| 336 if (ok() && module->export_table.size() > 1) { | 419 }); |
| 337 std::vector<WasmExport> sorted_exports(module->export_table); | 420 WasmExport* exp = &module->export_table.back(); |
| 338 const byte* base = start_; | 421 |
| 339 auto cmp_less = [base](const WasmExport& a, const WasmExport& b) { | 422 exp->name_offset = consume_string(&exp->name_length, true); |
| 340 // Return true if a < b. | 423 const byte* pos = pc(); |
| 341 uint32_t len = a.name_length; | 424 exp->kind = static_cast<WasmExternalKind>(consume_u8("export kind")); |
| 342 if (len != b.name_length) return len < b.name_length; | 425 switch (exp->kind) { |
| 343 return memcmp(base + a.name_offset, base + b.name_offset, len) < | 426 case kExternalFunction: { |
| 344 0; | 427 WasmFunction* func = nullptr; |
| 345 }; | 428 exp->index = consume_func_index(module, &func); |
| 346 std::stable_sort(sorted_exports.begin(), sorted_exports.end(), | 429 module->num_exported_functions++; |
| 347 cmp_less); | 430 if (func) func->exported = true; |
| 348 auto it = sorted_exports.begin(); | 431 break; |
| 349 WasmExport* last = &*it++; | 432 } |
| 350 for (auto end = sorted_exports.end(); it != end; last = &*it++) { | 433 case kExternalTable: { |
| 351 DCHECK(!cmp_less(*it, *last)); // Vector must be sorted. | 434 WasmIndirectFunctionTable* table = nullptr; |
| 352 if (!cmp_less(*last, *it)) { | 435 exp->index = consume_table_index(module, &table); |
| 353 const byte* pc = start_ + it->name_offset; | 436 if (table) table->exported = true; |
| 354 error(pc, pc, | 437 break; |
| 355 "Duplicate export name '%.*s' for functions %d and %d", | 438 } |
| 356 it->name_length, pc, last->func_index, it->func_index); | 439 case kExternalMemory: { |
| 357 break; | 440 uint32_t index = consume_u32v("memory index"); |
| 358 } | 441 if (index != 0) error("invalid memory index != 0"); |
| 359 } | 442 module->mem_export = true; |
| 360 } | 443 break; |
| 361 break; | 444 } |
| 362 } | 445 case kExternalGlobal: { |
| 363 case WasmSection::Code::Max: | 446 WasmGlobal* global = nullptr; |
| 364 // Skip unknown sections. | 447 exp->index = consume_global_index(module, &global); |
| 365 TRACE("Unknown section: '"); | 448 if (global) global->exported = true; |
| 366 for (uint32_t i = 0; i != string_length; ++i) { | 449 break; |
| 367 TRACE("%c", *(section_name_start + i)); | 450 } |
| 368 } | 451 default: |
| 369 TRACE("'\n"); | 452 error(pos, pos, "invalid export kind 0x%02x", exp->kind); |
| 370 consume_bytes(section_length); | 453 break; |
| 371 break; | 454 } |
| 372 } | 455 } |
| 373 | 456 // Check for duplicate exports. |
| 374 if (pc_ != expected_section_end) { | 457 if (ok() && module->export_table.size() > 1) { |
| 375 const char* diff = pc_ < expected_section_end ? "shorter" : "longer"; | 458 std::vector<WasmExport> sorted_exports(module->export_table); |
| 376 size_t expected_length = static_cast<size_t>(section_length); | 459 const byte* base = start_; |
| 377 size_t actual_length = static_cast<size_t>(pc_ - section_start); | 460 auto cmp_less = [base](const WasmExport& a, const WasmExport& b) { |
| 378 error(pc_, pc_, | 461 // Return true if a < b. |
| 379 "section \"%s\" %s (%zu bytes) than specified (%zu bytes)", | 462 if (a.name_length != b.name_length) { |
| 380 WasmSection::getName(section), diff, actual_length, | 463 return a.name_length < b.name_length; |
| 381 expected_length); | 464 } |
| 382 break; | 465 return memcmp(base + a.name_offset, base + b.name_offset, |
| 383 } | 466 a.name_length) < 0; |
| 384 } | 467 }; |
| 385 | 468 std::stable_sort(sorted_exports.begin(), sorted_exports.end(), |
| 386 done: | 469 cmp_less); |
| 387 if (ok()) CalculateGlobalsOffsets(module); | 470 auto it = sorted_exports.begin(); |
| 471 WasmExport* last = &*it++; |
| 472 for (auto end = sorted_exports.end(); it != end; last = &*it++) { |
| 473 DCHECK(!cmp_less(*it, *last)); // Vector must be sorted. |
| 474 if (!cmp_less(*last, *it)) { |
| 475 const byte* pc = start_ + it->name_offset; |
| 476 error(pc, pc, |
| 477 "Duplicate export name '%.*s' for functions %d and %d", |
| 478 it->name_length, pc, last->index, it->index); |
| 479 break; |
| 480 } |
| 481 } |
| 482 } |
| 483 section_iter.advance(); |
| 484 } |
| 485 |
| 486 // ===== Start section =================================================== |
| 487 if (section_iter.section_code() == kStartSectionCode) { |
| 488 WasmFunction* func; |
| 489 const byte* pos = pc_; |
| 490 module->start_function_index = consume_func_index(module, &func); |
| 491 if (func && func->sig->parameter_count() > 0) { |
| 492 error(pos, "invalid start function: non-zero parameter count"); |
| 493 } |
| 494 section_iter.advance(); |
| 495 } |
| 496 |
| 497 // ===== Elements section ================================================ |
| 498 if (section_iter.section_code() == kElementSectionCode) { |
| 499 uint32_t element_count = consume_u32v("element count"); |
| 500 for (uint32_t i = 0; ok() && i < element_count; ++i) { |
| 501 uint32_t table_index = consume_u32v("table index"); |
| 502 if (table_index != 0) error("illegal table index != 0"); |
| 503 WasmInitExpr offset = consume_init_expr(module, kAstI32); |
| 504 uint32_t num_elem = consume_u32v("number of elements"); |
| 505 std::vector<uint32_t> vector; |
| 506 module->table_inits.push_back({table_index, offset, vector}); |
| 507 WasmTableInit* init = &module->table_inits.back(); |
| 508 init->entries.reserve(SafeReserve(num_elem)); |
| 509 for (uint32_t j = 0; ok() && j < num_elem; j++) { |
| 510 WasmFunction* func = nullptr; |
| 511 init->entries.push_back(consume_func_index(module, &func)); |
| 512 } |
| 513 } |
| 514 |
| 515 section_iter.advance(); |
| 516 } |
| 517 |
| 518 // ===== Code section ==================================================== |
| 519 if (section_iter.section_code() == kCodeSectionCode) { |
| 520 const byte* pos = pc_; |
| 521 uint32_t functions_count = consume_u32v("functions count"); |
| 522 if (functions_count != module->num_declared_functions) { |
| 523 error(pos, pos, "function body count %u mismatch (%u expected)", |
| 524 functions_count, module->num_declared_functions); |
| 525 } |
| 526 for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
| 527 WasmFunction* function = |
| 528 &module->functions[i + module->num_imported_functions]; |
| 529 uint32_t size = consume_u32v("body size"); |
| 530 function->code_start_offset = pc_offset(); |
| 531 function->code_end_offset = pc_offset() + size; |
| 532 consume_bytes(size, "function body"); |
| 533 } |
| 534 section_iter.advance(); |
| 535 } |
| 536 |
| 537 // ===== Data section ==================================================== |
| 538 if (section_iter.section_code() == kDataSectionCode) { |
| 539 uint32_t data_segments_count = consume_u32v("data segments count"); |
| 540 module->data_segments.reserve(SafeReserve(data_segments_count)); |
| 541 for (uint32_t i = 0; ok() && i < data_segments_count; ++i) { |
| 542 TRACE("DecodeDataSegment[%d] module+%d\n", i, |
| 543 static_cast<int>(pc_ - start_)); |
| 544 module->data_segments.push_back({ |
| 545 NO_INIT, // dest_addr |
| 546 0, // source_offset |
| 547 0 // source_size |
| 548 }); |
| 549 WasmDataSegment* segment = &module->data_segments.back(); |
| 550 DecodeDataSegmentInModule(module, segment); |
| 551 } |
| 552 section_iter.advance(); |
| 553 } |
| 554 |
| 555 // ===== Name section ==================================================== |
| 556 if (section_iter.section_code() == kNameSectionCode) { |
| 557 const byte* pos = pc_; |
| 558 uint32_t functions_count = consume_u32v("functions count"); |
| 559 if (functions_count != module->num_declared_functions) { |
| 560 error(pos, pos, "function name count %u mismatch (%u expected)", |
| 561 functions_count, module->num_declared_functions); |
| 562 } |
| 563 |
| 564 for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
| 565 WasmFunction* function = |
| 566 &module->functions[i + module->num_imported_functions]; |
| 567 function->name_offset = consume_string(&function->name_length, false); |
| 568 |
| 569 uint32_t local_names_count = consume_u32v("local names count"); |
| 570 for (uint32_t j = 0; ok() && j < local_names_count; j++) { |
| 571 uint32_t unused = 0; |
| 572 uint32_t offset = consume_string(&unused, false); |
| 573 USE(unused); |
| 574 USE(offset); |
| 575 } |
| 576 } |
| 577 section_iter.advance(); |
| 578 } |
| 579 |
| 580 // ===== Remaining sections ============================================== |
| 581 if (section_iter.more() && ok()) { |
| 582 error(pc(), pc(), "unexpected section: %s", |
| 583 SectionName(section_iter.section_code())); |
| 584 } |
| 585 |
| 586 if (ok()) { |
| 587 CalculateGlobalOffsets(module); |
| 588 PreinitializeIndirectFunctionTables(module); |
| 589 } |
| 388 const WasmModule* finished_module = module; | 590 const WasmModule* finished_module = module; |
| 389 ModuleResult result = toResult(finished_module); | 591 ModuleResult result = toResult(finished_module); |
| 390 if (FLAG_dump_wasm_module) { | 592 if (FLAG_dump_wasm_module) DumpModule(module, result); |
| 391 DumpModule(module, result); | |
| 392 } | |
| 393 return result; | 593 return result; |
| 394 } | 594 } |
| 395 | 595 |
| 396 uint32_t SafeReserve(uint32_t count) { | 596 uint32_t SafeReserve(uint32_t count) { |
| 397 // Avoid OOM by only reserving up to a certain size. | 597 // Avoid OOM by only reserving up to a certain size. |
| 398 const uint32_t kMaxReserve = 20000; | 598 const uint32_t kMaxReserve = 20000; |
| 399 return count < kMaxReserve ? count : kMaxReserve; | 599 return count < kMaxReserve ? count : kMaxReserve; |
| 400 } | 600 } |
| 401 | 601 |
| 402 void CheckForFunctions(WasmModule* module, WasmSection::Code section) { | |
| 403 if (module->functions.size() == 0) { | |
| 404 error(pc_ - 1, nullptr, "functions must appear before section %s", | |
| 405 WasmSection::getName(section)); | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 int CheckSectionOrder(int current_order, WasmSection::Code section) { | |
| 410 int next_order = WasmSection::getOrder(section); | |
| 411 if (next_order == 0) return current_order; | |
| 412 if (next_order == current_order) { | |
| 413 error(pc_, pc_, "section \"%s\" already defined", | |
| 414 WasmSection::getName(section)); | |
| 415 } | |
| 416 if (next_order < current_order) { | |
| 417 error(pc_, pc_, "section \"%s\" out of order", | |
| 418 WasmSection::getName(section)); | |
| 419 } | |
| 420 return next_order; | |
| 421 } | |
| 422 | |
| 423 // Decodes a single anonymous function starting at {start_}. | 602 // Decodes a single anonymous function starting at {start_}. |
| 424 FunctionResult DecodeSingleFunction(ModuleEnv* module_env, | 603 FunctionResult DecodeSingleFunction(ModuleEnv* module_env, |
| 425 WasmFunction* function) { | 604 WasmFunction* function) { |
| 426 pc_ = start_; | 605 pc_ = start_; |
| 427 function->sig = consume_sig(); // read signature | 606 function->sig = consume_sig(); // read signature |
| 428 function->name_offset = 0; // ---- name | 607 function->name_offset = 0; // ---- name |
| 429 function->name_length = 0; // ---- name length | 608 function->name_length = 0; // ---- name length |
| 430 function->code_start_offset = off(pc_); // ---- code start | 609 function->code_start_offset = off(pc_); // ---- code start |
| 431 function->code_end_offset = off(limit_); // ---- code end | 610 function->code_end_offset = off(limit_); // ---- code end |
| 432 | 611 |
| 433 if (ok()) VerifyFunctionBody(0, module_env, function); | 612 if (ok()) VerifyFunctionBody(0, module_env, function); |
| 434 | 613 |
| 435 FunctionResult result; | 614 FunctionResult result; |
| 436 result.MoveFrom(result_); // Copy error code and location. | 615 result.MoveFrom(result_); // Copy error code and location. |
| 437 result.val = function; | 616 result.val = function; |
| 438 return result; | 617 return result; |
| 439 } | 618 } |
| 440 | 619 |
| 441 // Decodes a single function signature at {start}. | 620 // Decodes a single function signature at {start}. |
| 442 FunctionSig* DecodeFunctionSignature(const byte* start) { | 621 FunctionSig* DecodeFunctionSignature(const byte* start) { |
| 443 pc_ = start; | 622 pc_ = start; |
| 444 FunctionSig* result = consume_sig(); | 623 FunctionSig* result = consume_sig(); |
| 445 return ok() ? result : nullptr; | 624 return ok() ? result : nullptr; |
| 446 } | 625 } |
| 447 | 626 |
| 627 WasmInitExpr DecodeInitExpr(const byte* start) { |
| 628 pc_ = start; |
| 629 return consume_init_expr(nullptr, kAstStmt); |
| 630 } |
| 631 |
| 448 private: | 632 private: |
| 449 Zone* module_zone; | 633 Zone* module_zone; |
| 450 ModuleResult result_; | 634 ModuleResult result_; |
| 451 ModuleOrigin origin_; | 635 ModuleOrigin origin_; |
| 452 | 636 |
| 453 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } | 637 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } |
| 454 | 638 |
| 455 // Decodes a single global entry inside a module starting at {pc_}. | 639 // Decodes a single global entry inside a module starting at {pc_}. |
| 456 void DecodeGlobalInModule(WasmGlobal* global) { | 640 void DecodeGlobalInModule(WasmModule* module, uint32_t index, |
| 457 global->name_offset = consume_string(&global->name_length, false); | 641 WasmGlobal* global) { |
| 458 if (ok() && | 642 global->type = consume_value_type(); |
| 459 !unibrow::Utf8::Validate(start_ + global->name_offset, | 643 global->mutability = consume_u8("mutability") != 0; |
| 460 global->name_length)) { | 644 const byte* pos = pc(); |
| 461 error("global name is not valid utf8"); | 645 global->init = consume_init_expr(module, kAstStmt); |
| 646 switch (global->init.kind) { |
| 647 case WasmInitExpr::kGlobalIndex: |
| 648 if (global->init.val.global_index >= index) { |
| 649 error("invalid global index in init expression"); |
| 650 } else if (module->globals[index].type != global->type) { |
| 651 error("type mismatch in global initialization"); |
| 652 } |
| 653 break; |
| 654 default: |
| 655 if (global->type != TypeOf(module, global->init)) { |
| 656 error(pos, pos, |
| 657 "type error in global initialization, expected %s, got %s", |
| 658 WasmOpcodes::TypeName(global->type), |
| 659 WasmOpcodes::TypeName(TypeOf(module, global->init))); |
| 660 } |
| 462 } | 661 } |
| 463 global->type = consume_local_type(); | |
| 464 if (global->type == kAstStmt) { | |
| 465 error("invalid global type"); | |
| 466 } | |
| 467 global->offset = 0; | |
| 468 global->exported = consume_u8("exported") != 0; | |
| 469 } | 662 } |
| 470 | 663 |
| 471 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { | 664 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { |
| 472 if (offset > limit) return false; | 665 if (offset > limit) return false; |
| 473 if ((offset + size) < offset) return false; // overflow | 666 if ((offset + size) < offset) return false; // overflow |
| 474 return (offset + size) <= limit; | 667 return (offset + size) <= limit; |
| 475 } | 668 } |
| 476 | 669 |
| 477 // Decodes a single data segment entry inside a module starting at {pc_}. | 670 // Decodes a single data segment entry inside a module starting at {pc_}. |
| 478 void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) { | 671 void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) { |
| 479 const byte* start = pc_; | 672 const byte* start = pc_; |
| 480 segment->dest_addr = consume_u32v("destination"); | 673 expect_u8("linear memory index", 0); |
| 674 segment->dest_addr = consume_init_expr(module, kAstI32); |
| 481 segment->source_size = consume_u32v("source size"); | 675 segment->source_size = consume_u32v("source size"); |
| 482 segment->source_offset = static_cast<uint32_t>(pc_ - start_); | 676 segment->source_offset = static_cast<uint32_t>(pc_ - start_); |
| 483 segment->init = true; | |
| 484 | 677 |
| 485 // Validate the data is in the module. | 678 // Validate the data is in the module. |
| 486 uint32_t module_limit = static_cast<uint32_t>(limit_ - start_); | 679 uint32_t module_limit = static_cast<uint32_t>(limit_ - start_); |
| 487 if (!IsWithinLimit(module_limit, segment->source_offset, | 680 if (!IsWithinLimit(module_limit, segment->source_offset, |
| 488 segment->source_size)) { | 681 segment->source_size)) { |
| 489 error(start, "segment out of bounds of module"); | 682 error(start, "segment out of bounds of module"); |
| 490 } | 683 } |
| 491 | 684 |
| 492 // Validate that the segment will fit into the (minimum) memory. | 685 consume_bytes(segment->source_size, "segment data"); |
| 493 uint32_t memory_limit = | |
| 494 WasmModule::kPageSize * (module ? module->min_mem_pages | |
| 495 : WasmModule::kMaxMemPages); | |
| 496 if (!IsWithinLimit(memory_limit, segment->dest_addr, | |
| 497 segment->source_size)) { | |
| 498 error(start, "segment out of bounds of memory"); | |
| 499 } | |
| 500 | |
| 501 consume_bytes(segment->source_size); | |
| 502 } | |
| 503 | |
| 504 // Decodes a single function table inside a module starting at {pc_}. | |
| 505 void DecodeFunctionTableInModule(WasmModule* module, | |
| 506 WasmIndirectFunctionTable* table) { | |
| 507 table->size = consume_u32v("function table entry count"); | |
| 508 table->max_size = table->size; | |
| 509 | |
| 510 if (table->max_size != table->size) { | |
| 511 error("invalid table maximum size"); | |
| 512 } | |
| 513 | |
| 514 for (uint32_t i = 0; ok() && i < table->size; ++i) { | |
| 515 uint16_t index = consume_u32v(); | |
| 516 if (index >= module->functions.size()) { | |
| 517 error(pc_ - sizeof(index), "invalid function index"); | |
| 518 break; | |
| 519 } | |
| 520 table->values.push_back(index); | |
| 521 } | |
| 522 } | 686 } |
| 523 | 687 |
| 524 // Calculate individual global offsets and total size of globals table. | 688 // Calculate individual global offsets and total size of globals table. |
| 525 void CalculateGlobalsOffsets(WasmModule* module) { | 689 void CalculateGlobalOffsets(WasmModule* module) { |
| 526 uint32_t offset = 0; | 690 uint32_t offset = 0; |
| 527 if (module->globals.size() == 0) { | 691 if (module->globals.size() == 0) { |
| 528 module->globals_size = 0; | 692 module->globals_size = 0; |
| 529 return; | 693 return; |
| 530 } | 694 } |
| 531 for (WasmGlobal& global : module->globals) { | 695 for (WasmGlobal& global : module->globals) { |
| 532 byte size = | 696 byte size = |
| 533 WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(global.type)); | 697 WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(global.type)); |
| 534 offset = (offset + size - 1) & ~(size - 1); // align | 698 offset = (offset + size - 1) & ~(size - 1); // align |
| 535 global.offset = offset; | 699 global.offset = offset; |
| 536 offset += size; | 700 offset += size; |
| 537 } | 701 } |
| 538 module->globals_size = offset; | 702 module->globals_size = offset; |
| 539 } | 703 } |
| 540 | 704 |
| 705 // TODO(titzer): this only works without overlapping initializations from |
| 706 // global bases for entries |
| 707 void PreinitializeIndirectFunctionTables(WasmModule* module) { |
| 708 // Fill all tables with invalid entries first. |
| 709 for (WasmIndirectFunctionTable& table : module->function_tables) { |
| 710 table.values.resize(table.size); |
| 711 for (size_t i = 0; i < table.size; i++) { |
| 712 table.values[i] = kInvalidFunctionIndex; |
| 713 } |
| 714 } |
| 715 for (WasmTableInit& init : module->table_inits) { |
| 716 if (init.offset.kind != WasmInitExpr::kI32Const) continue; |
| 717 if (init.table_index >= module->function_tables.size()) continue; |
| 718 WasmIndirectFunctionTable& table = |
| 719 module->function_tables[init.table_index]; |
| 720 for (size_t i = 0; i < init.entries.size(); i++) { |
| 721 size_t index = i + init.offset.val.i32_const; |
| 722 if (index < table.values.size()) { |
| 723 table.values[index] = init.entries[i]; |
| 724 } |
| 725 } |
| 726 } |
| 727 } |
| 728 |
| 541 // Verifies the body (code) of a given function. | 729 // Verifies the body (code) of a given function. |
| 542 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, | 730 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, |
| 543 WasmFunction* function) { | 731 WasmFunction* function) { |
| 544 if (FLAG_trace_wasm_decoder || FLAG_trace_wasm_decode_time) { | 732 if (FLAG_trace_wasm_decoder || FLAG_trace_wasm_decode_time) { |
| 545 OFStream os(stdout); | 733 OFStream os(stdout); |
| 546 os << "Verifying WASM function " << WasmFunctionName(function, menv) | 734 os << "Verifying WASM function " << WasmFunctionName(function, menv) |
| 547 << std::endl; | 735 << std::endl; |
| 548 } | 736 } |
| 549 FunctionBody body = {menv, function->sig, start_, | 737 FunctionBody body = {menv, function->sig, start_, |
| 550 start_ + function->code_start_offset, | 738 start_ + function->code_start_offset, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 561 char* buffer = new char[len]; | 749 char* buffer = new char[len]; |
| 562 strncpy(buffer, raw, len); | 750 strncpy(buffer, raw, len); |
| 563 buffer[len - 1] = 0; | 751 buffer[len - 1] = 0; |
| 564 | 752 |
| 565 // Copy error code and location. | 753 // Copy error code and location. |
| 566 result_.MoveFrom(result); | 754 result_.MoveFrom(result); |
| 567 result_.error_msg.reset(buffer); | 755 result_.error_msg.reset(buffer); |
| 568 } | 756 } |
| 569 } | 757 } |
| 570 | 758 |
| 571 // Reads a single 32-bit unsigned integer interpreted as an offset, checking | |
| 572 // the offset is within bounds and advances. | |
| 573 uint32_t consume_offset(const char* name = nullptr) { | |
| 574 uint32_t offset = consume_u32(name ? name : "offset"); | |
| 575 if (offset > static_cast<uint32_t>(limit_ - start_)) { | |
| 576 error(pc_ - sizeof(uint32_t), "offset out of bounds of module"); | |
| 577 } | |
| 578 return offset; | |
| 579 } | |
| 580 | |
| 581 // Reads a length-prefixed string, checking that it is within bounds. Returns | 759 // Reads a length-prefixed string, checking that it is within bounds. Returns |
| 582 // the offset of the string, and the length as an out parameter. | 760 // the offset of the string, and the length as an out parameter. |
| 583 uint32_t consume_string(uint32_t* length, bool validate_utf8) { | 761 uint32_t consume_string(uint32_t* length, bool validate_utf8) { |
| 584 *length = consume_u32v("string length"); | 762 *length = consume_u32v("string length"); |
| 585 uint32_t offset = pc_offset(); | 763 uint32_t offset = pc_offset(); |
| 586 TRACE(" +%u %-20s: (%u bytes)\n", offset, "string", *length); | |
| 587 const byte* string_start = pc_; | 764 const byte* string_start = pc_; |
| 588 // Consume bytes before validation to guarantee that the string is not oob. | 765 // Consume bytes before validation to guarantee that the string is not oob. |
| 589 consume_bytes(*length); | 766 consume_bytes(*length, "string"); |
| 590 if (ok() && validate_utf8 && | 767 if (ok() && validate_utf8 && |
| 591 !unibrow::Utf8::Validate(string_start, *length)) { | 768 !unibrow::Utf8::Validate(string_start, *length)) { |
| 592 error(string_start, "no valid UTF-8 string"); | 769 error(string_start, "no valid UTF-8 string"); |
| 593 } | 770 } |
| 594 return offset; | 771 return offset; |
| 595 } | 772 } |
| 596 | 773 |
| 597 uint32_t consume_sig_index(WasmModule* module, FunctionSig** sig) { | 774 uint32_t consume_sig_index(WasmModule* module, FunctionSig** sig) { |
| 598 const byte* pos = pc_; | 775 const byte* pos = pc_; |
| 599 uint32_t sig_index = consume_u32v("signature index"); | 776 uint32_t sig_index = consume_u32v("signature index"); |
| 600 if (sig_index >= module->signatures.size()) { | 777 if (sig_index >= module->signatures.size()) { |
| 601 error(pos, pos, "signature index %u out of bounds (%d signatures)", | 778 error(pos, pos, "signature index %u out of bounds (%d signatures)", |
| 602 sig_index, static_cast<int>(module->signatures.size())); | 779 sig_index, static_cast<int>(module->signatures.size())); |
| 603 *sig = nullptr; | 780 *sig = nullptr; |
| 604 return 0; | 781 return 0; |
| 605 } | 782 } |
| 606 *sig = module->signatures[sig_index]; | 783 *sig = module->signatures[sig_index]; |
| 607 return sig_index; | 784 return sig_index; |
| 608 } | 785 } |
| 609 | 786 |
| 610 uint32_t consume_func_index(WasmModule* module, WasmFunction** func) { | 787 uint32_t consume_func_index(WasmModule* module, WasmFunction** func) { |
| 788 return consume_index("function index", module->functions, func); |
| 789 } |
| 790 |
| 791 uint32_t consume_global_index(WasmModule* module, WasmGlobal** global) { |
| 792 return consume_index("global index", module->globals, global); |
| 793 } |
| 794 |
| 795 uint32_t consume_table_index(WasmModule* module, |
| 796 WasmIndirectFunctionTable** table) { |
| 797 return consume_index("table index", module->function_tables, table); |
| 798 } |
| 799 |
| 800 template <typename T> |
| 801 uint32_t consume_index(const char* name, std::vector<T>& vector, T** ptr) { |
| 611 const byte* pos = pc_; | 802 const byte* pos = pc_; |
| 612 uint32_t func_index = consume_u32v("function index"); | 803 uint32_t index = consume_u32v(name); |
| 613 if (func_index >= module->functions.size()) { | 804 if (index >= vector.size()) { |
| 614 error(pos, pos, "function index %u out of bounds (%d functions)", | 805 error(pos, pos, "%s %u out of bounds (%d entries)", name, index, |
| 615 func_index, static_cast<int>(module->functions.size())); | 806 static_cast<int>(vector.size())); |
| 616 *func = nullptr; | 807 *ptr = nullptr; |
| 617 return 0; | 808 return 0; |
| 618 } | 809 } |
| 619 *func = &module->functions[func_index]; | 810 *ptr = &vector[index]; |
| 620 return func_index; | 811 return index; |
| 812 } |
| 813 |
| 814 void consume_resizable_limits(const char* name, const char* units, |
| 815 uint32_t max_value, uint32_t* initial, |
| 816 uint32_t* maximum) { |
| 817 uint32_t flags = consume_u32v("resizable limits flags"); |
| 818 const byte* pos = pc(); |
| 819 *initial = consume_u32v("initial size"); |
| 820 if (*initial > max_value) { |
| 821 error(pos, pos, |
| 822 "initial %s size (%u %s) is larger than maximum allowable (%u)", |
| 823 name, *initial, units, max_value); |
| 824 } |
| 825 if (flags & 1) { |
| 826 pos = pc(); |
| 827 *maximum = consume_u32v("maximum size"); |
| 828 if (*maximum > max_value) { |
| 829 error(pos, pos, |
| 830 "maximum %s size (%u %s) is larger than maximum allowable (%u)", |
| 831 name, *maximum, units, max_value); |
| 832 } |
| 833 if (*maximum < *initial) { |
| 834 error(pos, pos, "maximum %s size (%u %s) is less than initial (%u %s)", |
| 835 name, *maximum, units, *initial, units); |
| 836 } |
| 837 } else { |
| 838 *maximum = 0; |
| 839 } |
| 840 } |
| 841 |
| 842 bool expect_u8(const char* name, uint8_t expected) { |
| 843 const byte* pos = pc(); |
| 844 uint8_t value = consume_u8(name); |
| 845 if (value != expected) { |
| 846 error(pos, pos, "expected %s 0x%02x, got 0x%02x", name, expected, value); |
| 847 return false; |
| 848 } |
| 849 return true; |
| 850 } |
| 851 |
| 852 WasmInitExpr consume_init_expr(WasmModule* module, LocalType expected) { |
| 853 const byte* pos = pc(); |
| 854 uint8_t opcode = consume_u8("opcode"); |
| 855 WasmInitExpr expr; |
| 856 unsigned len = 0; |
| 857 switch (opcode) { |
| 858 case kExprGetGlobal: { |
| 859 GlobalIndexOperand operand(this, pc() - 1); |
| 860 expr.kind = WasmInitExpr::kGlobalIndex; |
| 861 expr.val.global_index = operand.index; |
| 862 len = operand.length; |
| 863 break; |
| 864 } |
| 865 case kExprI32Const: { |
| 866 ImmI32Operand operand(this, pc() - 1); |
| 867 expr.kind = WasmInitExpr::kI32Const; |
| 868 expr.val.i32_const = operand.value; |
| 869 len = operand.length; |
| 870 break; |
| 871 } |
| 872 case kExprF32Const: { |
| 873 ImmF32Operand operand(this, pc() - 1); |
| 874 expr.kind = WasmInitExpr::kF32Const; |
| 875 expr.val.f32_const = operand.value; |
| 876 len = operand.length; |
| 877 break; |
| 878 } |
| 879 case kExprI64Const: { |
| 880 ImmI64Operand operand(this, pc() - 1); |
| 881 expr.kind = WasmInitExpr::kI64Const; |
| 882 expr.val.i64_const = operand.value; |
| 883 len = operand.length; |
| 884 break; |
| 885 } |
| 886 case kExprF64Const: { |
| 887 ImmF64Operand operand(this, pc() - 1); |
| 888 expr.kind = WasmInitExpr::kF64Const; |
| 889 expr.val.f64_const = operand.value; |
| 890 len = operand.length; |
| 891 break; |
| 892 } |
| 893 default: { |
| 894 error("invalid opcode in initialization expression"); |
| 895 expr.kind = WasmInitExpr::kNone; |
| 896 expr.val.i32_const = 0; |
| 897 } |
| 898 } |
| 899 consume_bytes(len, "init code"); |
| 900 if (!expect_u8("end opcode", kExprEnd)) { |
| 901 expr.kind = WasmInitExpr::kNone; |
| 902 } |
| 903 if (expected != kAstStmt && TypeOf(module, expr) != kAstI32) { |
| 904 error(pos, pos, "type error in init expression, expected %s, got %s", |
| 905 WasmOpcodes::TypeName(expected), |
| 906 WasmOpcodes::TypeName(TypeOf(module, expr))); |
| 907 } |
| 908 return expr; |
| 621 } | 909 } |
| 622 | 910 |
| 623 // Reads a single 8-bit integer, interpreting it as a local type. | 911 // Reads a single 8-bit integer, interpreting it as a local type. |
| 624 LocalType consume_local_type() { | 912 LocalType consume_value_type() { |
| 625 byte val = consume_u8("local type"); | 913 byte val = consume_u8("value type"); |
| 626 LocalTypeCode t = static_cast<LocalTypeCode>(val); | 914 LocalTypeCode t = static_cast<LocalTypeCode>(val); |
| 627 switch (t) { | 915 switch (t) { |
| 628 case kLocalVoid: | |
| 629 return kAstStmt; | |
| 630 case kLocalI32: | 916 case kLocalI32: |
| 631 return kAstI32; | 917 return kAstI32; |
| 632 case kLocalI64: | 918 case kLocalI64: |
| 633 return kAstI64; | 919 return kAstI64; |
| 634 case kLocalF32: | 920 case kLocalF32: |
| 635 return kAstF32; | 921 return kAstF32; |
| 636 case kLocalF64: | 922 case kLocalF64: |
| 637 return kAstF64; | 923 return kAstF64; |
| 638 case kLocalS128: | 924 case kLocalS128: |
| 639 return kAstS128; | 925 return kAstS128; |
| 640 default: | 926 default: |
| 641 error(pc_ - 1, "invalid local type"); | 927 error(pc_ - 1, "invalid local type"); |
| 642 return kAstStmt; | 928 return kAstStmt; |
| 643 } | 929 } |
| 644 } | 930 } |
| 645 | 931 |
| 646 // Parses a type entry, which is currently limited to functions only. | 932 // Parses a type entry, which is currently limited to functions only. |
| 647 FunctionSig* consume_sig() { | 933 FunctionSig* consume_sig() { |
| 648 const byte* pos = pc_; | 934 if (!expect_u8("type form", kWasmFunctionTypeForm)) return nullptr; |
| 649 byte form = consume_u8("type form"); | |
| 650 if (form != kWasmFunctionTypeForm) { | |
| 651 error(pos, pos, "expected function type form (0x%02x), got: 0x%02x", | |
| 652 kWasmFunctionTypeForm, form); | |
| 653 return nullptr; | |
| 654 } | |
| 655 // parse parameter types | 935 // parse parameter types |
| 656 uint32_t param_count = consume_u32v("param count"); | 936 uint32_t param_count = consume_u32v("param count"); |
| 657 std::vector<LocalType> params; | 937 std::vector<LocalType> params; |
| 658 for (uint32_t i = 0; i < param_count; ++i) { | 938 for (uint32_t i = 0; ok() && i < param_count; ++i) { |
| 659 LocalType param = consume_local_type(); | 939 LocalType param = consume_value_type(); |
| 660 if (param == kAstStmt) error(pc_ - 1, "invalid void parameter type"); | |
| 661 params.push_back(param); | 940 params.push_back(param); |
| 662 } | 941 } |
| 663 | 942 |
| 664 // parse return types | 943 // parse return types |
| 665 const byte* pt = pc_; | 944 const byte* pt = pc_; |
| 666 uint32_t return_count = consume_u32v("return count"); | 945 uint32_t return_count = consume_u32v("return count"); |
| 667 if (return_count > kMaxReturnCount) { | 946 if (return_count > kMaxReturnCount) { |
| 668 error(pt, pt, "return count of %u exceeds maximum of %u", return_count, | 947 error(pt, pt, "return count of %u exceeds maximum of %u", return_count, |
| 669 kMaxReturnCount); | 948 kMaxReturnCount); |
| 670 return nullptr; | 949 return nullptr; |
| 671 } | 950 } |
| 672 std::vector<LocalType> returns; | 951 std::vector<LocalType> returns; |
| 673 for (uint32_t i = 0; i < return_count; ++i) { | 952 for (uint32_t i = 0; ok() && i < return_count; ++i) { |
| 674 LocalType ret = consume_local_type(); | 953 LocalType ret = consume_value_type(); |
| 675 if (ret == kAstStmt) error(pc_ - 1, "invalid void return type"); | |
| 676 returns.push_back(ret); | 954 returns.push_back(ret); |
| 677 } | 955 } |
| 678 | 956 |
| 957 if (failed()) { |
| 958 // Decoding failed, return void -> void |
| 959 return new (module_zone) FunctionSig(0, 0, nullptr); |
| 960 } |
| 961 |
| 679 // FunctionSig stores the return types first. | 962 // FunctionSig stores the return types first. |
| 680 LocalType* buffer = | 963 LocalType* buffer = |
| 681 module_zone->NewArray<LocalType>(param_count + return_count); | 964 module_zone->NewArray<LocalType>(param_count + return_count); |
| 682 uint32_t b = 0; | 965 uint32_t b = 0; |
| 683 for (uint32_t i = 0; i < return_count; ++i) buffer[b++] = returns[i]; | 966 for (uint32_t i = 0; i < return_count; ++i) buffer[b++] = returns[i]; |
| 684 for (uint32_t i = 0; i < param_count; ++i) buffer[b++] = params[i]; | 967 for (uint32_t i = 0; i < param_count; ++i) buffer[b++] = params[i]; |
| 685 | 968 |
| 686 return new (module_zone) FunctionSig(return_count, param_count, buffer); | 969 return new (module_zone) FunctionSig(return_count, param_count, buffer); |
| 687 } | 970 } |
| 688 }; | 971 }; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 707 error_code = kError; | 990 error_code = kError; |
| 708 size_t len = strlen(msg) + 1; | 991 size_t len = strlen(msg) + 1; |
| 709 char* result = new char[len]; | 992 char* result = new char[len]; |
| 710 strncpy(result, msg, len); | 993 strncpy(result, msg, len); |
| 711 result[len - 1] = 0; | 994 result[len - 1] = 0; |
| 712 error_msg.reset(result); | 995 error_msg.reset(result); |
| 713 } | 996 } |
| 714 }; | 997 }; |
| 715 | 998 |
| 716 Vector<const byte> FindSection(const byte* module_start, const byte* module_end, | 999 Vector<const byte> FindSection(const byte* module_start, const byte* module_end, |
| 717 WasmSection::Code code) { | 1000 WasmSectionCode code) { |
| 718 Decoder decoder(module_start, module_end); | 1001 Decoder decoder(module_start, module_end); |
| 719 | 1002 |
| 720 uint32_t magic_word = decoder.consume_u32("wasm magic"); | 1003 uint32_t magic_word = decoder.consume_u32("wasm magic"); |
| 721 if (magic_word != kWasmMagic) decoder.error("wrong magic word"); | 1004 if (magic_word != kWasmMagic) decoder.error("wrong magic word"); |
| 722 | 1005 |
| 723 uint32_t magic_version = decoder.consume_u32("wasm version"); | 1006 uint32_t magic_version = decoder.consume_u32("wasm version"); |
| 724 if (magic_version != kWasmVersion) decoder.error("wrong wasm version"); | 1007 if (magic_version != kWasmVersion) decoder.error("wrong wasm version"); |
| 725 | 1008 |
| 726 while (decoder.more() && decoder.ok()) { | 1009 WasmSectionIterator section_iter(decoder); |
| 727 // Read the section name. | 1010 while (section_iter.more()) { |
| 728 uint32_t string_length = decoder.consume_u32v("section name length"); | 1011 if (section_iter.section_code() == code) { |
| 729 const byte* section_name_start = decoder.pc(); | 1012 return Vector<const uint8_t>(section_iter.section_start(), |
| 730 decoder.consume_bytes(string_length); | 1013 section_iter.section_length()); |
| 731 if (decoder.failed()) break; | |
| 732 | |
| 733 WasmSection::Code section = | |
| 734 WasmSection::lookup(section_name_start, string_length); | |
| 735 | |
| 736 // Read and check the section size. | |
| 737 uint32_t section_length = decoder.consume_u32v("section length"); | |
| 738 | |
| 739 const byte* section_start = decoder.pc(); | |
| 740 decoder.consume_bytes(section_length); | |
| 741 if (section == code && decoder.ok()) { | |
| 742 return Vector<const uint8_t>(section_start, section_length); | |
| 743 } | 1014 } |
| 1015 decoder.consume_bytes(section_iter.section_length(), "section payload"); |
| 1016 section_iter.advance(); |
| 744 } | 1017 } |
| 745 | 1018 |
| 746 return Vector<const uint8_t>(); | 1019 return Vector<const uint8_t>(); |
| 747 } | 1020 } |
| 748 | 1021 |
| 749 } // namespace | 1022 } // namespace |
| 750 | 1023 |
| 751 ModuleResult DecodeWasmModule(Isolate* isolate, Zone* zone, | 1024 ModuleResult DecodeWasmModule(Isolate* isolate, Zone* zone, |
| 752 const byte* module_start, const byte* module_end, | 1025 const byte* module_start, const byte* module_end, |
| 753 bool verify_functions, ModuleOrigin origin) { | 1026 bool verify_functions, ModuleOrigin origin) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 768 static_cast<int>(zone->allocation_size() - decode_memory_start)); | 1041 static_cast<int>(zone->allocation_size() - decode_memory_start)); |
| 769 return result; | 1042 return result; |
| 770 } | 1043 } |
| 771 | 1044 |
| 772 FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, | 1045 FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, |
| 773 const byte* end) { | 1046 const byte* end) { |
| 774 ModuleDecoder decoder(zone, start, end, kWasmOrigin); | 1047 ModuleDecoder decoder(zone, start, end, kWasmOrigin); |
| 775 return decoder.DecodeFunctionSignature(start); | 1048 return decoder.DecodeFunctionSignature(start); |
| 776 } | 1049 } |
| 777 | 1050 |
| 1051 WasmInitExpr DecodeWasmInitExprForTesting(const byte* start, const byte* end) { |
| 1052 AccountingAllocator allocator; |
| 1053 Zone zone(&allocator); |
| 1054 ModuleDecoder decoder(&zone, start, end, kWasmOrigin); |
| 1055 return decoder.DecodeInitExpr(start); |
| 1056 } |
| 1057 |
| 778 FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, | 1058 FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, |
| 779 ModuleEnv* module_env, | 1059 ModuleEnv* module_env, |
| 780 const byte* function_start, | 1060 const byte* function_start, |
| 781 const byte* function_end) { | 1061 const byte* function_end) { |
| 782 HistogramTimerScope wasm_decode_function_time_scope( | 1062 HistogramTimerScope wasm_decode_function_time_scope( |
| 783 isolate->counters()->wasm_decode_function_time()); | 1063 isolate->counters()->wasm_decode_function_time()); |
| 784 size_t size = function_end - function_start; | 1064 size_t size = function_end - function_start; |
| 785 if (function_start > function_end) return FunctionError("start > end"); | 1065 if (function_start > function_end) return FunctionError("start > end"); |
| 786 if (size > kMaxFunctionSize) | 1066 if (size > kMaxFunctionSize) |
| 787 return FunctionError("size > maximum function size"); | 1067 return FunctionError("size > maximum function size"); |
| 788 isolate->counters()->wasm_function_size_bytes()->AddSample( | 1068 isolate->counters()->wasm_function_size_bytes()->AddSample( |
| 789 static_cast<int>(size)); | 1069 static_cast<int>(size)); |
| 790 WasmFunction* function = new WasmFunction(); | 1070 WasmFunction* function = new WasmFunction(); |
| 791 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); | 1071 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); |
| 792 return decoder.DecodeSingleFunction(module_env, function); | 1072 return decoder.DecodeSingleFunction(module_env, function); |
| 793 } | 1073 } |
| 794 | 1074 |
| 795 FunctionOffsetsResult DecodeWasmFunctionOffsets(const byte* module_start, | 1075 FunctionOffsetsResult DecodeWasmFunctionOffsets( |
| 796 const byte* module_end) { | 1076 const byte* module_start, const byte* module_end, |
| 1077 uint32_t num_imported_functions) { |
| 1078 // Find and decode the code section. |
| 797 Vector<const byte> code_section = | 1079 Vector<const byte> code_section = |
| 798 FindSection(module_start, module_end, WasmSection::Code::FunctionBodies); | 1080 FindSection(module_start, module_end, kCodeSectionCode); |
| 799 Decoder decoder(code_section.start(), code_section.end()); | 1081 Decoder decoder(code_section.start(), code_section.end()); |
| 800 if (!code_section.start()) decoder.error("no code section"); | 1082 FunctionOffsets table; |
| 1083 if (!code_section.start()) { |
| 1084 decoder.error("no code section"); |
| 1085 return decoder.toResult(std::move(table)); |
| 1086 } |
| 1087 |
| 1088 // Reserve entries for the imported functions. |
| 1089 table.reserve(num_imported_functions); |
| 1090 for (uint32_t i = 0; i < num_imported_functions; i++) { |
| 1091 table.push_back(std::make_pair(0, 0)); |
| 1092 } |
| 801 | 1093 |
| 802 uint32_t functions_count = decoder.consume_u32v("functions count"); | 1094 uint32_t functions_count = decoder.consume_u32v("functions count"); |
| 803 FunctionOffsets table; | |
| 804 // Take care of invalid input here. | 1095 // Take care of invalid input here. |
| 805 if (functions_count < static_cast<unsigned>(code_section.length()) / 2) | 1096 if (functions_count < static_cast<unsigned>(code_section.length()) / 2) |
| 806 table.reserve(functions_count); | 1097 table.reserve(functions_count); |
| 807 int section_offset = static_cast<int>(code_section.start() - module_start); | 1098 int section_offset = static_cast<int>(code_section.start() - module_start); |
| 808 DCHECK_LE(0, section_offset); | 1099 DCHECK_LE(0, section_offset); |
| 809 for (uint32_t i = 0; i < functions_count && decoder.ok(); ++i) { | 1100 for (uint32_t i = 0; i < functions_count && decoder.ok(); ++i) { |
| 810 uint32_t size = decoder.consume_u32v("body size"); | 1101 uint32_t size = decoder.consume_u32v("body size"); |
| 811 int offset = static_cast<int>(section_offset + decoder.pc_offset()); | 1102 int offset = static_cast<int>(section_offset + decoder.pc_offset()); |
| 812 table.push_back(std::make_pair(offset, static_cast<int>(size))); | 1103 table.push_back(std::make_pair(offset, static_cast<int>(size))); |
| 813 DCHECK(table.back().first >= 0 && table.back().second >= 0); | 1104 DCHECK(table.back().first >= 0 && table.back().second >= 0); |
| 814 decoder.consume_bytes(size); | 1105 decoder.consume_bytes(size); |
| 815 } | 1106 } |
| 816 if (decoder.more()) decoder.error("unexpected additional bytes"); | 1107 if (decoder.more()) decoder.error("unexpected additional bytes"); |
| 817 | 1108 |
| 818 return decoder.toResult(std::move(table)); | 1109 return decoder.toResult(std::move(table)); |
| 819 } | 1110 } |
| 820 | 1111 |
| 821 } // namespace wasm | 1112 } // namespace wasm |
| 822 } // namespace internal | 1113 } // namespace internal |
| 823 } // namespace v8 | 1114 } // namespace v8 |
| OLD | NEW |