Skip to content

Commit

Permalink
symbolic: Remove support for stack-spilled arguments
Browse files Browse the repository at this point in the history
Many ABIs impose some kind of alignment constraints on the stack
pointer. For example, both the AArch32 and x86_64 SysV ABIs specify
that the end of the spilled argument list is aligned to 2*w where w
is the number of bytes in a word. Since macaw-symbolic has no notion
of the ABI in use, its ABI-agnostic code could only ever satisfy such
alignment constraints accidentally. Clients wishing to spill arguments
to the stack should do so with ABI-specific functionality.
  • Loading branch information
Your Name committed Sep 23, 2024
1 parent 0d00be3 commit 6a7d1f7
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ pushStackFrame bak mem regs spilledArgs = do
pure (regs', mem')

-- | Like 'MSS.createArrayStack', but puts the stack pointer in @sp@ directly.
--
-- Does not allow allocating stack slots, use 'pushStackFrame' for that.
allocStack ::
C.IsSymBackend sym bak =>
(?memOpts :: MM.MemOptions) =>
Expand All @@ -181,7 +179,6 @@ allocStack ::
)
allocStack bak mem regs sz = do
let ?ptrWidth = ptrRepr
let slots = MSS.ExtraStackSlots 0
(MSS.ArrayStack _base top _arr, mem') <- MSS.createArrayStack bak mem slots sz
(MSS.ArrayStack _base top _arr, mem') <- MSS.createArrayStack bak mem sz
let regs' = regs Lens.& stackPointerReg Lens..~ StackPointer top
pure (regs', mem')
22 changes: 2 additions & 20 deletions symbolic/src/Data/Macaw/Symbolic/Stack.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
module Data.Macaw.Symbolic.Stack
( stackArray
, mallocStack
, ExtraStackSlots(..)
, ArrayStack(..)
, createArrayStack
, allocStackSpace
Expand Down Expand Up @@ -66,14 +65,6 @@ mallocStack ::
mallocStack bak mem sz =
CLM.doMalloc bak CLM.StackAlloc CLM.Mutable "stack_alloc" mem sz stackAlign

-- | Allocate this many pointer-sized stack slots, which are reserved for
-- stack-spilled arguments.
newtype ExtraStackSlots = ExtraStackSlots { getExtraStackSlots :: Int }
-- Derive the Read instance using the `newtype` strategy, not the
-- `stock` strategy. This allows parsing ExtraStackSlots values in
-- optparse-applicative-based command-line parsers using the `Read` instance.
deriving newtype (Enum, Eq, Integral, Num, Ord, Read, Real, Show)

-- | An allocation representing the program stack, backed by an SMT array
data ArrayStack sym w
= ArrayStack
Expand All @@ -99,12 +90,10 @@ createArrayStack ::
CLM.HasLLVMAnn sym =>
bak ->
CLM.MemImpl sym ->
-- | The caller must ensure the size of these is less than the allocation size
ExtraStackSlots ->
-- | Size of stack allocation
WI.SymExpr sym (WI.BaseBVType w) ->
IO (ArrayStack sym w, CLM.MemImpl sym)
createArrayStack bak mem slots sz = do
createArrayStack bak mem sz = do
let sym = C.backendGetSym bak
arr <- stackArray sym
(base, mem1) <- mallocStack bak mem sz
Expand All @@ -113,14 +102,7 @@ createArrayStack bak mem slots sz = do
-- Put the stack pointer at the end of the stack allocation, i.e., an offset
-- that is one less than the allocation's size.
off <- WI.bvSub sym sz =<< WI.bvOne sym ?ptrWidth
end <- CLM.doPtrAddOffset bak mem2 base off

let ptrBytes = WI.intValue ?ptrWidth `div` 8
let slots' = fromIntegral (getExtraStackSlots slots)
let slotsBytes = slots' * ptrBytes
slotsBytesBv <- WI.bvLit sym ?ptrWidth (BVS.mkBV ?ptrWidth slotsBytes)
top <- CLM.ptrSub sym ?ptrWidth end slotsBytesBv

top <- CLM.doPtrAddOffset bak mem2 base off
pure (ArrayStack base top arr, mem2)

-- | Allocate stack space by subtracting from the stack pointer
Expand Down
2 changes: 1 addition & 1 deletion symbolic/src/Data/Macaw/Symbolic/Testing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ defaultRegs bak archVals mem = do
let mib = 1024 * kib
stackSize <- WI.bvLit sym ?ptrWidth (BVS.mkBV ?ptrWidth (2 * mib))
(MSS.ArrayStack stackBasePtr _stackTopPtr _stackArrayStorage, mem') <-
MSS.createArrayStack bak mem (MSS.ExtraStackSlots 0) stackSize
MSS.createArrayStack bak mem stackSize
stackInitialOffset <- WI.bvLit sym ?ptrWidth (BVS.mkBV ?ptrWidth mib)
sp <- CLM.ptrAdd sym ?ptrWidth stackBasePtr stackInitialOffset

Expand Down
6 changes: 1 addition & 5 deletions x86_symbolic/src/Data/Macaw/X86/Symbolic/ABI/SysV.hs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,6 @@ pushStackFrame bak mem regs spilledArgs retAddr = do
pure (regs', mem')

-- | Like 'MSS.createArrayStack', but puts the stack pointer in @rsp@ directly.
--
-- Does not allow allocating stack slots, use 'allocStackFrame' or
-- 'pushStackFrame' for that.
allocStack ::
C.IsSymBackend sym bak =>
(?memOpts :: MM.MemOptions) =>
Expand All @@ -250,7 +247,6 @@ allocStack ::
)
allocStack bak mem regs sz = do
let ?ptrWidth = ptrRepr
let slots = MSS.ExtraStackSlots 0
(MSS.ArrayStack _base top _arr, mem') <- MSS.createArrayStack bak mem slots sz
(MSS.ArrayStack _base top _arr, mem') <- MSS.createArrayStack bak mem sz
let regs' = regs Lens.& stackPointerReg Lens..~ StackPointer top
pure (regs', mem')

0 comments on commit 6a7d1f7

Please sign in to comment.