Skip to content

Commit

Permalink
Fix memory error wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 13, 2024
1 parent cabc04b commit 1653978
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
14 changes: 11 additions & 3 deletions lvtc/src/Wasm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Wasm
, TypeSectionType (..)
, TypeSection (..)
, FunctionSection (..)
, MemorySectionLimits (..)
, MemorySection (..)
, ExportSectionExportType (..)
, ExportSectionExport (..)
Expand Down Expand Up @@ -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)

Expand Down
37 changes: 22 additions & 15 deletions lvtc/src/WasmUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module WasmUtils
fillBlankTypeSectionType,
fillBlankTypeSection,
fillBlankFunctionSection,
fillBlankMemorySectionLimits,
fillBlankMemorySection,
fillBlankExportSectionExport,
fillBlankExportSection,
Expand Down Expand Up @@ -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) =
Expand Down
9 changes: 7 additions & 2 deletions lvtc/src/WatToWasm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions lvtc/src/WriteWasm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

--

Expand Down

0 comments on commit 1653978

Please sign in to comment.