Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crucible-llvm: Generalize override registration code #1189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crucible-llvm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# next

* Override registration code was generalized. `bind_llvm_{handle,func}`
now don't require a whole `LLVMContext`, just a `GlobalVar Mem`, and are
polymorphic over `ext`.
* `build_llvm_override` is now generic over the `ext` type parameter. This
should be a backwards-compatible change.
* `LLVMOverride` now has an additional `ext` type parameter. See the Haddocks
Expand Down
6 changes: 4 additions & 2 deletions crucible-llvm/src/Lang/Crucible/LLVM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ registerModuleFn handleWarning mtrans sym =
s = UseCFG cfg (postdomInfo cfg)
binds <- use (stateContext . functionBindings)
let llvmCtx = mtrans ^. transContext
bind_llvm_handle llvmCtx (L.decName decl) h s
let mvar = llvmMemVar llvmCtx
bind_llvm_handle mvar (L.decName decl) h s

when (isJust $ lookupHandleMap h $ fnBindings binds) $
do loc <- liftIO . getCurrentProgramLoc =<< getSymInterface
Expand Down Expand Up @@ -157,7 +158,8 @@ registerLazyModuleFn handleWarning mtrans sym =

-- Bind the function handle to the appropriate global symbol.
let llvmCtx = mtrans ^. transContext
bind_llvm_handle llvmCtx (L.decName decl) h s
let mvar = llvmMemVar llvmCtx
bind_llvm_handle mvar (L.decName decl) h s


llvmGlobalsToCtx
Expand Down
27 changes: 13 additions & 14 deletions crucible-llvm/src/Lang/Crucible/LLVM/Intrinsics/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,12 @@ register_llvm_override llvmOverride = do
-- allocation in the LLVM memory.
bind_llvm_handle ::
(IsSymInterface sym, HasPtrWidth wptr) =>
LLVMContext arch ->
GlobalVar Mem ->
L.Symbol ->
FnHandle args ret ->
FnState p sym LLVM args ret ->
OverrideSim p sym LLVM rtp l a ()
bind_llvm_handle llvmCtx nm hdl impl = do
let mvar = llvmMemVar llvmCtx
FnState p sym ext args ret ->
OverrideSim p sym ext rtp l a ()
bind_llvm_handle mvar nm hdl impl = do
bindFnHandle hdl impl
mem <- readGlobal mvar
mem' <- ovrWithBackend $ \bak -> liftIO $ bindLLVMFunPtr bak nm hdl mem
Expand All @@ -406,19 +405,19 @@ bind_llvm_handle llvmCtx nm hdl impl = do
-- global function allocation in the LLVM memory.
bind_llvm_func ::
(IsSymInterface sym, HasPtrWidth wptr) =>
LLVMContext arch ->
GlobalVar Mem ->
L.Symbol ->
Ctx.Assignment TypeRepr args ->
TypeRepr ret ->
FnState p sym LLVM args ret ->
OverrideSim p sym LLVM rtp l a ()
bind_llvm_func llvmCtx nm args ret impl = do
FnState p sym ext args ret ->
OverrideSim p sym ext rtp l a ()
bind_llvm_func mvar nm args ret impl = do
let L.Symbol strNm = nm
let fnm = functionNameFromText (Text.pack strNm)
ctx <- use stateContext
let ha = simHandleAllocator ctx
h <- liftIO $ mkHandle' ha fnm args ret
bind_llvm_handle llvmCtx nm h impl
bind_llvm_handle mvar nm h impl

-- | Low-level function to register LLVM overrides.
--
Expand All @@ -430,11 +429,11 @@ bind_llvm_func llvmCtx nm args ret impl = do
-- Useful when you don\'t have access to a full LLVM AST, e.g., when parsing
-- Crucible CFGs written in crucible-syntax. For more usual cases, use
-- 'Lang.Crucible.LLVM.Intrinsics.register_llvm_overrides'.
do_register_llvm_override :: forall p args ret sym arch wptr l a rtp.
do_register_llvm_override :: forall p args ret sym ext arch wptr l a rtp.
(IsSymInterface sym, HasPtrWidth wptr, HasLLVMAnn sym) =>
LLVMContext arch ->
LLVMOverride p sym LLVM args ret ->
OverrideSim p sym LLVM rtp l a ()
LLVMOverride p sym ext args ret ->
OverrideSim p sym ext rtp l a ()
do_register_llvm_override llvmctx llvmOverride = do
let decl = llvmOverride_declare llvmOverride
let (L.Symbol str_nm) = L.decName decl
Expand All @@ -449,7 +448,7 @@ do_register_llvm_override llvmctx llvmOverride = do
llvmDeclToFunHandleRepr' decl $ \args ret -> do
o <- build_llvm_override fnm overrideArgs overrideRet args ret
(\asgn -> llvmOverride_def llvmOverride mvar asgn)
bind_llvm_func llvmctx (L.decName decl) args ret (UseOverride o)
bind_llvm_func mvar (L.decName decl) args ret (UseOverride o)

-- | Create an allocation for an override and register it.
--
Expand Down
Loading