-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
llvm-{syntax,cli}: Register more overrides for libc and LLVM functions (
#1236) This will allow us to more easily test the behavior of these overrides, without involving the translation code.
- Loading branch information
1 parent
e861406
commit 448103b
Showing
5 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(defun @main () Unit | ||
(start start: | ||
(let g (resolve-global "memset")) | ||
(let h (load-handle (Ptr 64) ((Ptr 64) (Ptr 32) (Ptr 64)) g)) | ||
|
||
(let p (alloca none (bv 64 8))) | ||
(let c (ptr 32 0 (bv 32 0))) | ||
(let sz (ptr 64 0 (bv 64 4))) | ||
(let _ (funcall h p c sz)) | ||
|
||
(return ()))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
==== Begin Simulation ==== | ||
|
||
==== Finish Simulation ==== | ||
==== No proof obligations ==== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
crucible-llvm-syntax/src/Lang/Crucible/LLVM/Syntax/Overrides.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{-# LANGUAGE ImportQualifiedPost #-} | ||
{-# LANGUAGE ImplicitParams #-} | ||
|
||
module Lang.Crucible.LLVM.Syntax.Overrides | ||
( registerLLVMOverrides | ||
) where | ||
|
||
import Control.Monad.IO.Class (liftIO) | ||
import Data.Foldable qualified as F | ||
import Data.List qualified as List | ||
|
||
import Text.LLVM.AST qualified as L | ||
|
||
import Lang.Crucible.Backend qualified as C | ||
import Lang.Crucible.Simulator qualified as C | ||
|
||
import Lang.Crucible.LLVM.Functions qualified as CLLVM | ||
import Lang.Crucible.LLVM.Intrinsics.Libc qualified as Libc | ||
import Lang.Crucible.LLVM.Intrinsics.LLVM qualified as LLVM | ||
import Lang.Crucible.LLVM.Intrinsics qualified as CLLVM | ||
import Lang.Crucible.LLVM.MemModel qualified as CLLVM | ||
import Lang.Crucible.LLVM.Translation qualified as CLLVM | ||
import Lang.Crucible.LLVM.TypeContext qualified as CLLVM | ||
|
||
-- | Register overrides for libc functions and LLVM intrinsics | ||
registerLLVMOverrides :: | ||
( C.IsSymBackend sym bak | ||
, CLLVM.HasLLVMAnn sym | ||
, CLLVM.HasPtrWidth wptr | ||
, ?lc :: CLLVM.TypeContext | ||
, ?intrinsicsOpts :: CLLVM.IntrinsicsOptions | ||
, ?memOpts :: CLLVM.MemOptions | ||
) => | ||
bak -> | ||
CLLVM.LLVMContext arch -> | ||
C.OverrideSim p sym ext rtp a r [CLLVM.SomeLLVMOverride p sym ext] | ||
registerLLVMOverrides bak llvmCtx = do | ||
let ovs = List.concat [Libc.libc_overrides, LLVM.basic_llvm_overrides] | ||
let decls = | ||
List.map (\(CLLVM.SomeLLVMOverride ov) -> CLLVM.llvmOverride_declare ov) (F.toList ovs) | ||
|
||
let mvar = CLLVM.llvmMemVar llvmCtx | ||
F.forM_ decls $ \decl -> do | ||
let L.Symbol name = L.decName decl | ||
let aliases = [] | ||
-- See the module comment on "Lang.Crucible.LLVM.Functions" for why this | ||
-- part is necessary. | ||
C.modifyGlobal mvar $ \mem -> | ||
liftIO (CLLVM.registerFunPtr bak mem name (L.decName decl) aliases) | ||
|
||
let tmpls = map (\(CLLVM.SomeLLVMOverride ov) -> CLLVM.basic_llvm_override ov) ovs | ||
CLLVM.register_llvm_overrides_ llvmCtx tmpls decls |