fail on too big align value deserialization

This commit is contained in:
Ilya Rezvov
2025-01-17 20:19:23 -07:00
parent 37833b67c1
commit f300874996
3 changed files with 9 additions and 4 deletions
+2
View File
@@ -17,6 +17,7 @@ import Data.Bits
import Data.Word (Word8, Word32, Word64) import Data.Word (Word8, Word32, Word64)
import Data.Int (Int8, Int32, Int64) import Data.Int (Int8, Int32, Int64)
import Data.Serialize import Data.Serialize
import Control.Monad (when)
import Data.Primitive.ByteArray as BA import Data.Primitive.ByteArray as BA
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS import qualified Data.ByteString.Lazy as LBS
@@ -340,6 +341,7 @@ instance Serialize MemArg where
put MemArg { align, offset } = putULEB128 align >> putULEB128 offset put MemArg { align, offset } = putULEB128 align >> putULEB128 offset
get = do get = do
align <- getULEB128 32 align <- getULEB128 32
when (align >= 32) $ fail "malformed memop flags"
offset <- getULEB128 32 offset <- getULEB128 32
return $ MemArg { align, offset } return $ MemArg { align, offset }
+6 -3
View File
@@ -193,10 +193,13 @@ getLabel lbl = do
withLabel :: [ValueType] -> Checker a -> Checker a withLabel :: [ValueType] -> Checker a -> Checker a
withLabel result = withReaderT (\ctx -> ctx { labels = result : labels ctx }) withLabel result = withReaderT (\ctx -> ctx { labels = result : labels ctx })
isMemArgValid :: Int -> MemArg -> Checker () isMemArgValid :: Natural -> MemArg -> Checker ()
isMemArgValid sizeInBytes MemArg { align } = if 2 ^ align <= sizeInBytes then return () else throwError AlignmentOverflow isMemArgValid sizeInBytes MemArg { align } =
if 2 ^ align <= sizeInBytes
then return ()
else throwError AlignmentOverflow
checkMemoryInstr :: Int -> MemArg -> Checker () checkMemoryInstr :: Natural -> MemArg -> Checker ()
checkMemoryInstr size memarg = do checkMemoryInstr size memarg = do
isMemArgValid size memarg isMemArgValid size memarg
Ctx { mems } <- ask Ctx { mems } <- ask
+1 -1
View File
@@ -19,7 +19,7 @@ main = do
files <- files <-
filter (List.isSuffixOf ".wast") filter (List.isSuffixOf ".wast")
<$> Directory.listDirectory "tests/spec" <$> Directory.listDirectory "tests/spec"
-- let files = ["table_grow.wast"] -- let files = ["align.wast"]
scriptTestCases <- (`mapM` files) $ \file -> do scriptTestCases <- (`mapM` files) $ \file -> do
test <- LBS.readFile ("tests/spec/" ++ file) test <- LBS.readFile ("tests/spec/" ++ file)
return $ testCase file $ do return $ testCase file $ do