From 6075818a56d36a29b1e1257ad47fa3e63116c42e Mon Sep 17 00:00:00 2001 From: Ilya Rezvov Date: Tue, 27 Feb 2018 15:45:39 -0800 Subject: [PATCH] const expression get_global can refer only imported constant globals --- src/Language/Wasm/Validate.hs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Language/Wasm/Validate.hs b/src/Language/Wasm/Validate.hs index ac4bdc2..bf6318a 100644 --- a/src/Language/Wasm/Validate.hs +++ b/src/Language/Wasm/Validate.hs @@ -116,7 +116,8 @@ data Ctx = Ctx { globals :: [GlobalType], locals :: [ValueType], labels :: [Maybe ValueType], - returns :: Maybe ValueType + returns :: Maybe ValueType, + importedGlobals :: Natural } deriving (Show, Eq) type Checker = ReaderT Ctx (StateT Int (Except ValidationResult)) @@ -409,7 +410,10 @@ isConstExpression ((I64Const _):rest) = isConstExpression rest isConstExpression ((F32Const _):rest) = isConstExpression rest isConstExpression ((F64Const _):rest) = isConstExpression rest isConstExpression ((GetGlobal idx):rest) = do - Ctx {globals} <- ask + Ctx {globals, importedGlobals} <- ask + if importedGlobals <= idx + then throwError InvalidConstantExpr + else return () case globals !! fromIntegral idx of Const _ -> isConstExpression rest Mut _ -> throwError InvalidConstantExpr @@ -436,7 +440,8 @@ ctxFromModule locals labels returns m@Module {types, functions, tables, mems, gl globals = globalImports ++ map (\(Global g _) -> g) globals, locals, labels, - returns + returns, + importedGlobals = fromIntegral $ length globalImports } where getTableType (Import _ _ (ImportTable tableType)) = Just tableType