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-opcodes.h" | 8 #include "src/wasm/wasm-opcodes.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 namespace wasm { | 12 namespace wasm { |
13 | 13 |
| 14 #define VOID_VOID_SIG 0, kLocalVoid |
| 15 #define INT_INT_SIG 1, kLocalI32, kLocalI32 |
| 16 |
| 17 #define U32(v) \ |
| 18 static_cast<byte>(v), static_cast<byte>(v >> 8), static_cast<byte>(v >> 16), \ |
| 19 static_cast<byte>(v >> 24) |
| 20 #define U16(v) static_cast<byte>(v), static_cast<byte>(v >> 8) |
| 21 #define U8(v) static_cast<byte>(v) |
| 22 |
| 23 #define SIG_INDEX(v) U16(v) |
| 24 #define FUNC_INDEX(v) U16(v) |
| 25 #define NAME_OFFSET(v) U32(v) |
| 26 |
| 27 #define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16(0) |
| 28 |
14 class WasmModuleVerifyTest : public TestWithZone { | 29 class WasmModuleVerifyTest : public TestWithZone { |
15 public: | 30 public: |
16 ModuleResult DecodeModule(const byte* module_start, const byte* module_end) { | 31 ModuleResult DecodeModule(const byte* module_start, const byte* module_end) { |
17 return DecodeWasmModule(nullptr, zone(), module_start, module_end, false, | 32 return DecodeWasmModule(nullptr, zone(), module_start, module_end, false, |
18 false); | 33 false); |
19 } | 34 } |
20 }; | 35 }; |
21 | 36 |
22 | 37 |
23 #define EXPECT_VERIFIES(data) \ | 38 #define EXPECT_VERIFIES(data) \ |
(...skipping 14 matching lines...) Expand all Loading... |
38 | 53 |
39 struct LocalTypePair { | 54 struct LocalTypePair { |
40 uint8_t code; | 55 uint8_t code; |
41 LocalType type; | 56 LocalType type; |
42 } kLocalTypes[] = {{kLocalI32, kAstI32}, | 57 } kLocalTypes[] = {{kLocalI32, kAstI32}, |
43 {kLocalI64, kAstI64}, | 58 {kLocalI64, kAstI64}, |
44 {kLocalF32, kAstF32}, | 59 {kLocalF32, kAstF32}, |
45 {kLocalF64, kAstF64}}; | 60 {kLocalF64, kAstF64}}; |
46 | 61 |
47 | 62 |
48 // TODO(titzer): use these macros everywhere below. | |
49 #define U32_LE(v) \ | |
50 static_cast<byte>(v), static_cast<byte>((v) >> 8), \ | |
51 static_cast<byte>((v) >> 16), static_cast<byte>((v) >> 24) | |
52 | |
53 | |
54 #define U16_LE(v) static_cast<byte>(v), static_cast<byte>((v) >> 8) | |
55 | |
56 | |
57 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { | 63 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { |
58 static const byte data[1]{kDeclEnd}; | 64 static const byte data[1]{kDeclEnd}; |
59 { | 65 { |
60 ModuleResult result = DecodeModule(data, data); | 66 ModuleResult result = DecodeModule(data, data); |
61 EXPECT_TRUE(result.ok()); | 67 EXPECT_TRUE(result.ok()); |
62 if (result.val) delete result.val; | 68 if (result.val) delete result.val; |
63 } | 69 } |
64 { | 70 { |
65 ModuleResult result = DecodeModule(data, data + 1); | 71 ModuleResult result = DecodeModule(data, data + 1); |
66 EXPECT_TRUE(result.ok()); | 72 EXPECT_TRUE(result.ok()); |
67 if (result.val) delete result.val; | 73 if (result.val) delete result.val; |
68 } | 74 } |
69 } | 75 } |
70 | 76 |
71 | 77 |
72 TEST_F(WasmModuleVerifyTest, OneGlobal) { | 78 TEST_F(WasmModuleVerifyTest, OneGlobal) { |
73 static const byte data[] = { | 79 static const byte data[] = { |
74 kDeclGlobals, | 80 kDeclGlobals, |
75 1, | 81 1, |
76 0, | 82 NAME_OFFSET(0), |
77 0, | |
78 0, | |
79 0, // name offset | |
80 kMemI32, // memory type | 83 kMemI32, // memory type |
81 0, // exported | 84 0, // exported |
82 }; | 85 }; |
83 | 86 |
84 { | 87 { |
85 // Should decode to exactly one global. | 88 // Should decode to exactly one global. |
86 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 89 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
87 EXPECT_TRUE(result.ok()); | 90 EXPECT_TRUE(result.ok()); |
88 EXPECT_EQ(1, result.val->globals->size()); | 91 EXPECT_EQ(1, result.val->globals->size()); |
89 EXPECT_EQ(0, result.val->functions->size()); | 92 EXPECT_EQ(0, result.val->functions->size()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } else { | 131 } else { |
129 buffer.push_back(static_cast<byte>(out)); | 132 buffer.push_back(static_cast<byte>(out)); |
130 break; | 133 break; |
131 } | 134 } |
132 } | 135 } |
133 } | 136 } |
134 | 137 |
135 | 138 |
136 TEST_F(WasmModuleVerifyTest, NGlobals) { | 139 TEST_F(WasmModuleVerifyTest, NGlobals) { |
137 static const byte data[] = { | 140 static const byte data[] = { |
138 0, 0, 0, 0, // name offset | 141 NAME_OFFSET(0), // name offset |
139 kMemI32, // memory type | 142 kMemI32, // memory type |
140 0, // exported | 143 0, // exported |
141 }; | 144 }; |
142 for (uint32_t i = 0; i < 1000000; i = i * 7 + 1) { | 145 for (uint32_t i = 0; i < 1000000; i = i * 7 + 1) { |
143 std::vector<byte> buffer; | 146 std::vector<byte> buffer; |
144 buffer.push_back(kDeclGlobals); | 147 buffer.push_back(kDeclGlobals); |
145 AppendUint32v(buffer, i); | 148 AppendUint32v(buffer, i); |
146 for (uint32_t j = 0; j < i; j++) { | 149 for (uint32_t j = 0; j < i; j++) { |
147 buffer.insert(buffer.end(), data, data + arraysize(data)); | 150 buffer.insert(buffer.end(), data, data + arraysize(data)); |
148 } | 151 } |
149 | 152 |
150 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); | 153 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); |
151 EXPECT_TRUE(result.ok()); | 154 EXPECT_TRUE(result.ok()); |
152 if (result.val) delete result.val; | 155 if (result.val) delete result.val; |
153 } | 156 } |
154 } | 157 } |
155 | 158 |
156 | 159 |
157 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameOffset) { | 160 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameOffset) { |
158 static const byte data[] = { | 161 static const byte data[] = { |
159 kDeclGlobals, | 162 kDeclGlobals, |
160 1, // declare one global | 163 1, // declare one global |
161 0, | 164 NAME_OFFSET(300), // name offset |
162 3, | 165 kMemI32, // memory type |
163 0, | 166 0, // exported |
164 0, // name offset | |
165 kMemI32, // memory type | |
166 0, // exported | |
167 }; | 167 }; |
168 | 168 |
169 EXPECT_FAILURE(data); | 169 EXPECT_FAILURE(data); |
170 } | 170 } |
171 | 171 |
172 | 172 |
173 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) { | 173 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) { |
174 static const byte data[] = { | 174 static const byte data[] = { |
175 kDeclGlobals, | 175 kDeclGlobals, |
176 1, // declare one global | 176 1, // declare one global |
177 0, | 177 NAME_OFFSET(0), // name offset |
178 0, | 178 33, // memory type |
179 0, | 179 0, // exported |
180 0, // name offset | |
181 33, // memory type | |
182 0, // exported | |
183 }; | 180 }; |
184 | 181 |
185 EXPECT_FAILURE(data); | 182 EXPECT_FAILURE(data); |
186 } | 183 } |
187 | 184 |
188 | 185 |
189 TEST_F(WasmModuleVerifyTest, TwoGlobals) { | 186 TEST_F(WasmModuleVerifyTest, TwoGlobals) { |
190 static const byte data[] = { | 187 static const byte data[] = { |
191 kDeclGlobals, | 188 kDeclGlobals, |
192 2, | 189 2, |
193 0, | 190 NAME_OFFSET(0), // #0: name offset |
194 0, | 191 kMemF32, // memory type |
195 0, | 192 0, // exported |
196 0, // #0: name offset | 193 NAME_OFFSET(0), // #1: name offset |
197 kMemF32, // memory type | 194 kMemF64, // memory type |
198 0, // exported | 195 1, // exported |
199 0, | |
200 0, | |
201 0, | |
202 0, // #1: name offset | |
203 kMemF64, // memory type | |
204 1, // exported | |
205 }; | 196 }; |
206 | 197 |
207 { | 198 { |
208 // Should decode to exactly two globals. | 199 // Should decode to exactly two globals. |
209 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 200 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
210 EXPECT_TRUE(result.ok()); | 201 EXPECT_TRUE(result.ok()); |
211 EXPECT_EQ(2, result.val->globals->size()); | 202 EXPECT_EQ(2, result.val->globals->size()); |
212 EXPECT_EQ(0, result.val->functions->size()); | 203 EXPECT_EQ(0, result.val->functions->size()); |
213 EXPECT_EQ(0, result.val->data_segments->size()); | 204 EXPECT_EQ(0, result.val->data_segments->size()); |
214 | 205 |
(...skipping 16 matching lines...) Expand all Loading... |
231 for (size_t size = 1; size < arraysize(data); size++) { | 222 for (size_t size = 1; size < arraysize(data); size++) { |
232 // Should fall off end of module bytes. | 223 // Should fall off end of module bytes. |
233 ModuleResult result = DecodeModule(data, data + size); | 224 ModuleResult result = DecodeModule(data, data + size); |
234 EXPECT_FALSE(result.ok()); | 225 EXPECT_FALSE(result.ok()); |
235 if (result.val) delete result.val; | 226 if (result.val) delete result.val; |
236 } | 227 } |
237 } | 228 } |
238 | 229 |
239 | 230 |
240 TEST_F(WasmModuleVerifyTest, OneSignature) { | 231 TEST_F(WasmModuleVerifyTest, OneSignature) { |
241 static const byte data[] = { | 232 { |
242 kDeclSignatures, 1, 0, kLocalVoid // void -> void | 233 static const byte data[] = {kDeclSignatures, 1, VOID_VOID_SIG}; |
243 }; | 234 EXPECT_VERIFIES(data); |
244 EXPECT_VERIFIES(data); | 235 } |
| 236 |
| 237 { |
| 238 static const byte data[] = {kDeclSignatures, 1, INT_INT_SIG}; |
| 239 EXPECT_VERIFIES(data); |
| 240 } |
245 } | 241 } |
246 | 242 |
247 | 243 |
248 TEST_F(WasmModuleVerifyTest, MultipleSignatures) { | 244 TEST_F(WasmModuleVerifyTest, MultipleSignatures) { |
249 static const byte data[] = { | 245 static const byte data[] = { |
250 kDeclSignatures, | 246 kDeclSignatures, |
251 3, | 247 3, |
252 0, | 248 0, |
253 kLocalVoid, // void -> void | 249 kLocalVoid, // void -> void |
254 1, | 250 1, |
(...skipping 25 matching lines...) Expand all Loading... |
280 EXPECT_FALSE(result.ok()); | 276 EXPECT_FALSE(result.ok()); |
281 if (result.val) delete result.val; | 277 if (result.val) delete result.val; |
282 } | 278 } |
283 } | 279 } |
284 | 280 |
285 | 281 |
286 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { | 282 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { |
287 static const byte data[] = { | 283 static const byte data[] = { |
288 kDeclFunctions, 1, | 284 kDeclFunctions, 1, |
289 // func#0 ------------------------------------------------------ | 285 // func#0 ------------------------------------------------------ |
290 0, 0, // signature index | 286 SIG_INDEX(0), // signature index |
291 0, 0, 0, 0, // name offset | 287 NAME_OFFSET(0), // name offset |
292 0, 0, 0, 0, // code start offset | 288 U32(0), // code start offset |
293 0, 0, 0, 0, // code end offset | 289 U32(0), // code end offset |
294 1, 2, // local int32 count | 290 U16(899), // local int32 count |
295 3, 4, // local int64 count | 291 U16(799), // local int64 count |
296 5, 6, // local float32 count | 292 U16(699), // local float32 count |
297 7, 8, // local float64 count | 293 U16(599), // local float64 count |
298 0, // exported | 294 0, // exported |
299 1 // external | 295 1 // external |
300 }; | 296 }; |
301 | 297 |
302 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 298 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
303 EXPECT_FALSE(result.ok()); | 299 EXPECT_FALSE(result.ok()); |
304 if (result.val) delete result.val; | 300 if (result.val) delete result.val; |
305 } | 301 } |
306 | 302 |
307 | 303 |
308 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { | 304 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { |
309 const int kCodeStartOffset = 23; | 305 const int kCodeStartOffset = 23; |
310 const int kCodeEndOffset = kCodeStartOffset + 1; | 306 const int kCodeEndOffset = kCodeStartOffset + 1; |
311 | 307 |
312 static const byte data[] = { | 308 static const byte data[] = { |
313 kDeclSignatures, 1, | 309 kDeclSignatures, 1, |
314 // sig#0 ------------------------------------------------------- | 310 // sig#0 ------------------------------------------------------- |
315 0, 0, // void -> void | 311 VOID_VOID_SIG, |
316 // func#0 ------------------------------------------------------ | 312 // func#0 ------------------------------------------------------ |
317 kDeclFunctions, 1, | 313 kDeclFunctions, 1, |
318 kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName, 0, | 314 kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName, |
319 0, // signature index | 315 SIG_INDEX(0), // signature index |
320 9, 0, 0, 0, // name offset | 316 NAME_OFFSET(9), // name offset |
321 11, 2, // local int32 count | 317 U16(1466), // local int32 count |
322 13, 4, // local int64 count | 318 U16(1355), // local int64 count |
323 15, 6, // local float32 count | 319 U16(1244), // local float32 count |
324 17, 8, // local float64 count | 320 U16(1133), // local float64 count |
325 1, 0, // size | 321 1, 0, // size |
326 kExprNop, | 322 kExprNop, |
327 }; | 323 }; |
328 | 324 |
329 { | 325 { |
330 // Should decode to exactly one function. | 326 // Should decode to exactly one function. |
331 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 327 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
332 EXPECT_TRUE(result.ok()); | 328 EXPECT_TRUE(result.ok()); |
333 EXPECT_EQ(0, result.val->globals->size()); | 329 EXPECT_EQ(0, result.val->globals->size()); |
334 EXPECT_EQ(1, result.val->signatures->size()); | 330 EXPECT_EQ(1, result.val->signatures->size()); |
335 EXPECT_EQ(1, result.val->functions->size()); | 331 EXPECT_EQ(1, result.val->functions->size()); |
336 EXPECT_EQ(0, result.val->data_segments->size()); | 332 EXPECT_EQ(0, result.val->data_segments->size()); |
337 EXPECT_EQ(0, result.val->function_table->size()); | 333 EXPECT_EQ(0, result.val->function_table->size()); |
338 | 334 |
339 WasmFunction* function = &result.val->functions->back(); | 335 WasmFunction* function = &result.val->functions->back(); |
340 | 336 |
341 EXPECT_EQ(9, function->name_offset); | 337 EXPECT_EQ(9, function->name_offset); |
342 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 338 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
343 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 339 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
344 | 340 |
345 EXPECT_EQ(523, function->local_i32_count); | 341 EXPECT_EQ(1466, function->local_i32_count); |
346 EXPECT_EQ(1037, function->local_i64_count); | 342 EXPECT_EQ(1355, function->local_i64_count); |
347 EXPECT_EQ(1551, function->local_f32_count); | 343 EXPECT_EQ(1244, function->local_f32_count); |
348 EXPECT_EQ(2065, function->local_f64_count); | 344 EXPECT_EQ(1133, function->local_f64_count); |
349 | 345 |
350 EXPECT_TRUE(function->exported); | 346 EXPECT_TRUE(function->exported); |
351 EXPECT_FALSE(function->external); | 347 EXPECT_FALSE(function->external); |
352 | 348 |
353 if (result.val) delete result.val; | 349 if (result.val) delete result.val; |
354 } | 350 } |
355 | 351 |
356 for (size_t size = 5; size < arraysize(data); size++) { | 352 for (size_t size = 5; size < arraysize(data); size++) { |
357 // Should fall off end of module bytes. | 353 // Should fall off end of module bytes. |
358 ModuleResult result = DecodeModule(data, data + size); | 354 ModuleResult result = DecodeModule(data, data + size); |
359 EXPECT_FALSE(result.ok()); | 355 EXPECT_FALSE(result.ok()); |
360 if (result.val) delete result.val; | 356 if (result.val) delete result.val; |
361 } | 357 } |
362 } | 358 } |
363 | 359 |
364 | 360 |
365 TEST_F(WasmModuleVerifyTest, OneFunctionImported) { | 361 TEST_F(WasmModuleVerifyTest, OneFunctionImported) { |
366 static const byte data[] = { | 362 static const byte data[] = { |
367 kDeclSignatures, 1, | 363 kDeclSignatures, 1, |
368 // sig#0 ------------------------------------------------------- | 364 // sig#0 ------------------------------------------------------- |
369 0, 0, // void -> void | 365 VOID_VOID_SIG, kDeclFunctions, 1, |
370 kDeclFunctions, 1, | |
371 // func#0 ------------------------------------------------------ | 366 // func#0 ------------------------------------------------------ |
372 kDeclFunctionImport, // no name, no locals, imported | 367 kDeclFunctionImport, // no name, no locals, imported |
373 0, 0, // signature index | 368 SIG_INDEX(0), |
374 }; | 369 }; |
375 | 370 |
376 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 371 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
377 EXPECT_TRUE(result.ok()); | 372 EXPECT_TRUE(result.ok()); |
378 EXPECT_EQ(1, result.val->functions->size()); | 373 EXPECT_EQ(1, result.val->functions->size()); |
379 WasmFunction* function = &result.val->functions->back(); | 374 WasmFunction* function = &result.val->functions->back(); |
380 | 375 |
381 EXPECT_EQ(0, function->name_offset); | 376 EXPECT_EQ(0, function->name_offset); |
382 EXPECT_EQ(0, function->code_start_offset); | 377 EXPECT_EQ(0, function->code_start_offset); |
383 EXPECT_EQ(0, function->code_end_offset); | 378 EXPECT_EQ(0, function->code_end_offset); |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 | 631 |
637 for (int source_offset = 0; source_offset < 5 + kDataSize; source_offset++) { | 632 for (int source_offset = 0; source_offset < 5 + kDataSize; source_offset++) { |
638 for (int source_size = -1; source_size < 5 + kDataSize; source_size += 3) { | 633 for (int source_size = -1; source_size < 5 + kDataSize; source_size += 3) { |
639 byte data[] = { | 634 byte data[] = { |
640 kDeclMemory, | 635 kDeclMemory, |
641 mem_size_log2, | 636 mem_size_log2, |
642 mem_size_log2, | 637 mem_size_log2, |
643 1, | 638 1, |
644 kDeclDataSegments, | 639 kDeclDataSegments, |
645 1, | 640 1, |
646 U32_LE(dest_addr), | 641 U32(dest_addr), |
647 U32_LE(source_offset), | 642 U32(source_offset), |
648 U32_LE(source_size), | 643 U32(source_size), |
649 1, // init | 644 1, // init |
650 }; | 645 }; |
651 | 646 |
652 STATIC_ASSERT(kDataSize == arraysize(data)); | 647 STATIC_ASSERT(kDataSize == arraysize(data)); |
653 | 648 |
654 if (source_offset < kDataSize && source_size >= 0 && | 649 if (source_offset < kDataSize && source_size >= 0 && |
655 (source_offset + source_size) <= kDataSize) { | 650 (source_offset + source_size) <= kDataSize) { |
656 EXPECT_VERIFIES(data); | 651 EXPECT_VERIFIES(data); |
657 } else { | 652 } else { |
658 EXPECT_FAILURE(data); | 653 EXPECT_FAILURE(data); |
(...skipping 12 matching lines...) Expand all Loading... |
671 | 666 |
672 for (int dest_addr = mem_size - source_size; | 667 for (int dest_addr = mem_size - source_size; |
673 dest_addr < mem_size + source_size; dest_addr++) { | 668 dest_addr < mem_size + source_size; dest_addr++) { |
674 byte data[] = { | 669 byte data[] = { |
675 kDeclMemory, | 670 kDeclMemory, |
676 mem_size_log2, | 671 mem_size_log2, |
677 mem_size_log2, | 672 mem_size_log2, |
678 1, | 673 1, |
679 kDeclDataSegments, | 674 kDeclDataSegments, |
680 1, | 675 1, |
681 U32_LE(dest_addr), | 676 U32(dest_addr), |
682 U32_LE(source_offset), | 677 U32(source_offset), |
683 U32_LE(source_size), | 678 U32(source_size), |
684 1, // init | 679 1, // init |
685 }; | 680 }; |
686 | 681 |
687 if (dest_addr <= (mem_size - source_size)) { | 682 if (dest_addr <= (mem_size - source_size)) { |
688 EXPECT_VERIFIES(data); | 683 EXPECT_VERIFIES(data); |
689 } else { | 684 } else { |
690 EXPECT_FAILURE(data); | 685 EXPECT_FAILURE(data); |
691 } | 686 } |
692 } | 687 } |
693 } | 688 } |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 EXPECT_VERIFIES(data); | 1006 EXPECT_VERIFIES(data); |
1012 } | 1007 } |
1013 | 1008 |
1014 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs) { | 1009 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs) { |
1015 static const byte data[] = {kDeclImportTable, 0}; | 1010 static const byte data[] = {kDeclImportTable, 0}; |
1016 EXPECT_FAILURE(data); | 1011 EXPECT_FAILURE(data); |
1017 } | 1012 } |
1018 | 1013 |
1019 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { | 1014 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { |
1020 static const byte data[] = { | 1015 static const byte data[] = { |
1021 kDeclSignatures, | 1016 kDeclSignatures, 0, kDeclImportTable, 1, |
1022 0, | 1017 SIG_INDEX(0), // sig index |
1023 kDeclImportTable, | 1018 NAME_OFFSET(1), // module name |
1024 1, | 1019 NAME_OFFSET(1), // function name |
1025 0, | |
1026 0, // sig index | |
1027 1, | |
1028 0, | |
1029 0, | |
1030 0, // module name | |
1031 1, | |
1032 0, | |
1033 0, | |
1034 0 // function name | |
1035 }; | 1020 }; |
1036 EXPECT_FAILURE(data); | 1021 EXPECT_FAILURE(data); |
1037 } | 1022 } |
1038 | 1023 |
1039 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { | 1024 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { |
1040 static const byte data[] = { | 1025 static const byte data[] = { |
1041 kDeclSignatures, | 1026 kDeclSignatures, |
1042 1, | 1027 1, |
1043 0, | 1028 VOID_VOID_SIG, |
1044 static_cast<byte>(kAstStmt), | |
1045 kDeclImportTable, | 1029 kDeclImportTable, |
1046 1, | 1030 1, // -- |
1047 0, | 1031 SIG_INDEX(0), // sig index |
1048 0, // sig index | 1032 NAME_OFFSET(1), // module name |
1049 1, | 1033 NAME_OFFSET(1) // function name |
1050 0, | |
1051 0, | |
1052 0, // module name | |
1053 1, | |
1054 0, | |
1055 0, | |
1056 0 // function name | |
1057 }; | 1034 }; |
1058 EXPECT_VERIFIES(data); | 1035 EXPECT_VERIFIES(data); |
1059 } | 1036 } |
1060 | 1037 |
1061 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { | 1038 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { |
1062 static const byte data[] = { | 1039 static const byte data[] = { |
1063 kDeclSignatures, | 1040 kDeclSignatures, 1, VOID_VOID_SIG, kDeclImportTable, 1, |
1064 1, | 1041 SIG_INDEX(0), // sig index |
1065 0, | 1042 NAME_OFFSET(1), // module name |
1066 static_cast<byte>(kAstStmt), | 1043 NAME_OFFSET(1), // function name |
1067 kDeclImportTable, | |
1068 1, | |
1069 0, | |
1070 0, // sig index | |
1071 1, | |
1072 0, | |
1073 0, | |
1074 0, // module name | |
1075 1, | |
1076 0, | |
1077 0, | |
1078 0 // function name | |
1079 }; | 1044 }; |
1080 | 1045 |
1081 for (size_t length = 5; length < sizeof(data); length++) { | 1046 for (size_t length = 5; length < sizeof(data); length++) { |
1082 ModuleResult result = DecodeModule(data, data + length); | 1047 ModuleResult result = DecodeModule(data, data + length); |
1083 EXPECT_FALSE(result.ok()); | 1048 EXPECT_FALSE(result.ok()); |
1084 if (result.val) delete result.val; | 1049 if (result.val) delete result.val; |
1085 } | 1050 } |
1086 } | 1051 } |
1087 | 1052 |
| 1053 TEST_F(WasmModuleVerifyTest, ExportTable_empty) { |
| 1054 static const byte data[] = {kDeclSignatures, 0, kDeclFunctions, 0, |
| 1055 kDeclExportTable, 0}; |
| 1056 EXPECT_VERIFIES(data); |
| 1057 } |
| 1058 |
| 1059 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { |
| 1060 static const byte data[] = {kDeclSignatures, 0, kDeclExportTable, 0}; |
| 1061 EXPECT_FAILURE(data); |
| 1062 } |
| 1063 |
| 1064 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { |
| 1065 static const byte data[] = {kDeclExportTable, 0}; |
| 1066 EXPECT_FAILURE(data); |
| 1067 } |
| 1068 |
| 1069 TEST_F(WasmModuleVerifyTest, ExportTableOne) { |
| 1070 static const byte data[] = { |
| 1071 kDeclSignatures, 1, // sigs |
| 1072 VOID_VOID_SIG, // -- |
| 1073 kDeclFunctions, 1, // functions |
| 1074 EMPTY_FUNCTION(0), // -- |
| 1075 kDeclExportTable, 1, // exports |
| 1076 FUNC_INDEX(0), // -- |
| 1077 NAME_OFFSET(0) // -- |
| 1078 }; |
| 1079 EXPECT_VERIFIES(data); |
| 1080 } |
| 1081 |
| 1082 TEST_F(WasmModuleVerifyTest, ExportTableTwo) { |
| 1083 static const byte data[] = { |
| 1084 kDeclSignatures, 1, // sigs |
| 1085 VOID_VOID_SIG, // -- |
| 1086 kDeclFunctions, 1, // functions |
| 1087 EMPTY_FUNCTION(0), // -- |
| 1088 kDeclExportTable, 2, // exports |
| 1089 FUNC_INDEX(0), // -- |
| 1090 NAME_OFFSET(1), // -- |
| 1091 FUNC_INDEX(0), // -- |
| 1092 NAME_OFFSET(2) // -- |
| 1093 }; |
| 1094 EXPECT_VERIFIES(data); |
| 1095 } |
| 1096 |
| 1097 TEST_F(WasmModuleVerifyTest, ExportTableThree) { |
| 1098 static const byte data[] = { |
| 1099 kDeclSignatures, 1, // sigs |
| 1100 VOID_VOID_SIG, // -- |
| 1101 kDeclFunctions, 3, // functions |
| 1102 EMPTY_FUNCTION(0), // -- |
| 1103 EMPTY_FUNCTION(0), // -- |
| 1104 EMPTY_FUNCTION(0), // -- |
| 1105 kDeclExportTable, 3, // exports |
| 1106 FUNC_INDEX(0), // -- |
| 1107 NAME_OFFSET(1), // -- |
| 1108 FUNC_INDEX(1), // -- |
| 1109 NAME_OFFSET(2), // -- |
| 1110 FUNC_INDEX(2), // -- |
| 1111 NAME_OFFSET(2) // -- |
| 1112 }; |
| 1113 EXPECT_VERIFIES(data); |
| 1114 } |
| 1115 |
| 1116 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) { |
| 1117 for (int i = 0; i < 6; i++) { |
| 1118 const byte data[] = { |
| 1119 kDeclSignatures, 1, // sigs |
| 1120 VOID_VOID_SIG, // -- |
| 1121 kDeclFunctions, 3, // functions |
| 1122 EMPTY_FUNCTION(0), // -- |
| 1123 EMPTY_FUNCTION(0), // -- |
| 1124 EMPTY_FUNCTION(0), // -- |
| 1125 kDeclExportTable, 1, // exports |
| 1126 FUNC_INDEX(i), // -- |
| 1127 NAME_OFFSET(1) // -- |
| 1128 }; |
| 1129 |
| 1130 if (i < 3) { |
| 1131 EXPECT_VERIFIES(data); |
| 1132 } else { |
| 1133 EXPECT_FAILURE(data); |
| 1134 } |
| 1135 } |
| 1136 } |
| 1137 |
| 1138 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) { |
| 1139 static const byte data[] = { |
| 1140 kDeclSignatures, 1, // sigs |
| 1141 VOID_VOID_SIG, // -- |
| 1142 kDeclFunctions, 1, // functions |
| 1143 EMPTY_FUNCTION(0), // -- |
| 1144 kDeclExportTable, 1, // exports |
| 1145 FUNC_INDEX(0), // -- |
| 1146 NAME_OFFSET(0) // -- |
| 1147 }; |
| 1148 |
| 1149 for (int length = 13; length < sizeof(data); length++) { |
| 1150 ModuleResult result = DecodeModule(data, data + length); |
| 1151 EXPECT_FALSE(result.ok()); |
| 1152 if (result.val) delete result.val; |
| 1153 } |
| 1154 } |
| 1155 |
1088 } // namespace wasm | 1156 } // namespace wasm |
1089 } // namespace internal | 1157 } // namespace internal |
1090 } // namespace v8 | 1158 } // namespace v8 |
OLD | NEW |