Commit Graph

1642 Commits

Author SHA1 Message Date
Pietro Fezzardi ddebd55629 Add FilteredGraphTraits with tests
This patch adds a two markers for llvm::GraphTraits:
 - NodePairFilteredGraph
 - EdgeFilteredGraph

Both these markers allow to specify a static predicate that is used to
filter edges.
This predicate is a boolean function such that:
  - for NodePairFilteredGraph, it takes a pair of const NodeRef & that
    are used to represent an edge, and it evaluates a given property of
    that pair;
  - for EdgeFilteredGraph, it takes a const EdgeRef & that represents an
    edge, and it evaluates a given property on the edge.
The filtered graph contains only the edges for which the predicate
evaluates true.

Notice that the predicate must have static lifetime, meaning that all
the edge properties must be entirely evaluated on the pair of node (for
NodePairFilteredGraph) or on the edge (for EdgeFilteredGraph).
This means that you cannot pass mutable state to the predicate at
runtime.

The new markers are designed to interoperate well with llvm::Inverse and
to allow you to traverse the marked graphs with llvm::depth_first,
llvm::inverse_depth_first, llvm::breadth_first, and to compute dominator
trees and post-dominator trees on filtered graphs.
2020-06-13 10:36:08 +02:00
Pietro Fezzardi d39c124344 DotGraphObject: support Dom and PostDom Trees 2020-06-13 10:36:08 +02:00
Pietro Fezzardi 45516f89b8 Add Inverse GraphTraits for DotNode and DotGraph 2020-06-13 10:36:08 +02:00
Pietro Fezzardi 2bf702b74b DotGraph: add EdgeRef and ChildEdgeIteratorType
Add EdgeRef and ChildEdgeIteratorType to GraphTraits<DotNode *> and to
GraphTraits<const DotNode *>.

This allows iterating on edges of DotGraphs, which will be useful for
some testing on edge properties.
2020-06-10 00:23:54 +02:00
Pietro Fezzardi a955cb9a50 [DotGraphObject.h] Fix DotGraph::end() methods
This commits fixes a typo, probably a leftover from copy-paste, that
caused `DotGraph::end()` methods to actually return `begin()`.

Nobody was relying on this, which caused this bug to go unnoticed.
2020-06-10 00:23:51 +02:00
Pietro Fezzardi 66873d9c26 Fix legacy::FunctionPassManager initialization and finalization.
This commit adds calls to
`legacy::FunctionPassManager::doInitialization()` and
`legacy::FunctionPassManager::doFinalization()` before and after calls
to `legacy::FunctionPassManager::run()`.

`legacy::FunctionPassManager`s must be explicitly initialized before
running and finalized after running.
Given that we used these managers only in two places and their are the
only ones that need explicit initialization and finalization, we forgot
to do it.

This caused problems with recent versions of llvm.
In particular, when compiled with aggressive optimizations (at least
`-O2`), missed initialization resulted in null pointers being
dereferenced, causing crashes.
The crashes were caused by the compiler optimizing away `nullptr` checks
on values returned from `getPSI`.
2020-06-06 02:25:45 +02:00
Alessandro Di Federico 21c0524125 Merge branch 'feature/arm-thumb' 2020-06-02 22:32:11 +02:00
Alessandro Di Federico 9401e33d3f Re-enable working tests
Some tests have been disabled in the past, but they work.
Specifically, this commit re-enables the `memset` and `fibonacci` tests.
The commit also describes why the test remaining disabled are so.
2020-06-02 21:14:57 +02:00
Alessandro Di Federico 2e0266ed05 mv {JumpTargetManager,BinaryFile}::nameForAddress 2020-06-02 21:14:56 +02:00
Alessandro Di Federico 3f79683c18 Drop CFG suffix from CFGForm::Values 2020-06-02 21:14:55 +02:00
Alessandro Di Federico 23f1a7b342 Reduce blocks analyzed by AVI
AVI and InstCombine are the bottleneck of the lifting process.
This commit introduces a whitelist of jump targets that are considered
by AVI during harvesting.
The whitelist is initialized by the jump targets that are new with
respect to the last run of AVI. Then, it's expanded with all the jump
targets that can reach the initial set of jump targets through direct
jumps.
2020-06-02 21:14:50 +02:00
Alessandro Di Federico b27beecc0f Preliminary harvesting: s/InstCombine/ConstProp/ 2020-06-02 10:57:02 +02:00
Alessandro Di Federico c202b989af AVI: drop one InstCombinePass
Preparing for AVI we used to run InstCombine twice in the
pipeline. Apparently, this was not acutally necessary.
2020-06-02 10:57:02 +02:00
Alessandro Di Federico 6e6aeac45d Keep sum of pushed values in RunningStatistics 2020-06-02 10:57:02 +02:00
Alessandro Di Federico 1f7b23bb5a Introduce harvesting statistics 2020-06-02 10:57:02 +02:00
Alessandro Di Federico 99486d0b6b Update tests' references after new dispatcher 2020-06-02 10:57:02 +02:00
Alessandro Di Federico 773e386f22 Prevent SIGSEGV in translation with guard pages
When translation of code overflows into an unmapped page, we can get a
SIGSEGV. To avoid this, for each set of contiguous pages, we add "guard
page" containing an architecture-specific pattern that ensure basic
block termination.
2020-06-02 10:57:02 +02:00
Alessandro Di Federico d10178483d Introduce the new MetaAddress
Unlike the previous iteration of `MetaAddress`, which tried to stuff all
the parts of `MetaAddress` within the existing `PC` CSV, this
implementation adds a set of new CSVs (or marks some existing ones as) to
represent the four portions of the current PC's `MetaAddress`.

* Introduce `ProgramCounterHandler`: a class responsible to maintain the
  PC-related CSVs. This class is also used to manipulate the new
  dispatcher.
* `AdvancedValueInfo`: update for new MetaAddress.
* External jump handler: do not clobber registers.
  When introducing support for dynamic binaries, we didn't realize that
  in x86-64 we were clobbering `r11`. To avoid this, we have to jump to
  an address stored in memory. However, due to the new `MetaAddress`,
  obtaining a *jumpable* address from the PC-related CSVs might require
  some computations (and it does in ARM). Therefore, we introduce a new
  global variable, `jumpablepc`, whose only role is to contain the
  jumpable version of the program counter and then be the target of the
  memory-indirect jump instruction.
* Labels care only about absolute addresses.
* CSAA: mark call site, even if no accesses.
2020-06-02 10:57:02 +02:00
Alessandro Di Federico 10435feb59 Whitespace and other minor changes 2020-05-14 22:55:18 +02:00
Alessandro Di Federico 89258b832d Add ARM's PCMContextIndex
`PCMContextIndex` is the index of the PC register within the `mcontex_t`
`struct`.
2020-05-14 22:55:18 +02:00
Alessandro Di Federico 1e5fd5cc25 BinaryFile::readRawValue: prevent invalid reads
It might happend that, while parsing an input binary, we are request to
read at a valid address, but a for a size leading outside of a valid
page.

This commit, copies as much data as possible from the valid page into a
temporary buffer of the appropriate size, and then performs the read on
that temporary buffer.
2020-05-14 22:22:31 +02:00
Alessandro Di Federico 429567b885 Handle binaries without ELF program headers
Not all binaries have ELF program headers, e.g., Mach-O binaries.
2020-05-14 22:22:31 +02:00
Alessandro Di Federico 20d346aaf7 Fix handling of Mach-O LC_UNIXTHREAD 2020-05-14 22:22:31 +02:00
Alessandro Di Federico b5d719efb5 Indent multiline log entries 2020-05-14 22:03:37 +02:00
Alessandro Di Federico 2e8e594304 StackAnalysis: handle UndefValue 2020-05-14 21:59:04 +02:00
Alessandro Di Federico 885d705938 FunctionIsolation: handle nullptr and undefs 2020-05-14 21:59:04 +02:00
Alessandro Di Federico b5a1fa2c32 Disable Architecture's copy constructor 2020-05-14 21:59:03 +02:00
Alessandro Di Federico d704e88550 getBasicBlockPC: handle empty blocks 2020-05-14 11:59:05 +02:00
Alessandro Di Federico 2548df7092 QuickMetadata: improve ConstantInt handling 2020-05-14 11:59:05 +02:00
Alessandro Di Federico 22232441dd getName: print pointer as fallback 2020-05-14 11:59:05 +02:00
Alessandro Di Federico 34d7a8e364 Let ConstantRangeSet::dump work on any stream 2020-05-14 11:59:05 +02:00
Alessandro Di Federico e51d6e5458 Make the revng script compatible with clang's ASan 2020-05-14 11:59:05 +02:00
Alessandro Di Federico de172232e6 Run the most specific tests first 2020-05-14 11:59:05 +02:00
Alessandro Di Federico 149ef61759 Fix unit tests linking
For unit tests we used to call `add_executable` directly. However, this
prevents using RPATH as appropriate.
2020-05-14 11:59:05 +02:00
Alessandro Di Federico aea5ebe0cc Enable "autobrief" doxygen comments
This option automatically detects brief description without `\brief`.
2020-05-14 11:59:05 +02:00
Alessandro Di Federico 9e52b0c473 Switch ARM tests to Thumb 2020-05-14 11:59:05 +02:00
Alessandro Di Federico 2531cc3c60 The link register has to be an ABI register
This is to prevent the PC (or similar CSVs) from being considerate as a
link register.
2020-05-14 11:59:05 +02:00
Alessandro Di Federico 9cfb96c18c Run InstCombine on generated code
We now run InstCombine on the generated code in order to simplify
further analyses. In particular this is useful to handle code such as
the following:

    %pc = load @pc
    %is_thumb = and %pc, 1
    %address = and %pc, ~1
    %pc2 = or %is_thumb, %pc

After InstCombine `%pc` and `%pc2` are collapsed.
2020-05-14 11:59:05 +02:00
Pietro Fezzardi 1c02dfd2a5 CSAA: strip bitcasts on pointers to env 2020-05-14 11:59:02 +02:00
Alessandro Di Federico dc48188f37 Introduce MetaAddress
`MetaAddress` replaces all the `uint64_t` used to represent a virtual
address. Its main features are:

* It has a non-zero representation of invalid addresses.
* It supports tags to represent code that has different interpretations but
  resides at the same address in memory (namely ARM vs Thumb).
* Arithmetic operations cannot overflow.
* It supports epochs, a way we intend to employ to handle self-modifying code
  (i.e., different code at the same address at different times).
* It supports "address spaces", which enable handling architectures with
  multiple address spaces.
* It fits in two 64-bit registers.
2020-05-14 11:58:18 +02:00
Alessandro Di Federico e336a33545 Move GCBI::getPC to IRHepers.h 2020-05-14 11:58:18 +02:00
Alessandro Di Federico 58a070349b Fixes in handling DWARF information 2020-05-14 11:58:18 +02:00
Alessandro Di Federico 35171a789d Sort translated basic blocks in reverse post-order
After translation, `revng-lift` now sorts all the basic blocks in
reverse post-order to ease manual reading of the generated modules.
2020-05-14 11:58:18 +02:00
Alessandro Di Federico 20c367b88f Add ArchitectureName to revng.input.architecture 2020-05-14 11:58:18 +02:00
Alessandro Di Federico da9e6094d3 Introduce Label::hasValue 2020-05-14 11:58:18 +02:00
Alessandro Di Federico d83eca0902 Use ConstantExpr::isCast()
Simplify `getConstValue` in `IRHelpers.h` to use `ConstantExpr::isCast`.
2020-05-14 11:58:18 +02:00
Alessandro Di Federico cb803dce70 BinaryFile::parseELF: fix labels creation
The loop creating the labes in `BinaryFile::parseELF` was badly indent
and resulted in the creation of labels only if the `.dynamic` section
was available.
2020-05-14 11:58:18 +02:00
Alessandro Di Federico a52fb24e02 Various improvements to StackAnalysis
* Ignore casts in `BasicBlockState::get`
* Fix some types
* Do not move `Element::bottom()`
2020-05-14 11:58:18 +02:00
Alessandro Di Federico 78dafb04da IFI::cloneInstruction: handle ConstantAggregate 2020-05-14 11:58:18 +02:00
Alessandro Di Federico 72fc95bda9 Purge OriginalInstructionAddresses when deleting
`JumpTargetManager::purgeTranslation` deletes some basic blocks that
need to re-translated, however references to some instructions remained
in `OriginalInstructionAddresses`.
2020-05-14 11:58:18 +02:00