Commit Graph

431 Commits

Author SHA1 Message Date
Andrea Gussoni ce902e36b6 generateAst: generalize the switch fallthrough 2021-11-15 12:01:00 +01:00
Andrea Gussoni 6dcffcce67 generateAst: Handle switch postdominator
Handle switch nodes with all but one case edges inlined, by electing the
not inlined edge as the path conducting to the postdominator, and
avoiding parts of the graph disconnecting during the tiling procedure.
2021-11-15 11:57:36 +01:00
Pietro Fezzardi 067f2fffe1 Add ModelToHeader 2021-11-15 11:47:17 +01:00
Pietro Fezzardi 032463c539 Fix handling of calls with unnamed callees 2021-10-12 09:57:30 +02:00
Alvise de Faveri 43ae7a91cb DLA: Add DeduplicateUnionFields Step
Add a step that recognizes if two subtrees of a union node are
topologically equivalent and merges them. This corresponds to removing
duplicate fields in unions.

This deduplication was prevously done while emitting layouts.

A check is inserted into DLAMakeLayouts to assert that, after
constructing unions, no union has only one child, which could be the
case if we didn't deduplicate union fields in the graph.
2021-10-06 16:37:37 +02:00
Alvise de Faveri 7e72fc4be1 DLA: Add RemoveConflictingEdges step
Add a DLA step that removes all instance-at-offset-0 edges between nodes
that already have an inheritance edge between them.
2021-10-05 18:16:21 +02:00
Pietro Fezzardi fda2b0042f Drop Beautify dependency on MarkForSerialization 2021-09-22 19:01:44 +02:00
Pietro Fezzardi ce0083a7ff Extract MarkForSerialization in separate library 2021-09-22 15:03:12 +02:00
Andrea Gussoni 16e7b2f051 RegionCFG: Improve moveEdgeTarget
`moveEdgeTarget` and `moveEdgeSource` now directly modify the
successor/predecessor field in place in the edge data structure, instead
of extracting and reinserting later the edge.

This avoid a subtle bug, found in the `createTile` helper function, that
caused the swap of a `then` and `else` branch during a tile creation
step, that consequently broke the semantics in the recovered GHAST.
2021-09-01 15:11:06 +02:00
Pietro Fezzardi f057441ce8 Fix linking 2021-08-20 11:21:04 +02:00
Andrea Gussoni 55c620ab31 RegionCFGTree: refactor connectContinueNode
THe creation and connection of continue nodes is now down in the
population of the collapsed region, avoiding external methods.
2021-08-19 18:51:24 +02:00
Andrea Gussoni 4ce2189bec RegionCFGTree: remove copyNodesAndEdgesFrom 2021-08-19 18:51:24 +02:00
Andrea Gussoni 7323cecec1 RestructureCFG: improve break node creation
Break node creation is now improved to avoid edge index swapping that
can cause inversion of conditions later on in the decompilation
pipeline.

To do this, the creation and connection of the break nodes is done
during the bulk insertion of nodes in a collapsed region.
2021-08-19 18:51:14 +02:00
Andrea Gussoni 3bb3437466 RegionCFG: Add successor index in RegionCFG dots 2021-08-19 18:50:34 +02:00
Alvise de Faveri a0f95e0370 Modify CollapseSingleChild to allow single members
* Inheritance straight lines are now collapsed by CollapseSingleChild
* Don't collapse if parent has more than one parent or if the child has
more than one parent
* Allow the possibility to construct single-member struct: if this
struct is inherited by many other structs, "flattening" the struct is not
the most sensible thing to do, since inheritance information is something
we want to preserve in these cases.
2021-07-27 11:59:48 +02:00
Pietro Fezzardi 10d1eab7f4 Decompiler: update to new model APIs 2021-07-21 11:09:28 +02:00
Alvise de Faveri 4eb50d0bb5 Add Unit Test for DLACollapseSingleChild 2021-06-30 17:50:12 +02:00
Alvise de Faveri c1e4b940d0 Add CollapseSingleChild step to DLA 2021-06-30 17:50:12 +02:00
Pietro Fezzardi 6efc356c47 Drop dependency on GCBI and BlockType
During combing, GCBI's BlockType was used to detect unexpectedPC and
anyPC. This method is does not work anymore (since revng started
dropping such info from isolated functions and replacing that with
llvm::unreachable instructions).

This commit drops the dependency on GCBI and BlockType, and starts
detecting blocks to inline by simply looking at their terminator. If
it's an llvm::unreachable, the block is detected as inlineable.
2021-06-30 10:38:00 +02:00
Alvise de Faveri 47db3fdb11 Promote AccessSizes to full-fledged TS nodes
While doing thie, also drop `printAccessNode()` from DLA DebugPrinter.

The information about which LLVM instruction originated a given access
node is already available in the csv generated by the `dla-accesses-log`
logger.
2021-06-25 18:07:17 +02:00
Pietro Fezzardi 6a2e130ec6 Move openFunctionFile to new Utils library 2021-06-21 14:15:30 +02:00
Alvise de Faveri bfdef657f2 Move DLA files to separate folder 2021-06-18 18:24:04 +02:00
Alvise de Faveri 105edb69a3 DLA: Decouple DLATypeSystem from LLVM Values
- Remove `CreateInterProceduralTypes` and `CreateIntraProceduralTypes`
  from the StepManager and put them in a separate
  `DLATypeSystemLLVMBuilder` object that is in charge of initializing
  the DLATypeSystem graph.
- Remove `MakeLayouts` from the StepManager and split into two free
  functions: `makeLayouts` and `makeLayoutMap()`
- Remove all LLVM-related stuff (Module, LayoutTypePtrs and mappings
  between these and DLATypeSystemNodes) from DLATypeSystem
- Add an IntEqClasses member to DLATypeSystem, to use to map between
  LayoutTypePtrs and Layouts
- Add a TSDebugPrinter object inside the DLATypeSystem, which by default
  prints only IDs
- Define an LLVMTSDebugPrinter inside DLATypeSystemBuilder which
  overrides the default printer's behavior, printing LLVM-related info.
- Add the possibility to dump into a .csv file the bindings between
  Nodes in the TypeSystem and `llvm::Value`s
2021-06-18 18:12:14 +02:00
Pietro Fezzardi dde8af9b53 GenerateAst.h: update DomTree API for children
The old getChildren API is gone in llvm-12.
2021-05-27 17:14:25 +02:00
Pietro Fezzardi edc2fe5c84 DLAPass.h: remove deprecated include
This caused compilation error with llvm-12
2021-05-27 15:06:38 +02:00
Pietro Fezzardi 461b1cc715 DLATypeSystem: simplify ownership, lookup, removal
Before this commit DLATypeSystem used a
std::set<std::unique_ptr<LayoutTypeSystemNode> to hold nodes, allowing
heterogenous lookup with bare pointers, fast removal being a set,
and providing address stability without requiring an ordering between
LayoutTypeSystemNodes.

The same requirements is now obtained more cleanly using with a
BumpPtrAllocator for nodes, and a set of naked pointers to them to
enable fast lookup and removal.
2021-05-14 11:23:40 +02:00
Pietro Fezzardi 2b73e29249 Decouple LayoutTypeSystemNode from LLVM IR 2021-05-14 11:23:40 +02:00
Alvise de Faveri 6fbf5724d1 Add Value Manipulation Analysis 2021-05-14 11:18:14 +02:00
Pietro Fezzardi 80b1a9e7fa Add RemoveLLVMDbgIntrinsicsPass
This pass removes all llvm debug intrinsics from the IR.
It is enabled by default in all decompilation pipelines.
2021-04-29 03:06:58 +02:00
Pietro Fezzardi 36a525314a Detect isolated functions using FunctionTags 2021-04-27 21:19:06 +02:00
Pietro Fezzardi 35e5e2fc83 RegionCFGTree: introduce DeadNodesQuarantine
With this quarantine, when nodes are removed from RegionCFG, they are
not really freed, but they are held here until the RegionCFG itself goes
out of scope.
This is unfortunately necessary now, since the CFG restructuring
algorithm uses maps and sets (e.g. Backedges.) that are indexed using
a BasicBlockNodeT *.

If we don't hold the removed nodes in quarantine, the system allocator
can reuse the blocks, allocating new nodes at the same address, and
causing false-positive hits in some of the mentioned maps. This was the
most straightforward solution for now.

Other solutions we have considered:
- use a special monotonic allocator for BasicBlockNodes
  - this should work, but in principle it gives the same results as the
    current solution, with more boilerplate. Also, at the moment
    std::unique_ptr is not allocator aware, so we would need to change
    BlockNodes to not use them, and this would require even more
    boilerplate.
- change the API for RegionCFG::removeNode, to take as arguments the
  reference to the data structure and maps that must be updated, so that
  when we remove the node from RegionCFG we also clear it from the maps.
  However, this is very invasive, it requires changing the public facing
  API, it requirese coupling the RegionCFG API with internal details,
  and in the future it would need to be updated for every new map that
  must be updated on removal of a node.

This commit fixes a bug causing a failing assertion on Backedges that
jump from an inner MetaRegion to an outer MetaRegion after region
collapsing.
2021-04-15 10:33:39 +02:00
Pietro Fezzardi f265c8c55f BasicBlockNode: explicitly delete copy-constructor
Constructor declarations for `BasicBlockNode` were not consistent.
We had default-constructor, and move constructor explicitly deleted, but
the copy constructor was not explicitly deleted, even though it was
implicitly deleted. Make deletion explicit, in accordance to the fact
that all other special member functions for construction and assignment
are deleted.
2021-04-15 09:42:47 +02:00
Andrea Gussoni 0502b2345d Implement NoFallThroughPromoter
Implement a beautify phase which does the following:
- Compute, for every scope in the AST, if that scope is `fallthrough`
or `nofallthrough` scope. Basically, the `nofallthrough` scopes are
scope which ends with a `return`, `continue', or `break`.
- Using the information computed before, we can promote the scope of an
`IfNode` using the following criterion: if one of the two branches of
the `IfNode` is a `nofallthrough` scope, we are sure that the other
branch is not reachable from the former one. We can therefore, promote
the latter as `fallthrough` block of the `IfNode` (of course taking care
of inverting the condition statement if we are promoting to
`fallthrough` the `then` branch.
If both the `then` and the `else` branches can be promoted as
`nofallthrough`, we have a function that evaluates the weight of the two
branches, and promotes the heavier one. This helps reducing the
Cognitive Complexity of the generated code
2021-04-07 11:53:32 +02:00
Alessandro Di Federico ae368f9302 ReversePostOrderTraversal has been moved to revng 2021-03-08 11:11:47 +01:00
Alessandro Di Federico 1217296328 TypeShrinking has been moved to revng 2021-03-08 10:26:44 +01:00
Pietro Fezzardi b092c4f505 Add ThreadSafeClangTooling library
This library provides a thin locking wrapper around clang::tooling
invocations.
It should be used instead of performing direct clang::tooling
invocations by all programs that use revng-c and may run more than one
ClangTool concurrently.

This is necessary because clang::tooling internally uses llvm's cl::opt
for parsing command line options.
cl::opt uses a global variable for the parser under the hood so parsing
two command lines concurrently is not safe.
Similarly, cl::opt typically uses global variables to hold options, so
it is not safe to execute a ClangTool concurrently to another tool
that is parsing a new set of options, because there might be race
conditions between threads reading and writing the same options at the
same time.

The new library introduces a thin locking layer so that the end-user
does not need to know or worry about these details.
2021-03-05 16:12:41 +01:00
Pietro Fezzardi 2277bc898a Drop unused DecompilerResourceFinder
Now the decompiler does not need to find resources on disk anymore.
2021-03-05 16:12:41 +01:00
Pietro Fezzardi bb1f2d629e CDecompilerPass: cleanup clang::tooling invocation 2021-03-03 19:02:33 +01:00
Andrea Gussoni ecd20d21a1 Remove dummies from the AST during simplification
Actually remove dummy nodes that are purged during the `purgeDummies`
normalization phase (which is in charge of removing dummy nodes that are
not superfluos for our AST representation).

These dummies where laying around untouched, and caused errors when
iterating over all the AST while collecting the weight after the
combing.
2021-03-03 12:35:35 +01:00
Pietro Fezzardi e95c21db25 Use Model to detect isolated functions 2021-02-23 10:39:05 +01:00
Pietro Fezzardi 448d7366ac Small lib to detect isolated functions using Model 2021-02-18 11:59:05 +01:00
Pietro Fezzardi 444663ed94 RestructureCFG: refactor logging of MetaRegions 2021-02-18 11:59:04 +01:00
Pietro Fezzardi 2998494d18 RegionCFG: use meaningful names for dummy nodes
This helps debugging.
2021-02-18 11:59:03 +01:00
Alvise de Faveri 495e725d14 ADT: Enclose RPOExt in the llvm namespace 2021-02-17 16:50:32 +01:00
Pietro Fezzardi f01fe2ec7a MarkForSerialization: fix needsVarDecl 2021-02-05 15:45:11 +01:00
Pietro Fezzardi 904aab0f6f LayoutTypePtr: drop friend struct std::less
This was necessary before defaulting operator<=>, now it's not necessary
anymore.
2021-02-03 12:02:25 +01:00
Pietro Fezzardi 3f1ee6ea64 Stub of DLA integration in Clang AST emission 2021-02-02 11:23:53 +01:00
Pietro Fezzardi 088015e11c Forward SCEV and DLA results to decompilation 2021-02-02 11:23:53 +01:00
Pietro Fezzardi 9d1d6afe6e MarkForSerialization: extract into its own Pass 2021-02-02 11:23:53 +01:00
Pietro Fezzardi 2ff7044bb9 Decompiler: forward-declare types coming from DLA
This commits enable the emission of rich types associated with function
signatures. This types are forward-declared in the decompiled C code
before the definition of each decompiled function that uses them.

The types we emit for now are the types that the DLA is able to compute
(if any) for the return values and the arguments of the function.

Such types are not yet used in the body of the function, nor in the
function declaration. These are the next steps to come.
2021-02-02 11:23:53 +01:00