Skip to content

Commit 083a004

Browse files
committed
Add three global allocation options for LLVM
When using no automatic allocations, constant globals must be manually allocated just the same as mutable globals. ``` let llvm_use_constant name = do { llvm_alloc_global name; llvm_points_to (llvm_global name) (llvm_global_initializer name); }; ```
1 parent bfca3b6 commit 083a004

File tree

4 files changed

+49
-36
lines changed

4 files changed

+49
-36
lines changed

saw-central/src/SAWCentral/Crucible/LLVM/Builtins.hs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,12 +1042,7 @@ setupGlobalAllocs cc mspec mem0 = foldM go mem0 $ mspec ^. MS.csGlobalAllocs
10421042
gimap = view Crucible.globalInitMap mtrans
10431043
case Map.lookup symbol gimap of
10441044
Just (g, Right (mt, _)) -> ccWithBackend cc $ \bak ->
1045-
do when (L.gaConstant $ L.globalAttrs g) . throwMethodSpec mspec $ mconcat
1046-
[ "Global variable \""
1047-
, name
1048-
, "\" is not mutable"
1049-
]
1050-
let sz = Crucible.memTypeSize (Crucible.llvmDataLayout ?lc) mt
1045+
do let sz = Crucible.memTypeSize (Crucible.llvmDataLayout ?lc) mt
10511046
sz' <- W4.bvLit sym ?ptrWidth $ Crucible.bytesToBV ?ptrWidth sz
10521047
alignment <-
10531048
case L.globalAlign g of
@@ -1061,7 +1056,10 @@ setupGlobalAllocs cc mspec mem0 = foldM go mem0 $ mspec ^. MS.csGlobalAllocs
10611056
]
10621057
Just al -> return al
10631058
_ -> pure $ Crucible.memTypeAlign (Crucible.llvmDataLayout ?lc) mt
1064-
(ptr, mem') <- Crucible.doMalloc bak Crucible.GlobalAlloc Crucible.Mutable name mem sz' alignment
1059+
let mutability
1060+
| L.gaConstant (L.globalAttrs g) = Crucible.Immutable
1061+
| otherwise = Crucible.Mutable
1062+
(ptr, mem') <- Crucible.doMalloc bak Crucible.GlobalAlloc mutability name mem sz' alignment
10651063
pure $ Crucible.registerGlobal mem' [symbol] ptr
10661064
_ -> throwMethodSpec mspec $ mconcat
10671065
[ "Global variable \""
@@ -1701,7 +1699,7 @@ setupLLVMCrucibleContext pathSat lm action =
17011699
what4PushMuxOps <- gets rwWhat4PushMuxOps
17021700
laxPointerOrdering <- gets rwLaxPointerOrdering
17031701
laxLoadsAndStores <- gets rwLaxLoadsAndStores
1704-
allocAllGlobals <- gets rwAllocAllGlobals
1702+
globalAllocMode <- gets rwLLVMGlobalAllocMode
17051703
noSatisfyingWriteFreshConstant <- gets rwNoSatisfyingWriteFreshConstant
17061704
pathSatSolver <- gets rwPathSatSolver
17071705
what4Eval <- gets rwWhat4Eval
@@ -1760,11 +1758,17 @@ setupLLVMCrucibleContext pathSat lm action =
17601758
intrinsics halloc stdout
17611759
bindings (Crucible.llvmExtensionImpl ?memOpts)
17621760
Common.SAWCruciblePersonality
1763-
let memoryInitializer = if allocAllGlobals
1764-
then Crucible.initializeAllMemory
1765-
else Crucible.initializeMemoryConstGlobals
1766-
mem <- Crucible.populateConstGlobals bak (view Crucible.globalInitMap mtrans)
1767-
=<< memoryInitializer bak ctx llvm_mod
1761+
1762+
mem <-
1763+
case globalAllocMode of
1764+
LLVMAllocConstantGlobals ->
1765+
Crucible.populateConstGlobals bak (view Crucible.globalInitMap mtrans)
1766+
=<< Crucible.initializeMemoryConstGlobals bak ctx llvm_mod
1767+
LLVMAllocAllGlobals ->
1768+
Crucible.populateConstGlobals bak (view Crucible.globalInitMap mtrans)
1769+
=<< Crucible.initializeAllMemory bak ctx llvm_mod
1770+
LLVMAllocNoGlobals ->
1771+
Crucible.initializeMemory (const False) bak ctx llvm_mod
17681772

17691773
let globals = Crucible.llvmGlobals (Crucible.llvmMemVar ctx) mem
17701774

saw-central/src/SAWCentral/Crucible/LLVM/CrucibleLLVM.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module SAWCentral.Crucible.LLVM.CrucibleLLVM
6868
, GlobalInitializerMap
6969
, initializeMemoryConstGlobals
7070
, initializeAllMemory
71+
, initializeMemory
7172
, makeGlobalMap
7273
, populateConstGlobals
7374
-- * Re-exports from "Lang.Crucible.LLVM.Translation"
@@ -166,7 +167,8 @@ import qualified Lang.Crucible.LLVM.TypeContext as TyCtx
166167

167168
import Lang.Crucible.LLVM.Globals
168169
(GlobalInitializerMap, makeGlobalMap, populateConstGlobals,
169-
initializeAllMemory, initializeMemoryConstGlobals)
170+
initializeAllMemory, initializeMemoryConstGlobals,
171+
initializeMemory)
170172

171173
import Lang.Crucible.LLVM.Translation
172174
(llvmMemVar, transContext, llvmPtrWidth, llvmTypeCtx,

saw-central/src/SAWCentral/Value.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ module SAWCentral.Value (
7070
TopLevelRW(..),
7171
-- used by ... a lot of places, let's not try to make a list just yet
7272
TopLevel(..),
73+
-- Used by TopLevelRW
74+
LLVMGlobalAllocMode(..),
7375
-- used by SAWCentral.Builtins, SAWScript.REPL.Monad, SAWScript.Interpreter,
7476
-- SAWServer.TopLevel
7577
runTopLevel,
@@ -809,6 +811,12 @@ data JavaCodebase =
809811
-- ^ At least one Java-related command has been invoked successfully.
810812
-- We cache the resulting 'JSS.Codebase' for subsequent commands.
811813

814+
data LLVMGlobalAllocMode
815+
= LLVMAllocConstantGlobals -- ^ constants are allocated, globals need llvm_alloc_global
816+
| LLVMAllocAllGlobals -- ^ all globals are allocated
817+
| LLVMAllocNoGlobals -- ^ No globals are allocated, use llvm_alloc_global and llvm_alloc_constant
818+
deriving (Show)
819+
812820
data TopLevelRW =
813821
TopLevelRW
814822
{ rwValueInfo :: Map SS.LName (SS.PrimitiveLifecycle, SS.Schema, Value)
@@ -847,7 +855,7 @@ data TopLevelRW =
847855
, rwLaxLoadsAndStores :: Bool
848856
, rwLaxPointerOrdering :: Bool
849857
, rwDebugIntrinsics :: Bool
850-
, rwAllocAllGlobals :: Bool -- ^ allocate all globals unconditionally
858+
, rwLLVMGlobalAllocMode :: LLVMGlobalAllocMode
851859

852860
-- FIXME: These might be better split into "simulator hash-consing" and "tactic hash-consing"
853861
, rwWhat4HashConsing :: Bool

saw-script/src/SAWScript/Interpreter.hs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ buildTopLevelEnv proxy opts scriptArgv =
10791079
, rwLaxArith = False
10801080
, rwLaxPointerOrdering = False
10811081
, rwLaxLoadsAndStores = False
1082-
, rwAllocAllGlobals = False
1082+
, rwLLVMGlobalAllocMode = LLVMAllocConstantGlobals
10831083
, rwDebugIntrinsics = True
10841084
, rwWhat4HashConsing = False
10851085
, rwWhat4HashConsingX86 = False
@@ -1886,15 +1886,10 @@ disable_lax_loads_and_stores = do
18861886
rw <- getTopLevelRW
18871887
putTopLevelRW rw { rwLaxLoadsAndStores = False }
18881888

1889-
enable_alloc_all_globals :: TopLevel ()
1890-
enable_alloc_all_globals = do
1889+
llvm_set_global_alloc_mode :: LLVMGlobalAllocMode -> TopLevel ()
1890+
llvm_set_global_alloc_mode mode = do
18911891
rw <- getTopLevelRW
1892-
putTopLevelRW rw { rwAllocAllGlobals = True }
1893-
1894-
disable_alloc_all_globals :: TopLevel ()
1895-
disable_alloc_all_globals = do
1896-
rw <- getTopLevelRW
1897-
putTopLevelRW rw { rwAllocAllGlobals = False }
1892+
putTopLevelRW rw { rwLLVMGlobalAllocMode = mode }
18981893

18991894
set_solver_cache_path :: Text -> TopLevel ()
19001895
set_solver_cache_path pathtxt = do
@@ -2704,18 +2699,6 @@ primitives = Map.fromList
27042699
Current
27052700
[ "Disable relaxed validity checking for memory loads and stores in Crucible." ]
27062701

2707-
, prim "enable_alloc_all_globals" "TopLevel ()"
2708-
(pureVal enable_alloc_all_globals)
2709-
Experimental
2710-
[ "Enable allocation of all globals automatically. This is necessary when"
2711-
, " constants depend on the addresses of globals."
2712-
]
2713-
2714-
, prim "disable_alloc_all_globals" "TopLevel ()"
2715-
(pureVal disable_alloc_all_globals)
2716-
Experimental
2717-
[ "Disable allocation of all globals automatically." ]
2718-
27192702
, prim "set_path_sat_solver" "String -> TopLevel ()"
27202703
(pureVal set_path_sat_solver)
27212704
Experimental
@@ -5364,6 +5347,22 @@ primitives = Map.fromList
53645347
Current
53655348
[ "Legacy alternative name for `llvm_null`." ]
53665349

5350+
, prim "llvm_alloc_all_globals" "TopLevel ()"
5351+
(pureVal (llvm_set_global_alloc_mode LLVMAllocAllGlobals))
5352+
Experimental
5353+
[ "Enable allocation of all constant and mutable globals automatically."
5354+
]
5355+
5356+
, prim "llvm_alloc_constant_globals" "TopLevel ()"
5357+
(pureVal (llvm_set_global_alloc_mode LLVMAllocConstantGlobals))
5358+
Experimental
5359+
[ "Enable allocation of constant globals automatically. (default)" ]
5360+
5361+
, prim "llvm_alloc_no_globals" "TopLevel ()"
5362+
(pureVal (llvm_set_global_alloc_mode LLVMAllocNoGlobals))
5363+
Experimental
5364+
[ "Disable allocation of all globals automatically." ]
5365+
53675366
, prim "llvm_global"
53685367
"String -> SetupValue"
53695368
(pureVal CIR.anySetupGlobal)

0 commit comments

Comments
 (0)