Commit Graph

58 Commits

Author SHA1 Message Date
Andrea Gussoni 3aaf4d939e Update OriginalBB map when moving nodes.
Update the `OriginalBB` map (which will be later used for retrieving the
original basic block linked to a certain BBNode) during nested
`RegionCFG` creation and during flattening, which are steps that modify
the allocation of the `BBNode` objects.
2019-04-15 10:05:17 +02:00
Andrea Gussoni 1c66c145cb RegionCFG::initialize method now template
The `RegionCFG::initialize` method is now a template method, which
accepts as input any kind of object which exposes the `GraphTraits`.

In this way we can build a `RegionCFG` object not only starting from
LLVM IR, which can be very useful for testing purpose.

Since we also removed the information about the original
`llvm::BasicBlock` inside the `BasicBlockNode`, we need to keep this
link to later feed this information to the GHAST in an external data
structure (potentially a `std::map`).
2019-04-15 10:04:40 +02:00
Andrea Gussoni 2c3056527d Remove any pointer to BasicBlock inside BBNode
The `BasicBlockNode` does not contain anymore any pointer to the
`llvm::BasicBlock` object that generated the `BasicBlockNode`.

This change is necessary for decoupling completely the `BasicBlockNode`
and `RegionCFG` classes from the LLVM IR, so that we can build up a
`RegionCFG` from any object which implements the `GraphTraits`.

The `RegionCFG` does not contain anymore a map between
`llvm::BasicBlock` and `BasicBlockNode`.
2019-04-15 10:03:08 +02:00
Andrea Gussoni e9f2b4239a Remove backedges refresh 2019-04-14 20:16:51 +02:00
Andrea Gussoni e8b59c2ff0 Update Backedges instead of refreshing it
Once we compute the set of backedges, they should be fixed and should
not change during the restructuring phase, otherwise we may end-up
inserting loops including outlined node if the information about the
backedges changes completely during the analysis (edge that were
backedges and for which a corresponding SCS was computed become forward
edges, and viceversa, and this behaviors break our assumptions that
outlined node do not take part in loops ever).

This means that we need to keep updated the set of backedges computed at
the beginning instead of recomputing it at each restructuring iteration.

We need to take particular care in updating the information in presence
of the default `SetNode` inserted in case of an entry dispatcher.

The actual implementation is pretty naive, and the update of the
`Backedges` `std::set` is for sure not optimal, we should consider using
a `std::multimap` for it.

We should also consider updating the reverse postorder instead of
recomputing it, but at it seems that recomputing it may cause
sub-optimal restructuring, but no errors should be triggered.
2019-04-12 15:01:49 +02:00
Andrea Gussoni d7e62c8777 New SCS merge step for abnormal retreating.
New SCS merge step for situations in which we have an abnormal edge
exiting from an SCS.

This criterion, during the preprocessing of the metaregions, if an SCS
with an abnormal retreating edge is found (an abnormal retreating is a
retreating edge which target is outside the scope of the current SCS
under analysis), merges the metaregion under analysis with the one which
has been created starting from the backedge.

This preprocessing phase is made in a fixed point fashion.
2019-04-11 16:44:59 +02:00
Andrea Gussoni ad31920b63 Fix for default SetNode membership.
When creating the collapsed node, remember to add in the substitution
phase of the old nodes of the regions (relative to the outer regions),
also any nodes added as default `SetNode`s for any entry dispatcher
inserted.
2019-04-11 16:35:10 +02:00
Andrea Gussoni 0afaa0412c Create new break/continue for each edge
Create a new `break` node for each outgoing edge from the SCS, and a new
`continue` node for each retreating edge.
2019-04-11 16:32:56 +02:00
Andrea Gussoni c3654b5ef7 Additional asserts to verify restructuring
Add an assert which verify that all the targets of the retreating edges
fall inside the SCS under analysis.

Add other asserts to verify that the graphs passed to the comb pass are
acyclic after restructuring.
2019-04-11 16:29:46 +02:00
Andrea Gussoni b1cf76ef4b removeNotReachables removes from MetaRegion
The `removeNotReachables` method now can take care of removing any
reference of the removed nodes from any `MetaRegion` which contains any
reference to it.
2019-04-09 20:12:41 +02:00
Andrea Gussoni 5aee9c2711 Fix iterator usage and map access
Now avoiding `predecessors()` iterator invalidation and enforcing
bounds check when using `ClonedMap` during first iteration outlining.

Also avoiding usage of `FirstCandidate` once the real head of a
`RegionCFG` has been elected (only `Head` ptr should be used).
2019-04-09 20:09:12 +02:00
Andrea Gussoni 486ddeb4b6 Fix first iteration outling edges movement
In case of `check` nodes some edges could not be attached to the head
dispatcher, if necessary.
2019-04-09 11:38:44 +02:00
Andrea Gussoni eddd6e1924 Change a use of addEdge with moveEdgeTarget 2019-04-09 10:49:10 +02:00
Andrea Gussoni f061095caa Fix first iteration outling
Fix an incorrect iteration over the predecessors of outlined nodes,
which caused iteration invalidation since we are modifying edges in the
meanwhile.
2019-04-09 10:33:57 +02:00
Andrea Gussoni 3ee8eb964b Fix addEdge usage in favor of moveEdgeTarget
Changed an incorrect use of the `addEdge` method in favor of
`moveEdgeTarget` when inserting the `set` nodes.
2019-04-09 09:41:00 +02:00
Andrea Gussoni 3cf9ffd3ca Move beautify passes in DecompilationPass
Moved a lot of passes that apply optimizations on the AST in the
decompilation pass.

All the optimization functions are now in a dedicated file
(`CDecompilerBeautify.cpp`) and the only function used as interface with
the `CDecompilerAction` pass is the `beautifyAST` function.

This means that now the simplifications will be applied on the already
flattened AST.

Some basic transformations have been left in the `RestructureCFG` pass,
to avoid having an AST of poor quality as output of the pass.
2019-03-14 18:03:36 +01:00
Andrea Gussoni 2e49e21925 Removed Switch BBNode and IfEqual AST node
Removed the `Switch` BBNode, which was used to create the intermediates
nodes for making an original `switch` node a nested tree of `if` checks.

Also removed the `IfEqual` AST node, which was used to represent the
intermediate check nodes in the AST, for later reconstructing the
original `Switch` node in the AST, when possible.
2019-03-12 14:55:02 +01:00
Andrea Gussoni 4a49a60b99 Fix and convention enforcing
Some fixes and conventions enforcing before decompilation pipeline
restructure.
2019-03-12 13:49:53 +01:00
Andrea Gussoni f27597b999 Introduce the Switch BasicBlockNode type
Introduced a new `BasicBlockNode` type to represent the nodes created
when building the nested tree in place of the `switch` instruction.
2019-03-08 15:31:30 +01:00
Andrea Gussoni 20cec12b5e After semplification while loops might be empty
Now considering the fact that the body of a loop may become empty if
some simplification take place (e.g. we match a `while` whose body is
composed only by a check with `break` and `continue` branches).
2019-03-08 15:27:50 +01:00
Andrea Gussoni 318c7dabd7 Add computation before continue nodes
When a `while` loop is matched and promoted, add to every `continue`
node in the current scope the instructions needed for the computation of
the loop condition.
2019-03-08 15:27:50 +01:00
Andrea Gussoni eb78d6121f Match do-while and while loops
Match `do-while` and `while` loops, transform the in our AST preserving the
information about the `IfNode` which computes the condition of loop, and
emit them in the decompiled code.

Also added a pass which removes useless continue nodes.
2019-03-08 15:27:50 +01:00
Andrea Gussoni aee582a586 Make pass names compliant to the convention 2019-03-08 15:26:46 +01:00
Pietro Fezzardi 3b2dac4e85 Enforce clang-format 2019-02-25 12:29:15 +01:00
Pietro Fezzardi cb08b4ac31 tmp 2019-02-12 20:55:28 +01:00
Pietro Fezzardi f4d5fec5bc First fully functional EnforceCFGComingPass 2019-02-11 12:18:44 +01:00
Andrea Gussoni 929c0c4cdf Flattening for AST
Implemented the flattening also for the AST trees, taking also care of
adjusting the pointers to the new BBNode in the `root` `RegionCFG`.
2019-02-11 12:18:44 +01:00
Andrea Gussoni 2e9ff8449f Improved debugging messages 2019-02-05 10:17:47 +01:00
Andrea Gussoni c43b7989ce Improved SCS identification phase
Now in the `SCS` identification phase, when creating a SCS, if we
contain a node which is the source of another (sub)SCS, we also include
that SCS in the current SCS (in order to increase the number of nested
subregions).

The exception to this situation is when the source of the
other SCS is the head of the region itself under analysis.
2019-02-05 10:17:41 +01:00
Andrea Gussoni 497bd0d02e Fix exit dispatcher creation 2019-02-04 18:34:14 +01:00
Pietro Fezzardi 2afd5053e6 Rework exit and entry dispatcher trees 2019-02-01 19:24:56 +01:00
Pietro Fezzardi 2c93512f90 Rework BasicBlockNode class 2019-02-01 15:08:53 +01:00
Andrea Gussoni 0cdb63630e Restructure ASTTree anatomy
Added a pointer to the `RootNode` in the ASTTree, and added a
`dumpOnFile` method for easier serialization.
2019-01-30 18:35:24 +01:00
Andrea Gussoni 9202742343 TEMP disable some functions 2019-01-30 16:46:36 +01:00
Pietro Fezzardi 099ae4d479 First stub of flattening 2019-01-30 09:22:20 +01:00
Pietro Fezzardi 20a336344e Add state variable info to dummy BasicBlockNodes 2019-01-25 18:36:37 +01:00
Pietro Fezzardi 3b8400992f Fix dangling pointers to RegionCFG stack variables 2019-01-25 17:20:24 +01:00
Andrea Gussoni c70b6f1097 Implemented de-optimization on RegionCFGTree
Now, during the simplification of short-circuits the so called
`de-optimization` is also applied to the corresponding `RegionCFGTree`.

This allows us to avoid loss of information during the iterative
refinement phase, which basically divides for ever the existencies of
two nodes that have been cloned starting from a single original one.
2019-01-25 13:45:03 +01:00
Andrea Gussoni 05d736cbb1 AST serialization is now performed on file
We now perform the AST serialization directly on file, without using
stderr. In this way we can follow the evolution of simplifications and
changes to the AST tree.
2019-01-25 13:35:20 +01:00
Andrea Gussoni d591ae13e6 Improved serialization of graphs during inflate
Improved the `dumpOnDotFile` function to take as a parameter also a
sub-folder name.

Modified the `inflate` function to dump on file the graphs after each
modification.
2019-01-25 13:32:28 +01:00
Pietro Fezzardi f5fcc4aa24 Completely redesign ownership model for RegionCFG 2019-01-25 13:13:35 +01:00
Pietro Fezzardi 744900d4d5 RestructureCFGPass: add skeleton for flattening 2019-01-22 19:16:48 +01:00
Pietro Fezzardi 40d677e560 RestructureCFG: early check for isolated functions 2019-01-22 19:16:48 +01:00
Pietro Fezzardi ea965ac743 EnforceCFGCombingPass: improve assertions 2019-01-22 19:16:48 +01:00
Pietro Fezzardi 00169c5833 Switch to new cmake package for revamb libraries
This change makes `CMakeLists.txt` files less verbose and allows proper
registration of debug `Logger`s from revamb `Support` library,
enabling their use without manually enabling them.
This also means that they can be properly enabled with command line
arguments and that we can remove some hacks in the code that was put in
place to work around this limitation.
2019-01-22 19:16:48 +01:00
Andrea Gussoni 05a5d80151 Short-circuit simplification concept
First concept of short-circuit simplification.
The `isEqual` operator between ASTNodes is very limited (it does only
check if two nodes originate from the same `BasicBlock` in the original
IR.
2019-01-22 19:16:48 +01:00
Andrea Gussoni 8f89ea0f7d Outline not reachable nodes removal
Outlined the removal of nodes not reachable from the entry node in an
external function. This is useful to reuse the function not only for the
main graph, but also for the collapsed regions.
In this way we can remove not reachable nodes in the inner regions, such
as dandling `break` and `continue` nodes (found in an endless loop).
2019-01-21 18:28:59 +01:00
Andrea Gussoni f94bd7d99a Refresh backedges after each transformation
This fix enables the correct updating of the backedges present in the
graph, since each transformation pass could in principle modify some of
the backedges present in the graph
2019-01-21 18:28:59 +01:00
Andrea Gussoni 5245cd71e5 Added assertions
Added more asserts to quickly identify problems in which we already
incurred
2019-01-17 17:29:39 +01:00
Andrea Gussoni c9ca88c12f Improved debugging information
Enriched the debuggin information provided by the logger, and also
manually enabled it, until `opt` will accept additional command line
parameters
2019-01-17 17:27:33 +01:00