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 "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
6 | 6 |
7 #include "src/wasm/module-decoder.h" | 7 #include "src/wasm/module-decoder.h" |
8 #include "src/wasm/wasm-macro-gen.h" | 8 #include "src/wasm/wasm-macro-gen.h" |
9 #include "src/wasm/wasm-opcodes.h" | 9 #include "src/wasm/wasm-opcodes.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 namespace wasm { | 13 namespace wasm { |
14 | 14 |
15 #define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16_LE(0) | 15 #define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16_LE(0) |
| 16 #define EMPTY_FUNCTION_SIZE ((size_t)5) |
16 #define EMPTY_BODY 0 | 17 #define EMPTY_BODY 0 |
| 18 #define EMPTY_BODY_SIZE ((size_t)1) |
17 #define NOP_BODY 2, 0, kExprNop | 19 #define NOP_BODY 2, 0, kExprNop |
| 20 #define NOP_BODY_SIZE ((size_t)3) |
18 #define VOID_VOID_SIG 0, kLocalVoid | 21 #define VOID_VOID_SIG 0, kLocalVoid |
| 22 #define VOID_VOID_SIG_SIZE ((size_t)2) |
19 #define INT_INT_SIG 1, kLocalI32, kLocalI32 | 23 #define INT_INT_SIG 1, kLocalI32, kLocalI32 |
| 24 #define INT_INT_SIG_SIZE ((size_t)3) |
| 25 |
| 26 #define SECTION(NAME, EXTRA_SIZE) \ |
| 27 U32V_1(WASM_SECTION_##NAME##_SIZE + (EXTRA_SIZE)), WASM_SECTION_##NAME |
20 | 28 |
21 #define EXPECT_VERIFIES(data) \ | 29 #define EXPECT_VERIFIES(data) \ |
22 do { \ | 30 do { \ |
23 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ | 31 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ |
24 EXPECT_TRUE(result.ok()); \ | 32 EXPECT_TRUE(result.ok()); \ |
25 if (result.val) delete result.val; \ | 33 if (result.val) delete result.val; \ |
26 } while (false) | 34 } while (false) |
27 | 35 |
28 #define EXPECT_FAILURE_LEN(data, length) \ | 36 #define EXPECT_FAILURE_LEN(data, length) \ |
29 do { \ | 37 do { \ |
30 ModuleResult result = DecodeModule(data, data + length); \ | 38 ModuleResult result = DecodeModule(data, data + length); \ |
31 EXPECT_FALSE(result.ok()); \ | 39 EXPECT_FALSE(result.ok()); \ |
32 if (result.val) delete result.val; \ | 40 if (result.val) delete result.val; \ |
33 } while (false) | 41 } while (false) |
34 | 42 |
35 #define EXPECT_FAILURE(data) EXPECT_FAILURE_LEN(data, sizeof(data)) | 43 #define EXPECT_FAILURE(data) EXPECT_FAILURE_LEN(data, sizeof(data)) |
36 | 44 |
37 #define EXPECT_OFF_END_FAILURE(data, min, max) \ | 45 #define EXPECT_OFF_END_FAILURE(data, min, max) \ |
38 do { \ | 46 do { \ |
39 for (size_t length = min; length < max; length++) { \ | 47 for (size_t length = min; length < max; length++) { \ |
40 EXPECT_FAILURE_LEN(data, length); \ | 48 EXPECT_FAILURE_LEN(data, length); \ |
41 } \ | 49 } \ |
42 } while (false) | 50 } while (false) |
43 | 51 |
| 52 static size_t SizeOfVarInt(size_t value) { |
| 53 size_t size = 0; |
| 54 do { |
| 55 size++; |
| 56 value = value >> 7; |
| 57 } while (value > 0); |
| 58 return size; |
| 59 } |
| 60 |
44 struct LocalTypePair { | 61 struct LocalTypePair { |
45 uint8_t code; | 62 uint8_t code; |
46 LocalType type; | 63 LocalType type; |
47 } kLocalTypes[] = {{kLocalI32, kAstI32}, | 64 } kLocalTypes[] = {{kLocalI32, kAstI32}, |
48 {kLocalI64, kAstI64}, | 65 {kLocalI64, kAstI64}, |
49 {kLocalF32, kAstF32}, | 66 {kLocalF32, kAstF32}, |
50 {kLocalF64, kAstF64}}; | 67 {kLocalF64, kAstF64}}; |
51 | 68 |
52 class WasmModuleVerifyTest : public TestWithZone { | 69 class WasmModuleVerifyTest : public TestWithZone { |
53 public: | 70 public: |
(...skipping 13 matching lines...) Expand all Loading... |
67 ModuleResult DecodeModuleNoHeader(const byte* module_start, | 84 ModuleResult DecodeModuleNoHeader(const byte* module_start, |
68 const byte* module_end) { | 85 const byte* module_end) { |
69 return DecodeWasmModule(nullptr, zone(), module_start, module_end, false, | 86 return DecodeWasmModule(nullptr, zone(), module_start, module_end, false, |
70 kWasmOrigin); | 87 kWasmOrigin); |
71 } | 88 } |
72 }; | 89 }; |
73 | 90 |
74 TEST_F(WasmModuleVerifyTest, WrongMagic) { | 91 TEST_F(WasmModuleVerifyTest, WrongMagic) { |
75 for (uint32_t x = 1; x; x <<= 1) { | 92 for (uint32_t x = 1; x; x <<= 1) { |
76 const byte data[] = {U32_LE(kWasmMagic ^ x), U32_LE(kWasmVersion), | 93 const byte data[] = {U32_LE(kWasmMagic ^ x), U32_LE(kWasmVersion), |
77 kDeclEnd}; | 94 SECTION(END, 0)}; |
78 ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data)); | 95 ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data)); |
79 EXPECT_FALSE(result.ok()); | 96 EXPECT_FALSE(result.ok()); |
80 if (result.val) delete result.val; | 97 if (result.val) delete result.val; |
81 } | 98 } |
82 } | 99 } |
83 | 100 |
84 TEST_F(WasmModuleVerifyTest, WrongVersion) { | 101 TEST_F(WasmModuleVerifyTest, WrongVersion) { |
85 for (uint32_t x = 1; x; x <<= 1) { | 102 for (uint32_t x = 1; x; x <<= 1) { |
86 const byte data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion ^ x), | 103 const byte data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion ^ x), |
87 kDeclEnd}; | 104 SECTION(END, 0)}; |
88 ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data)); | 105 ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data)); |
89 EXPECT_FALSE(result.ok()); | 106 EXPECT_FALSE(result.ok()); |
90 if (result.val) delete result.val; | 107 if (result.val) delete result.val; |
91 } | 108 } |
92 } | 109 } |
93 | 110 |
94 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { | 111 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { |
95 static const byte data[] = {kDeclEnd}; | 112 static const byte data[] = {SECTION(END, 0)}; |
96 EXPECT_VERIFIES(data); | 113 EXPECT_VERIFIES(data); |
97 } | 114 } |
98 | 115 |
99 TEST_F(WasmModuleVerifyTest, OneGlobal) { | 116 TEST_F(WasmModuleVerifyTest, OneGlobal) { |
100 static const byte data[] = { | 117 static const byte data[] = { |
101 kDeclGlobals, | 118 SECTION(GLOBALS, 7), // -- |
102 1, | 119 1, |
103 NAME_LENGTH(1), 'g', // name | 120 NAME_LENGTH(1), |
104 kMemI32, // memory type | 121 'g', // name |
105 0, // exported | 122 kMemI32, // memory type |
| 123 0, // exported |
106 }; | 124 }; |
107 | 125 |
108 { | 126 { |
109 // Should decode to exactly one global. | 127 // Should decode to exactly one global. |
110 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 128 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
111 EXPECT_TRUE(result.ok()); | 129 EXPECT_TRUE(result.ok()); |
112 EXPECT_EQ(1, result.val->globals.size()); | 130 EXPECT_EQ(1, result.val->globals.size()); |
113 EXPECT_EQ(0, result.val->functions.size()); | 131 EXPECT_EQ(0, result.val->functions.size()); |
114 EXPECT_EQ(0, result.val->data_segments.size()); | 132 EXPECT_EQ(0, result.val->data_segments.size()); |
115 | 133 |
116 WasmGlobal* global = &result.val->globals.back(); | 134 WasmGlobal* global = &result.val->globals.back(); |
117 | 135 |
118 EXPECT_EQ(1, global->name_length); | 136 EXPECT_EQ(1, global->name_length); |
119 EXPECT_EQ(MachineType::Int32(), global->type); | 137 EXPECT_EQ(MachineType::Int32(), global->type); |
120 EXPECT_EQ(0, global->offset); | 138 EXPECT_EQ(0, global->offset); |
121 EXPECT_FALSE(global->exported); | 139 EXPECT_FALSE(global->exported); |
122 | 140 |
123 if (result.val) delete result.val; | 141 if (result.val) delete result.val; |
124 } | 142 } |
125 | 143 |
126 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); | 144 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
127 } | 145 } |
128 | 146 |
129 | 147 |
130 TEST_F(WasmModuleVerifyTest, ZeroGlobals) { | 148 TEST_F(WasmModuleVerifyTest, ZeroGlobals) { |
131 static const byte data[] = { | 149 static const byte data[] = { |
132 kDeclGlobals, 0, // declare 0 globals | 150 SECTION(GLOBALS, 1), // -- |
| 151 0, // declare 0 globals |
133 }; | 152 }; |
134 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 153 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
135 EXPECT_TRUE(result.ok()); | 154 EXPECT_TRUE(result.ok()); |
136 if (result.val) delete result.val; | 155 if (result.val) delete result.val; |
137 } | 156 } |
138 | 157 |
139 | 158 |
140 static void AppendUint32v(std::vector<byte>& buffer, uint32_t val) { | 159 static void AppendUint32v(std::vector<byte>& buffer, uint32_t val) { |
141 while (true) { | 160 while (true) { |
142 uint32_t next = val >> 7; | 161 uint32_t next = val >> 7; |
(...skipping 11 matching lines...) Expand all Loading... |
154 | 173 |
155 TEST_F(WasmModuleVerifyTest, NGlobals) { | 174 TEST_F(WasmModuleVerifyTest, NGlobals) { |
156 static const byte data[] = { | 175 static const byte data[] = { |
157 NO_NAME, // name length | 176 NO_NAME, // name length |
158 kMemI32, // memory type | 177 kMemI32, // memory type |
159 0, // exported | 178 0, // exported |
160 }; | 179 }; |
161 | 180 |
162 for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) { | 181 for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) { |
163 std::vector<byte> buffer; | 182 std::vector<byte> buffer; |
164 buffer.push_back(kDeclGlobals); | 183 size_t size = |
165 AppendUint32v(buffer, i); | 184 WASM_SECTION_GLOBALS_SIZE + SizeOfVarInt(i) + i * sizeof(data); |
| 185 const byte globals[] = {U32V_5(size), WASM_SECTION_GLOBALS}; |
| 186 for (size_t g = 0; g != sizeof(globals); ++g) { |
| 187 buffer.push_back(globals[g]); |
| 188 } |
| 189 AppendUint32v(buffer, i); // Number of globals. |
166 for (uint32_t j = 0; j < i; j++) { | 190 for (uint32_t j = 0; j < i; j++) { |
167 buffer.insert(buffer.end(), data, data + sizeof(data)); | 191 buffer.insert(buffer.end(), data, data + sizeof(data)); |
168 } | 192 } |
169 | 193 |
170 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); | 194 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); |
171 EXPECT_TRUE(result.ok()); | 195 EXPECT_TRUE(result.ok()); |
172 if (result.val) delete result.val; | 196 if (result.val) delete result.val; |
173 } | 197 } |
174 } | 198 } |
175 | 199 |
| 200 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameOffset) { |
| 201 static const byte data[] = { |
| 202 SECTION(GLOBALS, 7), |
| 203 1, // declare one global |
| 204 NO_NAME, // name offset |
| 205 33, // memory type |
| 206 0, // exported |
| 207 }; |
| 208 |
| 209 EXPECT_FAILURE(data); |
| 210 } |
176 | 211 |
177 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) { | 212 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) { |
178 static const byte data[] = { | 213 static const byte data[] = { |
179 kDeclGlobals, | 214 SECTION(GLOBALS, 7), |
180 1, // declare one global | 215 1, // declare one global |
181 NO_NAME, // name length | 216 NO_NAME, // name offset |
182 33, // memory type | 217 33, // memory type |
183 0, // exported | 218 0, // exported |
184 }; | 219 }; |
185 | 220 |
186 EXPECT_FAILURE(data); | 221 EXPECT_FAILURE(data); |
187 } | 222 } |
188 | 223 |
189 | 224 |
190 TEST_F(WasmModuleVerifyTest, TwoGlobals) { | 225 TEST_F(WasmModuleVerifyTest, TwoGlobals) { |
191 static const byte data[] = { | 226 static const byte data[] = { |
192 kDeclGlobals, | 227 SECTION(GLOBALS, 13), |
193 2, | 228 2, |
194 NO_NAME, // #0: name length | 229 NO_NAME, // #0: name length |
195 kMemF32, // memory type | 230 kMemF32, // memory type |
196 0, // exported | 231 0, // exported |
197 NO_NAME, // #1: name length | 232 NO_NAME, // #1: name length |
198 kMemF64, // memory type | 233 kMemF64, // memory type |
199 1, // exported | 234 1, // exported |
200 }; | 235 }; |
201 | 236 |
202 { | 237 { |
(...skipping 19 matching lines...) Expand all Loading... |
222 | 257 |
223 if (result.val) delete result.val; | 258 if (result.val) delete result.val; |
224 } | 259 } |
225 | 260 |
226 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); | 261 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
227 } | 262 } |
228 | 263 |
229 | 264 |
230 TEST_F(WasmModuleVerifyTest, OneSignature) { | 265 TEST_F(WasmModuleVerifyTest, OneSignature) { |
231 { | 266 { |
232 static const byte data[] = {kDeclSignatures, 1, VOID_VOID_SIG}; | 267 static const byte data[] = {SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, |
| 268 VOID_VOID_SIG}; |
233 EXPECT_VERIFIES(data); | 269 EXPECT_VERIFIES(data); |
234 } | 270 } |
235 | 271 |
236 { | 272 { |
237 static const byte data[] = {kDeclSignatures, 1, INT_INT_SIG}; | 273 static const byte data[] = {SECTION(SIGNATURES, 1 + INT_INT_SIG_SIZE), 1, |
| 274 INT_INT_SIG}; |
238 EXPECT_VERIFIES(data); | 275 EXPECT_VERIFIES(data); |
239 } | 276 } |
240 } | 277 } |
241 | 278 |
242 | 279 |
243 TEST_F(WasmModuleVerifyTest, MultipleSignatures) { | 280 TEST_F(WasmModuleVerifyTest, MultipleSignatures) { |
244 static const byte data[] = { | 281 static const byte data[] = { |
245 kDeclSignatures, | 282 SECTION(SIGNATURES, 10), |
246 3, | 283 3, |
247 0, | 284 0, |
248 kLocalVoid, // void -> void | 285 kLocalVoid, // void -> void |
249 1, | 286 1, |
250 kLocalI32, | 287 kLocalI32, |
251 kLocalF32, // f32 -> i32 | 288 kLocalF32, // f32 -> i32 |
252 2, | 289 2, |
253 kLocalI32, | 290 kLocalI32, |
254 kLocalF64, | 291 kLocalF64, |
255 kLocalF64, // (f64,f64) -> i32 | 292 kLocalF64, // (f64,f64) -> i32 |
(...skipping 12 matching lines...) Expand all Loading... |
268 EXPECT_EQ(2, result.val->signatures[2]->parameter_count()); | 305 EXPECT_EQ(2, result.val->signatures[2]->parameter_count()); |
269 } | 306 } |
270 if (result.val) delete result.val; | 307 if (result.val) delete result.val; |
271 | 308 |
272 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); | 309 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
273 } | 310 } |
274 | 311 |
275 | 312 |
276 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { | 313 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { |
277 static const byte data[] = { | 314 static const byte data[] = { |
278 kDeclFunctions, 1, | 315 SECTION(FUNCTIONS, 25), 1, |
279 // func#0 ------------------------------------------------------ | 316 // func#0 ------------------------------------------------------ |
280 SIG_INDEX(0), // signature index | 317 SIG_INDEX(0), // signature index |
281 NO_NAME, // name length | 318 NO_NAME, // name length |
282 U32_LE(0), // code start offset | 319 U32_LE(0), // code start offset |
283 U32_LE(0), // code end offset | 320 U32_LE(0), // code end offset |
284 U16_LE(899), // local int32 count | 321 U16_LE(899), // local int32 count |
285 U16_LE(799), // local int64 count | 322 U16_LE(799), // local int64 count |
286 U16_LE(699), // local float32 count | 323 U16_LE(699), // local float32 count |
287 U16_LE(599), // local float64 count | 324 U16_LE(599), // local float64 count |
288 0, // exported | 325 0, // exported |
289 1 // external | 326 1 // external |
290 }; | 327 }; |
291 | 328 |
292 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 329 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
293 EXPECT_FALSE(result.ok()); | 330 EXPECT_FALSE(result.ok()); |
294 if (result.val) delete result.val; | 331 if (result.val) delete result.val; |
295 } | 332 } |
296 | 333 |
297 | 334 |
298 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { | 335 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { |
299 const int kCodeStartOffset = 30; | 336 const int kCodeStartOffset = 51; |
300 const int kCodeEndOffset = kCodeStartOffset + 1; | 337 const int kCodeEndOffset = kCodeStartOffset + 1; |
301 | 338 |
302 static const byte data[] = { | 339 static const byte data[] = { |
303 kDeclSignatures, 1, | 340 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, |
304 // sig#0 ------------------------------------------------------- | 341 // sig#0 ------------------------------------------------------- |
305 VOID_VOID_SIG, | 342 VOID_VOID_SIG, |
306 // func#0 ------------------------------------------------------ | 343 // func#0 ------------------------------------------------------ |
307 kDeclFunctions, 1, | 344 SECTION(FUNCTIONS, 19), 1, |
308 kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName, | 345 kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName, |
309 SIG_INDEX(0), // signature index | 346 SIG_INDEX(0), // signature index |
310 NAME_LENGTH(2), 'h', 'i', // name | 347 NAME_LENGTH(2), 'h', 'i', // name |
311 U16_LE(1466), // local int32 count | 348 U16_LE(1466), // local int32 count |
312 U16_LE(1355), // local int64 count | 349 U16_LE(1355), // local int64 count |
313 U16_LE(1244), // local float32 count | 350 U16_LE(1244), // local float32 count |
314 U16_LE(1133), // local float64 count | 351 U16_LE(1133), // local float64 count |
315 1, 0, // size | 352 1, 0, // size |
316 kExprNop, | 353 kExprNop, |
317 }; | 354 }; |
318 | 355 |
319 { | 356 { |
320 // Should decode to exactly one function. | 357 // Should decode to exactly one function. |
321 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 358 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
322 EXPECT_TRUE(result.ok()); | 359 EXPECT_TRUE(result.ok()); |
323 EXPECT_EQ(0, result.val->globals.size()); | 360 EXPECT_EQ(0, result.val->globals.size()); |
324 EXPECT_EQ(1, result.val->signatures.size()); | 361 EXPECT_EQ(1, result.val->signatures.size()); |
325 EXPECT_EQ(1, result.val->functions.size()); | 362 EXPECT_EQ(1, result.val->functions.size()); |
326 EXPECT_EQ(0, result.val->data_segments.size()); | 363 EXPECT_EQ(0, result.val->data_segments.size()); |
327 EXPECT_EQ(0, result.val->function_table.size()); | 364 EXPECT_EQ(0, result.val->function_table.size()); |
328 | 365 |
329 WasmFunction* function = &result.val->functions.back(); | 366 WasmFunction* function = &result.val->functions.back(); |
330 | 367 |
331 EXPECT_EQ(18, function->name_offset); | 368 EXPECT_EQ(39, function->name_offset); |
332 EXPECT_EQ(2, function->name_length); | 369 EXPECT_EQ(2, function->name_length); |
333 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 370 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
334 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 371 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
335 | 372 |
336 EXPECT_EQ(1466, function->local_i32_count); | 373 EXPECT_EQ(1466, function->local_i32_count); |
337 EXPECT_EQ(1355, function->local_i64_count); | 374 EXPECT_EQ(1355, function->local_i64_count); |
338 EXPECT_EQ(1244, function->local_f32_count); | 375 EXPECT_EQ(1244, function->local_f32_count); |
339 EXPECT_EQ(1133, function->local_f64_count); | 376 EXPECT_EQ(1133, function->local_f64_count); |
340 | 377 |
341 EXPECT_TRUE(function->exported); | 378 EXPECT_TRUE(function->exported); |
342 EXPECT_FALSE(function->external); | 379 EXPECT_FALSE(function->external); |
343 | 380 |
344 if (result.val) delete result.val; | 381 if (result.val) delete result.val; |
345 } | 382 } |
346 | 383 |
347 EXPECT_OFF_END_FAILURE(data, 5, sizeof(data)); | 384 EXPECT_OFF_END_FAILURE(data, 16, sizeof(data)); |
348 } | 385 } |
349 | 386 |
350 | 387 |
351 TEST_F(WasmModuleVerifyTest, OneFunctionImported) { | 388 TEST_F(WasmModuleVerifyTest, OneFunctionImported) { |
352 static const byte data[] = { | 389 static const byte data[] = { |
353 kDeclSignatures, 1, | 390 SECTION(SIGNATURES, VOID_VOID_SIG_SIZE), 1, |
354 // sig#0 ------------------------------------------------------- | 391 // sig#0 ------------------------------------------------------- |
355 VOID_VOID_SIG, kDeclFunctions, 1, | 392 VOID_VOID_SIG, SECTION(FUNCTIONS, 6), 1, |
356 // func#0 ------------------------------------------------------ | 393 // func#0 ------------------------------------------------------ |
357 kDeclFunctionImport, // no name, no locals, imported | 394 kDeclFunctionImport, // no name, no locals, imported |
358 SIG_INDEX(0), | 395 SIG_INDEX(0), |
359 }; | 396 }; |
360 | 397 |
361 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 398 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
362 EXPECT_TRUE(result.ok()); | 399 EXPECT_TRUE(result.ok()); |
363 EXPECT_EQ(1, result.val->functions.size()); | 400 EXPECT_EQ(1, result.val->functions.size()); |
364 WasmFunction* function = &result.val->functions.back(); | 401 WasmFunction* function = &result.val->functions.back(); |
365 | 402 |
366 EXPECT_EQ(0, function->name_length); | 403 EXPECT_EQ(0, function->name_length); |
367 EXPECT_EQ(0, function->code_start_offset); | 404 EXPECT_EQ(0, function->code_start_offset); |
368 EXPECT_EQ(0, function->code_end_offset); | 405 EXPECT_EQ(0, function->code_end_offset); |
369 | 406 |
370 EXPECT_EQ(0, function->local_i32_count); | 407 EXPECT_EQ(0, function->local_i32_count); |
371 EXPECT_EQ(0, function->local_i64_count); | 408 EXPECT_EQ(0, function->local_i64_count); |
372 EXPECT_EQ(0, function->local_f32_count); | 409 EXPECT_EQ(0, function->local_f32_count); |
373 EXPECT_EQ(0, function->local_f64_count); | 410 EXPECT_EQ(0, function->local_f64_count); |
374 | 411 |
375 EXPECT_FALSE(function->exported); | 412 EXPECT_FALSE(function->exported); |
376 EXPECT_TRUE(function->external); | 413 EXPECT_TRUE(function->external); |
377 | 414 |
378 if (result.val) delete result.val; | 415 if (result.val) delete result.val; |
379 } | 416 } |
380 | 417 |
381 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody) { | 418 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody) { |
382 static const byte kCodeStartOffset = 19; | 419 static const byte kCodeStartOffset = 40; |
383 static const byte kCodeEndOffset = kCodeStartOffset + 1; | 420 static const byte kCodeEndOffset = kCodeStartOffset + 1; |
384 | 421 |
385 static const byte data[] = { | 422 static const byte data[] = { |
386 kDeclSignatures, 1, | 423 SECTION(SIGNATURES, 3), 1, |
387 // sig#0 ------------------------------------------------------- | 424 // sig#0 ------------------------------------------------------- |
388 0, 0, // void -> void | 425 0, 0, // void -> void |
389 kDeclFunctions, 1, | 426 SECTION(FUNCTIONS, 7), 1, |
390 // func#0 ------------------------------------------------------ | 427 // func#0 ------------------------------------------------------ |
391 0, // no name, no locals | 428 0, // no name, no locals |
392 0, 0, // signature index | 429 0, 0, // signature index |
393 1, 0, // body size | 430 1, 0, // body size |
394 kExprNop // body | 431 kExprNop // body |
395 }; | 432 }; |
396 | 433 |
397 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 434 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
398 EXPECT_TRUE(result.ok()); | 435 EXPECT_TRUE(result.ok()); |
399 EXPECT_EQ(1, result.val->functions.size()); | 436 EXPECT_EQ(1, result.val->functions.size()); |
400 WasmFunction* function = &result.val->functions.back(); | 437 WasmFunction* function = &result.val->functions.back(); |
401 | 438 |
402 EXPECT_EQ(0, function->name_length); | 439 EXPECT_EQ(0, function->name_length); |
403 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 440 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
404 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 441 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
405 | 442 |
406 EXPECT_EQ(0, function->local_i32_count); | 443 EXPECT_EQ(0, function->local_i32_count); |
407 EXPECT_EQ(0, function->local_i64_count); | 444 EXPECT_EQ(0, function->local_i64_count); |
408 EXPECT_EQ(0, function->local_f32_count); | 445 EXPECT_EQ(0, function->local_f32_count); |
409 EXPECT_EQ(0, function->local_f64_count); | 446 EXPECT_EQ(0, function->local_f64_count); |
410 | 447 |
411 EXPECT_FALSE(function->exported); | 448 EXPECT_FALSE(function->exported); |
412 EXPECT_FALSE(function->external); | 449 EXPECT_FALSE(function->external); |
413 | 450 |
414 if (result.val) delete result.val; | 451 if (result.val) delete result.val; |
415 } | 452 } |
416 | 453 |
417 | 454 |
418 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody_WithLocals) { | 455 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody_WithLocals) { |
419 static const byte kCodeStartOffset = 27; | 456 static const byte kCodeStartOffset = 48; |
420 static const byte kCodeEndOffset = kCodeStartOffset + 1; | 457 static const byte kCodeEndOffset = kCodeStartOffset + 1; |
421 | 458 |
422 static const byte data[] = { | 459 static const byte data[] = { |
423 kDeclSignatures, 1, | 460 SECTION(SIGNATURES, 3), 1, |
424 // sig#0 ------------------------------------------------------- | 461 // sig#0 ------------------------------------------------------- |
425 0, 0, // void -> void | 462 0, 0, // void -> void |
426 kDeclFunctions, 1, | 463 SECTION(FUNCTIONS, 15), 1, |
427 // func#0 ------------------------------------------------------ | 464 // func#0 ------------------------------------------------------ |
428 kDeclFunctionLocals, 0, 0, // signature index | 465 kDeclFunctionLocals, 0, 0, // signature index |
429 1, 2, // local int32 count | 466 1, 2, // local int32 count |
430 3, 4, // local int64 count | 467 3, 4, // local int64 count |
431 5, 6, // local float32 count | 468 5, 6, // local float32 count |
432 7, 8, // local float64 count | 469 7, 8, // local float64 count |
433 1, 0, // body size | 470 1, 0, // body size |
434 kExprNop // body | 471 kExprNop // body |
435 }; | 472 }; |
436 | 473 |
(...skipping 12 matching lines...) Expand all Loading... |
449 EXPECT_EQ(2055, function->local_f64_count); | 486 EXPECT_EQ(2055, function->local_f64_count); |
450 | 487 |
451 EXPECT_FALSE(function->exported); | 488 EXPECT_FALSE(function->exported); |
452 EXPECT_FALSE(function->external); | 489 EXPECT_FALSE(function->external); |
453 | 490 |
454 if (result.val) delete result.val; | 491 if (result.val) delete result.val; |
455 } | 492 } |
456 | 493 |
457 | 494 |
458 TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) { | 495 TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) { |
459 static const byte kCodeStartOffset = 8 + 4 + 5 + 4 + 18; | 496 static const byte kCodeStartOffset = 75; |
460 static const byte kCodeEndOffset = kCodeStartOffset + 3; | 497 static const byte kCodeEndOffset = kCodeStartOffset + 3; |
461 static const byte kDataSegmentSourceOffset = kCodeEndOffset + 6; | 498 static const byte kDataSegmentSourceOffset = kCodeEndOffset + 20; |
462 | 499 |
463 static const byte data[] = { | 500 static const byte data[] = { |
464 kDeclMemory, 28, 28, 1, | 501 SECTION(MEMORY, 3), 28, 28, 1, |
465 // global#0 -------------------------------------------------- | 502 // global#0 -------------------------------------------------- |
466 kDeclGlobals, 1, 0, // name length | 503 SECTION(GLOBALS, 7), 1, |
467 kMemU8, // memory type | 504 0, // name length |
468 0, // exported | 505 kMemU8, // memory type |
| 506 0, // exported |
469 // sig#0 ----------------------------------------------------- | 507 // sig#0 ----------------------------------------------------- |
470 kDeclSignatures, 1, 0, 0, // void -> void | 508 SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void |
471 // func#0 ---------------------------------------------------- | 509 // func#0 ---------------------------------------------------- |
472 kDeclFunctions, 1, kDeclFunctionLocals | kDeclFunctionName, 0, | 510 SECTION(FUNCTIONS, 20), 1, kDeclFunctionLocals | kDeclFunctionName, 0, |
473 0, // signature index | 511 0, // signature index |
474 2, 'h', 'i', // name | 512 2, 'h', 'i', // name |
475 1, 2, // local int32 count | 513 1, 2, // local int32 count |
476 3, 4, // local int64 count | 514 3, 4, // local int64 count |
477 5, 6, // local float32 count | 515 5, 6, // local float32 count |
478 7, 8, // local float64 count | 516 7, 8, // local float64 count |
479 3, 0, // body size | 517 3, 0, // body size |
480 kExprNop, // func#0 body | 518 kExprNop, // func#0 body |
481 kExprNop, // func#0 body | 519 kExprNop, // func#0 body |
482 kExprNop, // func#0 body | 520 kExprNop, // func#0 body |
483 // segment#0 ------------------------------------------------- | 521 // segment#0 ------------------------------------------------- |
484 kDeclDataSegments, 1, U32V_3(0x8b3ae), // dest addr | 522 SECTION(DATA_SEGMENTS, 14), 1, |
485 U32V_1(5), // source size | 523 U32V_3(0x8b3ae), // dest addr |
486 0, 1, 2, 3, 4, // data bytes | 524 U32V_1(5), // source size |
| 525 0, 1, 2, 3, 4, // data bytes |
487 // rest ------------------------------------------------------ | 526 // rest ------------------------------------------------------ |
488 kDeclEnd, | 527 SECTION(END, 0), |
489 }; | 528 }; |
490 | 529 |
491 { | 530 { |
492 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 531 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
493 EXPECT_TRUE(result.ok()); | 532 EXPECT_TRUE(result.ok()); |
494 EXPECT_EQ(1, result.val->globals.size()); | 533 EXPECT_EQ(1, result.val->globals.size()); |
495 EXPECT_EQ(1, result.val->functions.size()); | 534 EXPECT_EQ(1, result.val->functions.size()); |
496 EXPECT_EQ(1, result.val->data_segments.size()); | 535 EXPECT_EQ(1, result.val->data_segments.size()); |
497 | 536 |
498 WasmGlobal* global = &result.val->globals.back(); | 537 WasmGlobal* global = &result.val->globals.back(); |
499 | 538 |
500 EXPECT_EQ(0, global->name_length); | 539 EXPECT_EQ(0, global->name_length); |
501 EXPECT_EQ(MachineType::Uint8(), global->type); | 540 EXPECT_EQ(MachineType::Uint8(), global->type); |
502 EXPECT_EQ(0, global->offset); | 541 EXPECT_EQ(0, global->offset); |
503 EXPECT_FALSE(global->exported); | 542 EXPECT_FALSE(global->exported); |
504 | 543 |
505 WasmFunction* function = &result.val->functions.back(); | 544 WasmFunction* function = &result.val->functions.back(); |
506 | 545 |
507 EXPECT_EQ(27, function->name_offset); | 546 EXPECT_EQ(63, function->name_offset); |
508 EXPECT_EQ(2, function->name_length); | 547 EXPECT_EQ(2, function->name_length); |
509 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 548 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
510 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 549 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
511 | 550 |
512 EXPECT_FALSE(function->exported); | 551 EXPECT_FALSE(function->exported); |
513 EXPECT_FALSE(function->external); | 552 EXPECT_FALSE(function->external); |
514 | 553 |
515 WasmDataSegment* segment = &result.val->data_segments.back(); | 554 WasmDataSegment* segment = &result.val->data_segments.back(); |
516 | 555 |
517 EXPECT_EQ(0x8b3ae, segment->dest_addr); | 556 EXPECT_EQ(0x8b3ae, segment->dest_addr); |
518 EXPECT_EQ(kDataSegmentSourceOffset, segment->source_offset); | 557 EXPECT_EQ(kDataSegmentSourceOffset, segment->source_offset); |
519 EXPECT_EQ(5, segment->source_size); | 558 EXPECT_EQ(5, segment->source_size); |
520 EXPECT_TRUE(segment->init); | 559 EXPECT_TRUE(segment->init); |
521 | 560 |
522 if (result.val) delete result.val; | 561 if (result.val) delete result.val; |
523 } | 562 } |
524 } | 563 } |
525 | 564 |
526 | 565 |
527 TEST_F(WasmModuleVerifyTest, OneDataSegment) { | 566 TEST_F(WasmModuleVerifyTest, OneDataSegment) { |
528 const byte kDataSegmentSourceOffset = 8 + 10; | 567 const byte kDataSegmentSourceOffset = 39; |
529 const byte data[] = { | 568 const byte data[] = { |
530 kDeclMemory, 28, 28, 1, | 569 SECTION(MEMORY, 3), |
531 kDeclDataSegments, 1, | 570 28, |
| 571 28, |
| 572 1, |
| 573 SECTION(DATA_SEGMENTS, 8), |
| 574 1, |
532 U32V_3(0x9bbaa), // dest addr | 575 U32V_3(0x9bbaa), // dest addr |
533 U32V_1(3), // source size | 576 U32V_1(3), // source size |
534 'a', 'b', 'c' // data bytes | 577 'a', |
| 578 'b', |
| 579 'c' // data bytes |
535 }; | 580 }; |
536 | 581 |
537 { | 582 { |
538 EXPECT_VERIFIES(data); | 583 EXPECT_VERIFIES(data); |
539 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 584 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
540 EXPECT_TRUE(result.ok()); | 585 EXPECT_TRUE(result.ok()); |
541 EXPECT_EQ(0, result.val->globals.size()); | 586 EXPECT_EQ(0, result.val->globals.size()); |
542 EXPECT_EQ(0, result.val->functions.size()); | 587 EXPECT_EQ(0, result.val->functions.size()); |
543 EXPECT_EQ(1, result.val->data_segments.size()); | 588 EXPECT_EQ(1, result.val->data_segments.size()); |
544 | 589 |
545 WasmDataSegment* segment = &result.val->data_segments.back(); | 590 WasmDataSegment* segment = &result.val->data_segments.back(); |
546 | 591 |
547 EXPECT_EQ(0x9bbaa, segment->dest_addr); | 592 EXPECT_EQ(0x9bbaa, segment->dest_addr); |
548 EXPECT_EQ(kDataSegmentSourceOffset, segment->source_offset); | 593 EXPECT_EQ(kDataSegmentSourceOffset, segment->source_offset); |
549 EXPECT_EQ(3, segment->source_size); | 594 EXPECT_EQ(3, segment->source_size); |
550 EXPECT_TRUE(segment->init); | 595 EXPECT_TRUE(segment->init); |
551 | 596 |
552 if (result.val) delete result.val; | 597 if (result.val) delete result.val; |
553 } | 598 } |
554 | 599 |
555 EXPECT_OFF_END_FAILURE(data, 5, sizeof(data)); | 600 EXPECT_OFF_END_FAILURE(data, 13, sizeof(data)); |
556 } | 601 } |
557 | 602 |
558 | 603 |
559 TEST_F(WasmModuleVerifyTest, TwoDataSegments) { | 604 TEST_F(WasmModuleVerifyTest, TwoDataSegments) { |
560 const byte kDataSegment0SourceOffset = 8 + 10; | 605 const byte kDataSegment0SourceOffset = 39; |
561 const byte kDataSegment1SourceOffset = 8 + 10 + 8; | 606 const byte kDataSegment1SourceOffset = 39 + 8; |
562 | 607 |
563 const byte data[] = { | 608 const byte data[] = { |
564 kDeclMemory, 28, 28, 1, | 609 SECTION(MEMORY, 3), |
565 kDeclDataSegments, 2, | 610 28, |
566 U32V_3(0x7ffee), // #0: dest addr | 611 28, |
567 U32V_1(4), // source size | 612 1, |
568 1, 2, 3, 4, // data bytes | 613 SECTION(DATA_SEGMENTS, 31), |
569 U32V_3(0x6ddcc), // #1: dest addr | 614 2, // segment count |
570 U32V_1(10), // source size | 615 U32V_3(0x7ffee), // #0: dest addr |
571 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // data bytes | 616 U32V_1(4), // source size |
| 617 1, |
| 618 2, |
| 619 3, |
| 620 4, // data bytes |
| 621 U32V_3(0x6ddcc), // #1: dest addr |
| 622 U32V_1(10), // source size |
| 623 1, |
| 624 2, |
| 625 3, |
| 626 4, |
| 627 5, |
| 628 6, |
| 629 7, |
| 630 8, |
| 631 9, |
| 632 10 // data bytes |
572 }; | 633 }; |
573 | 634 |
574 { | 635 { |
575 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 636 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
576 EXPECT_TRUE(result.ok()); | 637 EXPECT_TRUE(result.ok()); |
577 EXPECT_EQ(0, result.val->globals.size()); | 638 EXPECT_EQ(0, result.val->globals.size()); |
578 EXPECT_EQ(0, result.val->functions.size()); | 639 EXPECT_EQ(0, result.val->functions.size()); |
579 EXPECT_EQ(2, result.val->data_segments.size()); | 640 EXPECT_EQ(2, result.val->data_segments.size()); |
580 | 641 |
581 WasmDataSegment* s0 = &result.val->data_segments[0]; | 642 WasmDataSegment* s0 = &result.val->data_segments[0]; |
582 WasmDataSegment* s1 = &result.val->data_segments[1]; | 643 WasmDataSegment* s1 = &result.val->data_segments[1]; |
583 | 644 |
584 EXPECT_EQ(0x7ffee, s0->dest_addr); | 645 EXPECT_EQ(0x7ffee, s0->dest_addr); |
585 EXPECT_EQ(kDataSegment0SourceOffset, s0->source_offset); | 646 EXPECT_EQ(kDataSegment0SourceOffset, s0->source_offset); |
586 EXPECT_EQ(4, s0->source_size); | 647 EXPECT_EQ(4, s0->source_size); |
587 EXPECT_TRUE(s0->init); | 648 EXPECT_TRUE(s0->init); |
588 | 649 |
589 EXPECT_EQ(0x6ddcc, s1->dest_addr); | 650 EXPECT_EQ(0x6ddcc, s1->dest_addr); |
590 EXPECT_EQ(kDataSegment1SourceOffset, s1->source_offset); | 651 EXPECT_EQ(kDataSegment1SourceOffset, s1->source_offset); |
591 EXPECT_EQ(10, s1->source_size); | 652 EXPECT_EQ(10, s1->source_size); |
592 EXPECT_TRUE(s1->init); | 653 EXPECT_TRUE(s1->init); |
593 | 654 |
594 if (result.val) delete result.val; | 655 if (result.val) delete result.val; |
595 } | 656 } |
596 | 657 |
597 EXPECT_OFF_END_FAILURE(data, 5, sizeof(data)); | 658 EXPECT_OFF_END_FAILURE(data, 13, sizeof(data)); |
598 } | 659 } |
599 | 660 |
600 | |
601 TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidDest) { | 661 TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidDest) { |
602 const int source_size = 3; | 662 const int source_size = 3; |
603 | 663 |
604 for (byte mem_pages = 1; mem_pages < 16; mem_pages++) { | 664 for (byte mem_pages = 1; mem_pages < 16; mem_pages++) { |
605 int mem_size = mem_pages * 0x10000; // 64k pages. | 665 int mem_size = mem_pages * 0x10000; // 64k pages. |
606 | 666 |
607 for (int dest_addr = mem_size - source_size; | 667 for (int dest_addr = mem_size - source_size; |
608 dest_addr < mem_size + source_size; dest_addr++) { | 668 dest_addr < mem_size + source_size; dest_addr++) { |
609 byte data[] = { | 669 byte data[] = {SECTION(MEMORY, 3), |
610 kDeclMemory, | 670 mem_pages, |
611 mem_pages, | 671 mem_pages, |
612 mem_pages, | 672 1, |
613 1, | 673 SECTION(DATA_SEGMENTS, 14), |
614 kDeclDataSegments, | 674 1, |
615 1, | 675 U32V_3(dest_addr), |
616 U32V_3(dest_addr), | 676 U32V_1(source_size), |
617 U32V_1(source_size), | 677 'a', |
618 'a', 'b', 'c' | 678 'b', |
619 }; | 679 'c'}; |
620 | 680 |
621 if (dest_addr <= (mem_size - source_size)) { | 681 if (dest_addr <= (mem_size - source_size)) { |
622 EXPECT_VERIFIES(data); | 682 EXPECT_VERIFIES(data); |
623 } else { | 683 } else { |
624 EXPECT_FAILURE(data); | 684 EXPECT_FAILURE(data); |
625 } | 685 } |
626 } | 686 } |
627 } | 687 } |
628 } | 688 } |
629 | 689 |
630 | 690 |
631 // To make below tests for indirect calls much shorter. | 691 // To make below tests for indirect calls much shorter. |
632 #define FUNCTION(sig_index, external) kDeclFunctionImport, SIG_INDEX(sig_index) | 692 #define FUNCTION(sig_index, external) kDeclFunctionImport, SIG_INDEX(sig_index) |
633 | 693 |
634 TEST_F(WasmModuleVerifyTest, OneIndirectFunction) { | 694 TEST_F(WasmModuleVerifyTest, OneIndirectFunction) { |
635 static const byte data[] = { | 695 static const byte data[] = { |
636 // sig#0 ------------------------------------------------------- | 696 // sig#0 ------------------------------------------------------- |
637 kDeclSignatures, 1, 0, 0, // void -> void | 697 SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void |
638 // func#0 ------------------------------------------------------ | 698 // func#0 ------------------------------------------------------ |
639 kDeclFunctions, 1, FUNCTION(0, 0), | 699 SECTION(FUNCTIONS, 4), 1, FUNCTION(0, 0), |
640 // indirect table ---------------------------------------------- | 700 // indirect table ---------------------------------------------- |
641 kDeclFunctionTable, 1, U32V_1(0)}; | 701 SECTION(FUNCTION_TABLE, 2), 1, U32V_1(0)}; |
642 | 702 |
643 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 703 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
644 EXPECT_TRUE(result.ok()); | 704 EXPECT_TRUE(result.ok()); |
645 if (result.ok()) { | 705 if (result.ok()) { |
646 EXPECT_EQ(1, result.val->signatures.size()); | 706 EXPECT_EQ(1, result.val->signatures.size()); |
647 EXPECT_EQ(1, result.val->functions.size()); | 707 EXPECT_EQ(1, result.val->functions.size()); |
648 EXPECT_EQ(1, result.val->function_table.size()); | 708 EXPECT_EQ(1, result.val->function_table.size()); |
649 EXPECT_EQ(0, result.val->function_table[0]); | 709 EXPECT_EQ(0, result.val->function_table[0]); |
650 } | 710 } |
651 if (result.val) delete result.val; | 711 if (result.val) delete result.val; |
652 } | 712 } |
653 | 713 |
654 | 714 |
655 TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) { | 715 TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) { |
656 static const byte data[] = { | 716 static const byte data[] = { |
657 // sig#0 ------------------------------------------------------- | 717 // sig#0 ------------------------------------------------------- |
658 kDeclSignatures, 2, 0, 0, // void -> void | 718 SECTION(SIGNATURES, 5), 2, 0, 0, // void -> void |
659 0, kLocalI32, // void -> i32 | 719 0, kLocalI32, // void -> i32 |
660 // func#0 ------------------------------------------------------ | 720 // func#0 ------------------------------------------------------ |
661 kDeclFunctions, 4, FUNCTION(0, 1), // -- | 721 SECTION(FUNCTIONS, 13), 4, FUNCTION(0, 1), // -- |
662 FUNCTION(1, 1), // -- | 722 FUNCTION(1, 1), // -- |
663 FUNCTION(0, 1), // -- | 723 FUNCTION(0, 1), // -- |
664 FUNCTION(1, 1), // -- | 724 FUNCTION(1, 1), // -- |
665 // indirect table ---------------------------------------------- | 725 // indirect table ---------------------------------------------- |
666 kDeclFunctionTable, 8, | 726 SECTION(FUNCTION_TABLE, 9), 8, |
667 U32V_1(0), // -- | 727 U32V_1(0), // -- |
668 U32V_1(1), // -- | 728 U32V_1(1), // -- |
669 U32V_1(2), // -- | 729 U32V_1(2), // -- |
670 U32V_1(3), // -- | 730 U32V_1(3), // -- |
671 U32V_1(0), // -- | 731 U32V_1(0), // -- |
672 U32V_1(1), // -- | 732 U32V_1(1), // -- |
673 U32V_1(2), // -- | 733 U32V_1(2), // -- |
674 U32V_1(3), // -- | 734 U32V_1(3), // -- |
675 }; | 735 }; |
676 | 736 |
677 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 737 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
678 EXPECT_TRUE(result.ok()); | 738 EXPECT_TRUE(result.ok()); |
679 if (result.ok()) { | 739 if (result.ok()) { |
680 EXPECT_EQ(2, result.val->signatures.size()); | 740 EXPECT_EQ(2, result.val->signatures.size()); |
681 EXPECT_EQ(4, result.val->functions.size()); | 741 EXPECT_EQ(4, result.val->functions.size()); |
682 EXPECT_EQ(8, result.val->function_table.size()); | 742 EXPECT_EQ(8, result.val->function_table.size()); |
683 for (int i = 0; i < 8; i++) { | 743 for (int i = 0; i < 8; i++) { |
684 EXPECT_EQ(i & 3, result.val->function_table[i]); | 744 EXPECT_EQ(i & 3, result.val->function_table[i]); |
685 } | 745 } |
686 } | 746 } |
687 if (result.val) delete result.val; | 747 if (result.val) delete result.val; |
688 } | 748 } |
689 | 749 |
690 | 750 |
691 TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) { | 751 TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) { |
692 static const byte data[] = { | 752 static const byte data[] = { |
693 // sig#0 ------------------------------------------------------- | 753 // sig#0 ------------------------------------------------------- |
694 kDeclSignatures, 1, 0, 0, // void -> void | 754 SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void |
695 // indirect table ---------------------------------------------- | 755 // indirect table ---------------------------------------------- |
696 kDeclFunctionTable, 1, 0, 0, | 756 SECTION(FUNCTION_TABLE, 3), 1, 0, 0, |
697 }; | 757 }; |
698 | 758 |
699 EXPECT_FAILURE(data); | 759 EXPECT_FAILURE(data); |
700 } | 760 } |
701 | 761 |
702 | 762 |
703 TEST_F(WasmModuleVerifyTest, IndirectFunctionInvalidIndex) { | 763 TEST_F(WasmModuleVerifyTest, IndirectFunctionInvalidIndex) { |
704 static const byte data[] = { | 764 static const byte data[] = { |
705 // sig#0 ------------------------------------------------------- | 765 // sig#0 ------------------------------------------------------- |
706 kDeclSignatures, 1, 0, 0, // void -> void | 766 SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void |
707 // functions --------------------------------------------------- | 767 // functions --------------------------------------------------- |
708 kDeclFunctions, 1, FUNCTION(0, 1), | 768 SECTION(FUNCTIONS, 4), 1, FUNCTION(0, 1), |
709 // indirect table ---------------------------------------------- | 769 // indirect table ---------------------------------------------- |
710 kDeclFunctionTable, 1, 1, 0, | 770 SECTION(FUNCTION_TABLE, 3), 1, 1, 0, |
711 }; | 771 }; |
712 | 772 |
713 EXPECT_FAILURE(data); | 773 EXPECT_FAILURE(data); |
714 } | 774 } |
715 | 775 |
716 | 776 |
717 class WasmSignatureDecodeTest : public TestWithZone {}; | 777 class WasmSignatureDecodeTest : public TestWithZone {}; |
718 | 778 |
719 | 779 |
720 TEST_F(WasmSignatureDecodeTest, Ok_v_v) { | 780 TEST_F(WasmSignatureDecodeTest, Ok_v_v) { |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
867 EXPECT_EQ(2, function->code_start_offset); | 927 EXPECT_EQ(2, function->code_start_offset); |
868 EXPECT_EQ(arraysize(data), function->code_end_offset); | 928 EXPECT_EQ(arraysize(data), function->code_end_offset); |
869 // TODO(titzer): verify encoding of local declarations | 929 // TODO(titzer): verify encoding of local declarations |
870 EXPECT_FALSE(function->external); | 930 EXPECT_FALSE(function->external); |
871 EXPECT_FALSE(function->exported); | 931 EXPECT_FALSE(function->exported); |
872 } | 932 } |
873 | 933 |
874 if (result.val) delete result.val; | 934 if (result.val) delete result.val; |
875 } | 935 } |
876 | 936 |
877 TEST_F(WasmModuleVerifyTest, UnknownSectionNoLen) { | 937 TEST_F(WasmModuleVerifyTest, SectionWithoutNameLength) { |
878 const byte data[] = { | 938 const byte data[] = {1}; |
879 kMaxModuleSectionCode, // unknown section without length. | |
880 }; | |
881 EXPECT_FAILURE(data); | 939 EXPECT_FAILURE(data); |
882 } | 940 } |
883 | 941 |
884 TEST_F(WasmModuleVerifyTest, UnknownSectionEmpty) { | 942 TEST_F(WasmModuleVerifyTest, TheLoneliestOfValidModulesTheTrulyEmptyOne) { |
885 for (int i = 0; i < 255 - kMaxModuleSectionCode; ++i) { | 943 const byte data[] = { |
886 const byte data[] = { | 944 1, // Section size. |
887 byte(kMaxModuleSectionCode + i), 0, // empty unknown section | 945 0, // Empty section name. |
888 }; | 946 // No section name, no content, nothing but sadness. |
889 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 947 }; |
890 EXPECT_TRUE(result.ok()); | 948 EXPECT_VERIFIES(data); |
891 if (result.val) delete result.val; | |
892 } | |
893 } | 949 } |
894 | 950 |
895 TEST_F(WasmModuleVerifyTest, UnknownSectionOne) { | 951 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionEmpty) { |
896 static const byte data[] = { | 952 const byte data[] = { |
897 kMaxModuleSectionCode, | 953 5, // Section size. |
898 1, // LEB128 1 | 954 4, 'l', 'u', 'l', 'z', // unknown section. |
899 0, // one byte section | |
900 }; | 955 }; |
901 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 956 EXPECT_VERIFIES(data); |
902 EXPECT_TRUE(result.ok()); | |
903 if (result.val) delete result.val; | |
904 } | 957 } |
905 | 958 |
906 TEST_F(WasmModuleVerifyTest, UnknownSectionTen) { | 959 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionNonEmpty) { |
907 static const byte data[] = { | 960 const byte data[] = { |
908 kMaxModuleSectionCode, | 961 10, // Section size. |
909 10, // LEB128 10 | 962 4, 'l', 'u', 'l', 'z', // unknown section. |
910 1, | 963 // Section content: |
911 2, | 964 0xff, 0xff, 0xff, 0xff, 0xff, |
912 3, | |
913 4, | |
914 5, | |
915 6, | |
916 7, | |
917 8, | |
918 9, | |
919 10, // 10 byte section | |
920 }; | 965 }; |
921 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 966 EXPECT_VERIFIES(data); |
922 EXPECT_TRUE(result.ok()); | 967 } |
923 if (result.val) delete result.val; | 968 |
| 969 TEST_F(WasmModuleVerifyTest, SignatureFollowedByEmptyUnknownSection) { |
| 970 const byte data[] = { |
| 971 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG, |
| 972 // ----------------------------------------------------------- |
| 973 5, // Section size. |
| 974 4, 'l', 'u', 'l', 'z', // unknown section. |
| 975 }; |
| 976 EXPECT_VERIFIES(data); |
| 977 } |
| 978 |
| 979 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSection) { |
| 980 const byte data[] = { |
| 981 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG, |
| 982 // ----------------------------------------------------------- |
| 983 10, // Section size. |
| 984 4, 'l', 'u', 'l', 'z', // unknown section. |
| 985 0xff, 0xff, 0xff, 0xff, 0xff, |
| 986 }; |
| 987 EXPECT_VERIFIES(data); |
| 988 } |
| 989 |
| 990 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSectionWithLongLEB) { |
| 991 const byte data[] = { |
| 992 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG, |
| 993 // ----------------------------------------------------------- |
| 994 0x85, 0x80, 0x80, 0x80, 0x00, // Section size: 1 but in a 5-byte LEB. |
| 995 4, 'l', 'u', 'l', 'z', // unknown section. |
| 996 }; |
| 997 EXPECT_VERIFIES(data); |
924 } | 998 } |
925 | 999 |
926 TEST_F(WasmModuleVerifyTest, UnknownSectionOverflow) { | 1000 TEST_F(WasmModuleVerifyTest, UnknownSectionOverflow) { |
927 static const byte data[] = { | 1001 static const byte data[] = { |
928 kMaxModuleSectionCode, | 1002 13, // Section size. |
929 11, // LEB128 11 | 1003 1, // Section name length. |
930 1, | 1004 '\0', // Section name. |
931 2, | 1005 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // 10 byte section |
932 3, | |
933 4, | |
934 5, | |
935 6, | |
936 7, | |
937 8, | |
938 9, | |
939 10, // 10 byte section | |
940 }; | 1006 }; |
941 EXPECT_FAILURE(data); | 1007 EXPECT_FAILURE(data); |
942 } | 1008 } |
943 | 1009 |
944 TEST_F(WasmModuleVerifyTest, UnknownSectionUnderflow) { | 1010 TEST_F(WasmModuleVerifyTest, UnknownSectionUnderflow) { |
945 static const byte data[] = { | 1011 static const byte data[] = { |
946 kMaxModuleSectionCode, | 1012 0xff, 0xff, 0xff, 0xff, 0x0f, // Section size LEB128 0xffffffff |
947 0xff, | 1013 1, '\0', // Section name and name length. |
948 0xff, | 1014 1, 2, 3, 4, // 4 byte section |
949 0xff, | |
950 0xff, | |
951 0x0f, // LEB128 0xffffffff | |
952 1, | |
953 2, | |
954 3, | |
955 4, // 4 byte section | |
956 }; | 1015 }; |
957 EXPECT_FAILURE(data); | 1016 EXPECT_FAILURE(data); |
958 } | 1017 } |
959 | 1018 |
960 TEST_F(WasmModuleVerifyTest, UnknownSectionLoop) { | 1019 TEST_F(WasmModuleVerifyTest, UnknownSectionLoop) { |
961 // Would infinite loop decoding if wrapping and allowed. | 1020 // Would infinite loop decoding if wrapping and allowed. |
962 static const byte data[] = { | 1021 static const byte data[] = { |
963 kMaxModuleSectionCode, | 1022 0xfa, 0xff, 0xff, 0xff, 0x0f, // Section size LEB128 0xfffffffa |
964 0xfa, | 1023 1, '\0', // Section name and name length. |
965 0xff, | 1024 1, 2, 3, 4, // 4 byte section |
966 0xff, | |
967 0xff, | |
968 0x0f, // LEB128 0xfffffffa | |
969 1, | |
970 2, | |
971 3, | |
972 4, // 4 byte section | |
973 }; | 1025 }; |
974 EXPECT_FAILURE(data); | 1026 EXPECT_FAILURE(data); |
975 } | 1027 } |
976 | 1028 |
977 TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) { | 1029 TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) { |
978 static const byte data[] = { | 1030 static const byte data[] = { |
979 kMaxModuleSectionCode, | 1031 3, // Section size. |
980 1, // LEB128 1 | 1032 1, |
981 0, // one byte section | 1033 '\0', // Section name: LEB128 1, string '\0' |
982 kDeclGlobals, | 1034 0, // one byte section |
| 1035 SECTION(GLOBALS, 7), |
983 1, | 1036 1, |
984 0, // name length | 1037 0, // name length |
985 kMemI32, // memory type | 1038 kMemI32, // memory type |
986 0, // exported | 1039 0, // exported |
987 }; | 1040 }; |
988 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 1041 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
989 EXPECT_TRUE(result.ok()); | 1042 EXPECT_TRUE(result.ok()); |
990 | 1043 |
991 EXPECT_EQ(1, result.val->globals.size()); | 1044 EXPECT_EQ(1, result.val->globals.size()); |
992 EXPECT_EQ(0, result.val->functions.size()); | 1045 EXPECT_EQ(0, result.val->functions.size()); |
993 EXPECT_EQ(0, result.val->data_segments.size()); | 1046 EXPECT_EQ(0, result.val->data_segments.size()); |
994 | 1047 |
995 WasmGlobal* global = &result.val->globals.back(); | 1048 WasmGlobal* global = &result.val->globals.back(); |
996 | 1049 |
997 EXPECT_EQ(0, global->name_length); | 1050 EXPECT_EQ(0, global->name_length); |
998 EXPECT_EQ(MachineType::Int32(), global->type); | 1051 EXPECT_EQ(MachineType::Int32(), global->type); |
999 EXPECT_EQ(0, global->offset); | 1052 EXPECT_EQ(0, global->offset); |
1000 EXPECT_FALSE(global->exported); | 1053 EXPECT_FALSE(global->exported); |
1001 | 1054 |
1002 if (result.val) delete result.val; | 1055 if (result.val) delete result.val; |
1003 } | 1056 } |
1004 | 1057 |
1005 TEST_F(WasmModuleVerifyTest, ImportTable_empty) { | 1058 TEST_F(WasmModuleVerifyTest, ImportTable_empty) { |
1006 static const byte data[] = {kDeclSignatures, 0, kDeclImportTable, 0}; | 1059 static const byte data[] = {SECTION(SIGNATURES, 1), 0, |
| 1060 SECTION(IMPORT_TABLE, 1), 0}; |
1007 EXPECT_VERIFIES(data); | 1061 EXPECT_VERIFIES(data); |
1008 } | 1062 } |
1009 | 1063 |
1010 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs) { | 1064 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs) { |
1011 static const byte data[] = {kDeclImportTable, 0}; | 1065 static const byte data[] = {SECTION(IMPORT_TABLE, 1), 0}; |
1012 EXPECT_FAILURE(data); | 1066 EXPECT_FAILURE(data); |
1013 } | 1067 } |
1014 | 1068 |
1015 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { | 1069 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { |
1016 static const byte data[] = { | 1070 static const byte data[] = { |
1017 kDeclSignatures, 0, kDeclImportTable, 1, | 1071 SECTION(SIGNATURES, 1), 0, SECTION(IMPORT_TABLE, 6), 1, |
1018 IMPORT_SIG_INDEX(0), // sig index | 1072 IMPORT_SIG_INDEX(0), // sig index |
1019 NAME_LENGTH(1), 'm', // module name | 1073 NAME_LENGTH(1), 'm', // module name |
1020 NAME_LENGTH(1), 'f', // function name | 1074 NAME_LENGTH(1), 'f', // function name |
1021 }; | 1075 }; |
1022 EXPECT_FAILURE(data); | 1076 EXPECT_FAILURE(data); |
1023 } | 1077 } |
1024 | 1078 |
1025 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { | 1079 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { |
1026 static const byte data[] = { | 1080 static const byte data[] = { |
1027 kDeclSignatures, | 1081 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
1028 1, | 1082 1, |
1029 VOID_VOID_SIG, | 1083 VOID_VOID_SIG, |
1030 kDeclImportTable, | 1084 SECTION(IMPORT_TABLE, 6), |
1031 1, // -- | 1085 1, // -- |
1032 IMPORT_SIG_INDEX(0), // sig index | 1086 IMPORT_SIG_INDEX(0), // sig index |
1033 NAME_LENGTH(1), 'm', // module name | 1087 NAME_LENGTH(1), |
1034 NAME_LENGTH(1), 'f', // function name | 1088 'm', // module name |
| 1089 NAME_LENGTH(1), |
| 1090 'f', // function name |
1035 }; | 1091 }; |
1036 EXPECT_VERIFIES(data); | 1092 EXPECT_VERIFIES(data); |
1037 } | 1093 } |
1038 | 1094 |
1039 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) { | 1095 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) { |
1040 static const byte data[] = { | 1096 static const byte data[] = { |
1041 kDeclSignatures, | 1097 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
1042 1, | 1098 1, |
1043 VOID_VOID_SIG, | 1099 VOID_VOID_SIG, |
1044 kDeclImportTable, | 1100 SECTION(IMPORT_TABLE, 6), |
1045 1, // -- | 1101 1, // -- |
1046 IMPORT_SIG_INDEX(0), // sig index | 1102 IMPORT_SIG_INDEX(0), // sig index |
1047 NO_NAME, // module name | 1103 NO_NAME, // module name |
1048 NAME_LENGTH(1), 'f' // function name | 1104 NAME_LENGTH(1), |
| 1105 'f' // function name |
1049 }; | 1106 }; |
1050 EXPECT_FAILURE(data); | 1107 EXPECT_FAILURE(data); |
1051 } | 1108 } |
1052 | 1109 |
1053 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { | 1110 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { |
1054 static const byte data[] = { | 1111 static const byte data[] = { |
1055 kDeclSignatures, 1, VOID_VOID_SIG, kDeclImportTable, 1, | 1112 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
| 1113 1, |
| 1114 VOID_VOID_SIG, |
| 1115 SECTION(IMPORT_TABLE, 6), |
| 1116 1, |
1056 IMPORT_SIG_INDEX(0), // sig index | 1117 IMPORT_SIG_INDEX(0), // sig index |
1057 NAME_LENGTH(1), 'm', // module name | 1118 NAME_LENGTH(1), |
1058 NAME_LENGTH(1), 'f', // function name | 1119 'm', // module name |
| 1120 NAME_LENGTH(1), |
| 1121 'f', // function name |
1059 }; | 1122 }; |
1060 | 1123 |
1061 EXPECT_OFF_END_FAILURE(data, 5, sizeof(data)); | 1124 EXPECT_OFF_END_FAILURE(data, 16, sizeof(data)); |
1062 } | 1125 } |
1063 | 1126 |
1064 TEST_F(WasmModuleVerifyTest, ExportTable_empty1) { | 1127 TEST_F(WasmModuleVerifyTest, ExportTable_empty1) { |
1065 static const byte data[] = {kDeclSignatures, 1, VOID_VOID_SIG, | 1128 static const byte data[] = {SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
1066 kDeclFunctions, 1, EMPTY_FUNCTION(0), | 1129 1, |
1067 kDeclExportTable, 0}; | 1130 VOID_VOID_SIG, |
| 1131 SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), |
| 1132 1, |
| 1133 EMPTY_FUNCTION(0), |
| 1134 SECTION(EXPORT_TABLE, 1), |
| 1135 0}; |
1068 EXPECT_VERIFIES(data); | 1136 EXPECT_VERIFIES(data); |
1069 } | 1137 } |
1070 | 1138 |
1071 TEST_F(WasmModuleVerifyTest, ExportTable_empty2) { | 1139 TEST_F(WasmModuleVerifyTest, ExportTable_empty2) { |
1072 static const byte data[] = {kDeclSignatures, 0, kDeclFunctions, 0, | 1140 static const byte data[] = {SECTION(SIGNATURES, 1), 0, |
1073 kDeclExportTable, 0}; | 1141 SECTION(FUNCTIONS, 1), 0, |
| 1142 SECTION(EXPORT_TABLE, 1), 0}; |
1074 // TODO(titzer): current behavior treats empty functions section as missing. | 1143 // TODO(titzer): current behavior treats empty functions section as missing. |
1075 EXPECT_FAILURE(data); | 1144 EXPECT_FAILURE(data); |
1076 } | 1145 } |
1077 | 1146 |
1078 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { | 1147 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { |
1079 static const byte data[] = {kDeclSignatures, 0, kDeclExportTable, 0}; | 1148 static const byte data[] = {SECTION(SIGNATURES, 1), 0, |
| 1149 SECTION(EXPORT_TABLE, 1), 0}; |
1080 EXPECT_FAILURE(data); | 1150 EXPECT_FAILURE(data); |
1081 } | 1151 } |
1082 | 1152 |
1083 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { | 1153 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { |
1084 static const byte data[] = {kDeclExportTable, 0}; | 1154 static const byte data[] = {SECTION(EXPORT_TABLE, 1), 0}; |
1085 EXPECT_FAILURE(data); | 1155 EXPECT_FAILURE(data); |
1086 } | 1156 } |
1087 | 1157 |
1088 TEST_F(WasmModuleVerifyTest, ExportTableOne) { | 1158 TEST_F(WasmModuleVerifyTest, ExportTableOne) { |
1089 static const byte data[] = { | 1159 static const byte data[] = { |
1090 kDeclSignatures, 1, // sigs | 1160 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
1091 VOID_VOID_SIG, // -- | 1161 1, // sigs |
1092 kDeclFunctions, 1, // functions | 1162 VOID_VOID_SIG, // -- |
1093 EMPTY_FUNCTION(0), // -- | 1163 SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), |
1094 kDeclExportTable, 1, // exports | 1164 1, // functions |
1095 FUNC_INDEX(0), // -- | 1165 EMPTY_FUNCTION(0), // -- |
1096 NO_NAME // -- | 1166 SECTION(EXPORT_TABLE, 7), |
| 1167 1, // exports |
| 1168 FUNC_INDEX(0), // -- |
| 1169 NO_NAME // -- |
1097 }; | 1170 }; |
1098 EXPECT_VERIFIES(data); | 1171 EXPECT_VERIFIES(data); |
1099 } | 1172 } |
1100 | 1173 |
1101 TEST_F(WasmModuleVerifyTest, ExportTableTwo) { | 1174 TEST_F(WasmModuleVerifyTest, ExportTableTwo) { |
1102 static const byte data[] = { | 1175 static const byte data[] = { |
1103 kDeclSignatures, 1, // sigs | 1176 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
1104 VOID_VOID_SIG, // -- | 1177 1, // sigs |
1105 kDeclFunctions, 1, // functions | 1178 VOID_VOID_SIG, // -- |
1106 EMPTY_FUNCTION(0), // -- | 1179 SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), |
1107 kDeclExportTable, 2, // exports | 1180 1, // functions |
1108 FUNC_INDEX(0), // -- | 1181 EMPTY_FUNCTION(0), // -- |
1109 NAME_LENGTH(4), 'n', 'a', 'm', 'e', // -- | 1182 SECTION(EXPORT_TABLE, 12), |
1110 FUNC_INDEX(0), // -- | 1183 2, // exports |
1111 NAME_LENGTH(3), 'n', 'o', 'm' // -- | 1184 FUNC_INDEX(0), // -- |
| 1185 NAME_LENGTH(4), |
| 1186 'n', |
| 1187 'a', |
| 1188 'm', |
| 1189 'e', // -- |
| 1190 FUNC_INDEX(0), // -- |
| 1191 NAME_LENGTH(3), |
| 1192 'n', |
| 1193 'o', |
| 1194 'm' // -- |
1112 }; | 1195 }; |
1113 EXPECT_VERIFIES(data); | 1196 EXPECT_VERIFIES(data); |
1114 } | 1197 } |
1115 | 1198 |
1116 TEST_F(WasmModuleVerifyTest, ExportTableThree) { | 1199 TEST_F(WasmModuleVerifyTest, ExportTableThree) { |
1117 static const byte data[] = { | 1200 static const byte data[] = { |
1118 kDeclSignatures, 1, // sigs | 1201 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
1119 VOID_VOID_SIG, // -- | 1202 1, // sigs |
1120 kDeclFunctions, 3, // functions | 1203 VOID_VOID_SIG, // -- |
1121 EMPTY_FUNCTION(0), // -- | 1204 SECTION(FUNCTIONS, 1 + 3 * EMPTY_FUNCTION_SIZE), |
1122 EMPTY_FUNCTION(0), // -- | 1205 3, // functions |
1123 EMPTY_FUNCTION(0), // -- | 1206 EMPTY_FUNCTION(0), // -- |
1124 kDeclExportTable, 3, // exports | 1207 EMPTY_FUNCTION(0), // -- |
1125 FUNC_INDEX(0), // -- | 1208 EMPTY_FUNCTION(0), // -- |
1126 NAME_LENGTH(1), 'a', // -- | 1209 SECTION(EXPORT_TABLE, 10), |
1127 FUNC_INDEX(1), // -- | 1210 3, // exports |
1128 NAME_LENGTH(1), 'b', // -- | 1211 FUNC_INDEX(0), // -- |
1129 FUNC_INDEX(2), // -- | 1212 NAME_LENGTH(1), |
1130 NAME_LENGTH(1), 'c' // -- | 1213 'a', // -- |
| 1214 FUNC_INDEX(1), // -- |
| 1215 NAME_LENGTH(1), |
| 1216 'b', // -- |
| 1217 FUNC_INDEX(2), // -- |
| 1218 NAME_LENGTH(1), |
| 1219 'c' // -- |
1131 }; | 1220 }; |
1132 EXPECT_VERIFIES(data); | 1221 EXPECT_VERIFIES(data); |
1133 } | 1222 } |
1134 | 1223 |
1135 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) { | 1224 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) { |
1136 for (int i = 0; i < 6; i++) { | 1225 for (int i = 0; i < 6; i++) { |
1137 const byte data[] = { | 1226 const byte data[] = { |
1138 kDeclSignatures, 1, // sigs | 1227 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
1139 VOID_VOID_SIG, // -- | 1228 1, // sigs |
1140 kDeclFunctions, 3, // functions | 1229 VOID_VOID_SIG, // -- |
1141 EMPTY_FUNCTION(0), // -- | 1230 SECTION(FUNCTIONS, 1 + 3 * EMPTY_FUNCTION_SIZE), |
1142 EMPTY_FUNCTION(0), // -- | 1231 3, // functions |
1143 EMPTY_FUNCTION(0), // -- | 1232 EMPTY_FUNCTION(0), // -- |
1144 kDeclExportTable, 1, // exports | 1233 EMPTY_FUNCTION(0), // -- |
1145 FUNC_INDEX(i), // -- | 1234 EMPTY_FUNCTION(0), // -- |
1146 NAME_LENGTH(2), 'e', 'x', // -- | 1235 SECTION(EXPORT_TABLE, 5), |
| 1236 1, // exports |
| 1237 FUNC_INDEX(i), // -- |
| 1238 NAME_LENGTH(2), |
| 1239 'e', |
| 1240 'x', // -- |
1147 }; | 1241 }; |
1148 | 1242 |
1149 if (i < 3) { | 1243 if (i < 3) { |
1150 EXPECT_VERIFIES(data); | 1244 EXPECT_VERIFIES(data); |
1151 } else { | 1245 } else { |
1152 EXPECT_FAILURE(data); | 1246 EXPECT_FAILURE(data); |
1153 } | 1247 } |
1154 } | 1248 } |
1155 } | 1249 } |
1156 | 1250 |
1157 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) { | 1251 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) { |
1158 static const byte data[] = { | 1252 static const byte data[] = { |
1159 kDeclSignatures, 1, // sigs | 1253 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), |
1160 VOID_VOID_SIG, // -- | 1254 1, // sigs |
1161 kDeclFunctions, 1, // functions | 1255 VOID_VOID_SIG, // -- |
1162 EMPTY_FUNCTION(0), // -- | 1256 SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), |
1163 kDeclExportTable, 1, // exports | 1257 1, // functions |
1164 FUNC_INDEX(0), // -- | 1258 EMPTY_FUNCTION(0), // -- |
1165 NO_NAME // -- | 1259 SECTION(EXPORT_TABLE, 1 + 6), |
| 1260 1, // exports |
| 1261 FUNC_INDEX(0), // -- |
| 1262 NO_NAME // -- |
1166 }; | 1263 }; |
1167 | 1264 |
1168 for (int length = 13; length < sizeof(data); length++) { | 1265 for (int length = 33; length < sizeof(data); length++) { |
1169 ModuleResult result = DecodeModule(data, data + length); | 1266 ModuleResult result = DecodeModule(data, data + length); |
1170 EXPECT_FALSE(result.ok()); | 1267 EXPECT_FALSE(result.ok()); |
1171 if (result.val) delete result.val; | 1268 if (result.val) delete result.val; |
1172 } | 1269 } |
1173 } | 1270 } |
1174 | 1271 |
1175 #define SIGNATURES_SECTION(count, ...) \ | 1272 #define SIGNATURES_SECTION(count, ...) \ |
1176 kDeclSignatures, U32V_1(count), __VA_ARGS__ | 1273 SECTION(SIGNATURES, 1 + 3 * (count)), U32V_1(count), __VA_ARGS__ |
1177 #define FUNCTION_SIGNATURES_SECTION(count, ...) \ | 1274 #define FUNCTION_SIGNATURES_SECTION(count, ...) \ |
1178 kDeclFunctionSignatures, U32V_1(count), __VA_ARGS__ | 1275 SECTION(FUNCTION_SIGNATURES, 1 + (count)), U32V_1(count), __VA_ARGS__ |
1179 #define FUNCTION_BODIES_SECTION(count, ...) \ | |
1180 kDeclFunctionBodies, U32V_1(count), __VA_ARGS__ | |
1181 #define NAMES_SECTION(count, ...) kDeclNames, U32V_1(count), __VA_ARGS__ | |
1182 | 1276 |
1183 #define FOO_STRING 3, 'f', 'o', 'o' | 1277 #define FOO_STRING 3, 'f', 'o', 'o' |
1184 #define NO_LOCAL_NAMES 0 | 1278 #define NO_LOCAL_NAMES 0 |
1185 | 1279 |
1186 #define EMPTY_SIGNATURES_SECTION kDeclSignatures, 0 | 1280 #define EMPTY_SIGNATURES_SECTION SECTION(SIGNATURES, 1), 0 |
1187 #define EMPTY_FUNCTION_SIGNATURES_SECTION kDeclFunctionSignatures, 0 | 1281 #define EMPTY_FUNCTION_SIGNATURES_SECTION SECTION(FUNCTION_SIGNATURES, 1), 0 |
1188 #define EMPTY_FUNCTION_BODIES_SECTION kDeclFunctionBodies, 0 | 1282 #define EMPTY_FUNCTION_BODIES_SECTION SECTION(FUNCTION_BODIES, 1), 0 |
1189 #define EMPTY_NAMES_SECTION kDeclNames, 0 | 1283 #define EMPTY_NAMES_SECTION SECTION(NAMES, 1), 0 |
1190 | 1284 |
1191 TEST_F(WasmModuleVerifyTest, FunctionSignatures_empty) { | 1285 TEST_F(WasmModuleVerifyTest, FunctionSignatures_empty) { |
1192 static const byte data[] = {kDeclSignatures, 0, kDeclFunctionSignatures, 0}; | 1286 static const byte data[] = {SECTION(SIGNATURES, 1), 0, |
| 1287 SECTION(FUNCTION_SIGNATURES, 1), 0}; |
1193 EXPECT_VERIFIES(data); | 1288 EXPECT_VERIFIES(data); |
1194 } | 1289 } |
1195 | 1290 |
1196 TEST_F(WasmModuleVerifyTest, FunctionSignatures_one) { | 1291 TEST_F(WasmModuleVerifyTest, FunctionSignatures_one) { |
1197 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), | 1292 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), |
1198 FUNCTION_SIGNATURES_SECTION(1, 0)}; | 1293 FUNCTION_SIGNATURES_SECTION(1, 0)}; |
1199 EXPECT_VERIFIES(data); | 1294 EXPECT_VERIFIES(data); |
1200 } | 1295 } |
1201 | 1296 |
1202 TEST_F(WasmModuleVerifyTest, FunctionBodies_empty) { | 1297 TEST_F(WasmModuleVerifyTest, FunctionBodies_empty) { |
1203 static const byte data[] = {EMPTY_SIGNATURES_SECTION, | 1298 static const byte data[] = {EMPTY_SIGNATURES_SECTION, |
1204 EMPTY_FUNCTION_SIGNATURES_SECTION, | 1299 EMPTY_FUNCTION_SIGNATURES_SECTION, |
1205 EMPTY_FUNCTION_BODIES_SECTION}; | 1300 EMPTY_FUNCTION_BODIES_SECTION}; |
1206 EXPECT_VERIFIES(data); | 1301 EXPECT_VERIFIES(data); |
1207 } | 1302 } |
1208 | 1303 |
1209 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_empty) { | 1304 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_empty) { |
1210 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), | 1305 static const byte data[] = { |
1211 FUNCTION_SIGNATURES_SECTION(1, 0), | 1306 SIGNATURES_SECTION(1, VOID_VOID_SIG), FUNCTION_SIGNATURES_SECTION(1, 0), |
1212 FUNCTION_BODIES_SECTION(1, EMPTY_BODY)}; | 1307 SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE), 1, EMPTY_BODY}; |
1213 EXPECT_VERIFIES(data); | 1308 EXPECT_VERIFIES(data); |
1214 } | 1309 } |
1215 | 1310 |
1216 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_nop) { | 1311 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_nop) { |
1217 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), | 1312 static const byte data[] = { |
1218 FUNCTION_SIGNATURES_SECTION(1, 0), | 1313 SIGNATURES_SECTION(1, VOID_VOID_SIG), FUNCTION_SIGNATURES_SECTION(1, 0), |
1219 FUNCTION_BODIES_SECTION(1, NOP_BODY)}; | 1314 SECTION(FUNCTION_BODIES, 1 + NOP_BODY_SIZE), 1, NOP_BODY}; |
1220 EXPECT_VERIFIES(data); | 1315 EXPECT_VERIFIES(data); |
1221 } | 1316 } |
1222 | 1317 |
1223 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch1) { | 1318 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch1) { |
1224 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), | 1319 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), |
1225 FUNCTION_SIGNATURES_SECTION(2, 0, 0), | 1320 FUNCTION_SIGNATURES_SECTION(2, 0, 0), |
1226 FUNCTION_BODIES_SECTION(1, EMPTY_BODY)}; | 1321 SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE), 1, |
| 1322 EMPTY_BODY}; |
1227 EXPECT_FAILURE(data); | 1323 EXPECT_FAILURE(data); |
1228 } | 1324 } |
1229 | 1325 |
1230 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch2) { | 1326 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch2) { |
1231 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), | 1327 static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG), |
1232 FUNCTION_SIGNATURES_SECTION(1, 0), | 1328 FUNCTION_SIGNATURES_SECTION(1, 0), |
1233 FUNCTION_BODIES_SECTION(2, NOP_BODY, NOP_BODY)}; | 1329 SECTION(FUNCTION_BODIES, 1 + 2 * NOP_BODY_SIZE), |
| 1330 2, |
| 1331 NOP_BODY, |
| 1332 NOP_BODY}; |
1234 EXPECT_FAILURE(data); | 1333 EXPECT_FAILURE(data); |
1235 } | 1334 } |
1236 | 1335 |
1237 TEST_F(WasmModuleVerifyTest, Names_empty) { | 1336 TEST_F(WasmModuleVerifyTest, Names_empty) { |
1238 static const byte data[] = { | 1337 static const byte data[] = { |
1239 EMPTY_SIGNATURES_SECTION, EMPTY_FUNCTION_SIGNATURES_SECTION, | 1338 EMPTY_SIGNATURES_SECTION, EMPTY_FUNCTION_SIGNATURES_SECTION, |
1240 EMPTY_FUNCTION_BODIES_SECTION, EMPTY_NAMES_SECTION}; | 1339 EMPTY_FUNCTION_BODIES_SECTION, EMPTY_NAMES_SECTION}; |
1241 EXPECT_VERIFIES(data); | 1340 EXPECT_VERIFIES(data); |
1242 } | 1341 } |
1243 | 1342 |
1244 TEST_F(WasmModuleVerifyTest, Names_one_empty) { | 1343 TEST_F(WasmModuleVerifyTest, Names_one_empty) { |
1245 static const byte data[] = { | 1344 static const byte data[] = { |
1246 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1345 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- |
1247 FUNCTION_SIGNATURES_SECTION(1, 0), // -- | 1346 FUNCTION_SIGNATURES_SECTION(1, 0), // -- |
1248 FUNCTION_BODIES_SECTION(1, EMPTY_BODY), // -- | 1347 SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE), |
1249 NAMES_SECTION(1, FOO_STRING, NO_LOCAL_NAMES) // -- | 1348 1, |
| 1349 EMPTY_BODY, // -- |
| 1350 SECTION(NAMES, 1 + 5), |
| 1351 1, |
| 1352 FOO_STRING, |
| 1353 NO_LOCAL_NAMES // -- |
1250 }; | 1354 }; |
1251 EXPECT_VERIFIES(data); | 1355 EXPECT_VERIFIES(data); |
1252 } | 1356 } |
1253 | 1357 |
1254 TEST_F(WasmModuleVerifyTest, Names_two_empty) { | 1358 TEST_F(WasmModuleVerifyTest, Names_two_empty) { |
1255 static const byte data[] = { | 1359 static const byte data[] = { |
1256 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1360 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- |
1257 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- | 1361 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- |
1258 FUNCTION_BODIES_SECTION(2, EMPTY_BODY, EMPTY_BODY), // -- | 1362 SECTION(FUNCTION_BODIES, 1 + 2 * EMPTY_BODY_SIZE), // -- |
1259 NAMES_SECTION(2, FOO_STRING, NO_LOCAL_NAMES, // -- | 1363 2, |
1260 FOO_STRING, NO_LOCAL_NAMES) // -- | 1364 EMPTY_BODY, |
| 1365 EMPTY_BODY, // -- |
| 1366 SECTION(NAMES, 1 + 10), |
| 1367 2, // -- |
| 1368 FOO_STRING, |
| 1369 NO_LOCAL_NAMES, // -- |
| 1370 FOO_STRING, |
| 1371 NO_LOCAL_NAMES, // -- |
1261 }; | 1372 }; |
1262 EXPECT_VERIFIES(data); | 1373 EXPECT_VERIFIES(data); |
1263 } | 1374 } |
1264 | 1375 |
1265 } // namespace wasm | 1376 } // namespace wasm |
1266 } // namespace internal | 1377 } // namespace internal |
1267 } // namespace v8 | 1378 } // namespace v8 |
OLD | NEW |