From f1666bd5eb86324867b50a9aa3d039832183f8d1 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 15 Nov 2018 10:20:12 -0800 Subject: Fix segment size validation for imported memories (#1745) Without this wasm-opt can't operation on emscripten-produced SIDE_MODULES's which have zero sized memory imports. Technically is not a validation failure if you have segments that are larger than your initial memory, regardless of whether you import them. For non-imported memories it can be helpful though, so leaving it in to catch those errors. --- src/wasm/wasm-validator.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/wasm/wasm-validator.cpp') diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index fbd31b920..769f17d59 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -974,9 +974,14 @@ static void validateMemory(Module& module, ValidationInfo& info) { if (curr.shared) info.shouldBeTrue(info.features & Feature::Atomics, "memory", "memory is shared, but atomics are disabled"); for (auto& segment : curr.segments) { if (!info.shouldBeEqual(segment.offset->type, i32, segment.offset, "segment offset should be i32")) continue; - info.shouldBeTrue(checkOffset(segment.offset, segment.data.size(), module.memory.initial * Memory::kPageSize), segment.offset, "segment offset should be reasonable"); + info.shouldBeTrue(checkOffset(segment.offset, segment.data.size(), curr.initial * Memory::kPageSize), segment.offset, "segment offset should be reasonable"); Index size = segment.data.size(); - info.shouldBeTrue(size <= curr.initial * Memory::kPageSize, segment.data.size(), "segment size should fit in memory (initial)"); + // If the memory is imported we don't actually know its initial size. + // Specifically wasm dll's import a zero sized memory which is perfectly + // valid. + if (!curr.imported()) { + info.shouldBeTrue(size <= curr.initial * Memory::kPageSize, segment.data.size(), "segment size should fit in memory (initial)"); + } if (segment.offset->is()) { Index start = segment.offset->cast()->value.geti32(); Index end = start + size; -- cgit v1.2.3