Commit Graph

298 Commits

Author SHA1 Message Date
Langston Barrett
ecb2a3650c symbolic: Remove redundant unwrapping/rewrapping of register struct
There was a lot of redundant unwrapping and re-wrapping of the
assignment of `TypeRepr`s to the architecture register context into and
out of a `StructRepr`. Remove this.

Also, add an hlint configuration file to help avoid this in the future,
and a Github Actions workflow to enforce this in CI.
2024-09-11 16:26:10 -07:00
Langston Barrett
897960d722 symbolic: Haddocks on stack setup code 2024-09-03 15:35:08 -04:00
Langston Barrett
761f6587c5 symbolic: Remove dead code 2024-09-03 15:35:08 -04:00
Langston Barrett
3797214312 symbolic: Support allocating stack slots for spilled arguments 2024-09-03 15:35:08 -04:00
Langston Barrett
a28c96e5ed symbolic: Refine discussion about the placement of the stack pointer
dbb4c83 introduced this stack-pointer-in-the-middle approach, but
the commit message has no further commentary on why it was seen as
necessary. I've written up why *I* believe it to be necessary, and
removed what I see as a misleading comment.

Also, return a pointer to the end of the stack, because that's what
most clients will want.
2024-09-03 15:35:08 -04:00
Langston Barrett
caef1d30c1 symbolic: Factor stack setup out of Testing into its own module
Several Macaw-based projects use a similar stack setup:

* `pate`: c43542818b/src/Pate/Verification/InlineCallee.hs (L135-L143)
* `ambient-verifier`: https://github.com/GaloisInc/ambient-verifier/blob/open-source/src/Ambient/Verifier/SymbolicExecution.hs#L1272-L1280

We can share code by factoring it into its own module.
2024-09-03 15:35:08 -04:00
Ryan Scott
9954dd6d01 Fix -Wx-partial warnings uncovered by GHC 9.8 2024-08-08 09:34:03 -04:00
Langston Barrett
83d3907054 Use Crucible goal-proving helpers in testing code 2024-07-16 15:30:35 -04:00
Ryan Scott
4db0341e0a Bump submodules to allow building with what4-1.6.*
This bumps the `what4` submodule to the 1.6.* version series and updates the
`.cabal` files in the `macaw` repo accordingly.

Bumping the `what4` submodule also requires bringing in corresponding changes
in the `crucible`, `llvm-pretty`, and `llvm-pretty-bc-parser` submodules, so I
have done that as well.
2024-06-13 04:25:12 -04:00
Ryan Scott
7e1694b0ef macaw-symbolic: Fix interval bounds in mkGlobalPointerValidityPred
Fixes #279.
2024-01-24 18:50:11 -05:00
Ryan Scott
aaa5ea1234 Generalize lazy memory model using HasMacawLazySimulatorState
This introduces a `HasMacawLazySimulatorState` data type, which provides a
"classy lens" for accessing a `MacawLazySimulatorState` within some Crucible
personality type. It also generalizes the lazy `macaw-symbolic` memory model in
`Data.Macaw.Symbolic.Memory.Lazy` to be polymorphic over
`HasMacawLazySimulatorState` instances. The upside is that it is now possible
to use the lazy memory model at other personality types besides just
`MacawLazySimulatorState`, making it much easier to extend the memory model.

Because there is a `HasMacawLazySimulatorState` instance for
`MacawLazySimulatorState`, existing code that uses `MacawLazySimulatorState`
should continue to compile without changes.

Fixes #357.
2023-12-12 15:29:48 -05:00
Ryan Scott
c2c2a3d6bd macaw-symbolic: Implement MacawFreshSymbolic for all macaw Types
Rather than `error`ing, we now generate fresh constants for all possible
`macaw` `Type`s that are supplied to the `MacawFreshSymbolic` operation.

Fixes #301.
2023-11-17 17:08:34 -05:00
Ryan Scott
984f7cb368 Support building with GHC 9.6
This patch contains a handful of tweaks needed to make the libraries in the
`macaw` repo build with GHC 9.6:

* GHC 9.6 bundles `mtl-2.3.*`, which no longer re-exports `Control.Monad`,
  `Control.Monad.Trans`, and similar modules from `mtl`-related modules. To
  accommodate this, various imports have been made more explicit.
* I have disambiguated a use of `Data.Parameterized.NatRepr.withKnownNat` in
  `macaw-aarch32` to avoid clashing with a newly exported function of the same
  name in `GHC.TypeNats`.
* I have bumped various upper version bounds on `doctest`,
  `optparse-applicative`, and `what4` to allow building these libraries with
  GHC 9.6.
* I have bumped the following submodules to bring in GHC 9.6–related changes:
  * `asl-translator`: GaloisInc/asl-translator#53
  * `crucible`: GaloisInc/crucible#1102
  * `dwarf`: GaloisInc/dwarf#6
  * `elf-edit`: GaloisInc/elf-edit#38
  * `flexdis86`: GaloisInc/flexdis86#54
  * `grift`: GaloisInc/grift#9
  * `llvm-pretty`: elliottt/llvm-pretty#112
  * `llvm-pretty-bc-parser`: GaloisInc/llvm-pretty-bc-parser#225
  * `semmc`: GaloisInc/semmc#80
  * `what4`: GaloisInc/what4#235
2023-08-21 08:16:10 -04:00
Ryan Scott
e6a3fb0b87 Bump what4 submodule to version 1.4
The only other changes required are (1) deleting an unused dependency on
`what4-serialize`, and (2) raising upper version bounds on `what4`.

This brings in submodule changes from the following:

* GaloisInc/asl-translator#48, which performed a similar `what4` adaptation.
* GaloisInc/semmc#78, which performed a similar `what4` adaptation.
* GaloisIns/crucible#1068, which ensures that everything can build against
  `tasty-sugar >= 2.0` (the version of the library that `what4-1.4` depends on).
2023-03-21 16:05:53 -04:00
Ryan Scott
88d024990b macaw-symbolic: Note lazy memory model in the changelog 2023-03-14 13:27:07 -04:00
Ryan Scott
73b8a49d38 macaw-symbolic: Support newGlobalMemory{,With} in lazy memory model as well 2023-03-14 13:27:07 -04:00
Ryan Scott
dc7c1759f1 macaw-symbolic: Test both memory model configurations in test suites 2023-03-14 13:27:07 -04:00
Ryan Scott
d8fbe228ef macaw-symbolic: Clarify that lazy memory model sacrifices space for time 2023-03-14 13:27:07 -04:00
Ryan Scott
36cade1690 Bundle more options into MemModelConfig
This folds the menagerie of various configuration option arguments to
`macawExtensions` into the `MemModelConfig` data type. The advantage to doing
this is that is will make it easier to extend the memory model configuration
options in the future without needlessly foisting breaking changes on all
`macaw-symbolic` users.

Unfortunately, it does require a breaking change to get to this point, but the
migration path is straightforward for existing code. I have included this
migration story in the `macaw-symbolic` changelog.
2023-03-14 13:27:07 -04:00
Ryan Scott
4a28748030 macaw-symbolic: Add alternative, lazy memory model
The current `macaw-symbolic` memory model has issues when scaling up to
binaries that have several megabytes or more in size. This patch introduces a
new memory model (in `Data.Macaw.Symbolic.Memory.Lazy`) that serves as a mostly
drop-in replacement for the existing memory model (which I now refer to as the
"default" memoy model). The lazy memory model scales better by incrementally
populating the SMT array backing global memory over the course of a run of the
simulator. For the full details, see `Note [Lazy memory model]`.

I performed some refactoring to share common bits between the default and lazy
memory models.

Fixes #282.
2023-03-14 13:27:07 -04:00
Ryan Scott
b5e656c663 Add MemoryModelConfig option for concretizing pointers
This patch introduces a `MemModelConfig` data type for configuring the finer
details of `macaw-symbolic`'s memory model. For now, there is a single option,
which configures whether the memory model should attempt to concretize pointers
during a read or write, which can sometimes be beneficial for performance
reasons. The details of how concretization work can be found in the new
`Data.Macaw.Symbolic.Concretize` module.

Subsequent commits will add more configurable knobs to `MemModelConfig`.

Fixes #323.
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
e6420fc006 macaw-symbolic: Include addresses in populateRelocation
When populating `COPY` relocations, it is helpful to know the address of the
relocation so that it can be related back to the name of the global symbol
whose value it is copying. Unfortunately, the type of `populateRelocation` does
not make it straightforward to compute this address. This patch includes three
additional arguments to `populateRelocation` (the relocation's `Memory`, its
`MemSegment`, and its `MemAddr`) to more easily facilitate computing the
address.

This is a breaking API change, albet it is a fairly straightforward change to
adapt to for most consumers.

This is related to #47, although this is not a full fix for the issue.
2022-09-13 15:26:12 -04:00
robdockins
d9525554ca
Merge pull request #310 from GaloisInc/rwd/redundant-checks
Remove redundant "valid pointer" checks from memory loads and stores.
2022-08-08 14:26:40 -07:00
Tristan Ravitch
20c4083330 Add a ChangeLog entry 2022-08-08 12:37:34 -07:00
Robert Dockins
8cf16cdfcd Remove redundant "valid pointer" checks from memory loads and stores.
The internal memory model operations already assert the same checks.
2022-08-03 09:41:58 -07:00
Ryan Scott
734039274d macaw-symbolic: Generalize some uses of CrucibleState to SimState
This patch was motivated by the need to call `doGetGlobal` from a Crucible
override, where the `SimState` is instantiated with `OverrideLang` rather than
`CrucibleLang`, the latter of which is used in the `CrucibleState` type
synonym. While I was in town, I generalized the types of other operations in
`Data.Macaw.Symbolic.MemOps` where it was reasonable.
2022-07-19 17:03:44 -04:00
Ryan Scott
2791b1050f Adapt to GaloisInc/crucible#998
This bumps the `crucible` submodule to bring in the changes from
GaloisInc/crucible#998, which adds `?memOpts :: MemOptions` constraints to
a handful of additional functions. This requires adding constraints to
some functions in `macaw-symbolic` to accommodate, as well as bumping the
`semmc` submodule to bring in analogous changes from GaloisInc/semmc#76.
2022-06-28 12:10:11 -04: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
54f8793145
Submodule updates (#291)
The primary changes are version bounds to let everything build against the
latest what4 release (1.3)
2022-05-24 18:45:23 -07:00
Tristan Ravitch
659cfff6c9
aarch32: Implement support for conditional calls (#289)
The core of macaw cannot represent conditional calls because the existing block terminators are not sufficiently expressive and it doesn't support creating synthetic blocks to represent control flow not directly tied to machine addresses.

To work around this, we introduce ARM-specific block terminators for conditional calls and plumb them through up to macaw-aarch32-symbolic.

Fixes #288
2022-05-20 15:17:26 -07: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
Ryan Scott
ef0ece6a72 Make newMergedGlobalMemoryWith work over any Foldable collection
This would come in handy for an application where I wish to pass a
`NonEmptyVector` to `newMergedGlobalMemoryWith`. Currently, I have to convert
the `NonEmptyVector` to a `NonEmpty` list to accomplish this, wish seems
wasteful given that `newMergedGlobalMemoryWith` only needs to use the
`Foldable` interface.
2022-04-04 15:03:56 -04:00
Brett Boston
adf3d67614
Add ability to load multiple memories into a flat address space (#268)
This change adds a function `newMergedGlobalMemoryWith`, which acts like
`newGlobalMemoryWith` but takes a list of macaw memories and merges them
into a flat address space.  This aids in reasoning dynamically linked
programs.
2022-03-17 20:34:30 -07:00
Tristan Ravitch
45f8af1e5a
[symbolic] Optimize how initial binary memories are populated (#267)
Before, the API provided by macaw-symbolic asserted the initial value of each byte of memory individually. This was fairly expensive for large binaries, as each such assertion flushed the solver pipe.

This change generates a large conjunction of assertions and sends them all at once. In unscientific testing, this saved half an hour on a large binary.

API Changes:

- Note that it introduces a minor API change. The optimization required that the `sym` parameter be concretely an `ExprBuilder`.
2022-03-10 16:43:00 -08:00
Ryan Scott
a43151963d Note personality-related tweaks in the changelog 2022-02-21 13:27:20 -05:00
Ryan Scott
5c801c5803 Universally quantify personality type in Lookup{Function,Syscall}Handle
This is needed for situations where you want to be able to inspect the
personality type in function/syscall lookups.
2022-02-21 13:27:20 -05:00
Kevin Quick
78b45a10c6 Only fix personality to (MS.MacawSimulatorState sym) where required. 2022-02-21 13:27:20 -05:00
Brett Boston
719aa8fb01
Translate PLTStubs as tail calls (#263)
* Translate PLTStubs as tail calls

This change modifies `addMacawParsedTermStmt` to translate `PLTStub`s as
tail calls.

* Replace CR.Call + CR.Return with CR.TailCall
2022-02-18 15:02:59 -08:00
Brett Boston
ad51ae3c54
Add support for standalone PIEs (#262)
* Add support for standalone PIEs

This changeset adds support for standalone position independent
executables (PIEs) that do not make use of procedure linkage tables.  It
does so by adding relative address support to `populateSegmentChunk` and
adding an additional simplification rule for Aarch32.

This covers part of the work for #234.

* Remove NoOp + replace mult with left shift
2022-02-03 13:51:11 -08:00
Rob Dockins
c572e372db Update changelog 2022-01-24 16:28:32 -08: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
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
Ryan Scott
f9b300a3d1 macaw-symbolic: Add fromCrucibleEndian function
This is like `toCrucibleEndian`, but in the opposite direction.
2022-01-04 16:27:57 -06:00
Ryan Scott
45f991ccdf macaw-symbolic: Consolidate duplicate definitions of toCrucibleEndian
There were two identical definitions of `toCrucibleEndian`, one in
`D.M.S.Memory` and another in `D.M.S.Testing`. This commit removes the
latter in favor of the former, which is actually exported.
2022-01-04 16:27:57 -06:00
Ryan Scott
c900dc4de8 macaw-symbolic: Replace mkName with safeSymbol
Fixes #251.
2022-01-04 16:27:24 -06:00
Tristan Ravitch
9c426986ff Revise handling of some pointer operations
Have all additions (at any bit width) go through the special PtrAdd
handler (rather than BVAdd). Also add special handlers for truncation and
extension.

These changes support architectures that do pointer operations at non-pointer
widths (e.g., to detect overflow).  These new operations apply the named
operations over just the offset of pointers, preserving the block id.
2021-12-23 13:54:29 -08:00