From b12b04945577d7076b2002c61bb0f5048057c65b Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Tue, 14 Nov 2023 12:53:51 -0500 Subject: [PATCH 1/2] macaw-ppc: Don't assume absolute IP addresses when decoding `macaw-ppc` was previously assuming that addresses are absolute, which is not true for position independent executables. Extracting the offset from the address is sufficient for our purposes here (note that taking the offset from the `MemSegmentOffset` would not be right, as that offset is relative to the segment start). This is the exact same issue that was noticed in https://github.com/GaloisInc/macaw/commit/37d8029c00d99335625ab615c7ae94fba18f9574 (in `macaw-aarch32`), but that commit forgot to fix things on the `macaw-ppc` end. --- macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs b/macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs index c1e2fee8..ecddd14d 100644 --- a/macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs +++ b/macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} @@ -34,6 +35,7 @@ import qualified Data.Macaw.Memory as MM import qualified Data.Macaw.Memory.Permissions as MMP import Data.Macaw.Types ( BVType ) import qualified Data.Parameterized.Map as MapF +import qualified Data.Parameterized.NatRepr as PN import qualified Data.Parameterized.Nonce as NC import qualified SemMC.Architecture.PPC as SP @@ -127,9 +129,9 @@ disassembleBlock lookupSemantics gs curIPAddr blockOff maxOffset = do -- Note: In PowerPC, the IP is incremented *after* an instruction -- executes, rather than before as in X86. We have to pass in the -- physical address of the instruction here. - ipVal <- case MM.asAbsoluteAddr (MM.segoffAddr curIPAddr) of - Nothing -> failAt gs blockOff curIPAddr (InstructionAtUnmappedAddr i) - Just addr -> return (BVValue (SP.addrWidth SP.knownVariant) (fromIntegral addr)) + let ipVal = MC.BVValue + (PN.knownNat @(ArchAddrWidth ppc)) + (fromIntegral (MM.addrOffset (MM.segoffAddr curIPAddr))) case lookupSemantics ipVal i of Nothing -> failAt gs blockOff curIPAddr (UnsupportedInstruction i) Just transformer -> do From ca64b568082b44e658b11212c18d8d990fb4cc31 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Tue, 14 Nov 2023 13:03:50 -0500 Subject: [PATCH 2/2] macaw-{aarch32,ppc}: Remove vestigial InstructionAtUnmappedAddr error types Now that `macaw-aarch32` and `macaw-ppc` properly handle position-independent code, the `InstructionAtUnmappedAddr` error (which could only be thrown if an IP address was found in position-independent code) is never thrown. Let's delete it. --- macaw-aarch32/src/Data/Macaw/ARM/Disassemble.hs | 1 - macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs | 1 - 2 files changed, 2 deletions(-) diff --git a/macaw-aarch32/src/Data/Macaw/ARM/Disassemble.hs b/macaw-aarch32/src/Data/Macaw/ARM/Disassemble.hs index f760da5e..25890eca 100644 --- a/macaw-aarch32/src/Data/Macaw/ARM/Disassemble.hs +++ b/macaw-aarch32/src/Data/Macaw/ARM/Disassemble.hs @@ -331,7 +331,6 @@ data TranslationError w = TranslationError { transErrorAddr :: MM.MemSegmentOff data TranslationErrorReason w = InvalidNextPC (MM.MemAddr w) (MM.MemAddr w) | DecodeError (ARMMemoryError w) | UnsupportedInstruction InstructionSet - | InstructionAtUnmappedAddr InstructionSet | GenerationError InstructionSet SG.GeneratorError deriving (Show) diff --git a/macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs b/macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs index ecddd14d..d0da8a76 100644 --- a/macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs +++ b/macaw-ppc/src/Data/Macaw/PPC/Disassemble.hs @@ -252,7 +252,6 @@ data TranslationError w = TranslationError { transErrorAddr :: MM.MemSegmentOff data TranslationErrorReason w = InvalidNextIP Word64 Word64 | DecodeError (PPCMemoryError w) | UnsupportedInstruction D.Instruction - | InstructionAtUnmappedAddr D.Instruction | GenerationError D.Instruction GeneratorError deriving (Show)