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 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
92 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { | 92 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { |
93 static const byte data[] = {kDeclEnd}; | 93 static const byte data[] = {kDeclEnd}; |
94 EXPECT_VERIFIES(data); | 94 EXPECT_VERIFIES(data); |
95 } | 95 } |
96 | 96 |
97 TEST_F(WasmModuleVerifyTest, OneGlobal) { | 97 TEST_F(WasmModuleVerifyTest, OneGlobal) { |
98 static const byte data[] = { | 98 static const byte data[] = { |
99 kDeclGlobals, | 99 kDeclGlobals, |
100 1, | 100 1, |
101 NAME_OFFSET(0), | 101 NAME_LENGTH(1), 'g', // name |
102 kMemI32, // memory type | 102 kMemI32, // memory type |
103 0, // exported | 103 0, // exported |
104 }; | 104 }; |
105 | 105 |
106 { | 106 { |
107 // Should decode to exactly one global. | 107 // Should decode to exactly one global. |
108 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 108 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
109 EXPECT_TRUE(result.ok()); | 109 EXPECT_TRUE(result.ok()); |
110 EXPECT_EQ(1, result.val->globals.size()); | 110 EXPECT_EQ(1, result.val->globals.size()); |
111 EXPECT_EQ(0, result.val->functions.size()); | 111 EXPECT_EQ(0, result.val->functions.size()); |
112 EXPECT_EQ(0, result.val->data_segments.size()); | 112 EXPECT_EQ(0, result.val->data_segments.size()); |
113 | 113 |
114 WasmGlobal* global = &result.val->globals.back(); | 114 WasmGlobal* global = &result.val->globals.back(); |
115 | 115 |
116 EXPECT_EQ(0, global->name_offset); | 116 EXPECT_EQ(1, global->name_length); |
117 EXPECT_EQ(MachineType::Int32(), global->type); | 117 EXPECT_EQ(MachineType::Int32(), global->type); |
118 EXPECT_EQ(0, global->offset); | 118 EXPECT_EQ(0, global->offset); |
119 EXPECT_FALSE(global->exported); | 119 EXPECT_FALSE(global->exported); |
120 | 120 |
121 if (result.val) delete result.val; | 121 if (result.val) delete result.val; |
122 } | 122 } |
123 | 123 |
124 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); | 124 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
125 } | 125 } |
126 | 126 |
(...skipping 18 matching lines...) Expand all Loading... |
145 } else { | 145 } else { |
146 buffer.push_back(static_cast<byte>(out)); | 146 buffer.push_back(static_cast<byte>(out)); |
147 break; | 147 break; |
148 } | 148 } |
149 } | 149 } |
150 } | 150 } |
151 | 151 |
152 | 152 |
153 TEST_F(WasmModuleVerifyTest, NGlobals) { | 153 TEST_F(WasmModuleVerifyTest, NGlobals) { |
154 static const byte data[] = { | 154 static const byte data[] = { |
155 NAME_OFFSET(0), // name offset | 155 NO_NAME, // name length |
156 kMemI32, // memory type | 156 kMemI32, // memory type |
157 0, // exported | 157 0, // exported |
158 }; | 158 }; |
159 | 159 |
160 for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) { | 160 for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) { |
161 std::vector<byte> buffer; | 161 std::vector<byte> buffer; |
162 buffer.push_back(kDeclGlobals); | 162 buffer.push_back(kDeclGlobals); |
163 AppendUint32v(buffer, i); | 163 AppendUint32v(buffer, i); |
164 for (uint32_t j = 0; j < i; j++) { | 164 for (uint32_t j = 0; j < i; j++) { |
165 buffer.insert(buffer.end(), data, data + sizeof(data)); | 165 buffer.insert(buffer.end(), data, data + sizeof(data)); |
166 } | 166 } |
167 | 167 |
168 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); | 168 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); |
169 EXPECT_TRUE(result.ok()); | 169 EXPECT_TRUE(result.ok()); |
170 if (result.val) delete result.val; | 170 if (result.val) delete result.val; |
171 } | 171 } |
172 } | 172 } |
173 | 173 |
174 | 174 |
175 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameOffset) { | |
176 static const byte data[] = { | |
177 kDeclGlobals, | |
178 1, // declare one global | |
179 NAME_OFFSET(300), // name offset | |
180 kMemI32, // memory type | |
181 0, // exported | |
182 }; | |
183 | |
184 EXPECT_FAILURE(data); | |
185 } | |
186 | |
187 | |
188 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) { | 175 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) { |
189 static const byte data[] = { | 176 static const byte data[] = { |
190 kDeclGlobals, | 177 kDeclGlobals, |
191 1, // declare one global | 178 1, // declare one global |
192 NAME_OFFSET(0), // name offset | 179 NO_NAME, // name length |
193 33, // memory type | 180 33, // memory type |
194 0, // exported | 181 0, // exported |
195 }; | 182 }; |
196 | 183 |
197 EXPECT_FAILURE(data); | 184 EXPECT_FAILURE(data); |
198 } | 185 } |
199 | 186 |
200 | 187 |
201 TEST_F(WasmModuleVerifyTest, TwoGlobals) { | 188 TEST_F(WasmModuleVerifyTest, TwoGlobals) { |
202 static const byte data[] = { | 189 static const byte data[] = { |
203 kDeclGlobals, | 190 kDeclGlobals, |
204 2, | 191 2, |
205 NAME_OFFSET(0), // #0: name offset | 192 NO_NAME, // #0: name length |
206 kMemF32, // memory type | 193 kMemF32, // memory type |
207 0, // exported | 194 0, // exported |
208 NAME_OFFSET(0), // #1: name offset | 195 NO_NAME, // #1: name length |
209 kMemF64, // memory type | 196 kMemF64, // memory type |
210 1, // exported | 197 1, // exported |
211 }; | 198 }; |
212 | 199 |
213 { | 200 { |
214 // Should decode to exactly two globals. | 201 // Should decode to exactly two globals. |
215 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 202 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
216 EXPECT_TRUE(result.ok()); | 203 EXPECT_TRUE(result.ok()); |
217 EXPECT_EQ(2, result.val->globals.size()); | 204 EXPECT_EQ(2, result.val->globals.size()); |
218 EXPECT_EQ(0, result.val->functions.size()); | 205 EXPECT_EQ(0, result.val->functions.size()); |
219 EXPECT_EQ(0, result.val->data_segments.size()); | 206 EXPECT_EQ(0, result.val->data_segments.size()); |
220 | 207 |
221 WasmGlobal* g0 = &result.val->globals[0]; | 208 WasmGlobal* g0 = &result.val->globals[0]; |
222 WasmGlobal* g1 = &result.val->globals[1]; | 209 WasmGlobal* g1 = &result.val->globals[1]; |
223 | 210 |
224 EXPECT_EQ(0, g0->name_offset); | 211 EXPECT_EQ(0, g0->name_length); |
225 EXPECT_EQ(MachineType::Float32(), g0->type); | 212 EXPECT_EQ(MachineType::Float32(), g0->type); |
226 EXPECT_EQ(0, g0->offset); | 213 EXPECT_EQ(0, g0->offset); |
227 EXPECT_FALSE(g0->exported); | 214 EXPECT_FALSE(g0->exported); |
228 | 215 |
229 EXPECT_EQ(0, g1->name_offset); | 216 EXPECT_EQ(0, g1->name_length); |
230 EXPECT_EQ(MachineType::Float64(), g1->type); | 217 EXPECT_EQ(MachineType::Float64(), g1->type); |
231 EXPECT_EQ(0, g1->offset); | 218 EXPECT_EQ(0, g1->offset); |
232 EXPECT_TRUE(g1->exported); | 219 EXPECT_TRUE(g1->exported); |
233 | 220 |
234 if (result.val) delete result.val; | 221 if (result.val) delete result.val; |
235 } | 222 } |
236 | 223 |
237 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); | 224 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
238 } | 225 } |
239 | 226 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 if (result.val) delete result.val; | 268 if (result.val) delete result.val; |
282 | 269 |
283 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); | 270 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
284 } | 271 } |
285 | 272 |
286 | 273 |
287 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { | 274 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { |
288 static const byte data[] = { | 275 static const byte data[] = { |
289 kDeclFunctions, 1, | 276 kDeclFunctions, 1, |
290 // func#0 ------------------------------------------------------ | 277 // func#0 ------------------------------------------------------ |
291 SIG_INDEX(0), // signature index | 278 SIG_INDEX(0), // signature index |
292 NAME_OFFSET(0), // name offset | 279 NO_NAME, // name length |
293 U32_LE(0), // code start offset | 280 U32_LE(0), // code start offset |
294 U32_LE(0), // code end offset | 281 U32_LE(0), // code end offset |
295 U16_LE(899), // local int32 count | 282 U16_LE(899), // local int32 count |
296 U16_LE(799), // local int64 count | 283 U16_LE(799), // local int64 count |
297 U16_LE(699), // local float32 count | 284 U16_LE(699), // local float32 count |
298 U16_LE(599), // local float64 count | 285 U16_LE(599), // local float64 count |
299 0, // exported | 286 0, // exported |
300 1 // external | 287 1 // external |
301 }; | 288 }; |
302 | 289 |
303 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 290 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
304 EXPECT_FALSE(result.ok()); | 291 EXPECT_FALSE(result.ok()); |
305 if (result.val) delete result.val; | 292 if (result.val) delete result.val; |
306 } | 293 } |
307 | 294 |
308 | 295 |
309 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { | 296 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { |
310 const int kCodeStartOffset = 31; | 297 const int kCodeStartOffset = 30; |
311 const int kCodeEndOffset = kCodeStartOffset + 1; | 298 const int kCodeEndOffset = kCodeStartOffset + 1; |
312 | 299 |
313 static const byte data[] = { | 300 static const byte data[] = { |
314 kDeclSignatures, 1, | 301 kDeclSignatures, 1, |
315 // sig#0 ------------------------------------------------------- | 302 // sig#0 ------------------------------------------------------- |
316 VOID_VOID_SIG, | 303 VOID_VOID_SIG, |
317 // func#0 ------------------------------------------------------ | 304 // func#0 ------------------------------------------------------ |
318 kDeclFunctions, 1, | 305 kDeclFunctions, 1, |
319 kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName, | 306 kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName, |
320 SIG_INDEX(0), // signature index | 307 SIG_INDEX(0), // signature index |
321 NAME_OFFSET(9), // name offset | 308 NAME_LENGTH(2), 'h', 'i', // name |
322 U16_LE(1466), // local int32 count | 309 U16_LE(1466), // local int32 count |
323 U16_LE(1355), // local int64 count | 310 U16_LE(1355), // local int64 count |
324 U16_LE(1244), // local float32 count | 311 U16_LE(1244), // local float32 count |
325 U16_LE(1133), // local float64 count | 312 U16_LE(1133), // local float64 count |
326 1, 0, // size | 313 1, 0, // size |
327 kExprNop, | 314 kExprNop, |
328 }; | 315 }; |
329 | 316 |
330 { | 317 { |
331 // Should decode to exactly one function. | 318 // Should decode to exactly one function. |
332 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 319 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
333 EXPECT_TRUE(result.ok()); | 320 EXPECT_TRUE(result.ok()); |
334 EXPECT_EQ(0, result.val->globals.size()); | 321 EXPECT_EQ(0, result.val->globals.size()); |
335 EXPECT_EQ(1, result.val->signatures.size()); | 322 EXPECT_EQ(1, result.val->signatures.size()); |
336 EXPECT_EQ(1, result.val->functions.size()); | 323 EXPECT_EQ(1, result.val->functions.size()); |
337 EXPECT_EQ(0, result.val->data_segments.size()); | 324 EXPECT_EQ(0, result.val->data_segments.size()); |
338 EXPECT_EQ(0, result.val->function_table.size()); | 325 EXPECT_EQ(0, result.val->function_table.size()); |
339 | 326 |
340 WasmFunction* function = &result.val->functions.back(); | 327 WasmFunction* function = &result.val->functions.back(); |
341 | 328 |
342 EXPECT_EQ(9, function->name_offset); | 329 EXPECT_EQ(18, function->name_offset); |
| 330 EXPECT_EQ(2, function->name_length); |
343 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 331 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
344 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 332 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
345 | 333 |
346 EXPECT_EQ(1466, function->local_i32_count); | 334 EXPECT_EQ(1466, function->local_i32_count); |
347 EXPECT_EQ(1355, function->local_i64_count); | 335 EXPECT_EQ(1355, function->local_i64_count); |
348 EXPECT_EQ(1244, function->local_f32_count); | 336 EXPECT_EQ(1244, function->local_f32_count); |
349 EXPECT_EQ(1133, function->local_f64_count); | 337 EXPECT_EQ(1133, function->local_f64_count); |
350 | 338 |
351 EXPECT_TRUE(function->exported); | 339 EXPECT_TRUE(function->exported); |
352 EXPECT_FALSE(function->external); | 340 EXPECT_FALSE(function->external); |
(...skipping 13 matching lines...) Expand all Loading... |
366 // func#0 ------------------------------------------------------ | 354 // func#0 ------------------------------------------------------ |
367 kDeclFunctionImport, // no name, no locals, imported | 355 kDeclFunctionImport, // no name, no locals, imported |
368 SIG_INDEX(0), | 356 SIG_INDEX(0), |
369 }; | 357 }; |
370 | 358 |
371 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 359 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
372 EXPECT_TRUE(result.ok()); | 360 EXPECT_TRUE(result.ok()); |
373 EXPECT_EQ(1, result.val->functions.size()); | 361 EXPECT_EQ(1, result.val->functions.size()); |
374 WasmFunction* function = &result.val->functions.back(); | 362 WasmFunction* function = &result.val->functions.back(); |
375 | 363 |
376 EXPECT_EQ(0, function->name_offset); | 364 EXPECT_EQ(0, function->name_length); |
377 EXPECT_EQ(0, function->code_start_offset); | 365 EXPECT_EQ(0, function->code_start_offset); |
378 EXPECT_EQ(0, function->code_end_offset); | 366 EXPECT_EQ(0, function->code_end_offset); |
379 | 367 |
380 EXPECT_EQ(0, function->local_i32_count); | 368 EXPECT_EQ(0, function->local_i32_count); |
381 EXPECT_EQ(0, function->local_i64_count); | 369 EXPECT_EQ(0, function->local_i64_count); |
382 EXPECT_EQ(0, function->local_f32_count); | 370 EXPECT_EQ(0, function->local_f32_count); |
383 EXPECT_EQ(0, function->local_f64_count); | 371 EXPECT_EQ(0, function->local_f64_count); |
384 | 372 |
385 EXPECT_FALSE(function->exported); | 373 EXPECT_FALSE(function->exported); |
386 EXPECT_TRUE(function->external); | 374 EXPECT_TRUE(function->external); |
(...skipping 15 matching lines...) Expand all Loading... |
402 0, 0, // signature index | 390 0, 0, // signature index |
403 1, 0, // body size | 391 1, 0, // body size |
404 kExprNop // body | 392 kExprNop // body |
405 }; | 393 }; |
406 | 394 |
407 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 395 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
408 EXPECT_TRUE(result.ok()); | 396 EXPECT_TRUE(result.ok()); |
409 EXPECT_EQ(1, result.val->functions.size()); | 397 EXPECT_EQ(1, result.val->functions.size()); |
410 WasmFunction* function = &result.val->functions.back(); | 398 WasmFunction* function = &result.val->functions.back(); |
411 | 399 |
412 EXPECT_EQ(0, function->name_offset); | 400 EXPECT_EQ(0, function->name_length); |
413 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 401 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
414 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 402 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
415 | 403 |
416 EXPECT_EQ(0, function->local_i32_count); | 404 EXPECT_EQ(0, function->local_i32_count); |
417 EXPECT_EQ(0, function->local_i64_count); | 405 EXPECT_EQ(0, function->local_i64_count); |
418 EXPECT_EQ(0, function->local_f32_count); | 406 EXPECT_EQ(0, function->local_f32_count); |
419 EXPECT_EQ(0, function->local_f64_count); | 407 EXPECT_EQ(0, function->local_f64_count); |
420 | 408 |
421 EXPECT_FALSE(function->exported); | 409 EXPECT_FALSE(function->exported); |
422 EXPECT_FALSE(function->external); | 410 EXPECT_FALSE(function->external); |
(...skipping 19 matching lines...) Expand all Loading... |
442 7, 8, // local float64 count | 430 7, 8, // local float64 count |
443 1, 0, // body size | 431 1, 0, // body size |
444 kExprNop // body | 432 kExprNop // body |
445 }; | 433 }; |
446 | 434 |
447 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 435 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
448 EXPECT_TRUE(result.ok()); | 436 EXPECT_TRUE(result.ok()); |
449 EXPECT_EQ(1, result.val->functions.size()); | 437 EXPECT_EQ(1, result.val->functions.size()); |
450 WasmFunction* function = &result.val->functions.back(); | 438 WasmFunction* function = &result.val->functions.back(); |
451 | 439 |
452 EXPECT_EQ(0, function->name_offset); | 440 EXPECT_EQ(0, function->name_length); |
453 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 441 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
454 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 442 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
455 | 443 |
456 EXPECT_EQ(513, function->local_i32_count); | 444 EXPECT_EQ(513, function->local_i32_count); |
457 EXPECT_EQ(1027, function->local_i64_count); | 445 EXPECT_EQ(1027, function->local_i64_count); |
458 EXPECT_EQ(1541, function->local_f32_count); | 446 EXPECT_EQ(1541, function->local_f32_count); |
459 EXPECT_EQ(2055, function->local_f64_count); | 447 EXPECT_EQ(2055, function->local_f64_count); |
460 | 448 |
461 EXPECT_FALSE(function->exported); | 449 EXPECT_FALSE(function->exported); |
462 EXPECT_FALSE(function->external); | 450 EXPECT_FALSE(function->external); |
463 | 451 |
464 if (result.val) delete result.val; | 452 if (result.val) delete result.val; |
465 } | 453 } |
466 | 454 |
467 | 455 |
468 TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) { | 456 TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) { |
469 static const byte kDeclMemorySize = 4; | 457 static const byte kCodeStartOffset = 8 + 4 + 5 + 4 + 18; |
470 static const byte kCodeStartOffset = | |
471 8 + 2 + kDeclMemorySize + kDeclGlobalSize + 4 + 2 + 17; | |
472 static const byte kCodeEndOffset = kCodeStartOffset + 3; | 458 static const byte kCodeEndOffset = kCodeStartOffset + 3; |
473 | 459 |
474 static const byte data[] = { | 460 static const byte data[] = { |
475 kDeclMemory, 28, 28, 1, | 461 kDeclMemory, 28, 28, 1, |
476 // global#0 -------------------------------------------------- | 462 // global#0 -------------------------------------------------- |
477 kDeclGlobals, 1, 0, 0, 0, 0, // name offset | 463 kDeclGlobals, 1, 0, // name length |
478 kMemU8, // memory type | 464 kMemU8, // memory type |
479 0, // exported | 465 0, // exported |
480 // sig#0 ----------------------------------------------------- | 466 // sig#0 ----------------------------------------------------- |
481 kDeclSignatures, 1, 0, 0, // void -> void | 467 kDeclSignatures, 1, 0, 0, // void -> void |
482 // func#0 ---------------------------------------------------- | 468 // func#0 ---------------------------------------------------- |
483 kDeclFunctions, 1, kDeclFunctionLocals | kDeclFunctionName, 0, | 469 kDeclFunctions, 1, kDeclFunctionLocals | kDeclFunctionName, 0, |
484 0, // signature index | 470 0, // signature index |
485 9, 0, 0, 0, // name offset | 471 2, 'h', 'i', // name |
486 1, 2, // local int32 count | 472 1, 2, // local int32 count |
487 3, 4, // local int64 count | 473 3, 4, // local int64 count |
488 5, 6, // local float32 count | 474 5, 6, // local float32 count |
489 7, 8, // local float64 count | 475 7, 8, // local float64 count |
490 3, 0, // body size | 476 3, 0, // body size |
491 kExprNop, // func#0 body | 477 kExprNop, // func#0 body |
492 kExprNop, // func#0 body | 478 kExprNop, // func#0 body |
493 kExprNop, // func#0 body | 479 kExprNop, // func#0 body |
494 // segment#0 ------------------------------------------------- | 480 // segment#0 ------------------------------------------------- |
495 kDeclDataSegments, 1, 0xae, 0xb3, 0x08, 0, // dest addr | 481 kDeclDataSegments, 1, 0xae, 0xb3, 0x08, 0, // dest addr |
496 15, 0, 0, 0, // source offset | 482 15, 0, 0, 0, // source offset |
497 5, 0, 0, 0, // source size | 483 5, 0, 0, 0, // source size |
498 1, // init | 484 1, // init |
499 // rest ------------------------------------------------------ | 485 // rest ------------------------------------------------------ |
500 kDeclEnd, | 486 kDeclEnd, |
501 }; | 487 }; |
502 | 488 |
503 { | 489 { |
504 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 490 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
505 EXPECT_TRUE(result.ok()); | 491 EXPECT_TRUE(result.ok()); |
506 EXPECT_EQ(1, result.val->globals.size()); | 492 EXPECT_EQ(1, result.val->globals.size()); |
507 EXPECT_EQ(1, result.val->functions.size()); | 493 EXPECT_EQ(1, result.val->functions.size()); |
508 EXPECT_EQ(1, result.val->data_segments.size()); | 494 EXPECT_EQ(1, result.val->data_segments.size()); |
509 | 495 |
510 WasmGlobal* global = &result.val->globals.back(); | 496 WasmGlobal* global = &result.val->globals.back(); |
511 | 497 |
512 EXPECT_EQ(0, global->name_offset); | 498 EXPECT_EQ(0, global->name_length); |
513 EXPECT_EQ(MachineType::Uint8(), global->type); | 499 EXPECT_EQ(MachineType::Uint8(), global->type); |
514 EXPECT_EQ(0, global->offset); | 500 EXPECT_EQ(0, global->offset); |
515 EXPECT_FALSE(global->exported); | 501 EXPECT_FALSE(global->exported); |
516 | 502 |
517 WasmFunction* function = &result.val->functions.back(); | 503 WasmFunction* function = &result.val->functions.back(); |
518 | 504 |
519 EXPECT_EQ(9, function->name_offset); | 505 EXPECT_EQ(27, function->name_offset); |
| 506 EXPECT_EQ(2, function->name_length); |
520 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 507 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
521 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 508 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
522 | 509 |
523 EXPECT_FALSE(function->exported); | 510 EXPECT_FALSE(function->exported); |
524 EXPECT_FALSE(function->external); | 511 EXPECT_FALSE(function->external); |
525 | 512 |
526 WasmDataSegment* segment = &result.val->data_segments.back(); | 513 WasmDataSegment* segment = &result.val->data_segments.back(); |
527 | 514 |
528 EXPECT_EQ(0x8b3ae, segment->dest_addr); | 515 EXPECT_EQ(0x8b3ae, segment->dest_addr); |
529 EXPECT_EQ(15, segment->source_offset); | 516 EXPECT_EQ(15, segment->source_offset); |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1026 EXPECT_FAILURE(data); | 1013 EXPECT_FAILURE(data); |
1027 } | 1014 } |
1028 | 1015 |
1029 TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) { | 1016 TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) { |
1030 static const byte data[] = { | 1017 static const byte data[] = { |
1031 kMaxModuleSectionCode, | 1018 kMaxModuleSectionCode, |
1032 1, // LEB128 1 | 1019 1, // LEB128 1 |
1033 0, // one byte section | 1020 0, // one byte section |
1034 kDeclGlobals, | 1021 kDeclGlobals, |
1035 1, | 1022 1, |
1036 0, | 1023 0, // name length |
1037 0, | |
1038 0, | |
1039 0, // name offset | |
1040 kMemI32, // memory type | 1024 kMemI32, // memory type |
1041 0, // exported | 1025 0, // exported |
1042 }; | 1026 }; |
1043 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 1027 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
1044 EXPECT_TRUE(result.ok()); | 1028 EXPECT_TRUE(result.ok()); |
1045 | 1029 |
1046 EXPECT_EQ(1, result.val->globals.size()); | 1030 EXPECT_EQ(1, result.val->globals.size()); |
1047 EXPECT_EQ(0, result.val->functions.size()); | 1031 EXPECT_EQ(0, result.val->functions.size()); |
1048 EXPECT_EQ(0, result.val->data_segments.size()); | 1032 EXPECT_EQ(0, result.val->data_segments.size()); |
1049 | 1033 |
1050 WasmGlobal* global = &result.val->globals.back(); | 1034 WasmGlobal* global = &result.val->globals.back(); |
1051 | 1035 |
1052 EXPECT_EQ(0, global->name_offset); | 1036 EXPECT_EQ(0, global->name_length); |
1053 EXPECT_EQ(MachineType::Int32(), global->type); | 1037 EXPECT_EQ(MachineType::Int32(), global->type); |
1054 EXPECT_EQ(0, global->offset); | 1038 EXPECT_EQ(0, global->offset); |
1055 EXPECT_FALSE(global->exported); | 1039 EXPECT_FALSE(global->exported); |
1056 | 1040 |
1057 if (result.val) delete result.val; | 1041 if (result.val) delete result.val; |
1058 } | 1042 } |
1059 | 1043 |
1060 TEST_F(WasmModuleVerifyTest, ImportTable_empty) { | 1044 TEST_F(WasmModuleVerifyTest, ImportTable_empty) { |
1061 static const byte data[] = {kDeclSignatures, 0, kDeclImportTable, 0}; | 1045 static const byte data[] = {kDeclSignatures, 0, kDeclImportTable, 0}; |
1062 EXPECT_VERIFIES(data); | 1046 EXPECT_VERIFIES(data); |
1063 } | 1047 } |
1064 | 1048 |
1065 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs) { | 1049 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs) { |
1066 static const byte data[] = {kDeclImportTable, 0}; | 1050 static const byte data[] = {kDeclImportTable, 0}; |
1067 EXPECT_FAILURE(data); | 1051 EXPECT_FAILURE(data); |
1068 } | 1052 } |
1069 | 1053 |
1070 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { | 1054 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { |
1071 static const byte data[] = { | 1055 static const byte data[] = { |
1072 kDeclSignatures, 0, kDeclImportTable, 1, | 1056 kDeclSignatures, 0, kDeclImportTable, 1, |
1073 SIG_INDEX(0), // sig index | 1057 IMPORT_SIG_INDEX(0), // sig index |
1074 NAME_OFFSET(1), // module name | 1058 NAME_LENGTH(1), 'm', // module name |
1075 NAME_OFFSET(1), // function name | 1059 NAME_LENGTH(1), 'f', // function name |
1076 }; | 1060 }; |
1077 EXPECT_FAILURE(data); | 1061 EXPECT_FAILURE(data); |
1078 } | 1062 } |
1079 | 1063 |
1080 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { | 1064 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { |
1081 static const byte data[] = { | 1065 static const byte data[] = { |
1082 kDeclSignatures, | 1066 kDeclSignatures, |
1083 1, | 1067 1, |
1084 VOID_VOID_SIG, | 1068 VOID_VOID_SIG, |
1085 kDeclImportTable, | 1069 kDeclImportTable, |
1086 1, // -- | 1070 1, // -- |
1087 U32V_1(0), // sig index | 1071 IMPORT_SIG_INDEX(0), // sig index |
1088 NAME_OFFSET(1), // module name | 1072 NAME_LENGTH(1), 'm', // module name |
1089 NAME_OFFSET(1) // function name | 1073 NAME_LENGTH(1), 'f', // function name |
1090 }; | 1074 }; |
1091 EXPECT_VERIFIES(data); | 1075 EXPECT_VERIFIES(data); |
1092 } | 1076 } |
1093 | 1077 |
1094 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) { | 1078 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) { |
1095 static const byte data[] = { | 1079 static const byte data[] = { |
1096 kDeclSignatures, | 1080 kDeclSignatures, |
1097 1, | 1081 1, |
1098 VOID_VOID_SIG, | 1082 VOID_VOID_SIG, |
1099 kDeclImportTable, | 1083 kDeclImportTable, |
1100 1, // -- | 1084 1, // -- |
1101 SIG_INDEX(0), // sig index | 1085 IMPORT_SIG_INDEX(0), // sig index |
1102 NAME_OFFSET(0), // module name | 1086 NO_NAME, // module name |
1103 NAME_OFFSET(1) // function name | 1087 NAME_LENGTH(1), 'f' // function name |
1104 }; | 1088 }; |
1105 EXPECT_FAILURE(data); | 1089 EXPECT_FAILURE(data); |
1106 } | 1090 } |
1107 | 1091 |
1108 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { | 1092 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { |
1109 static const byte data[] = { | 1093 static const byte data[] = { |
1110 kDeclSignatures, 1, VOID_VOID_SIG, kDeclImportTable, 1, | 1094 kDeclSignatures, 1, VOID_VOID_SIG, kDeclImportTable, 1, |
1111 SIG_INDEX(0), // sig index | 1095 IMPORT_SIG_INDEX(0), // sig index |
1112 NAME_OFFSET(1), // module name | 1096 NAME_LENGTH(1), 'm', // module name |
1113 NAME_OFFSET(1), // function name | 1097 NAME_LENGTH(1), 'f', // function name |
1114 }; | 1098 }; |
1115 | 1099 |
1116 EXPECT_OFF_END_FAILURE(data, 5, sizeof(data)); | 1100 EXPECT_OFF_END_FAILURE(data, 5, sizeof(data)); |
1117 } | 1101 } |
1118 | 1102 |
1119 TEST_F(WasmModuleVerifyTest, ExportTable_empty) { | 1103 TEST_F(WasmModuleVerifyTest, ExportTable_empty) { |
1120 static const byte data[] = {kDeclSignatures, 0, kDeclFunctions, 0, | 1104 static const byte data[] = {kDeclSignatures, 0, kDeclFunctions, 0, |
1121 kDeclExportTable, 0}; | 1105 kDeclExportTable, 0}; |
1122 EXPECT_VERIFIES(data); | 1106 EXPECT_VERIFIES(data); |
1123 } | 1107 } |
1124 | 1108 |
1125 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { | 1109 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { |
1126 static const byte data[] = {kDeclSignatures, 0, kDeclExportTable, 0}; | 1110 static const byte data[] = {kDeclSignatures, 0, kDeclExportTable, 0}; |
1127 EXPECT_FAILURE(data); | 1111 EXPECT_FAILURE(data); |
1128 } | 1112 } |
1129 | 1113 |
1130 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { | 1114 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { |
1131 static const byte data[] = {kDeclExportTable, 0}; | 1115 static const byte data[] = {kDeclExportTable, 0}; |
1132 EXPECT_FAILURE(data); | 1116 EXPECT_FAILURE(data); |
1133 } | 1117 } |
1134 | 1118 |
1135 TEST_F(WasmModuleVerifyTest, ExportTableOne) { | 1119 TEST_F(WasmModuleVerifyTest, ExportTableOne) { |
1136 static const byte data[] = { | 1120 static const byte data[] = { |
1137 kDeclSignatures, 1, // sigs | 1121 kDeclSignatures, 1, // sigs |
1138 VOID_VOID_SIG, // -- | 1122 VOID_VOID_SIG, // -- |
1139 kDeclFunctions, 1, // functions | 1123 kDeclFunctions, 1, // functions |
1140 EMPTY_FUNCTION(0), // -- | 1124 EMPTY_FUNCTION(0), // -- |
1141 kDeclExportTable, 1, // exports | 1125 kDeclExportTable, 1, // exports |
1142 FUNC_INDEX(0), // -- | 1126 FUNC_INDEX(0), // -- |
1143 NAME_OFFSET(0) // -- | 1127 NO_NAME // -- |
1144 }; | 1128 }; |
1145 EXPECT_VERIFIES(data); | 1129 EXPECT_VERIFIES(data); |
1146 } | 1130 } |
1147 | 1131 |
1148 TEST_F(WasmModuleVerifyTest, ExportTableTwo) { | 1132 TEST_F(WasmModuleVerifyTest, ExportTableTwo) { |
1149 static const byte data[] = { | 1133 static const byte data[] = { |
1150 kDeclSignatures, 1, // sigs | 1134 kDeclSignatures, 1, // sigs |
1151 VOID_VOID_SIG, // -- | 1135 VOID_VOID_SIG, // -- |
1152 kDeclFunctions, 1, // functions | 1136 kDeclFunctions, 1, // functions |
1153 EMPTY_FUNCTION(0), // -- | 1137 EMPTY_FUNCTION(0), // -- |
1154 kDeclExportTable, 2, // exports | 1138 kDeclExportTable, 2, // exports |
1155 FUNC_INDEX(0), // -- | 1139 FUNC_INDEX(0), // -- |
1156 NAME_OFFSET(1), // -- | 1140 NAME_LENGTH(4), 'n', 'a', 'm', 'e', // -- |
1157 FUNC_INDEX(0), // -- | 1141 FUNC_INDEX(0), // -- |
1158 NAME_OFFSET(2) // -- | 1142 NAME_LENGTH(3), 'n', 'o', 'm' // -- |
1159 }; | 1143 }; |
1160 EXPECT_VERIFIES(data); | 1144 EXPECT_VERIFIES(data); |
1161 } | 1145 } |
1162 | 1146 |
1163 TEST_F(WasmModuleVerifyTest, ExportTableThree) { | 1147 TEST_F(WasmModuleVerifyTest, ExportTableThree) { |
1164 static const byte data[] = { | 1148 static const byte data[] = { |
1165 kDeclSignatures, 1, // sigs | 1149 kDeclSignatures, 1, // sigs |
1166 VOID_VOID_SIG, // -- | 1150 VOID_VOID_SIG, // -- |
1167 kDeclFunctions, 3, // functions | 1151 kDeclFunctions, 3, // functions |
1168 EMPTY_FUNCTION(0), // -- | 1152 EMPTY_FUNCTION(0), // -- |
1169 EMPTY_FUNCTION(0), // -- | 1153 EMPTY_FUNCTION(0), // -- |
1170 EMPTY_FUNCTION(0), // -- | 1154 EMPTY_FUNCTION(0), // -- |
1171 kDeclExportTable, 3, // exports | 1155 kDeclExportTable, 3, // exports |
1172 FUNC_INDEX(0), // -- | 1156 FUNC_INDEX(0), // -- |
1173 NAME_OFFSET(1), // -- | 1157 NAME_LENGTH(1), 'a', // -- |
1174 FUNC_INDEX(1), // -- | 1158 FUNC_INDEX(1), // -- |
1175 NAME_OFFSET(2), // -- | 1159 NAME_LENGTH(1), 'b', // -- |
1176 FUNC_INDEX(2), // -- | 1160 FUNC_INDEX(2), // -- |
1177 NAME_OFFSET(2) // -- | 1161 NAME_LENGTH(1), 'c' // -- |
1178 }; | 1162 }; |
1179 EXPECT_VERIFIES(data); | 1163 EXPECT_VERIFIES(data); |
1180 } | 1164 } |
1181 | 1165 |
1182 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) { | 1166 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) { |
1183 for (int i = 0; i < 6; i++) { | 1167 for (int i = 0; i < 6; i++) { |
1184 const byte data[] = { | 1168 const byte data[] = { |
1185 kDeclSignatures, 1, // sigs | 1169 kDeclSignatures, 1, // sigs |
1186 VOID_VOID_SIG, // -- | 1170 VOID_VOID_SIG, // -- |
1187 kDeclFunctions, 3, // functions | 1171 kDeclFunctions, 3, // functions |
1188 EMPTY_FUNCTION(0), // -- | 1172 EMPTY_FUNCTION(0), // -- |
1189 EMPTY_FUNCTION(0), // -- | 1173 EMPTY_FUNCTION(0), // -- |
1190 EMPTY_FUNCTION(0), // -- | 1174 EMPTY_FUNCTION(0), // -- |
1191 kDeclExportTable, 1, // exports | 1175 kDeclExportTable, 1, // exports |
1192 FUNC_INDEX(i), // -- | 1176 FUNC_INDEX(i), // -- |
1193 NAME_OFFSET(1) // -- | 1177 NAME_LENGTH(2), 'e', 'x', // -- |
1194 }; | 1178 }; |
1195 | 1179 |
1196 if (i < 3) { | 1180 if (i < 3) { |
1197 EXPECT_VERIFIES(data); | 1181 EXPECT_VERIFIES(data); |
1198 } else { | 1182 } else { |
1199 EXPECT_FAILURE(data); | 1183 EXPECT_FAILURE(data); |
1200 } | 1184 } |
1201 } | 1185 } |
1202 } | 1186 } |
1203 | 1187 |
1204 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) { | 1188 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) { |
1205 static const byte data[] = { | 1189 static const byte data[] = { |
1206 kDeclSignatures, 1, // sigs | 1190 kDeclSignatures, 1, // sigs |
1207 VOID_VOID_SIG, // -- | 1191 VOID_VOID_SIG, // -- |
1208 kDeclFunctions, 1, // functions | 1192 kDeclFunctions, 1, // functions |
1209 EMPTY_FUNCTION(0), // -- | 1193 EMPTY_FUNCTION(0), // -- |
1210 kDeclExportTable, 1, // exports | 1194 kDeclExportTable, 1, // exports |
1211 FUNC_INDEX(0), // -- | 1195 FUNC_INDEX(0), // -- |
1212 NAME_OFFSET(0) // -- | 1196 NO_NAME // -- |
1213 }; | 1197 }; |
1214 | 1198 |
1215 for (int length = 13; length < sizeof(data); length++) { | 1199 for (int length = 13; length < sizeof(data); length++) { |
1216 ModuleResult result = DecodeModule(data, data + length); | 1200 ModuleResult result = DecodeModule(data, data + length); |
1217 EXPECT_FALSE(result.ok()); | 1201 EXPECT_FALSE(result.ok()); |
1218 if (result.val) delete result.val; | 1202 if (result.val) delete result.val; |
1219 } | 1203 } |
1220 } | 1204 } |
1221 | 1205 |
1222 } // namespace wasm | 1206 } // namespace wasm |
1223 } // namespace internal | 1207 } // namespace internal |
1224 } // namespace v8 | 1208 } // namespace v8 |
OLD | NEW |