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 class WasmModuleVerifyTest : public TestWithZone { | 14 class WasmModuleVerifyTest : public TestWithZone { |
15 public: | 15 public: |
16 ModuleResult DecodeModule(const byte* module_start, const byte* module_end) { | 16 ModuleResult DecodeModule(const byte* module_start, const byte* module_end) { |
17 return DecodeWasmModule(nullptr, zone(), module_start, module_end, false, | 17 return DecodeWasmModule(nullptr, zone(), module_start, module_end, false, |
18 false); | 18 false); |
19 } | 19 } |
20 }; | 20 }; |
21 | 21 |
22 | 22 |
23 #define EXPECT_VERIFIES(data) \ | 23 #define EXPECT_VERIFIES(data) \ |
24 do { \ | 24 do { \ |
25 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ | 25 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ |
26 EXPECT_TRUE(result.ok()); \ | 26 EXPECT_TRUE(result.ok()); \ |
| 27 if (result.val) delete result.val; \ |
27 } while (false) | 28 } while (false) |
28 | 29 |
29 | 30 |
30 #define EXPECT_FAILURE(data) \ | 31 #define EXPECT_FAILURE(data) \ |
31 do { \ | 32 do { \ |
32 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ | 33 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ |
33 EXPECT_FALSE(result.ok()); \ | 34 EXPECT_FALSE(result.ok()); \ |
| 35 if (result.val) delete result.val; \ |
34 } while (false) | 36 } while (false) |
35 | 37 |
36 | 38 |
37 struct LocalTypePair { | 39 struct LocalTypePair { |
38 uint8_t code; | 40 uint8_t code; |
39 LocalType type; | 41 LocalType type; |
40 } kLocalTypes[] = {{kLocalI32, kAstI32}, | 42 } kLocalTypes[] = {{kLocalI32, kAstI32}, |
41 {kLocalI64, kAstI64}, | 43 {kLocalI64, kAstI64}, |
42 {kLocalF32, kAstF32}, | 44 {kLocalF32, kAstF32}, |
43 {kLocalF64, kAstF64}}; | 45 {kLocalF64, kAstF64}}; |
44 | 46 |
45 | 47 |
46 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { | 48 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { |
47 static const byte data[1]{kDeclEnd}; | 49 static const byte data[1]{kDeclEnd}; |
48 { | 50 { |
49 ModuleResult result = DecodeModule(data, data); | 51 ModuleResult result = DecodeModule(data, data); |
50 EXPECT_TRUE(result.ok()); | 52 EXPECT_TRUE(result.ok()); |
| 53 if (result.val) delete result.val; |
51 } | 54 } |
52 { | 55 { |
53 ModuleResult result = DecodeModule(data, data + 1); | 56 ModuleResult result = DecodeModule(data, data + 1); |
54 EXPECT_TRUE(result.ok()); | 57 EXPECT_TRUE(result.ok()); |
| 58 if (result.val) delete result.val; |
55 } | 59 } |
56 } | 60 } |
57 | 61 |
58 | 62 |
59 TEST_F(WasmModuleVerifyTest, OneGlobal) { | 63 TEST_F(WasmModuleVerifyTest, OneGlobal) { |
60 const byte data[] = { | 64 const byte data[] = { |
61 kDeclGlobals, | 65 kDeclGlobals, |
62 1, | 66 1, |
63 0, | 67 0, |
64 0, | 68 0, |
65 0, | 69 0, |
66 0, // name offset | 70 0, // name offset |
67 kMemI32, // memory type | 71 kMemI32, // memory type |
68 0, // exported | 72 0, // exported |
69 }; | 73 }; |
70 | 74 |
71 { | 75 { |
72 // Should decode to exactly one global. | 76 // Should decode to exactly one global. |
73 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 77 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
74 EXPECT_TRUE(result.ok()); | 78 EXPECT_TRUE(result.ok()); |
75 EXPECT_EQ(1, result.val->globals->size()); | 79 EXPECT_EQ(1, result.val->globals->size()); |
76 EXPECT_EQ(0, result.val->functions->size()); | 80 EXPECT_EQ(0, result.val->functions->size()); |
77 EXPECT_EQ(0, result.val->data_segments->size()); | 81 EXPECT_EQ(0, result.val->data_segments->size()); |
78 | 82 |
79 WasmGlobal* global = &result.val->globals->back(); | 83 WasmGlobal* global = &result.val->globals->back(); |
80 | 84 |
81 EXPECT_EQ(0, global->name_offset); | 85 EXPECT_EQ(0, global->name_offset); |
82 EXPECT_EQ(MachineType::Int32(), global->type); | 86 EXPECT_EQ(MachineType::Int32(), global->type); |
83 EXPECT_EQ(0, global->offset); | 87 EXPECT_EQ(0, global->offset); |
84 EXPECT_EQ(false, global->exported); | 88 EXPECT_FALSE(global->exported); |
| 89 |
| 90 if (result.val) delete result.val; |
85 } | 91 } |
86 | 92 |
87 for (size_t size = 1; size < arraysize(data); size++) { | 93 for (size_t size = 1; size < arraysize(data); size++) { |
88 // Should fall off end of module bytes. | 94 // Should fall off end of module bytes. |
89 ModuleResult result = DecodeModule(data, data + size); | 95 ModuleResult result = DecodeModule(data, data + size); |
90 EXPECT_FALSE(result.ok()); | 96 EXPECT_FALSE(result.ok()); |
| 97 if (result.val) delete result.val; |
91 } | 98 } |
92 } | 99 } |
93 | 100 |
94 | 101 |
95 TEST_F(WasmModuleVerifyTest, ZeroGlobals) { | 102 TEST_F(WasmModuleVerifyTest, ZeroGlobals) { |
96 const byte data[] = { | 103 const byte data[] = { |
97 kDeclGlobals, 0, // declare 0 globals | 104 kDeclGlobals, 0, // declare 0 globals |
98 }; | 105 }; |
99 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 106 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
100 EXPECT_TRUE(result.ok()); | 107 EXPECT_TRUE(result.ok()); |
| 108 if (result.val) delete result.val; |
101 } | 109 } |
102 | 110 |
103 | 111 |
104 static void AppendUint32v(std::vector<byte>& buffer, uint32_t val) { | 112 static void AppendUint32v(std::vector<byte>& buffer, uint32_t val) { |
105 while (true) { | 113 while (true) { |
106 uint32_t next = val >> 7; | 114 uint32_t next = val >> 7; |
107 uint32_t out = val & 0x7f; | 115 uint32_t out = val & 0x7f; |
108 if (next) { | 116 if (next) { |
109 buffer.push_back(static_cast<byte>(0x80 | out)); | 117 buffer.push_back(static_cast<byte>(0x80 | out)); |
110 val = next; | 118 val = next; |
111 } else { | 119 } else { |
112 buffer.push_back(static_cast<byte>(out)); | 120 buffer.push_back(static_cast<byte>(out)); |
113 break; | 121 break; |
114 } | 122 } |
115 } | 123 } |
116 } | 124 } |
117 | 125 |
118 | 126 |
119 TEST_F(WasmModuleVerifyTest, NGlobals) { | 127 TEST_F(WasmModuleVerifyTest, NGlobals) { |
120 const byte data[] = { | 128 const byte data[] = { |
121 0, 0, 0, 0, // name offset | 129 0, 0, 0, 0, // name offset |
122 kMemI32, // memory type | 130 kMemI32, // memory type |
123 0, // exported | 131 0, // exported |
124 }; | 132 }; |
125 for (uint32_t i = 0; i < 1000000; i = i * 7 + 1) { | 133 for (uint32_t i = 0; i < 1000000; i = i * 7 + 1) { |
126 std::vector<byte> buffer; | 134 std::vector<byte> buffer; |
127 buffer.push_back(kDeclGlobals); | 135 buffer.push_back(kDeclGlobals); |
128 AppendUint32v(buffer, i); | 136 AppendUint32v(buffer, i); |
129 for (int j = 0; j < i; j++) { | 137 for (uint32_t j = 0; j < i; j++) { |
130 buffer.insert(buffer.end(), data, data + arraysize(data)); | 138 buffer.insert(buffer.end(), data, data + arraysize(data)); |
131 } | 139 } |
132 | 140 |
133 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); | 141 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); |
134 EXPECT_TRUE(result.ok()); | 142 EXPECT_TRUE(result.ok()); |
| 143 if (result.val) delete result.val; |
135 } | 144 } |
136 } | 145 } |
137 | 146 |
138 | 147 |
139 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameOffset) { | 148 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameOffset) { |
140 const byte data[] = { | 149 const byte data[] = { |
141 kDeclGlobals, | 150 kDeclGlobals, |
142 1, // declare one global | 151 1, // declare one global |
143 0, | 152 0, |
144 3, | 153 3, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 EXPECT_EQ(2, result.val->globals->size()); | 202 EXPECT_EQ(2, result.val->globals->size()); |
194 EXPECT_EQ(0, result.val->functions->size()); | 203 EXPECT_EQ(0, result.val->functions->size()); |
195 EXPECT_EQ(0, result.val->data_segments->size()); | 204 EXPECT_EQ(0, result.val->data_segments->size()); |
196 | 205 |
197 WasmGlobal* g0 = &result.val->globals->at(0); | 206 WasmGlobal* g0 = &result.val->globals->at(0); |
198 WasmGlobal* g1 = &result.val->globals->at(1); | 207 WasmGlobal* g1 = &result.val->globals->at(1); |
199 | 208 |
200 EXPECT_EQ(0, g0->name_offset); | 209 EXPECT_EQ(0, g0->name_offset); |
201 EXPECT_EQ(MachineType::Float32(), g0->type); | 210 EXPECT_EQ(MachineType::Float32(), g0->type); |
202 EXPECT_EQ(0, g0->offset); | 211 EXPECT_EQ(0, g0->offset); |
203 EXPECT_EQ(false, g0->exported); | 212 EXPECT_FALSE(g0->exported); |
204 | 213 |
205 EXPECT_EQ(0, g1->name_offset); | 214 EXPECT_EQ(0, g1->name_offset); |
206 EXPECT_EQ(MachineType::Float64(), g1->type); | 215 EXPECT_EQ(MachineType::Float64(), g1->type); |
207 EXPECT_EQ(0, g1->offset); | 216 EXPECT_EQ(0, g1->offset); |
208 EXPECT_EQ(true, g1->exported); | 217 EXPECT_TRUE(g1->exported); |
| 218 |
| 219 if (result.val) delete result.val; |
209 } | 220 } |
210 | 221 |
211 for (size_t size = 1; size < arraysize(data); size++) { | 222 for (size_t size = 1; size < arraysize(data); size++) { |
212 // Should fall off end of module bytes. | 223 // Should fall off end of module bytes. |
213 ModuleResult result = DecodeModule(data, data + size); | 224 ModuleResult result = DecodeModule(data, data + size); |
214 EXPECT_FALSE(result.ok()); | 225 EXPECT_FALSE(result.ok()); |
| 226 if (result.val) delete result.val; |
215 } | 227 } |
216 } | 228 } |
217 | 229 |
218 | 230 |
219 TEST_F(WasmModuleVerifyTest, OneSignature) { | 231 TEST_F(WasmModuleVerifyTest, OneSignature) { |
220 static const byte data[] = { | 232 static const byte data[] = { |
221 kDeclSignatures, 1, 0, kLocalVoid // void -> void | 233 kDeclSignatures, 1, 0, kLocalVoid // void -> void |
222 }; | 234 }; |
223 EXPECT_VERIFIES(data); | 235 EXPECT_VERIFIES(data); |
224 } | 236 } |
(...skipping 19 matching lines...) Expand all Loading... |
244 EXPECT_EQ(3, result.val->signatures->size()); | 256 EXPECT_EQ(3, result.val->signatures->size()); |
245 if (result.val->signatures->size() == 3) { | 257 if (result.val->signatures->size() == 3) { |
246 EXPECT_EQ(0, result.val->signatures->at(0)->return_count()); | 258 EXPECT_EQ(0, result.val->signatures->at(0)->return_count()); |
247 EXPECT_EQ(1, result.val->signatures->at(1)->return_count()); | 259 EXPECT_EQ(1, result.val->signatures->at(1)->return_count()); |
248 EXPECT_EQ(1, result.val->signatures->at(2)->return_count()); | 260 EXPECT_EQ(1, result.val->signatures->at(2)->return_count()); |
249 | 261 |
250 EXPECT_EQ(0, result.val->signatures->at(0)->parameter_count()); | 262 EXPECT_EQ(0, result.val->signatures->at(0)->parameter_count()); |
251 EXPECT_EQ(1, result.val->signatures->at(1)->parameter_count()); | 263 EXPECT_EQ(1, result.val->signatures->at(1)->parameter_count()); |
252 EXPECT_EQ(2, result.val->signatures->at(2)->parameter_count()); | 264 EXPECT_EQ(2, result.val->signatures->at(2)->parameter_count()); |
253 } | 265 } |
| 266 if (result.val) delete result.val; |
254 | 267 |
255 for (size_t size = 1; size < arraysize(data); size++) { | 268 for (size_t size = 1; size < arraysize(data); size++) { |
256 ModuleResult result = DecodeModule(data, data + size); | 269 ModuleResult result = DecodeModule(data, data + size); |
257 // Should fall off the end of module bytes. | 270 // Should fall off the end of module bytes. |
258 EXPECT_FALSE(result.ok()); | 271 EXPECT_FALSE(result.ok()); |
| 272 if (result.val) delete result.val; |
259 } | 273 } |
260 } | 274 } |
261 | 275 |
262 | 276 |
263 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { | 277 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { |
264 static const byte data[] = { | 278 static const byte data[] = { |
265 kDeclFunctions, 1, | 279 kDeclFunctions, 1, |
266 // func#0 ------------------------------------------------------ | 280 // func#0 ------------------------------------------------------ |
267 0, 0, // signature index | 281 0, 0, // signature index |
268 0, 0, 0, 0, // name offset | 282 0, 0, 0, 0, // name offset |
269 0, 0, 0, 0, // code start offset | 283 0, 0, 0, 0, // code start offset |
270 0, 0, 0, 0, // code end offset | 284 0, 0, 0, 0, // code end offset |
271 1, 2, // local int32 count | 285 1, 2, // local int32 count |
272 3, 4, // local int64 count | 286 3, 4, // local int64 count |
273 5, 6, // local float32 count | 287 5, 6, // local float32 count |
274 7, 8, // local float64 count | 288 7, 8, // local float64 count |
275 0, // exported | 289 0, // exported |
276 1 // external | 290 1 // external |
277 }; | 291 }; |
278 | 292 |
279 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 293 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
280 EXPECT_FALSE(result.ok()); | 294 EXPECT_FALSE(result.ok()); |
| 295 if (result.val) delete result.val; |
281 } | 296 } |
282 | 297 |
283 | 298 |
284 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { | 299 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { |
285 const int kCodeStartOffset = 23; | 300 const int kCodeStartOffset = 23; |
286 const int kCodeEndOffset = kCodeStartOffset + 1; | 301 const int kCodeEndOffset = kCodeStartOffset + 1; |
287 | 302 |
288 static const byte data[] = { | 303 static const byte data[] = { |
289 kDeclSignatures, 1, | 304 kDeclSignatures, 1, |
290 // sig#0 ------------------------------------------------------- | 305 // sig#0 ------------------------------------------------------- |
(...skipping 25 matching lines...) Expand all Loading... |
316 | 331 |
317 EXPECT_EQ(9, function->name_offset); | 332 EXPECT_EQ(9, function->name_offset); |
318 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 333 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
319 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 334 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
320 | 335 |
321 EXPECT_EQ(523, function->local_int32_count); | 336 EXPECT_EQ(523, function->local_int32_count); |
322 EXPECT_EQ(1037, function->local_int64_count); | 337 EXPECT_EQ(1037, function->local_int64_count); |
323 EXPECT_EQ(1551, function->local_float32_count); | 338 EXPECT_EQ(1551, function->local_float32_count); |
324 EXPECT_EQ(2065, function->local_float64_count); | 339 EXPECT_EQ(2065, function->local_float64_count); |
325 | 340 |
326 EXPECT_EQ(true, function->exported); | 341 EXPECT_TRUE(function->exported); |
327 EXPECT_EQ(false, function->external); | 342 EXPECT_FALSE(function->external); |
| 343 |
| 344 if (result.val) delete result.val; |
328 } | 345 } |
329 | 346 |
330 for (size_t size = 5; size < arraysize(data); size++) { | 347 for (size_t size = 5; size < arraysize(data); size++) { |
331 // Should fall off end of module bytes. | 348 // Should fall off end of module bytes. |
332 ModuleResult result = DecodeModule(data, data + size); | 349 ModuleResult result = DecodeModule(data, data + size); |
333 EXPECT_FALSE(result.ok()); | 350 EXPECT_FALSE(result.ok()); |
| 351 if (result.val) delete result.val; |
334 } | 352 } |
335 } | 353 } |
336 | 354 |
337 | 355 |
338 TEST_F(WasmModuleVerifyTest, OneFunctionImported) { | 356 TEST_F(WasmModuleVerifyTest, OneFunctionImported) { |
339 static const byte data[] = { | 357 static const byte data[] = { |
340 kDeclSignatures, 1, | 358 kDeclSignatures, 1, |
341 // sig#0 ------------------------------------------------------- | 359 // sig#0 ------------------------------------------------------- |
342 0, 0, // void -> void | 360 0, 0, // void -> void |
343 kDeclFunctions, 1, | 361 kDeclFunctions, 1, |
344 // func#0 ------------------------------------------------------ | 362 // func#0 ------------------------------------------------------ |
345 kDeclFunctionImport, // no name, no locals, imported | 363 kDeclFunctionImport, // no name, no locals, imported |
346 0, 0, // signature index | 364 0, 0, // signature index |
347 }; | 365 }; |
348 | 366 |
349 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 367 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
350 EXPECT_TRUE(result.ok()); | 368 EXPECT_TRUE(result.ok()); |
351 EXPECT_EQ(1, result.val->functions->size()); | 369 EXPECT_EQ(1, result.val->functions->size()); |
352 WasmFunction* function = &result.val->functions->back(); | 370 WasmFunction* function = &result.val->functions->back(); |
353 | 371 |
354 EXPECT_EQ(0, function->name_offset); | 372 EXPECT_EQ(0, function->name_offset); |
355 EXPECT_EQ(0, function->code_start_offset); | 373 EXPECT_EQ(0, function->code_start_offset); |
356 EXPECT_EQ(0, function->code_end_offset); | 374 EXPECT_EQ(0, function->code_end_offset); |
357 | 375 |
358 EXPECT_EQ(0, function->local_int32_count); | 376 EXPECT_EQ(0, function->local_int32_count); |
359 EXPECT_EQ(0, function->local_int64_count); | 377 EXPECT_EQ(0, function->local_int64_count); |
360 EXPECT_EQ(0, function->local_float32_count); | 378 EXPECT_EQ(0, function->local_float32_count); |
361 EXPECT_EQ(0, function->local_float64_count); | 379 EXPECT_EQ(0, function->local_float64_count); |
362 | 380 |
363 EXPECT_EQ(false, function->exported); | 381 EXPECT_FALSE(function->exported); |
364 EXPECT_EQ(true, function->external); | 382 EXPECT_TRUE(function->external); |
| 383 |
| 384 if (result.val) delete result.val; |
365 } | 385 } |
366 | 386 |
367 | 387 |
368 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody) { | 388 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody) { |
369 static const byte kCodeStartOffset = 11; | 389 static const byte kCodeStartOffset = 11; |
370 static const byte kCodeEndOffset = kCodeStartOffset + 1; | 390 static const byte kCodeEndOffset = kCodeStartOffset + 1; |
371 | 391 |
372 static const byte data[] = { | 392 static const byte data[] = { |
373 kDeclSignatures, 1, | 393 kDeclSignatures, 1, |
374 // sig#0 ------------------------------------------------------- | 394 // sig#0 ------------------------------------------------------- |
(...skipping 13 matching lines...) Expand all Loading... |
388 | 408 |
389 EXPECT_EQ(0, function->name_offset); | 409 EXPECT_EQ(0, function->name_offset); |
390 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 410 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
391 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 411 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
392 | 412 |
393 EXPECT_EQ(0, function->local_int32_count); | 413 EXPECT_EQ(0, function->local_int32_count); |
394 EXPECT_EQ(0, function->local_int64_count); | 414 EXPECT_EQ(0, function->local_int64_count); |
395 EXPECT_EQ(0, function->local_float32_count); | 415 EXPECT_EQ(0, function->local_float32_count); |
396 EXPECT_EQ(0, function->local_float64_count); | 416 EXPECT_EQ(0, function->local_float64_count); |
397 | 417 |
398 EXPECT_EQ(false, function->exported); | 418 EXPECT_FALSE(function->exported); |
399 EXPECT_EQ(false, function->external); | 419 EXPECT_FALSE(function->external); |
| 420 |
| 421 if (result.val) delete result.val; |
400 } | 422 } |
401 | 423 |
402 | 424 |
403 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody_WithLocals) { | 425 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody_WithLocals) { |
404 static const byte kCodeStartOffset = 19; | 426 static const byte kCodeStartOffset = 19; |
405 static const byte kCodeEndOffset = kCodeStartOffset + 1; | 427 static const byte kCodeEndOffset = kCodeStartOffset + 1; |
406 | 428 |
407 static const byte data[] = { | 429 static const byte data[] = { |
408 kDeclSignatures, 1, | 430 kDeclSignatures, 1, |
409 // sig#0 ------------------------------------------------------- | 431 // sig#0 ------------------------------------------------------- |
(...skipping 16 matching lines...) Expand all Loading... |
426 | 448 |
427 EXPECT_EQ(0, function->name_offset); | 449 EXPECT_EQ(0, function->name_offset); |
428 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 450 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
429 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 451 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
430 | 452 |
431 EXPECT_EQ(513, function->local_int32_count); | 453 EXPECT_EQ(513, function->local_int32_count); |
432 EXPECT_EQ(1027, function->local_int64_count); | 454 EXPECT_EQ(1027, function->local_int64_count); |
433 EXPECT_EQ(1541, function->local_float32_count); | 455 EXPECT_EQ(1541, function->local_float32_count); |
434 EXPECT_EQ(2055, function->local_float64_count); | 456 EXPECT_EQ(2055, function->local_float64_count); |
435 | 457 |
436 EXPECT_EQ(false, function->exported); | 458 EXPECT_FALSE(function->exported); |
437 EXPECT_EQ(false, function->external); | 459 EXPECT_FALSE(function->external); |
| 460 |
| 461 if (result.val) delete result.val; |
438 } | 462 } |
439 | 463 |
440 | 464 |
441 TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) { | 465 TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) { |
442 static const byte kCodeStartOffset = 2 + kDeclGlobalSize + 4 + 2 + 17; | 466 static const byte kCodeStartOffset = 2 + kDeclGlobalSize + 4 + 2 + 17; |
443 static const byte kCodeEndOffset = kCodeStartOffset + 3; | 467 static const byte kCodeEndOffset = kCodeStartOffset + 3; |
444 | 468 |
445 static const byte data[] = { | 469 static const byte data[] = { |
446 // global#0 -------------------------------------------------- | 470 // global#0 -------------------------------------------------- |
447 kDeclGlobals, 1, 0, 0, 0, 0, // name offset | 471 kDeclGlobals, 1, 0, 0, 0, 0, // name offset |
(...skipping 27 matching lines...) Expand all Loading... |
475 EXPECT_TRUE(result.ok()); | 499 EXPECT_TRUE(result.ok()); |
476 EXPECT_EQ(1, result.val->globals->size()); | 500 EXPECT_EQ(1, result.val->globals->size()); |
477 EXPECT_EQ(1, result.val->functions->size()); | 501 EXPECT_EQ(1, result.val->functions->size()); |
478 EXPECT_EQ(1, result.val->data_segments->size()); | 502 EXPECT_EQ(1, result.val->data_segments->size()); |
479 | 503 |
480 WasmGlobal* global = &result.val->globals->back(); | 504 WasmGlobal* global = &result.val->globals->back(); |
481 | 505 |
482 EXPECT_EQ(0, global->name_offset); | 506 EXPECT_EQ(0, global->name_offset); |
483 EXPECT_EQ(MachineType::Uint8(), global->type); | 507 EXPECT_EQ(MachineType::Uint8(), global->type); |
484 EXPECT_EQ(0, global->offset); | 508 EXPECT_EQ(0, global->offset); |
485 EXPECT_EQ(false, global->exported); | 509 EXPECT_FALSE(global->exported); |
486 | 510 |
487 WasmFunction* function = &result.val->functions->back(); | 511 WasmFunction* function = &result.val->functions->back(); |
488 | 512 |
489 EXPECT_EQ(9, function->name_offset); | 513 EXPECT_EQ(9, function->name_offset); |
490 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 514 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
491 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 515 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
492 | 516 |
493 EXPECT_EQ(false, function->exported); | 517 EXPECT_FALSE(function->exported); |
494 EXPECT_EQ(false, function->external); | 518 EXPECT_FALSE(function->external); |
495 | 519 |
496 WasmDataSegment* segment = &result.val->data_segments->back(); | 520 WasmDataSegment* segment = &result.val->data_segments->back(); |
497 | 521 |
498 EXPECT_EQ(0x8b3ae, segment->dest_addr); | 522 EXPECT_EQ(0x8b3ae, segment->dest_addr); |
499 EXPECT_EQ(15, segment->source_offset); | 523 EXPECT_EQ(15, segment->source_offset); |
500 EXPECT_EQ(5, segment->source_size); | 524 EXPECT_EQ(5, segment->source_size); |
501 EXPECT_EQ(true, segment->init); | 525 EXPECT_TRUE(segment->init); |
| 526 |
| 527 if (result.val) delete result.val; |
502 } | 528 } |
503 } | 529 } |
504 | 530 |
505 | 531 |
506 TEST_F(WasmModuleVerifyTest, OneDataSegment) { | 532 TEST_F(WasmModuleVerifyTest, OneDataSegment) { |
507 const byte data[] = { | 533 const byte data[] = { |
508 kDeclDataSegments, | 534 kDeclDataSegments, |
509 1, | 535 1, |
510 0xaa, | 536 0xaa, |
511 0xbb, | 537 0xbb, |
(...skipping 15 matching lines...) Expand all Loading... |
527 EXPECT_TRUE(result.ok()); | 553 EXPECT_TRUE(result.ok()); |
528 EXPECT_EQ(0, result.val->globals->size()); | 554 EXPECT_EQ(0, result.val->globals->size()); |
529 EXPECT_EQ(0, result.val->functions->size()); | 555 EXPECT_EQ(0, result.val->functions->size()); |
530 EXPECT_EQ(1, result.val->data_segments->size()); | 556 EXPECT_EQ(1, result.val->data_segments->size()); |
531 | 557 |
532 WasmDataSegment* segment = &result.val->data_segments->back(); | 558 WasmDataSegment* segment = &result.val->data_segments->back(); |
533 | 559 |
534 EXPECT_EQ(0x9bbaa, segment->dest_addr); | 560 EXPECT_EQ(0x9bbaa, segment->dest_addr); |
535 EXPECT_EQ(11, segment->source_offset); | 561 EXPECT_EQ(11, segment->source_offset); |
536 EXPECT_EQ(3, segment->source_size); | 562 EXPECT_EQ(3, segment->source_size); |
537 EXPECT_EQ(true, segment->init); | 563 EXPECT_TRUE(segment->init); |
| 564 |
| 565 if (result.val) delete result.val; |
538 } | 566 } |
539 | 567 |
540 for (size_t size = 1; size < arraysize(data); size++) { | 568 for (size_t size = 1; size < arraysize(data); size++) { |
541 // Should fall off end of module bytes. | 569 // Should fall off end of module bytes. |
542 ModuleResult result = DecodeModule(data, data + size); | 570 ModuleResult result = DecodeModule(data, data + size); |
543 EXPECT_FALSE(result.ok()); | 571 EXPECT_FALSE(result.ok()); |
| 572 if (result.val) delete result.val; |
544 } | 573 } |
545 } | 574 } |
546 | 575 |
547 | 576 |
548 TEST_F(WasmModuleVerifyTest, TwoDataSegments) { | 577 TEST_F(WasmModuleVerifyTest, TwoDataSegments) { |
549 const byte data[] = { | 578 const byte data[] = { |
550 kDeclDataSegments, | 579 kDeclDataSegments, |
551 2, | 580 2, |
552 0xee, | 581 0xee, |
553 0xff, | 582 0xff, |
(...skipping 29 matching lines...) Expand all Loading... |
583 EXPECT_EQ(0, result.val->globals->size()); | 612 EXPECT_EQ(0, result.val->globals->size()); |
584 EXPECT_EQ(0, result.val->functions->size()); | 613 EXPECT_EQ(0, result.val->functions->size()); |
585 EXPECT_EQ(2, result.val->data_segments->size()); | 614 EXPECT_EQ(2, result.val->data_segments->size()); |
586 | 615 |
587 WasmDataSegment* s0 = &result.val->data_segments->at(0); | 616 WasmDataSegment* s0 = &result.val->data_segments->at(0); |
588 WasmDataSegment* s1 = &result.val->data_segments->at(1); | 617 WasmDataSegment* s1 = &result.val->data_segments->at(1); |
589 | 618 |
590 EXPECT_EQ(0x7ffee, s0->dest_addr); | 619 EXPECT_EQ(0x7ffee, s0->dest_addr); |
591 EXPECT_EQ(9, s0->source_offset); | 620 EXPECT_EQ(9, s0->source_offset); |
592 EXPECT_EQ(4, s0->source_size); | 621 EXPECT_EQ(4, s0->source_size); |
593 EXPECT_EQ(false, s0->init); | 622 EXPECT_FALSE(s0->init); |
594 | 623 |
595 EXPECT_EQ(0x6ddcc, s1->dest_addr); | 624 EXPECT_EQ(0x6ddcc, s1->dest_addr); |
596 EXPECT_EQ(6, s1->source_offset); | 625 EXPECT_EQ(6, s1->source_offset); |
597 EXPECT_EQ(10, s1->source_size); | 626 EXPECT_EQ(10, s1->source_size); |
598 EXPECT_EQ(true, s1->init); | 627 EXPECT_TRUE(s1->init); |
| 628 |
| 629 if (result.val) delete result.val; |
599 } | 630 } |
600 | 631 |
601 for (size_t size = 1; size < arraysize(data); size++) { | 632 for (size_t size = 1; size < arraysize(data); size++) { |
602 // Should fall off end of module bytes. | 633 // Should fall off end of module bytes. |
603 ModuleResult result = DecodeModule(data, data + size); | 634 ModuleResult result = DecodeModule(data, data + size); |
604 EXPECT_FALSE(result.ok()); | 635 EXPECT_FALSE(result.ok()); |
| 636 if (result.val) delete result.val; |
605 } | 637 } |
606 } | 638 } |
607 | 639 |
608 | 640 |
609 // To make below tests for indirect calls much shorter. | 641 // To make below tests for indirect calls much shorter. |
610 #define FUNCTION(sig_index, external) \ | 642 #define FUNCTION(sig_index, external) \ |
611 kDeclFunctionImport, static_cast<byte>(sig_index), \ | 643 kDeclFunctionImport, static_cast<byte>(sig_index), \ |
612 static_cast<byte>(sig_index >> 8) | 644 static_cast<byte>(sig_index >> 8) |
613 | 645 |
614 | 646 |
615 TEST_F(WasmModuleVerifyTest, OneIndirectFunction) { | 647 TEST_F(WasmModuleVerifyTest, OneIndirectFunction) { |
616 static const byte data[] = { | 648 static const byte data[] = { |
617 // sig#0 ------------------------------------------------------- | 649 // sig#0 ------------------------------------------------------- |
618 kDeclSignatures, 1, 0, 0, // void -> void | 650 kDeclSignatures, 1, 0, 0, // void -> void |
619 // func#0 ------------------------------------------------------ | 651 // func#0 ------------------------------------------------------ |
620 kDeclFunctions, 1, FUNCTION(0, 0), | 652 kDeclFunctions, 1, FUNCTION(0, 0), |
621 // indirect table ---------------------------------------------- | 653 // indirect table ---------------------------------------------- |
622 kDeclFunctionTable, 1, 0, 0}; | 654 kDeclFunctionTable, 1, 0, 0}; |
623 | 655 |
624 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 656 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
625 EXPECT_TRUE(result.ok()); | 657 EXPECT_TRUE(result.ok()); |
626 if (result.ok()) { | 658 if (result.ok()) { |
627 EXPECT_EQ(1, result.val->signatures->size()); | 659 EXPECT_EQ(1, result.val->signatures->size()); |
628 EXPECT_EQ(1, result.val->functions->size()); | 660 EXPECT_EQ(1, result.val->functions->size()); |
629 EXPECT_EQ(1, result.val->function_table->size()); | 661 EXPECT_EQ(1, result.val->function_table->size()); |
630 EXPECT_EQ(0, result.val->function_table->at(0)); | 662 EXPECT_EQ(0, result.val->function_table->at(0)); |
631 } | 663 } |
| 664 if (result.val) delete result.val; |
632 } | 665 } |
633 | 666 |
634 | 667 |
635 TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) { | 668 TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) { |
636 static const byte data[] = { | 669 static const byte data[] = { |
637 // sig#0 ------------------------------------------------------- | 670 // sig#0 ------------------------------------------------------- |
638 kDeclSignatures, 2, 0, 0, // void -> void | 671 kDeclSignatures, 2, 0, 0, // void -> void |
639 0, kLocalI32, // void -> i32 | 672 0, kLocalI32, // void -> i32 |
640 // func#0 ------------------------------------------------------ | 673 // func#0 ------------------------------------------------------ |
641 kDeclFunctions, 4, FUNCTION(0, 1), FUNCTION(1, 1), FUNCTION(0, 1), | 674 kDeclFunctions, 4, FUNCTION(0, 1), FUNCTION(1, 1), FUNCTION(0, 1), |
642 FUNCTION(1, 1), | 675 FUNCTION(1, 1), |
643 // indirect table ---------------------------------------------- | 676 // indirect table ---------------------------------------------- |
644 kDeclFunctionTable, 8, 0, 0, 1, 0, 2, 0, 3, 0, 0, 0, 1, 0, 2, 0, 3, 0, | 677 kDeclFunctionTable, 8, 0, 0, 1, 0, 2, 0, 3, 0, 0, 0, 1, 0, 2, 0, 3, 0, |
645 }; | 678 }; |
646 | 679 |
647 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 680 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
648 EXPECT_TRUE(result.ok()); | 681 EXPECT_TRUE(result.ok()); |
649 if (result.ok()) { | 682 if (result.ok()) { |
650 EXPECT_EQ(2, result.val->signatures->size()); | 683 EXPECT_EQ(2, result.val->signatures->size()); |
651 EXPECT_EQ(4, result.val->functions->size()); | 684 EXPECT_EQ(4, result.val->functions->size()); |
652 EXPECT_EQ(8, result.val->function_table->size()); | 685 EXPECT_EQ(8, result.val->function_table->size()); |
653 for (int i = 0; i < 8; i++) { | 686 for (int i = 0; i < 8; i++) { |
654 EXPECT_EQ(i & 3, result.val->function_table->at(i)); | 687 EXPECT_EQ(i & 3, result.val->function_table->at(i)); |
655 } | 688 } |
656 } | 689 } |
| 690 if (result.val) delete result.val; |
657 } | 691 } |
658 | 692 |
659 | 693 |
660 TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) { | 694 TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) { |
661 static const byte data[] = { | 695 static const byte data[] = { |
662 // sig#0 ------------------------------------------------------- | 696 // sig#0 ------------------------------------------------------- |
663 kDeclSignatures, 1, 0, 0, // void -> void | 697 kDeclSignatures, 1, 0, 0, // void -> void |
664 // indirect table ---------------------------------------------- | 698 // indirect table ---------------------------------------------- |
665 kDeclFunctionTable, 1, 0, 0, | 699 kDeclFunctionTable, 1, 0, 0, |
666 }; | 700 }; |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 EXPECT_EQ(0, function->name_offset); | 868 EXPECT_EQ(0, function->name_offset); |
835 EXPECT_EQ(arraysize(data) - 1, function->code_start_offset); | 869 EXPECT_EQ(arraysize(data) - 1, function->code_start_offset); |
836 EXPECT_EQ(arraysize(data), function->code_end_offset); | 870 EXPECT_EQ(arraysize(data), function->code_end_offset); |
837 EXPECT_EQ(3, function->local_int32_count); | 871 EXPECT_EQ(3, function->local_int32_count); |
838 EXPECT_EQ(4, function->local_int64_count); | 872 EXPECT_EQ(4, function->local_int64_count); |
839 EXPECT_EQ(5, function->local_float32_count); | 873 EXPECT_EQ(5, function->local_float32_count); |
840 EXPECT_EQ(6, function->local_float64_count); | 874 EXPECT_EQ(6, function->local_float64_count); |
841 EXPECT_FALSE(function->external); | 875 EXPECT_FALSE(function->external); |
842 EXPECT_FALSE(function->exported); | 876 EXPECT_FALSE(function->exported); |
843 } | 877 } |
| 878 |
| 879 if (result.val) delete result.val; |
844 } | 880 } |
845 } // namespace wasm | 881 } // namespace wasm |
846 } // namespace internal | 882 } // namespace internal |
847 } // namespace v8 | 883 } // namespace v8 |
OLD | NEW |