Commit Graph

168 Commits

Author SHA1 Message Date
Your Name
dfaa7cbf68 x86-symbolic: Add mm* aliases for st* registers 2024-09-24 15:17:09 -04:00
Your Name
f4f1e1fe43 x86-symbolic: Indexes for the remaining registers 2024-09-24 14:28:28 -04:00
Your Name
896d35cba9 x86-symbolic: Remove terse register names from top-level exports
These names are so short that they might conflict with local names in
files that import this module. Instead, export them from `Regs` and
encourage qualified use of that module.
2024-09-24 13:50:39 -04:00
Your Name
a022c2bf18 x86-symbolic: Split out registers into their own module 2024-09-24 13:39:50 -04:00
Your Name
79cf6eb04d x86-symbolic: Add Index definitions, type aliases for X87 status regs 2024-09-24 13:23:29 -04:00
Your Name
5f28a031c3 x86-symbolic: Simplify register Index definitions, add flags registers 2024-09-24 13:15:26 -04:00
Your Name
d470abe976 symbolic: Remove support for stack-spilled arguments
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.
2024-09-24 10:09:55 -04:00
Your Name
dcc87334c7 x86-symbolic: Reuse upstream implementation of alignStackPointer 2024-09-23 15:49:03 -04:00
Your Name
33af2814d9 symbolic-aarch32: Use ABI-compatible stack setup code in test harness 2024-09-23 13:16:43 -04:00
Your Name
e741905b21 symbolic-aarch32: Use upstream code for stack-spilled arguments 2024-09-20 17:41:20 -04:00
Your Name
2398322a2c symbolic: Upstream stack spilled argument code from x86-symbolic
...so that it can also be used by AArch32.
2024-09-20 17:29:10 -04:00
Langston Barrett
31dcc1e3ea
x86-symbolic: Establish SysV stack alignment (#437)
Fixes #436.
2024-09-20 10:29:06 -04:00
Langston Barrett
f80b275f9c symbolic: Sort imports 2024-09-11 17:58:29 -04:00
Langston Barrett
edc9635fe7 x86-symbolic: Use SysV-compatible stack setup in test suite
Fix a logic bug (bytes, not bits!) along the way
2024-09-11 17:57:06 -04:00
Langston Barrett
9f2da79c29 symbolic: Try proving *all* safety conditions 2024-09-11 13:11:48 -04:00
Langston Barrett
be0a57f555 symbolic: Don't feed the stack pointer to the ResultExtractor
None of the supported architectures return values via the stack, and
tracking the stack pointer needlessly complicates the code.
2024-09-05 11:22:55 -04:00
Langston Barrett
145e005551 x86-symbolic: Sort imports 2024-09-04 12:27:39 -04:00
Langston Barrett
e9475850fa x86-symbolic: More SysV stack setup and manipulation 2024-09-04 12:26:45 -04:00
Langston Barrett
ffcab1cd40 x86-symbolic: A module for SysV stack manipulation
Begin splitting apart a giant, branching, monolithic stack setup
function into individual helpers. Introduce newtypes.
2024-09-04 11:15:50 -04:00
Langston Barrett
9955200837 x86-symbolic: Setting up a SysV-compatible stack 2024-09-03 16:16:20 -04:00
Langston Barrett
3dfc22bf41 x86-symbolic: Simplify definitions of Indexs for named registers 2024-08-16 15:32:49 -04:00
Langston Barrett
6cce9b1b98 x86-symbolic: Comment on register Index definitions 2024-08-16 15:32:49 -04:00
Langston Barrett
813d057881 x86-{symbolic,syntax}: Indexs and names for GP registers 2024-08-16 15:32:49 -04:00
Langston Barrett
97751b0895 x86-symbolic: Add Indexs into MacawCrucibleRegTypes for named registers 2024-08-16 15:32:49 -04:00
Langston Barrett
d017106154 x86-symbolic: Minor cleanup (alphabetization, typo fix) 2024-08-16 15:32:49 -04:00
Ryan Scott
1add47389a macaw-x86: Fix call semantics when call target involves the stack pointer
Previously, the `macaw-x86` semantics for `call` would retrieve the call target
*after* pushing the next instruction's address to the stack, but if the call
target involves the stack pointer, then this would mean that it would get the
next instruction's address when retrieving the call target. This is not what is
intended!

This patch fixes the issue by always retrieving the call target *before*
pushing the next instruction's address to the stack. I have added a test case
to the `macaw-x86-symbolic` test suite which demonstrates that this fix works
as intended.

Fixes #420.
2024-08-13 12:31:09 -04:00
Ryan Scott
1966a30909 symbolic: Make {lookup,update}Reg implementations panic rather than error
See https://github.com/GaloisInc/macaw/issues/412 for the remaining task of
allowing the return types of `GenArchVals`' `{lookup,update}Reg` functions to
indicate the possibility of an error.
2024-07-26 15:25:29 -04:00
Ryan Scott
a6ff58f473 macaw-x86-symbolic: Fix idiv/div semantics
When converting a Macaw value with the Macaw type `TupleType [x_1, ..., x_n]`
to Crucible, the resulting Crucible value will have the Crucible type
`StructType (EmptyCtx ::> ToCrucibleType x_n ::> ... ::> ToCrucibleType x_1)`.
(See `macawListToCrucible(M)` in `Data.Macaw.Symbolic.PersistentState` for
where this is implemented.) Note that the order of the tuple's fields is
reversed in the process of converting it to a Crucible struct. This is a
convention that one must keep in mind when dealing with Macaw tuples at the
Crucible level.

As it turns out, the part of `macaw-x86-symbolic` reponsible for interpreting
the semantics of the `idiv` instruction (for signed quotient/remainder) and the
`div` instruction (for unsigned quotient/remainder) were _not_ respecting this
convention. This is because the `macaw-x86-symbolic` semantics were returning a
Crucible struct consisting of `Empty :> quotient :> remainder)`, but at the
Macaw level, this was interpreted as the tuple `(remainder, quotient)`, which
is the opposite of the intended order. This led to subtle bugs such as those
observed in #393.

The solution is straightforward: have the `macaw-x86-symbolic` semantics
compute `Empty :> remainder :> quotient` instead. Somewhat counterintuitive,
but it does work.

Fixes #393.
2024-07-12 16:56:51 -04:00
Langston Barrett
c6c48b6f8b x86-symbolic: Format imports 2024-01-24 11:03:37 -05:00
Langston Barrett
8b90009aec x86-symbolic: Pretty-printing of exceptions 2024-01-24 11:03:37 -05:00
Langston Barrett
50e44e138c x86-symbolic: Add function to extract message from exceptions 2024-01-24 11:03:37 -05:00
Langston Barrett
a02343412d x86-symbolic: Structured exceptions for missing primitive function semantics 2024-01-23 16:39:15 -05:00
Langston Barrett
5f8300bd57 x86-symbolic: Create an exception type for missing semantics
This is an exception that consumers might want to catch.
2024-01-23 16:18:16 -05:00
Ryan Scott
dc7c1759f1 macaw-symbolic: Test both memory model configurations in test suites 2023-03-14 13:27:07 -04:00
Ryan Scott
97c61e471a Add basic support for simulating PLT stubs and shared libraries
This extends `Data.Macaw.Symbolic.Testing` in `macaw-symbolic` to be able to
handle binaries that depend on shared libraries. This is fully functional for
the x86-64 and AArch32 symbolic backends, and I have added test cases to the
respective repos demonstrating that it works. (The PowerPC backend is not yet
supported. At a minimum, this is blocked on GaloisInc/elf-edit#35.)

To implement this, I also needed to add some additional infrastructure to
`macaw-base` (I put this infrastructure here as it doesn't depend on any
Crucible-specific functionality):

* `Data.Macaw.Memory.ElfLoader.DynamicDependencies`: a basic ELF dynamic
  loader that performs a breadth-first search over all `DT_NEEDED` entries
  that an ELF binary depends on (both directly and indirectly).
* `Data.Macaw.Memory.ElfLoader.PLTStubs`: a collection of heuristics for
  detecting the addresses of PLT stubs in a dynamically linked binary.

It is worth noting that shared libraries are rife with nuance and subtlety,
and the way `macaw` models shared libraries is not 100% accurate. I have
written a length `Note [Shared libraries]` in `Data.Macaw.Symbolic.Testing`
to describe where corners had to be cut.

Fixes #318.
2023-02-23 17:16:12 -05:00
Ryan Scott
6e020bcde6 Fix -Wincomplete-uni-patterns warnings
GHC 9.2 adds `-Wincomplete-uni-patterns` to `-Wall`, which uncovers a slew of
previously unnoticed warnings in `macaw`. This patch fixes them, mostly by
adding explicit fall-through cases.
2022-05-31 15:50:48 -04:00
Tristan Ravitch
8e10643b0f
Fix tail call classification (#286)
The tail call classifier came after the jump classifier, which was a problem because it is less strict than the tail call classifier, meaning it would always fire.  This commit moves direct jump to be the last classifier applied, giving the others a chance.

Includes a test case in the ARM backend.

This requires some updates to some of the expected test results, as a few blocks are now classified as tail calls that were
plain jumps before.  They really could be considered either.  I think it would be nice if these could be classified as jumps instead, but the reason they are flagged as tail calls is mostly down to the fact that their surrounding context is so simple that either interpretation works.

Correcting this would require some heuristics based on additional analysis passes.

The test harness for macaw symbolic required a few changes because the new detection of some jumps as tail calls introduces new calls into the symbolic test suites. However, the symbolic testing harness did not support calls before.  Adding support required a bit of plumbing, including a more extensive code discovery pass.


Fixes #285
2022-05-10 07:29:55 -07:00
Daniel Matichuk
5b4d16dd8a
generalize treatment for terminal statements in CFG slicing code (#278)
* remove/generalize MacawBlockEnd from CFG slicing

* expose functions in symbolic backend

* hide bvLit from Backend import

* add CI version to workflow
2022-04-20 10:08:15 -07:00
Kevin Quick
78b45a10c6 Only fix personality to (MS.MacawSimulatorState sym) where required. 2022-02-21 13:27:20 -05:00
Rob Dockins
465a84ee49 Update with changes flowing from GaloicInc/crucible#945.
This mostly deals with the splitting of the old `sym` type into
two: one for dealing with expression creation, and a new simulator
backend type for dealing with control-flow and assertions.
2022-01-24 16:24:07 -08:00
Tristan Ravitch
63a65c3d85 x86: Fix failing proof obligations due to EvenParity
See the writeup in Crucible.hs in this commit for details. In short, the recent
changes to generalize `PtrAdd` triggered a failing proof obligation due to a use
of `llvmPointer_bv`.  The new implementation is as sound as the previous one,
but more general.

Fixes #260
2022-01-21 15:33:10 -08:00
Ryan Scott
ce10bc9243 Drop support for GHC 8.6
This allows us to remove gobs of CPP as a consequence.
2022-01-10 16:40:23 -05:00
Ryan Scott
7819377d84 macaw-x86-symbolic: Remove unused FloatInfoFromSSEType definition
GHC 9.0 uncovered this type family as being unused. (See
https://gitlab.haskell.org/ghc/ghc/-/issues/18470, which made
`-Wunused-top-binds` more clever about detecting unused, closed
type families like `FloatInfoFromSSEType`.) Let's remove it to avoid
an `-Wunused-top-binds` warning.
2022-01-10 16:40:23 -05:00
Ryan Scott
049096c506 Support building with GHC 9.0
This contains a variety of fixes needed to make the packages in the `macaw`
repo compile with GHC 9.0:

* GHC 9.0 implements simplified subsumption (see
  [here](https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/9.0?version_id=5fcd0a50e0872efb3c38a32db140506da8310d87#simplified-subsumption)).
  In most cases, adapting to this is a matter of manually eta expanding
  definitions, such as in `base:Data.Macaw.Analysis.RegisterUse`. In the case
  of `macaw-x86-symbolic:Data.Macaw.X86.Crucible`, the type signature of
  `evalExt` had to be made more specific to adapt to the loss of contravariance
  when typechecking `(->)`.
* GHC's constraint solver now solves constraints in each top-level group
  sooner (see
  [here](https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/9.0?version_id=5fcd0a50e0872efb3c38a32db140506da8310d87#the-order-of-th-splices-is-more-important)).
  This affects `macaw-aarch32` and `macaw-symbolic`, as they separate top-level
  groups with `$(return [])` Template Haskell splices. The previous locations
  of these splices made it so that the TH-generated instances in that package
  were not available to any code before the splice, resulting in type errors
  when compiled with GHC 9.0.

  To overcome this, I rearranged the TH-generated instances so that they appear
  before the top-level groups that make use of them.
* GHC 9.0 now enables `-Wstar-is-type` in `-Wall`, so this patch replaces some
  uses of `*` with `Data.Kind.Type`. `Data.Kind` requires the use of GHC 8.0 or
  later, so this patch also updates thes lower bounds on `base` to `>= 4.9` in
  the appropriate `.cabal` files. (I'm fairly certain that this requirement was
  already present implicity, but better to be explicit about it.)
* The `asl-translator`, `crucible`, and `semmc` submodules were updated to
  allow them to build with GHC 9.0. The `llvm-pretty` and
  `llvm-pretty-bc-parser` submodules were also bumped to accommodate unrelated
  changes in `crucible` that were brought in.
* The upper version bounds on `doctest` in `macaw-symbolic`'s test suite were
  raised to allow it to build with GHC 9.0.
2022-01-10 16:40:23 -05:00
Tristan Ravitch
9ce3d43188
AArch32: Support conditional returns (#243)
Adds support in macaw-aarch32 for conditional returns. These are not supported in core macaw, and are thus architecture-specific block terminators.

This required changes to the type of arch-specific block terminators. Before, `ArchTermStmt` was only parameterized by a state thread (`ids`).  This meant that they could not contain macaw (or crucible) values.  Some work on. AArch32 requires being able to store condition values in arch terminators (to support conditional returns). This change modifies the `ArchTermStmt` to enable this, which requires a bit of plumbing through various definitions and some extra instances.

In support of actually using this, it also became necessary to plumb fallthrough block labels through the architecture-specific terminator translation in macaw-symbolic.

Note that this change was overdue, as the PowerPC backend was storing macaw values in a way that would have rendered them unusable in the macaw-ppc-symbolic translation, had any interpretation been provided.  These new changes will enable a handler to be written for the conditional PowerPC trap instructions.

PowerPC, x86, and ARM have been updated.

Improves the macaw-aarch32 tests. There is now a command line option to save the generated macaw IR for each
discovered function to /tmp. Note that this reuses some infrastructure from the macaw-symbolic tests. This
shared functionality should be extracted into a macaw-testing library.
2021-11-19 16:20:50 -08:00
Ryan Scott
5547632f65 macaw-x86: Handle sign-extended immediates in def_push
See `Note [Sign-extending immediate operands in push]` in
`Data.Macaw.X86.Semantics` for the full story. I have also added a test case
in `macaw-x86-symbolic` which ensures that the stack-pointer-decrementing
logic behaves as one would expect.

Bumps in the `flexdis86` submodule to bring in GaloisInc/flexdis86#37.

Fixes #235.
2021-10-12 16:37:21 -04:00
Brett Boston
a336895da7
Add optional override for MacawArchStmtExtensions to genArchVals (#230)
This change adds an optional argument to `genArchVals` that allows client code to override the backend translation behavior of `MacawArchStmtExtension`s on a statement-by-statement basis.  The new argument has type `Maybe (MacawArchStmtExtensionOverride arch)`, where `MacawArchStmtExtensionOverride` is a function that takes a statement and a crucible state, and returns an optional tuple containing the value produced by the statement, as well as an updated state.  Returning 'Nothing' indicates that the backend should use its default handler for the statement.

Client code that wishes to maintain the existing default behavior in all cases can simply pass `Nothing` for the new argument to `genArchVals`.
2021-09-14 18:24:47 -07:00
Tristan Ravitch
380d732d0e
Implement system call support for x86 (#226)
Implement support for symbolically executing system calls in macaw-symbolic. **To update code that does not need to symbolically execute system calls (i.e., most clients of macaw-symbolic), just pass the new `unsupportedSyscalls` default handler as the fifth argument of `macawExtensions`.**

The primary interface is via the new `LookupSyscallHandle` callback passed to `macawExtensions`. This callback inspects the environment and returns a Crucible `FunctionHandle` that models the behavior of the requested system call. Note that this mechanism only supports concrete system calls (i.e., system calls where the system call number is concrete). The x86 backend has been updated to support this new functionality.

The representation of system calls in macaw is still architecture-specific (because there are interesting differences between system call instructions across architectures). The idea is that system calls are now treated in two steps:
1. A macaw-symbolic extension statement that looks up the override to invoke for the given syscall (returned as a Crucible FunctionHandle)
2. A call to that handle

We need this two step approach because the handlers that interpret syntax extension statements cannot symbolically branch (and thus cannot call overrides). The extension interpreter just looks up the necessary handle and uses the standard call/override machinery to handle any branching required to support the system call model functionality.

The major complication to this approach is that system calls need to update values in registers when they return. To capture these updates, the architecture-specific syntax extension needs to explicitly update any machine registers that could possibly be affected. The explicit updates are necessary because machine registers do not exist anymore at the macaw-symbolic level (at least within a block). To handle all of these constraints:
1. System calls are represented as extension functions at the macaw level when lifted from machine code.
2. During translation into crucible (via macaw-symbolic), the extension functions are translated into two statements: a function handle lookup and then a function call (with the return values being explicitly threaded through the Crucible function).
3. During symbolic execution, the lookup statement examines the environment to return the necessary function handle, while the handle is called via the normal machinery.

Note that the feature is entirely controlled by the `LookupSyscallHandle` function, which determines the system call dispatch policy. No system call models are included with this change.

Co-authored-by: Brett Boston <boston@galois.com>
2021-08-27 15:47:40 -07:00
Ryan Scott
7f7de2a59b
Adapt to GaloisInc/crucible#794 (#224)
GaloisInc/crucible#794 increases the number of functions that use
implicit `MemOptions`, including a handful of key LLVM memory model–related
functions. As a result, many parts of `macaw` need to add implicit `?memOpts`
parameters to accommodate to this change.
2021-08-23 20:39:08 -04:00
Sam Breese
135fb062bb
x86: Fix semantics for BSF and BSR instructions (#216)
* x86: Fix semantics for BSF and BSR instructions

* Add a test for BSR and BSF
2021-07-13 14:14:59 -04:00