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

Side by Side Diff: src/wasm/wasm-module.cc

Issue 1900153002: [wasm] Enforce strict ordering of WASM module sections. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix OOB 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 unified diff | Download patch
OLDNEW
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/macro-assembler.h" 5 #include "src/macro-assembler.h"
6 #include "src/objects.h" 6 #include "src/objects.h"
7 #include "src/v8.h" 7 #include "src/v8.h"
8 8
9 #include "src/simulator.h" 9 #include "src/simulator.h"
10 10
11 #include "src/wasm/ast-decoder.h" 11 #include "src/wasm/ast-decoder.h"
12 #include "src/wasm/module-decoder.h" 12 #include "src/wasm/module-decoder.h"
13 #include "src/wasm/wasm-module.h" 13 #include "src/wasm/wasm-module.h"
14 #include "src/wasm/wasm-result.h" 14 #include "src/wasm/wasm-result.h"
15 15
16 #include "src/compiler/wasm-compiler.h" 16 #include "src/compiler/wasm-compiler.h"
17 17
18 namespace v8 { 18 namespace v8 {
19 namespace internal { 19 namespace internal {
20 namespace wasm { 20 namespace wasm {
21 21
22 static const char* wasmSections[] = { 22 static const char* wasmSections[] = {
23 #define F(enumerator, string) string, 23 #define F(enumerator, order, string) string,
24 FOR_EACH_WASM_SECTION_TYPE(F) 24 FOR_EACH_WASM_SECTION_TYPE(F)
25 #undef F 25 #undef F
JF 2016/04/20 17:59:54 Need an entry for Code::Max. Should probably make
titzer 2016/04/21 10:59:22 done.
26 }; 26 };
27 27
28 static uint8_t wasmSectionsLengths[]{ 28 static uint8_t wasmSectionsLengths[]{
29 #define F(enumerator, string) sizeof(string) - 1, 29 #define F(enumerator, order, string) sizeof(string) - 1,
30 FOR_EACH_WASM_SECTION_TYPE(F) 30 FOR_EACH_WASM_SECTION_TYPE(F)
31 #undef F 31 #undef F
JF 2016/04/20 17:59:54 Same, but this one's weird.
titzer 2016/04/21 10:59:22 done
32 }; 32 };
33
34 static uint8_t wasmSectionsOrders[]{
35 #define F(enumerator, order, string) order,
36 FOR_EACH_WASM_SECTION_TYPE(F)
37 #undef F
JF 2016/04/20 17:59:54 Need a 0 at the end for Code::Max?
titzer 2016/04/21 10:59:22 done
38 };
33 39
34 static_assert(sizeof(wasmSections) / sizeof(wasmSections[0]) == 40 static_assert(sizeof(wasmSections) / sizeof(wasmSections[0]) ==
35 (size_t)WasmSection::Code::Max, 41 (size_t)WasmSection::Code::Max,
36 "expected enum WasmSection::Code to be monotonic from 0"); 42 "expected enum WasmSection::Code to be monotonic from 0");
37 43
38 WasmSection::Code WasmSection::begin() { return (WasmSection::Code)0; } 44 WasmSection::Code WasmSection::begin() { return (WasmSection::Code)0; }
39 WasmSection::Code WasmSection::end() { return WasmSection::Code::Max; } 45 WasmSection::Code WasmSection::end() { return WasmSection::Code::Max; }
40 WasmSection::Code WasmSection::next(WasmSection::Code code) { 46 WasmSection::Code WasmSection::next(WasmSection::Code code) {
41 return (WasmSection::Code)(1 + (uint32_t)code); 47 return (WasmSection::Code)(1 + (uint32_t)code);
42 } 48 }
43 49
44 const char* WasmSection::getName(WasmSection::Code code) { 50 const char* WasmSection::getName(WasmSection::Code code) {
45 return wasmSections[(size_t)code]; 51 return wasmSections[(size_t)code];
46 } 52 }
47 53
48 size_t WasmSection::getNameLength(WasmSection::Code code) { 54 size_t WasmSection::getNameLength(WasmSection::Code code) {
49 return wasmSectionsLengths[(size_t)code]; 55 return wasmSectionsLengths[(size_t)code];
50 } 56 }
51 57
58 int WasmSection::getOrder(WasmSection::Code code) {
59 return wasmSectionsOrders[(size_t)code];
60 }
61
62 WasmSection::Code WasmSection::lookup(const byte* string, uint32_t length) {
63 // TODO(jfb) Linear search, it may be better to do a common-prefix search.
64 for (Code i = begin(); i != end(); i = next(i)) {
65 if (getNameLength(i) == length && 0 == memcmp(getName(i), string, length)) {
66 return i;
67 }
68 }
69 return Code::Max;
70 }
71
52 std::ostream& operator<<(std::ostream& os, const WasmModule& module) { 72 std::ostream& operator<<(std::ostream& os, const WasmModule& module) {
53 os << "WASM module with "; 73 os << "WASM module with ";
54 os << (module.min_mem_pages * module.kPageSize) << " min mem"; 74 os << (module.min_mem_pages * module.kPageSize) << " min mem";
55 os << (module.max_mem_pages * module.kPageSize) << " max mem"; 75 os << (module.max_mem_pages * module.kPageSize) << " max mem";
56 os << module.functions.size() << " functions"; 76 os << module.functions.size() << " functions";
57 os << module.functions.size() << " globals"; 77 os << module.functions.size() << " globals";
58 os << module.functions.size() << " data segments"; 78 os << module.functions.size() << " data segments";
59 return os; 79 return os;
60 } 80 }
61 81
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 } 725 }
706 if (result->IsHeapNumber()) { 726 if (result->IsHeapNumber()) {
707 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); 727 return static_cast<int32_t>(HeapNumber::cast(*result)->value());
708 } 728 }
709 thrower.Error("WASM.compileRun() failed: Return value should be number"); 729 thrower.Error("WASM.compileRun() failed: Return value should be number");
710 return -1; 730 return -1;
711 } 731 }
712 } // namespace wasm 732 } // namespace wasm
713 } // namespace internal 733 } // namespace internal
714 } // namespace v8 734 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698