From 2cda1960716f55986e1d46345bd8663a22ae2b6d Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 25 Mar 2023 07:59:55 +0530 Subject: [PATCH 1/2] WASMDecoder: throw error when sectionId < expected --- src/libasr/codegen/wasm_decoder.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libasr/codegen/wasm_decoder.h b/src/libasr/codegen/wasm_decoder.h index 9edb61080d..9b3ca475b6 100644 --- a/src/libasr/codegen/wasm_decoder.h +++ b/src/libasr/codegen/wasm_decoder.h @@ -349,9 +349,15 @@ class WASMDecoder { "Expected: 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00"); } index += PREAMBLE_SIZE; + uint32_t expected_min_section_id = 1; while (index < wasm_bytes.size()) { uint32_t section_id = read_u32(wasm_bytes, index); uint32_t section_size = read_u32(wasm_bytes, index); + if (section_id < expected_min_section_id) { + throw CodeGenError("DecodeWASM: Invalid sectionId, expected id >= " + + std::to_string(expected_min_section_id)); + } + expected_min_section_id = section_id + 1; switch (section_id) { case 1U: decode_type_section(index); From ce9d03fb3d71bb0d10556c92518c0624bb91d45f Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 25 Mar 2023 08:01:48 +0530 Subject: [PATCH 2/2] WASMDecoder: Remove unused commented code --- src/libasr/codegen/wasm_decoder.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/libasr/codegen/wasm_decoder.h b/src/libasr/codegen/wasm_decoder.h index 9b3ca475b6..2e5ea8bd6d 100644 --- a/src/libasr/codegen/wasm_decoder.h +++ b/src/libasr/codegen/wasm_decoder.h @@ -361,35 +361,27 @@ class WASMDecoder { switch (section_id) { case 1U: decode_type_section(index); - // exit(0); break; case 2U: decode_imports_section(index); - // exit(0); break; case 3U: decode_function_section(index); - // exit(0); break; case 5U: decode_memory_section(index); - // exit(0); break; case 6U: decode_global_section(index); - // exit(0); break; case 7U: decode_export_section(index); - // exit(0); break; case 10U: decode_code_section(index); - // exit(0) break; case 11U: decode_data_section(index); - // exit(0) break; default: std::cout << "Unknown section id: " << section_id