Skip to content

Commit 57699a9

Browse files
authored
Merge pull request #1621 from Shaikh-Ubaid/wasm_section_order
WASM: give an error message if sections are in wrong order
2 parents a1ba1a1 + ce9d03f commit 57699a9

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/libasr/codegen/wasm_decoder.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,41 +349,39 @@ class WASMDecoder {
349349
"Expected: 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00");
350350
}
351351
index += PREAMBLE_SIZE;
352+
uint32_t expected_min_section_id = 1;
352353
while (index < wasm_bytes.size()) {
353354
uint32_t section_id = read_u32(wasm_bytes, index);
354355
uint32_t section_size = read_u32(wasm_bytes, index);
356+
if (section_id < expected_min_section_id) {
357+
throw CodeGenError("DecodeWASM: Invalid sectionId, expected id >= "
358+
+ std::to_string(expected_min_section_id));
359+
}
360+
expected_min_section_id = section_id + 1;
355361
switch (section_id) {
356362
case 1U:
357363
decode_type_section(index);
358-
// exit(0);
359364
break;
360365
case 2U:
361366
decode_imports_section(index);
362-
// exit(0);
363367
break;
364368
case 3U:
365369
decode_function_section(index);
366-
// exit(0);
367370
break;
368371
case 5U:
369372
decode_memory_section(index);
370-
// exit(0);
371373
break;
372374
case 6U:
373375
decode_global_section(index);
374-
// exit(0);
375376
break;
376377
case 7U:
377378
decode_export_section(index);
378-
// exit(0);
379379
break;
380380
case 10U:
381381
decode_code_section(index);
382-
// exit(0)
383382
break;
384383
case 11U:
385384
decode_data_section(index);
386-
// exit(0)
387385
break;
388386
default:
389387
std::cout << "Unknown section id: " << section_id

0 commit comments

Comments
 (0)