Commit Graph

3168 Commits

Author SHA1 Message Date
Pietro Fezzardi 5a826848fe RemovePCStoresPass: properly detect isolated funcs 2020-07-20 19:02:20 +02:00
Pietro Fezzardi 1a9ae59e4a RestructureCFG: major refactoring of switches
Now edges have sets representing labels.
There's no distinction between Dispatchers and Regular switches at the
GHAST level anymore.
2020-07-20 19:02:19 +02:00
Pietro Fezzardi 0e5dceeed4 ASTNode: fix isEqual method for switch nodes
This was broken after the introduction of `SwitchDispatcherNode`,
because
both `SwitchDispatcherNode` and `RegularSwitchNode` used the `isEqual`
method from `SwitchNode`.

Now the `isEqual` method is implemented only for the `ASTNode` base
class, and each leaf of the llvm-RTTI hierarchy of `ASTNode` properly
implements the comparison.
2020-07-20 19:02:19 +02:00
Pietro Fezzardi 7f6ce70c82 Refactor RegionCFG::inflate() method 2020-07-16 10:48:12 +02:00
Andrea Gussoni 47f797ba9a RegionCFGTree: fix AST simplification for switches
In presence of a switch node, which contains a case made up of
entirely a sequence of empty nodes (simplified by the
`simplifyAtomicSequence` recursive function), enable the removal of the
aforementioned case from the switch node.
2020-07-14 15:28:20 +02:00
Pietro Fezzardi a078be2478 RestructureCFG: avoid dedicated untangling of root
This commit removes a call to untangle() that was performed before the
whole combing algorithm on the root RegionCFG.

The call was redundant before introducing the weaving, and became
plainly wrong after adding the weaving, because it ended up trying to
untangle the root RegionCFG before weaving (which is supposed to run
first).

Removing the dedicated call fixes the bug, because weaving is performed
first as part of the call to generateAst().
2020-07-09 18:17:20 +02:00
Alain Carlucci be9bc6ce0a MetaAddress: remove header dependency from IRBuilder 2020-06-25 14:28:07 +02:00
Alessandro Di Federico 314cae3a1e Abandon removeUnreachableBlocks
`llvm::removeUnreachableBlocks` doesn't just drop dead basic blocks,
it also turns certain `SwitchInst` into `BranchInst`, which leads to
hard-to-debug issues in parts of the code where we don't assume
unexpected changes such as this.
2020-06-23 15:01:52 +02:00
Alessandro Di Federico d8cbc86b1c Isolate: handle IndirectBranchDispatcherHelper
Function isolation used to mishandle blocks of type
`BlockType::IndirectBranchDispatcherHelperBlock`. This led to have
empty basic blocks instead of the dispatcher for handling indirect
branches.
2020-06-23 14:55:13 +02:00
Pietro Fezzardi f7e8599c5e Enforce check-conventions.sh
All commits should pass check-conventions.sh from now on.
2020-06-15 15:40:02 +02:00
Andrea Gussoni afa0cfbab4 Emit actual case values for regular switches
We now emit `RegularSwitch` node type creating the vector containing the
case values by inspecting the actual values of the `ConstantInt` in the
IR.

Due to the introduction of weaving, we can have a cascade of switches,
and in this case the top switch should bring to a certain weaving switch
for multiple values of the case label.
For this reason, switch case are now represented by sets, which are
usually populated by a single value, but in presence of a weaving
switches they can represent the fact that for each value contained in
the set we must take a certain case label.
The backend of the decompiler has been updated to reflect this change,
in order to emit all the values for a certain `CaseSet` in `or` if the
size of the seat is greater than 1.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 95dbc22a36 Remove IfDispatcherNode
Remove the `IfDispatcherNode` from the hierarchy of the `ASTNode`s.
Disable the beautify routines since they are not relevant anymore.
2020-06-15 15:06:23 +02:00
Andrea Gussoni a264117aac Rename IfCheck in IfDispatcher
Now that check nodes are nomore a thing, rename the `ifcheck` in the
AST with a meaningful name.
2020-06-15 15:06:23 +02:00
Andrea Gussoni dc944d35f5 Rename SwitchCheck in SwitchDispatcher
Now that check nodes are nomore a thing, rename the switches in the AST
with a meaningful name.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 933dd20e44 Restructure dispatcher creation
Now that `Check` nodes are nomore a thing, restructure the dispatcher
creation code in order to be more clean.
2020-06-15 15:06:23 +02:00
Andrea Gussoni e611e47a20 Remove Check nodes
Check nodes are nomore a thing. Remove all methods and accessor related
to them.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 1c842fbd28 Rework Dispatcher kind attribute and constructor
Reorganize attribute `Kind` incorporating it into the `Type` attribute,
by lowering its memory footprint.
Modified the constructors, some accessors and helpers accordingly.
2020-06-15 15:06:23 +02:00
Andrea Gussoni be39c9a5e3 Actualize assert to new node types 2020-06-15 15:06:23 +02:00
Andrea Gussoni a8b25fdd52 Differentiate entry and exit dispatcher
Differentiate the entry and exit dispatchers introduced during the
restructuring with a different `Kind` enum inside the node.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 1f94d0fdd7 Remove assertions on SwitchNode
Disable assertions which are no more valid since `CheckNodes` are no
more present in the `RegionCFG`.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 0fb4fc6503 Enable the untangle procedure again 2020-06-15 15:06:22 +02:00
Andrea Gussoni ca747929e8 Build new dispatchers during restructuring
Build the new dispatcher nodes type (switches instead of nested trees of
`if`) during the control flow restructuring phase.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 0a630cc9ea Handle Switch in AST construction analysis 2020-06-15 15:06:22 +02:00
Andrea Gussoni aacb65238a HACK: temporarily disable untangle
Disable the untangle processing until we understand what we need to do
with the new switch nodes.

In this way we can proceed with the prototyping.
2020-06-15 15:06:22 +02:00
Pietro Fezzardi 2fc342d2e4 Add a global public Logger: VerifyLog
Rationale: it is a widespread practice, both in revng and in projects
that depend on it, to write verification functions that check specific
properties hold after different transformations on various data
structures.
Often, these verification function are very useful for debugging and
during development, but they can be very costly and we don't want to
always execute them at runtime.

This patch adds a global public Logger, called VerifyLog, that can be
enabled with the --debug-log=verify command line argument.
This Logger is intended to be used in revng and in projects that depend
on it, as a guard for costly calls to verification functions that do not
need to be performed on a typical execution, but only when debugging.

For now the only user is JumpTargetManager, but other uses are already
envisioned.
2020-06-14 23:34:14 +02:00
Pietro Fezzardi 0eb179baf8 GCBI: relax assumptions on AnyPC and UnexpectedPC
This commit makes possible to execute the GeneratedCodeBasicInfo (GCBI)
Pass even on LLVM IR where the AnyPC and UnexpectedPC BasicBlocks are
not present.
This makes the GCBI pass more flexible, enabling more other passes to
depend on it and to use it to retrieve informations on the generated
code without architecture-dependent hacks.

As an example, one can now use GCBI to retrieve the CSV representing the
Stack Pointer Register or the Program Counter Register without relying
on the register names (which are architecture-specific), even if some
optimization pass along the decompilation pipeline has removed AnyPC or
UnexpectedPC.

Notice that the contracts of GCBI's methods has not been changed.
Calling anyPC() or unexpectedPC() still asserts that those are not
nullptr.
The contract has just been moved from the execution stage of the pass
(basically runOnModule), to the APIs used to query the results.
2020-06-14 23:33:02 +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
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 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 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 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 20c367b88f Add ArchitectureName to revng.input.architecture 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 8deab9c7ea Whitespace and other minor changes
* Drop unused argument names from function prototypes
* Make `static` some methods
* Disable some copy constructors
* Fix casing of Doxygen `\file` directives
* Add some casts to make the compiler happy
* Initialize `hasRelocationAddend` for AArch64
* Use references in range-for where possible
* Drop default for `switch` statements covering all the entries of an `enum`
* Make some global variables `static`
* Drop dead functions
2020-05-14 11:58:18 +02:00
Pietro Fezzardi 6e2fa0c9cb Switch to llvm 10 2020-03-27 08:58:20 +01:00
Alain Carlucci 469ef9cae8 Rename emit to flush in Debug.h
The `emit` keyword is used in Qt and, if the method is called `emit`,
rev.ng headers cannot be included in a Qt project.
2020-02-27 12:12:25 +01:00
Pietro Fezzardi fc2912ca32 enforce clang-format 2020-01-15 18:24:28 +01:00
Pietro Fezzardi 491e1b9aad Switch to c++2a 2020-01-15 18:24:28 +01:00
Pietro Fezzardi c3a6a09f11 Upgrade to llvm-9 2020-01-15 18:24:28 +01:00
Andrea Gussoni e4df18f851 HACK: fix 128 uint and int emission 2020-01-15 18:16:24 +01:00
Andrea Gussoni 86f35b6d93 Various Untangle improvements
Introduced various untangle algorithm improvements:
- Improve untangle edge reorganization: improve the criterion used for
  reorganizing the edges between the old postdominator and the cloned
  one, after an inline procedure is attempted during the untangle.
- Untangle counter: introduce two new counters which take into account
  the number of times the untangle procedure is attempted and the actual
  times it is performed.
- Disable not dominated restriction: disable the criterion which
  restricts the untangle opportunities if we do not dominate entirely
  at least one of the two branches. This has likely broken the
  assumption that the postdominators of nodes do not changes after each
  untangle step has been carried out.
- Change dominance criterion: the dominance of the nodes belonging to
  the `then` and `else` nodes is now checked not with respect to the
  conditional node, but to the `then` and `else` edges of the conditional
  node.
- Untangle eager inlining: now, when the untangle procedure finds a
  suitable candidate for the inlining, proceed the complete inlining
  starting from the selected branch. By default new clones of all the
  nodes till the exit are created and attached to the branch, while the
  original nodes are detached. An additional pass which removes eventual
  dandling nodes (nodes which are not reachable from the entry node of
  the graph) is performed after the eager inlining. This means that if
  no other incoming edges to this group of nodes is present the original
  nodes will be purged.
  The post dominator now is updated during the inlining analysis, and
  the paths conducting to inlined exits are not taken into account for
  the post dominator computation.
- Enable untangle for mixed branches: perform the untangle even if the
  `then` and `else` branches share nodes. This was not feasible before
  because without the eager inlining we couldn't decide which edge to
  attach to the new postdominator clone. This problem is not present
  anymore, so we can handle any kind of topology in the untangle.
- Improved also the analysis information serialized by the decompiler.
2020-01-13 12:13:42 +01:00
Andrea Gussoni f0bedc973e Improve graph weight computation
Improve the duplication weight computation with the percentage increase.
2020-01-13 11:57:04 +01:00
Andrea Gussoni 30bc1d333e Decompile a single function
Add a flag to enable the decompilation of a single function.
In order to have a single flag shared between the `RestructureCFGPass`
and the `CDecompilerPass`, we added a new dedicated decompilation unit
called `TargetFunctionOption`.
2020-01-13 11:55:32 +01:00
Pietro Fezzardi 5a4e47c535 CDecompilerBeautify: fix wrong switch fallthrough 2019-11-25 15:30:41 +01:00