From 1653978aea69023a8bfe8743b1432a0aa6f78586 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sat, 13 Jan 2024 18:05:25 +0100 Subject: [PATCH] Fix memory error wasm --- lvtc/src/Wasm.hs | 14 +++++++++++--- lvtc/src/WasmUtils.hs | 37 ++++++++++++++++++++++--------------- lvtc/src/WatToWasm.hs | 9 +++++++-- lvtc/src/WriteWasm.hs | 14 ++++++++++---- 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/lvtc/src/Wasm.hs b/lvtc/src/Wasm.hs index 7719a37..7acf9c7 100644 --- a/lvtc/src/Wasm.hs +++ b/lvtc/src/Wasm.hs @@ -11,6 +11,7 @@ module Wasm , TypeSectionType (..) , TypeSection (..) , FunctionSection (..) + , MemorySectionLimits (..) , MemorySection (..) , ExportSectionExportType (..) , ExportSectionExport (..) @@ -56,13 +57,20 @@ data FunctionSection = } deriving (Show, Eq) +data MemorySectionLimits = + MSL { + hasMax :: Int, + minMS :: Int, + maxMS :: Int + } + deriving (Show, Eq) + data MemorySection = MS { headerMS :: Int, sizeMS :: Int, - hasMax :: Int, - minMS :: Int, - maxMS :: Int + nbLimits :: Int, + limits :: [MemorySectionLimits] } deriving (Show, Eq) diff --git a/lvtc/src/WasmUtils.hs b/lvtc/src/WasmUtils.hs index 5d08b74..d4445d0 100644 --- a/lvtc/src/WasmUtils.hs +++ b/lvtc/src/WasmUtils.hs @@ -19,6 +19,7 @@ module WasmUtils fillBlankTypeSectionType, fillBlankTypeSection, fillBlankFunctionSection, + fillBlankMemorySectionLimits, fillBlankMemorySection, fillBlankExportSectionExport, fillBlankExportSection, @@ -98,32 +99,38 @@ getDefaultMemorySection :: MemorySection getDefaultMemorySection = MS { headerMS = 0x05, sizeMS = 0x0, - hasMax = 0x0, - minMS = 0x0, - maxMS = 0x0 + nbLimits = 0, + limits = [] } -fillBlankMemorySection :: MemorySection -> MemorySection -fillBlankMemorySection (MS hM _ 0 miMS maMS) = - MS { - headerMS = hM, - sizeMS = size, +fillBlankMemorySectionLimits :: MemorySectionLimits -> MemorySectionLimits +fillBlankMemorySectionLimits (MSL 0 miMS maMS) = + MSL { hasMax = 0, minMS = miMS, maxMS = maMS } - where - size = 2 -fillBlankMemorySection (MS hM _ _ miMS maMS) = - MS { - headerMS = hM, - sizeMS = size, +fillBlankMemorySectionLimits (MSL _ miMS maMS) = + MSL { hasMax = 1, minMS = miMS, maxMS = maMS } + +getSizeMemorySectionLimits :: MemorySectionLimits -> Int +getSizeMemorySectionLimits (MSL 0 _ _) = 1 + 1 +getSizeMemorySectionLimits (MSL _ _ _) = 1 + 1 + 1 + +fillBlankMemorySection :: MemorySection -> MemorySection +fillBlankMemorySection (MS hM _ _ m) = + MS { + headerMS = hM, + sizeMS = size, + nbLimits = length m, + limits = m + } where - size = 3 + size = 1 + sum (map getSizeMemorySectionLimits m) fillBlankExportSectionExport :: ExportSectionExport -> ExportSectionExport fillBlankExportSectionExport (ESE _ n t i) = diff --git a/lvtc/src/WatToWasm.hs b/lvtc/src/WatToWasm.hs index d51625a..342d55a 100644 --- a/lvtc/src/WatToWasm.hs +++ b/lvtc/src/WatToWasm.hs @@ -74,8 +74,13 @@ funcDefsToFunctionSection fs = funcDefsToMemorySection :: [FuncDef] -> MemorySection funcDefsToMemorySection _ = fillBlankMemorySection (getDefaultMemorySection { - hasMax = 0, - minMS = 1 + limits = [ + fillBlankMemorySectionLimits (MSL { + hasMax = 0, + minMS = 1, + maxMS = 0 + }) + ] }) -- Export Section diff --git a/lvtc/src/WriteWasm.hs b/lvtc/src/WriteWasm.hs index a78d7af..4fb58af 100644 --- a/lvtc/src/WriteWasm.hs +++ b/lvtc/src/WriteWasm.hs @@ -59,11 +59,17 @@ functionSectionToByteString (FS a b c ld) = -- +memorySectionLimitToByteString :: MemorySectionLimits -> B.ByteString +memorySectionLimitToByteString (MSL 0 a _) = + B.pack [0, fromIntegral a] +memorySectionLimitToByteString (MSL _ a b) = + B.pack [0, fromIntegral a, fromIntegral b] + memorySectionToByteString :: MemorySection -> B.ByteString -memorySectionToByteString (MS a b 0 d _) = - B.pack (map fromIntegral ([a, b, 0, d])) -memorySectionToByteString (MS a b _ d e) = - B.pack (map fromIntegral ([a, b, 1, d, e])) +memorySectionToByteString (MS a b e ld) = + extendBytes + (B.pack (map fromIntegral ([a, b, e]))) + (map memorySectionLimitToByteString ld) --