Commit Graph

390 Commits

Author SHA1 Message Date
Valentin Robert
9e61b39f98 fix assertPred ignoring its boolean flag for equalities
This is a fix for #424. The fix consists in splitting the logic for
equality tests based on the value of isTrue. I have separated it as its
own function as it's needed for two branches, and Haskell lacks binding
or-patterns.
2024-08-21 10:13:50 -07:00
Valentin Robert
2e495d8de9 refactor assertPred to avoid duplicated checks 2024-08-21 10:13:44 -07:00
Ryan Scott
93b588a58b macaw-base: Resolve RISC-V relocations
This builds on top of the work in GaloisInc/elf-edit#45. For now, I only add
support for a select few relocation types, leaving the rest as future work.

This paves a way for an eventual fix for #414.
2024-07-31 10:11:53 -04:00
Valentin Robert
eb0a3c7ccc
Merge pull request #406 from GaloisInc/vr/register-use-bvand
support alignment ops when processing statements in `RegisterUse`
2024-07-25 12:14:33 -07:00
Stanislav Lyakhov
75e31f8338 Improve handling of JumpBound truncation constraints
Given some symbolic value, @x@, we'd like to compute its
possible upper and lower bounds after it is truncated to @w@ bits.

To do this, we first find the bound of x by (recursively) calling `exprRangePred`.
This bound is a statement of the following form (see `RangePred` for more info):
"r bits of x are bounded between @low@ and @high@".

Then, we check the following:
- If x has a bound (r, l, w)
AND
- If r is less than or equal to w
=> Pass-through the bound (r, l, w)
Otherwise, we deem x "unbounded"

Declaring x unbounded in the second case seems to throw away useful
information that causes many jump tables to remain unclassified.
We attempt to improve on that in this commit.

Consider an example where x is bounded by (64, 0, 10)
(that is, the 64 bits of x are constrained to be between 0 and 10)
and we want to find the bound of truncating x to 8 bits.

With the current logic, since 64 > 8, we'd declare x unbounded.
However, the bound (8, 0, 10) should also be valid: if 64 bits of
x are bounded to [0, 10], then surely 8 bits of x also lie between
0 and 10.

If the upper bound is instead larger than the largest 8-bit value, we
can truncate it to the largest value.
For example, (64, 0, 10000) becomes (8, 0, 255).
Instead of losing the bound completely, we're able to tighten it!
2024-07-24 13:44:04 -07:00
Valentin Robert
dc1591d168 support alignment ops when processing statements in RegisterUse
For some reason we were very conservative in our support or abstract
operations over the processor state in the `RegisterUse` analysis.

In particular, we were failing to process code such as:

r23 := (bv_and r21 (0xfffffffffffffff0 :: [64]))

whose goal is to align the value in r21 at a 16-byte boundary.

This resulted in us failing to analyze some code that was realigning its
stack pointer.  With this change, the same code succeeds at propagating
the abstract stack pointer offset forward.
2024-07-24 09:59:45 -07:00
Valentin Robert
32244627f2
Merge pull request #405 from GaloisInc/vr/bits-memint
add a `Bits` instance to `MemInt`
2024-07-24 08:43:02 -07:00
Valentin Robert
d2561b1078 add a Bits instance to MemInt
This helps supporting bitwise operations over `MemInt`s without having
to unwrap/rewrap them into `Int64`s.
2024-07-23 16:57:06 -07:00
Valentin Robert
b380174c8b expose memInt in Memory
A future fix I'm going to submit would benefit from having this exposed,
and we believe this to be a good idea regardless.
2024-07-23 16:09:03 -07:00
Valentin Robert
77a6c62352
parse subroutine type declaration formal parameters (#382)
* parse subroutine type declaration formal parameters

This is useful for Reopt as we want to get more accurate type
information for library functions.
2024-05-22 10:47:16 -07:00
Ryan Scott
442b0e9f00 macaw-base: Fix R_X86_64_RELATIVE offset computation
Fixes #316.
2024-04-11 09:06:50 -04:00
Valentin Robert
da66e03973
Merge pull request #377 from GaloisInc/vr/fix-doc
fix copy-pasted docstring
2024-04-08 11:24:20 -07:00
Valentin Robert
4fa43a439d Show instance for MemSymbol 2024-04-08 10:06:58 -07:00
Valentin Robert
0b9d062426 fix copy-pasted docstring 2024-04-08 10:03:47 -07:00
Valentin Robert
1d3f2a3331 add ShowF instance for FloatInfoRepr 2024-03-13 15:42:07 -07:00
Valentin Robert
8569727885 fix incorrect documentation 2024-01-22 18:37:11 -08:00
Valentin Robert
c8a8845983
Merge pull request #364 from GaloisInc/vr/minor-cleanups
minor cleanups
2024-01-05 13:27:09 -08:00
Valentin Robert
f8f406df4f minor cleanups 2024-01-05 13:26:44 -08:00
Valentin Robert
0f2264b7c4 add Pretty instance for NoReturnFunStatus 2024-01-05 11:46:25 -08:00
Valentin Robert
d2f7028e2d favor ArchAddrWidth and ArchSegmentOff type synonyms 2023-12-11 09:53:53 -05:00
Valentin Robert
9e09fc86e7
Merge pull request #353 from GaloisInc/vr/remove-extension
remove unused extension
2023-12-07 12:27:35 -08:00
Valentin Robert
822ea653c6
Merge pull request #354 from GaloisInc/vr/derive-show
derive `Show` for `FunctionArgAnalysisFailure`
2023-12-07 12:27:11 -08:00
Valentin Robert
b87da1501b derive Show for FunArgAnalysisFailure
It's sometimes practical to debug the map of analysis failures by
printing them all out, but we don't seem to have any facilities for
printing them.
2023-12-07 09:09:19 -08:00
Valentin Robert
dacd1562f0 remove unused extension 2023-12-07 09:08:52 -08:00
Valentin Robert
9197aaa38f fix typo 2023-12-06 21:21:53 -08:00
Ryan Scott
2fb4ab4291 macaw-base: Resolve PPC{32,64} relocations
This builds on top of the work in
https://github.com/GaloisInc/elf-edit/pull/41. For now, I only add support for
a select few relocation types, leaving the rest as future work.
2023-11-03 11:38:11 -04:00
Valentin Robert
4cfed47b7b fix BlockInvariants docstrings
The alignment was all wonky, and one field was not documented.
2023-09-07 16:57:30 -07: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
Valentin Robert
7e8f7b979b make error casing uniform
This was the only classifier error message not capitalized.
2023-08-11 16:53:41 -07:00
Valentin Robert
133f3bd9d5 Pretty instance for BlockExploreReason 2023-08-10 14:38:17 -07:00
Valentin Robert
24a18d26d3
Merge pull request #336 from GaloisInc/vr/explore-block-reason
explore BlockExploreReason in DiscoveryEvent
2023-08-10 14:37:45 -07:00
Valentin Robert
3638d6e2b0 explore BlockExploreReason in DiscoveryEvent
It can be nice from an external consumer's point of view to have the
reason for the exploration of a block.
2023-08-09 14:44:14 -07:00
Valentin Robert
417e8b780b remove redundant pragmas 2023-08-09 14:31:50 -07:00
Valentin Robert
e089aa220c applying reasonable hlint suggestions
Co-authored-by: Ryan Scott <rscott@galois.com>

newlines
2023-07-28 10:15:10 -07:00
Ryan Scott
e55add0b51 Support building with GHC 9.4
This contains a variety of tweaks needed to make the libraries in the
`macaw` repo build with GHC 9.4:

* `ST` no longer has a `MonadFail` instance. See
  [this section](https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/9.4?version_id=b60e52482a666d25638d59cd7e86851ddf971dc1#st-is-no-longer-an-instance-of-monadfail)
  of the GHC 9.4 Migration Guide. To adapt to this change, I had to change some
  uses of `fail` to `panic`, and I also had to avoid some partial pattern
  matches in `do`-notation to avoid incurring `MonadFail (ST s)` constraints.
* GHC 9.4 is pickier about undecidable superclass checking. As such, I needed to
  explicitly enable `UndecidableSuperClasses` in a handful of places.
* The following submodule changes were brought in to support building with
  GHC 9.4:
  * `asl-translator`: GaloisInc/asl-translator#51
  * `bv-sized`: GaloisInc/bv-sized#27
  * `bv-sized-float`: GaloisInc/bv-sized-float#4
  * `crucible`: GaloisInc/crucible#1073

    (This also requires bumping the `llvm-pretty`, `llvm-pretty-bc-parser`,
    and `what4` submodules as a side effect)
  * `dismantle`: GaloisInc/dismantle#40
  * `grift`: GaloisInc/grift#8
  * `macaw-loader`: GaloisInc/macaw-loader#17
  * `semmc`: GaloisInc/semmc#79
2023-04-18 15:17:23 -04:00
Ryan Scott
02c2bc468e macaw-base: Introduce *.Panic module
This is cargo-culted from the `*.Panic` module of a similar name in
`macaw-aarch32`. This will be useful in a subsequent commit in which we replace
some unreachable calls to `fail` with `panic`.
2023-04-18 15:17:23 -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
122db57c99 Consistently use symbol table functionality from elf-edit
This:

* Bumps the `elf-edit` submodule to bring in the changes from
  GaloisInc/elf-edit#34.
* Updates `Data.Macaw.Memory.ElfLoader` to consolidate the symbol table logic
  with the corresponding functions from `elf-edit`.

Fixes #277.
2023-02-23 17:16:12 -05:00
Ryan Scott
fa3f82314a macaw-base: Document hashable-related API changes 2023-02-20 17:49:17 -05:00
Valentin Robert
aa127f240f support hashable-1.4 2023-02-16 13:55:30 -08:00
Ryan Scott
560f292d16 macaw-base: Support loading R_ARM_COPY ELF relocations
We already have support for `R_X86_64_COPY` relocations, so adding support
for `R_ARM_COPY` on the AArch32 side is straightforward.

This is related to #47, although this is not a full fix for the issue.
2022-09-13 15:20:22 -04:00
Tristan Ravitch
6a4f406c68 Revisit handling of tail calls
It turns out that we have to be more conservative with tail call identification,
as incorrectly identifying a block as the target of a tail call (instead of a
branch) can cause other branch classifiers to fail if that block is the target
of another jump.

Ultimately, we will need to give up some tail call recognition (since they are
in general indistinguishable from jumps), and instead only identify known call
targets as tail call candidates.

With additional global analysis we could do better.

Fixes #294
2022-06-27 15:02:43 -07:00
Tristan Ravitch
f217bf860e Remove an unused NatRepr 2022-06-09 13:07:30 -07:00
Tristan Ravitch
3011920fc5 Improve a classifier failure diagnostic and fix a typo
The call classifier was missing a label, which led to a less readable error
message in the case where all classifiers fail.
2022-06-09 13:07:30 -07:00
Tristan Ravitch
9296692138 Fix a sign extension bug in the abstract domain calculation
The bug arose in the handling of `StackOffsetAbsVal`, which track an abstraction
of references relative to the stack pointer. The offsets in `StackOffsetAbsVal`
are `Int64`; they are signed because references are both above and below the
stack pointer.  The code constructing new values of this type was incorrectly
zero-extending new offsets instead of sign extending them.  This did not matter
on 64 bit architectures, as it happened to result in the same values.  It
substantially corrupted the abstract stack on PowerPC 32.  It did not seem to
affect AArch32, but that is likely just due to luck in compiler code generation
that does not require this level of precision in the abstract stack.

The resulting errors manifest in the `absEvalCall` function. Because of the lack
of sign extension in `StackOffsetAbsVal`s, it made the current stack pointer
look like a huge number, which caused *all* stack entries to be dropped after
function calls.

This fix simplifies the stack offset abstract value computation substantially
and ensures that signs are extended correctly. The commit adds a PowerPC32 test
case that only passes with this fix.
2022-06-09 13:07:30 -07:00
Tristan Ravitch
3d95517097 Remove an unused export
This function is used internally, but no clients use it
2022-06-09 13:07:30 -07: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
Ryan Scott
3f65a253a1 Fix -Wunused-imports warning with base-4.16.*
In `base-4.16.*`, `Nat` is now a type synonym for `Natural`, and `GHC.TypeLits`
now re-exports `Natural`. This causes a `-Wunused-imports` warning in
`macaw-base` as a consequence. I fixed the warning by tightening up the imports
slightly.
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
Ryan Scott
c86e13d35c Adapt to dynSymEntry being definition-aware in elf-edit
This bump the `elf-edit` submodule to bring in the changes from
https://github.com/GaloisInc/elf-edit/pull/29, which adds an additional
`VersionDefMap` argument to `elf-edit` to make it aware of version definitions.
This requires some changes to the API in `Data.Macaw.Memory.ElfLoader` to
accommodate.
2022-04-19 12:47:24 -04:00