From dca957e7958d47506cb95c60a103f8cb90394413 Mon Sep 17 00:00:00 2001 From: TimothyBanks Date: Fri, 17 Jul 2026 08:51:25 -0400 Subject: [PATCH] feat: Wasm Module Versioning --- src/libxrpl/tx/wasm/WasmiVM.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/libxrpl/tx/wasm/WasmiVM.cpp b/src/libxrpl/tx/wasm/WasmiVM.cpp index 24363e9d06..437c0fe7d7 100644 --- a/src/libxrpl/tx/wasm/WasmiVM.cpp +++ b/src/libxrpl/tx/wasm/WasmiVM.cpp @@ -819,7 +819,7 @@ readLEB128(Bytes const& wasmCode, size_t& offset) { auto result = uint32_t{}; auto shift = uint32_t{}; - while (true) + while (offset < wasmCode.size()) { auto byte = wasmCode[offset++]; result |= (byte & 0x7F) << shift; @@ -828,6 +828,10 @@ readLEB128(Bytes const& wasmCode, size_t& offset) break; } shift += 7; + if (shift >= 32) + { + break; + } } return result; } @@ -838,21 +842,43 @@ filterCustomSections(Bytes const& wasmCode, Filter&& filter) { auto offset = size_t{8}; // Skip Magic number and Version + if (wasmCode.size() <= offset) + { + // There is nothing to parse. + return; + } + while (offset < wasmCode.size()) { auto sectionId = wasmCode[offset++]; auto sectionSize = readLEB128(wasmCode, offset); auto nextSection = offset + sectionSize; + if (nextSection >= wasmCode.size()) + { + break; + } + if (sectionId == 0) { + // Wasm custom section marker. auto customSection = CustomSection{}; - auto size = readLEB128(wasmCode, offset); + + if (offset + size > wasmCode.size() || offset + size > nextSection) + { + return; + } + customSection.name = std::string_view{reinterpret_cast(wasmCode.data()) + offset, size}; offset += size; + if (offset >= nextSection) + { + break; + } + size = nextSection - offset; customSection.payload = std::span{ reinterpret_cast(wasmCode.data()) + offset, size};