Skip to content

Commit 44fcfda

Browse files
committed
wasm_reactor: run verifier
1 parent 7c9abc8 commit 44fcfda

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

wasm_reactor/app/WASMReactor.hs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Foreign
2626
import Foreign.C (CString, newCString, peekCStringLen)
2727
import Foreign.C.String (peekCString)
2828
import Foreign.C.Types (CChar)
29-
import Parser (CompilerFlags (CompilerFlags), initCompilerFlags, parseProgram)
29+
import Parser (CompilerFlags (CompilerFlags), exprs, initCompilerFlags, parseProgram)
3030
import Parser qualified
3131
import Text.Megaparsec (errorBundlePretty)
3232
import VM
@@ -39,6 +39,7 @@ import VM
3939
, runVM
4040
, runVMVM
4141
)
42+
import Verifier (verifyProgram)
4243

4344
-- TODO: Put this stuff into a seperate file
4445
runProgramRaw :: Ptr CChar -> Int -> IO ()
@@ -56,21 +57,25 @@ runProgramRaw progPtr progLen = do
5657

5758
runProgramRawBuffered :: Ptr CChar -> Int -> Ptr CChar -> Int -> Ptr (Ptr CChar) -> IO Int
5859
runProgramRawBuffered progPtr progLen inputPtr inputLen outputPtrPtr = do
59-
program <- peekCStringLen (progPtr, progLen)
60+
programStr <- peekCStringLen (progPtr, progLen)
6061
input <- peekCStringLen (inputPtr, inputLen)
61-
let p = parseProgram (Data.Text.pack program) Parser.initCompilerFlags
62+
let p = parseProgram (Data.Text.pack programStr) Parser.initCompilerFlags
6263
case p of
6364
Left err -> error $ errorBundlePretty err
6465
Right program -> do
65-
xxx <- evalStateT (compileProgram program) (initCompilerState program)
66-
let xxxPoint = locateLabel xxx "main"
67-
vm <- runVMVM $ (initVM (V.fromList xxx)){pc = xxxPoint, breakpoints = [], callStack = [StackFrame{returnAddress = xxxPoint, locals = []}], ioMode = VMBuffer, ioBuffer = IOBuffer{input = input, output = ""}}
68-
let output' = BS.pack $ output $ ioBuffer vm
69-
BU.unsafeUseAsCStringLen output' $ \(buf, len) -> do
70-
outputPtr <- mallocBytes len
71-
poke outputPtrPtr outputPtr
72-
copyBytes outputPtr buf len
73-
pure len
66+
verified <- verifyProgram "" (Data.Text.pack programStr) (exprs program)
67+
case verified of
68+
Left err -> error $ errorBundlePretty err
69+
Right _ -> do
70+
xxx <- evalStateT (compileProgram program) (initCompilerState program)
71+
let xxxPoint = locateLabel xxx "main"
72+
vm <- runVMVM $ (initVM (V.fromList xxx)){pc = xxxPoint, breakpoints = [], callStack = [StackFrame{returnAddress = xxxPoint, locals = []}], ioMode = VMBuffer, ioBuffer = IOBuffer{input = input, output = ""}}
73+
let output' = BS.pack $ output $ ioBuffer vm
74+
BU.unsafeUseAsCStringLen output' $ \(buf, len) -> do
75+
outputPtr <- mallocBytes len
76+
poke outputPtrPtr outputPtr
77+
copyBytes outputPtr buf len
78+
pure len
7479

7580
foreign export ccall mallocPtr :: IO (Ptr (Ptr a))
7681

0 commit comments

Comments
 (0)