Commit Graph

2366 Commits

Author SHA1 Message Date
Pietro Fezzardi e34dbe1b70 Add RemoveNewPCCallsPass
This pass removes all the calls to `newpc`.
This pass needs to run before the DLA and before the decompilation pass.
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 bebd3a3f33 Fix creation of new nodes during weave
Before this commit, all the nodes created by weaving were dispatcher
switches. This is wrong, because when you weave a switch coming from
original assembly code you lose information if you lower it to a
dispatcher (for example which variable you are switching on).

This commit likely breaks the emitted C code (which was actually broken
anyway in case a code switch was lowered to a dispatcher).
Later commits will propagate this change to the emission of C code to
fix it.
2020-07-20 19:02:19 +02:00
Pietro Fezzardi 1ca39d25f0 Add Weaved field to BasicBlockNode<T>
This is meant to represent the fact that a node was created during the
switch weaving.
2020-07-20 19:02:19 +02:00
Pietro Fezzardi a7b3e34d85 RegionCFGTree: add assertions in addArtificialNode
This is meant to ensure that we never create artificial nodes with
unexpected Kind.

Expected kinds are only Empty, Break, Continue.
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
Pietro Fezzardi bc9ac5f9eb Add void RegionCFG::dumpDotOnFile(const char *)
This method is just a wrapper, useful when debugging, when creating a
std::string from within gdb is painful.
2020-07-14 21:22:21 +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 a89ebde96a RegionCFGTree: handle switches in AST simplify
The functions `simplifyAtomicSequence` and `simplifyDummies` did not
handle switches properly.
This commit ensures they are handled.
2020-07-10 16:49:40 +02:00
Pietro Fezzardi 023a703e43 RegionCFGTree: update CaseSet for nested weaving 2020-07-10 16:49:40 +02:00
Pietro Fezzardi 0505b1754f RegionCFGTree: weave also switches with 3 cases 2020-07-10 16:49:40 +02:00
Pietro Fezzardi c28bcda012 RegionCFGTree: keep InlinedEdges up-to-date 2020-07-10 16:49:40 +02:00
Pietro Fezzardi 05e79bb607 RegionCFGTree: remove dead code 2020-07-10 16:49:40 +02:00
Pietro Fezzardi c5f584db65 RegionCFGTree: fix untangle dominance criterion
The untangle procedure evaluates the cost of Combing, the cost of
performing Untangling on the 'then' side, and the cost of performing the
Untangling on the 'else' side.
To compute the costs related to 'then' and 'else' branches, it uses the
dominance relationships between then/else and the nodes they can reach.

To do so, it builds a set of nodes that are dominated by the 'then'
__edge__, and by the 'else' __edge__.
Notice, __edge__ is important here.

Before this commit, the computation of these two sets was wrong.
To decide if node was dominated by the 'then' __edge__ it checked if the
node was actually dominated by the __target node of the 'then' edge__,
which is not correct. For a case where this is wrong see the following:

A --(else edge)-> B --> C --> D
 \                      ^
  \                    /
   -----(then edge)----

Here D is not dominated by the (then edge), but it is dominated by C,
which is the target of the (then edge).
A similar example can be devised for the else edge.

This commit fixes the computation of the two sets, using the dominance
w.r.t. the edge instead of the dominance w.r.t. the target node of the
edge.

This fixes some cases where the untangling was taking the wrong
decision.
2020-07-09 18:17:20 +02:00
Pietro Fezzardi 6c1721dbdd RegionCFGTree: remove dead code from untangling 2020-07-09 17:12:47 +02:00
Andrea Gussoni c35a186e58 [SWITCH] Correct createSequence for switch
Fix the `createSequence` function by actually replacing the cases of a
`SwitchNode`.
2020-06-30 18:11:32 +02:00
Andrea Gussoni 2ab2e779d9 [SWITCH] Introduce case replace methods
Introduce methods to replace the `ASTNode`s pointed by a `SwitchNode`.

This methods will be used in the during the AST refinement procedures.
2020-06-30 18:11:32 +02:00
Alessandro Di Federico b7660a06c3 [MetaAddress] Fix tie type
The tuple returned by the `tie` method used to have the type of the
`Type` field (`uint16_t`) for the `Address` field (`uint64_t`) and
viceversa.
2020-06-29 23:49:52 +02:00
Alain Carlucci be9bc6ce0a MetaAddress: remove header dependency from IRBuilder 2020-06-25 14:28:07 +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
Pietro Fezzardi 7c1ee4ac6b Make isASwitch inline 2020-06-15 15:30:08 +02:00
Pietro Fezzardi 921092aa6f Uniform ReversePostOrderTraversalExt with LLVM.
The other `_ext` graph iterators from LLVM all use `std::set` as default
template parameter used for the set types.
This commit make `ReversePostOrderTraversalExt` behave the same.
2020-06-15 15:27:47 +02:00
Andrea Gussoni 6367b822a0 Add default case in RegularSwitch
The switch node generation now takes care of setting the correct default
case in presence of a `RegularSwitchNode`. We cross check the default
case with the underlying switch instruction in the LLVM IR.
2020-06-15 15:06:23 +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 ca22f8567d Declare variables as late as possible 2020-06-15 15:06:23 +02:00
Andrea Gussoni 3a31495a50 Move cases in the switch constructors
Move case nodes using `std::move` during the construction of `switch`
AST nodes.
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 3cd475e96e Refactor IsASwitch in a static function
Refactor the computation of `IsASwitch` (which checks if the original
blocks contained a `switch` instruction) in a dedicated static function.
2020-06-15 15:06:23 +02:00
Andrea Gussoni aa11bcc307 Refactor switch AST generation phase
Isolated the part creating `switch` AST nodes in a dedicated section of
the `generateAST` method.

In addition, the creation of the standard nodes has been refactored with
a `switch` case covering all the possible situations.
2020-06-15 15:06:23 +02:00
Andrea Gussoni cd6edec660 Factor dispatcher creation code 2020-06-15 15:06:23 +02:00
Andrea Gussoni e4a24b2ebc ReversePostOrderTraversalExt in dedicated header 2020-06-15 15:06:23 +02:00
Andrea Gussoni 0bf43f75c1 Incremental update for DT and PDT 2020-06-15 15:06:23 +02:00
Andrea Gussoni da3a278489 Remove distinction between entry/exit dispatcher
Removed the distinction between entry and exit dispatcher nodes since
they are semantically equivalent.
Only the name differentiates them (useful for debugging purpose).
2020-06-15 15:06:23 +02:00
Andrea Gussoni 4f7661e7fe Restructure predecessors and successors methods
Drop duplicated code in `addPredecessor/addSuccessor` reusing the code
of respectively `hasPredecessor/hasSuccessor`.

Restructure the code of `hasPredecessor/hasSuccessor` with idiomatic use
of `std::find`.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 407f37c4f6 Merge two debug procedures 2020-06-15 15:06:23 +02:00
Andrea Gussoni 787b437723 Restructure the weaving
Restructure the core algorithm of the weaving, according to the
following idea:
- Compute the PDT.
- Iterate on the graph in post order.
- For every switch node S, take its immediate post dominator P.
- Let C be the set of cases for S and iterate in reverse post order on
  the nodes N between S and P.
- If N post-dominates at least one case in C, but not all of them, it's
  a candidate for weaving.
- Waving inserts a common predecessor K to the cases to be weaved. The
  weaved cases are removed from C, and K is added to C (C is the set of
  case labels for the switch S).
- The PDT is then updated to reflect the addition of K.
2020-06-15 15:06:23 +02:00
Andrea Gussoni c5671b2a17 Fix typos 2020-06-15 15:06:23 +02:00
Andrea Gussoni b0b45e7663 Remove leftovers and debugging stuff
Remove some leftovers and debugging instructions left from the
development.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 42b95d7054 Promote iteration to ranged for loop
As suggested in the MR, promoted `for` iteration to ranged `for`.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 42c81fb9d6 Remove assert on max number of successor
Remove an assert used to check that no switch nodes were present in the
graph. These assertions are not useful anymore since we now handle
generic conditional nodes.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 9d4023f5db Correct the wave -> weave typo 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 1732de392f Remove leftover useless comments 2020-06-15 15:06:23 +02:00
Andrea Gussoni dd10bbf99b Improve RegionCFG::initialize method
Removed some development leftover in the aforementioned method. In the
future we should consider adding some logic to not set as a successor
for each `switch` node the `unexpectedpc`, which causes sub-optimal
performances in the restructuring (and combing, and untangling, and
waeving).
2020-06-15 15:06:23 +02:00
Andrea Gussoni fcf87bf283 Reorder case switch in flattening
Reorder the cases in a switch to improve quality, as suggested in the
MR.
2020-06-15 15:06:23 +02:00
Andrea Gussoni d69364a7d1 Improve some ASTNode constructors
Improve constructors of `SwitchNode`, `RegularSwitchNode` and
`SwitchCheckNode` by adding a new default parameter for the successor
and therefore removing a useless parameter.
2020-06-15 15:06:23 +02:00