ArchitectureInfo: simplify checkForReturnAddr return type

This change updates the checkForReturnAddr record field type so that
rather than returning a value that was never used (and was always
going to be ReturnAddr anyway), the function now returns unit to
indicate success or fails via "fail" otherwise. This resulted in a
simplification of the various spots where checkForReturnAddr was called
or was implemented for each architecture.

As part of this change, tryClassifier was removed since that was only
used as part of the previous implementation.
This commit is contained in:
Jonathan Daugherty 2023-06-28 15:58:23 -07:00 committed by Daniel Matichuk
parent aacaaea7a2
commit 2a7692b19a
5 changed files with 9 additions and 18 deletions

View File

@ -17,7 +17,6 @@ module Data.Macaw.Architecture.Info
, BlockClassifierContext(..)
, Classifier(..)
, classifierLog
, tryClassifier
, classifierGuard
, classifierName
, liftClassifier
@ -73,13 +72,6 @@ data Classifier o = ClassifyFailed [ClassificationError]
classifierLog :: String -> Classifier ()
classifierLog msg = ClassifySucceeded [msg] ()
-- | Given a Classifier, build a new unconditionally succeeding one that
-- indicates whether the given classifier failed, but also carries all
-- of its errors and tracing information.
tryClassifier :: Classifier a -> Classifier Bool
tryClassifier (ClassifyFailed msgs) = ClassifySucceeded msgs False
tryClassifier (ClassifySucceeded msgs _) = ClassifySucceeded msgs True
-- | A Classifier-specific version of 'guard' that emits a log message
-- with the specified prefix if the guard expression is False (and also
-- fails).
@ -313,7 +305,7 @@ data ArchitectureInfo arch
, checkForReturnAddr :: forall ids
. RegState (ArchReg arch) (Value arch ids)
-> AbsProcessorState (ArchReg arch) ids
-> Classifier Bool
-> Classifier ()
-- ^ @checkForReturnAddr regs s@ returns true if the location
-- where the return address is normally stored in regs when
-- calling a function does indeed contain the abstract value

View File

@ -31,7 +31,6 @@ module Data.Macaw.Discovery.Classifier (
, classifierEndBlock
) where
import Control.Applicative ( Alternative(empty) )
import Control.Lens ( (^.), (&), (.~) )
import Control.Monad ( when, unless )
import qualified Control.Monad.Reader as CMR
@ -415,7 +414,6 @@ tailCallClassifier = classifierName "Tail call" $ do
unless (o == 0) $
fail "Expected stack height of 0"
-- Return address is pushed
isRet <- liftClassifier $ Info.checkForReturnAddr ainfo (classifierFinalRegState bcc) (classifierAbsState bcc)
unless isRet empty
liftClassifier $ Info.checkForReturnAddr ainfo (classifierFinalRegState bcc) (classifierAbsState bcc)
pure $! noreturnCallParsedContents bcc

View File

@ -49,7 +49,7 @@ arm_linux_info =
, MI.absEvalArchStmt = absEvalArchStmt
, MI.identifyCall = identifyCall
, MI.archCallParams = callParams preserveRegAcrossSyscall
, MI.checkForReturnAddr = \r s -> (guard $ isReturnValue s (r ^. MC.boundValue ARMReg.arm_LR)) >> return True
, MI.checkForReturnAddr = \r s -> (guard $ isReturnValue s (r ^. MC.boundValue ARMReg.arm_LR))
, MI.identifyReturn = \x y z -> MI.classifierLiftMaybe $ identifyReturn x y z
, MI.rewriteArchFn = rewritePrimFn
, MI.rewriteArchStmt = rewriteStmt

View File

@ -100,8 +100,8 @@ ppc64_linux_info binData =
, MI.absEvalArchFn = absEvalArchFn proxy
, MI.absEvalArchStmt = absEvalArchStmt proxy
, MI.identifyCall = identifyCall proxy
, MI.checkForReturnAddr = \r s -> MI.tryClassifier $ matchReturn s (r ^. MC.boundValue R.PPC_LNK)
, MI.identifyReturn = identifyReturn proxy
, MI.checkForReturnAddr = \r s -> (MI.classifierLiftMaybe $ matchReturn s (r ^. MC.boundValue R.PPC_LNK)) >> return ()
, MI.identifyReturn = \x y z -> MI.classifierLiftMaybe $ identifyReturn proxy x y z
, MI.rewriteArchFn = rewritePrimFn
, MI.rewriteArchStmt = rewriteStmt
, MI.rewriteArchTermStmt = rewriteTermStmt
@ -126,8 +126,8 @@ ppc32_linux_info =
, MI.absEvalArchFn = absEvalArchFn proxy
, MI.absEvalArchStmt = absEvalArchStmt proxy
, MI.identifyCall = identifyCall proxy
, MI.checkForReturnAddr = \r s -> MI.tryClassifier $ matchReturn s (r ^. MC.boundValue R.PPC_LNK)
, MI.identifyReturn = identifyReturn proxy
, MI.checkForReturnAddr = \r s -> (MI.classifierLiftMaybe $ matchReturn s (r ^. MC.boundValue R.PPC_LNK)) >> return ()
, MI.identifyReturn = \x y z -> MI.classifierLiftMaybe $ identifyReturn proxy x y z
, MI.rewriteArchFn = rewritePrimFn
, MI.rewriteArchStmt = rewriteStmt
, MI.rewriteArchTermStmt = rewriteTermStmt

View File

@ -8,6 +8,7 @@ module Data.Macaw.PPC.Identify
)
where
import Control.Monad ( guard )
import Control.Lens ( (^.) )
import Data.Parameterized.Some ( Some(..) )
import qualified Data.Sequence as Seq
@ -60,7 +61,7 @@ identifyReturn :: (PPCArchConstraints var)
-> MA.AbsProcessorState (PPCReg var) ids
-> Maybe (Seq.Seq (MC.Stmt (SP.AnyPPC var) ids))
identifyReturn _ stmts regState absState = do
Some MA.ReturnAddr <- matchReturn absState (regState ^. MC.boundValue MC.ip_reg)
matchReturn absState (regState ^. MC.boundValue MC.ip_reg)
return stmts
matchReturn :: (ppc ~ SP.AnyPPC var, PPCArchConstraints var)