Commit Graph

6113 Commits

Author SHA1 Message Date
Alessandro Di Federico c1bf5562e3 PromoteCSVs: use CSV initializer for non-GPRs
This commit drops calls to `_init_$CPU_STATE` for non-general purpose
registers. Instead, these part of the CPU state are initialized using
the initializer of the CSV, which is in turn provided by QEMU.

This enables us to properly initialize as appropriate fields such as
x86-64's DF flag.
2023-10-24 09:49:05 +02:00
Pietro Fezzardi dd1c2d30c5 Merge branch 'feature/dual-switch-simplify' 2023-10-23 18:29:43 +02:00
Andrea Gussoni 3e13765267 BeautifyGHAST: handle noreturn calls in beautify
Improve the `fallThroughScope` computation, in order to handle calls to
`NoReturn` functions in the analysis, representing the fact that they
induce a `noFallthrough` scope (i.e., execution will not ever resume
after the call, and therefore we can later improve the nesting of the
code with the `promoteNoFallthrough` pass).
2023-10-23 18:16:20 +02:00
Andrea Gussoni 733f5008b3 BeautifyGHAST: implement DualSwitch simplify
We introduce a simplification step, which looks for `switch`es that can
be reduced to simpler `if` statements.

Specifically, the logic is the following:
1) When we identify a `switch` statement composed by a single `case` and
   a possible default, we transform it into an `if` with the `case` now
   corresponding to the `then`, and the `default` corresponding to the
   `else`, if present.
2) When we identify a `switch` statement composed by two `case`s, and no
   `default` is present, we can promote it to an `if` with `then` `else`
   branches.

Other key details:
- The promotion happens only if we can identify at least one of the
  `case`s that have a single element in the `case` label. If this is not
  the case, we do not promote one to RHS of the `if` condition.
- A new `CompareNode` class, inheriting from `ExprNode`, is created, in
  order to represent the equality or inequality condition of an `IfNode`
  instance that is the result of the promotion. This `CompareNode` can
  represent for the LHS both an `llvm::Value` or the `loop_state_var`,
  while it embeds the RHS constant which completes the comparison.
- We remove `SwitchBreak` AST nodes that may now appear as children of
  an `if` node promoted from a `switch`.
- We introduce in the `CompareNode` the `weaved` concept. Indeed, if a
  promotion of a weaved `switch` happens, we should avoid the
  serialization of the instructions leading to the computation of the
  condition of the original `switch`, because they have been already
  emitted by the main related dominating `switch`.

We also introduce an additional simplification step, which takes care
of:
- Promoting `!(==)` to `(!=)` and `!(!=)` to `(==)`, if the inner
  equal/not equal is represented via a `CompareNode`.
- Promoting `x == 0` to `!x` and `x != 0` to `x`.

To be able to correctly emit (or not) the instructions computing a
condition of an `IfNode`, we need to add the `EmittBB` flag, an
additional parameter to the `buildGHASTCondition` function, which
controls the emission of the statements of a basic block computing a
condition.

Consequently, the `IfNode` acquires a `IsWeaved` field, which is used to
mirror the property having the same name on `SwitchNode`. Being now
possible a promotion from a dual `SwitchNode` to an `IfNode`, we need to
represent this property on the `IfNode` too.
2023-10-23 18:16:20 +02:00
Andrea Gussoni 2cef075697 ASTNode: refactor default in SwitchNode
The `default` `case` is now a standard `case`, and it is identified only
by having the `label` set empty.

Therefore, a list of beautify and transformation actions now do not need
special casing in order to handle the `default` `case`, which is reached
during the standard iteration over the `case`s.

A special accessor is still necessary, in order to correctly emit the
`default` `case` in the backend.
2023-10-23 18:16:20 +02:00
Andrea Gussoni 061bc52bc2 BeautifyGHAST: implicit statements simplification
Introduce implicit statements simplification phase, specifically:
- A implicit `return` simplification: `return` statements in `void`
  type functions, which are not followed by any other scope, can be
  omitted.
- A implicit `continue` simplification: `continue` statements whose
  fallthrough leads directly to the end of the cycle scope (i.e., to
  execute another iteration of the enclosing loop), can be omitted.

In order to avoid the printing of the implicit `return`, we need an
additional `emitReturn` parameter in the `emitBasicBlock` method of the
`CCodeGenerator` class.
2023-10-23 18:16:20 +02:00
Andrea Gussoni e0c530265a ASTTree: fix the BBASTMap population
When copying nodes from nested AST to the root one, we should take care
of overwriting the mapping between the old `OldCFGNode`, so that it now
points to the newer AST node representing it.
2023-10-23 18:16:20 +02:00
Andrea Gussoni a2e51a191c ASTNode: remove IsEmpty field from ASTNode
Remove the `IsEmpty` field from the `ASTNode` class, since that
information is redundant.
2023-10-23 18:16:20 +02:00
Andrea Gussoni 4c461c24ac ASTNode: uniform nodes printing syntax
Now, also `break` and `continue` on the AST, when printed, have the ID
number of the `ASTNode`, and in the name they preserve the original
`CFGNode` one also.
2023-10-23 18:16:20 +02:00
Andrea Gussoni f98e72d9a7 BeautifyGHAST: move SimplifyHybridNot header 2023-10-23 18:16:20 +02:00
Andrea Gussoni effbb461fd GenerateAST: improve UntangleWeight explanation 2023-10-23 18:16:20 +02:00
Pietro Fezzardi 16542abe97 Merge branch 'feature/ui-qa-fixes' 2023-10-23 18:12:31 +02:00
Giacomo Vercesi 0ca3c03de1 ModelTypeNames: fix printFunctionPrototype
The `printFunctionPrototype` function produced a prototype with a
`data-location-references` instead of a `data-location-definition`.
2023-10-23 10:20:54 +02:00
Giacomo Vercesi b042b5495a ModelTypeNames: fix printFunctionPrototype
The prototype of `printFunctionPrototype` has a mis-named argument
following the changes made in `2a7e06df`.
2023-10-23 10:20:54 +02:00
Giacomo Vercesi df877d4aec Drop unused statements
Drop `using`s that were unused.
2023-10-23 10:20:49 +02:00
Ivan Krysak 9a1239327c CallGraph: fix the slicing direction
After the changes to the direction `CrossRelation` output is parsed,
the slicer wasn't updated accordingly. Because of that, the slices were
as if they were produced backwards.

As for the full graph, since it starts from an artificially added node
that doesn't have any successors by definition - it was appearing empty
(only the non-emittable "root" node was present).
2023-10-20 15:54:58 +02:00
Pietro Fezzardi 75e98006d4 Rename artifacts for decompiled C headers
This also changes the names that show up in the VSCode-based UI, so that
they are coherent with what's included in the decompiled C code via the
related #include directives.
2023-10-19 17:12:25 +02:00
Pietro Fezzardi a78928141b Rename decompiled C headers 2023-10-19 17:08:25 +02:00
Pietro Fezzardi 48a708c07c Support printing NULL in decompiled C code 2023-10-19 16:18:41 +02:00
Pietro Fezzardi 4bc7a14649 ModelTypeNames: print function attributes 2023-10-18 15:39:17 +02:00
Pietro Fezzardi 740ef6f1d3 ModelToPTMLTypeHelpers: avoid redundant whitespace
Before this commit, the `printDefinition` helper functions for
`StructType` and `UnionType` were printing a duplicated whitespace when
generating the inline type definition as part of the declaration of a
field of a struct or union.

This commit skips the redundant whitespace when relevant.
2023-10-18 15:19:53 +02:00
Pietro Fezzardi a97287cef3 ModelToPTMLTypeHelpers: refactor APIs
This commit reorders the argument passed to various `printDeclaration`
and `printDefinition` helper functions, to make them more uniform across
each other, and to accept the same arguments in the same order.
2023-10-18 15:19:53 +02:00
Pietro Fezzardi 6925c3141a VMA: use standalone functions instead of lambdas 2023-10-18 15:19:52 +02:00
Pietro Fezzardi 75aff6a4a9 DLA: always copy fields when updating Model
Before this commit, DLA's backend often emitted things like:

```c
struct x {
  struct y {
    int32_t z;
    uint8_t padding[4];
  } _offset_0;
  uint8_t padding[8];
};
```

This is suboptimal, since the fields of y actually can just be inlined
into x, giving:

```c
struct x {
  int32_t z;
  uint8_t padding[12];
};
```

This behaviour was due to a logic bug carried over from old versions of
the code, and partially extended, but never revisited nor thought
through.

This commit fixes the problem, making the second behaviour the only
thing DLA ever does when updating the model.
Basically, struct fields are copied over field by field, instead of
inserting the bulk struct (y in the example) inside the larger struct to
update (x in the example).
2023-10-18 15:19:52 +02:00
Pietro Fezzardi 2b11a3f64e Merge branch 'feature/nodesbetween-fix' 2023-10-11 11:18:30 +02:00
Pietro Fezzardi cea24b455b Merge branch 'feature/nodesbetween-fix' 2023-10-11 11:17:54 +02:00
Andrea Gussoni 2f4e027ae7 RegionCFGTree: invoke findReachableNodes
Make use of the newly added `findReachableNodes` to identify the
reachable nodes from the `Then` or `Else` when there is no common
`PostDominator`.
2023-10-09 10:34:41 +02:00
Andrea Gussoni deee899ad0 RestructureCFG: improve first iteration outline
Improve the logic of the first iteration outline:
1) The cloning of the nodes involved in the first iteration outlining,
   is now performed with a series of DFS visits from all the late entry
   nodes in the `MetaRegion`, instead of performing a blind clone of all
   the nodes and removing those not needed.
2) Restoring the edges between the cloned nodes has been subject to
   minor changes.
3) The outline nodes are now assigned to a `MetaRegion` using a new
   logic, instead of blindly assigning them to the parent `MetaRegion`.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 496f0becd7 RestructureCFG: remove old Set node handling
Remove old broken code that handled `Set` nodes as source of retreatings
when inserting an `entry` dispatcher. This situation should no longer be
the case, because a `Set` node should not be the `source` of a
retreating edge. We assert this situation.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 4161834bc5 RestructureCFG: improve handling of retreatings
We now explicitly store the retreatings edges, that will eventually be
connected to the `continue` nodes, so that we can explicitly handle
them, without resorting to iterating over the predecessors of the
`EntryNode`, which although correct is a fragile method with respect to
defective situations.

In addition, we check that after the restructuring, all the retreating
edges that we connect to `continue` nodes, do point to the `EntryNode`
before the transformation.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 935dcdbdec RestructureCFG: improve ShortestPath
Add a dedicated Logger for the `ShortestPath` computation.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 6d4f3daf30 RestructureCFG: improve code layout
Improve the code layout and the logging of the `Backedge`s insertion.
Add the `debug_function` attribute to the `LogMetaRegions` function.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi 4cda5259e5 RestructureCFG: improve inclusion dominated nodes
Before this commit, the code was using an old arcane algorithm that was
adding and removing dummy frontier nodes, and relying on the
DominatorTree.

This commit drops the DominatorTree altogether, drops the use of
frontier nodes, and rewrites the algorithm only using successors and
predecessors.

Now a node outside a metaregion M is included in the metaregion only if
all its predecessors are part of the metaregion, and none of them is
already in another metaregion that is a transitive parent of M.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi 1ed19a35e2 RestructureCFG: try skip first iteration outlining
Before this commit, the whole body of each loop was outlined and
duplicated, only to be removed later if not necessary.
Now we only do this if there is more than one entry to the loop, which
is the simplest condition to detect when first iteration outlining is
needed.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi b8810858c8 RestructureCFG: drop RootMetaRegion placeholder 2023-10-09 10:34:41 +02:00
Pietro Fezzardi 57099e9e5d MetaRegion: initialize Parent to nullptr 2023-10-09 10:34:41 +02:00
Pietro Fezzardi 68fca82f46 RestructureCFG: call updateNodes more efficiently
Before this commit it was called on all metaregions, now only on parent
metaregions.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi 3cfad9f3f9 MetaRegion::updateNodes: simplify and add assert 2023-10-09 10:34:41 +02:00
Andrea Gussoni d2db8d4cfa ControlFlowEdgesGraph: improve at method
Improve the `at` method by giving it a coherent semantics with a
standard `at` map method semantics.
2023-10-09 10:33:48 +02:00
Andrea Gussoni e2090b8746 GraphAlgorithms: nodesBetween double DFS
We now implement the `nodesBetween` primitive with a double DFS visit.
Specifically, we have:
1) A forward DFS that starts from the `Source`, and stops at `Target`.
2) A backward DFS that starts from the `Target`, and stops at `Source`.
The final result is then the intersection of the nodes found by the two
above DFS visits.

A new `findReachableNodes` primitive is also added to perform a DFS from
a `Source` node, which stops at the `Stop` parameter node, if present.

Added unit tests to improve the coverage.
2023-10-09 10:33:48 +02:00
Alessandro Di Federico 154b4700fd Merge branch 'feature/c-qa-1' 2023-10-06 17:17:16 +02:00
Alessandro Di Federico 6645a68900 Merge branch 'feature/c-qa-1' 2023-10-06 17:16:42 +02:00
Alessandro Di Federico bd1b5e4ef9 newpc: add nomerge, willreturn, nounwind 2023-10-06 17:16:04 +02:00
Alessandro Di Federico 79c5f59449 DwarfImporter: handle invalid function addresses 2023-10-06 17:16:04 +02:00
Alessandro Di Federico a11435becf BinaryImporter: error on unsupported formats 2023-10-06 17:16:04 +02:00
Alessandro Di Federico c9b95c0556 DetectABI::propagatePrototypes: improve logging 2023-10-06 17:16:04 +02:00
Alessandro Di Federico 86e5ae8680 rm share/revng/well-known-models/CMakeLists.txt
Its presence lead to an undesirable situation where the file itself
would get copied to the install directory and using `add_subdirectory`
led to creating a set of support files (which would get installed too).

Simple solution: just push the content of that file in the root
`CMakeLists.txt`.
2023-10-06 17:16:04 +02:00
Alessandro Di Federico 4fe4d05895 CCodeGenerator::ParentPrototype: drop Parent 2023-10-06 17:14:29 +02:00
Alessandro Di Federico 2a7e06df1f printFunctionPrototype: add newline after ABI
This commit forces getLocationAttribute's argument to be false.
2023-10-06 17:14:29 +02:00
Alessandro Di Federico 6141717440 CCodeGenerator: emit comment after noreturn calls 2023-10-06 17:14:27 +02:00