Commit Graph

6113 Commits

Author SHA1 Message Date
Pietro Fezzardi aa1d6f5bc7 DLA: make CompactCompatibleArray commutative
Before this commit, the CompactCompatibleArray DLAStep had that caused
the compaction to be sensible to the order of the pairs of edges that
were compacted, and that also caused the compaction operation to overrun
the end of the containing struct in some corner cases.

This commit fixes both bugs.

Now the compaction routine is not ordering-sensitive anymore.
For each pair of edges <A, B> to compact it tries to compact them in 2
possible ways: by aligning A to B shifting A to lower offsets, and by
aligning B to A shifting B to lower offsets.
If both succeed, it picks the best result among the two, which doesn't
depend on the ordering of A and B.
This fixes the order-sensitiveness.

While reworking this logic, the logic was fixed so that if any of the
two compaction attempts causes to overrun the end of the containing
struct, the attempt is not considered successful anymore, and it's
discarded altogether.
2023-12-05 16:59:08 +01:00
Pietro Fezzardi 2268d84037 DLA: refactor offset-based stride computation 2023-12-05 16:59:08 +01:00
Pietro Fezzardi 2e4459faff Merge branch 'feature/adopt-tupletreereference' 2023-12-05 16:58:02 +01:00
Pietro Fezzardi 24cb43ce9c Make RawFunctionType::StackArgumentsType a TTR
This is homogeneous with all other nullable references to types in the
model, such as the StackFrameType in model::Function, and others.
2023-12-05 16:56:52 +01:00
Pietro Fezzardi 842b5e1075 Make Segment::Type a TupleTreeReference
This is homogeneous with all other nullable references to types in the
model, such as the StackFrameType in model::Function, and others.
2023-12-05 16:56:52 +01:00
Alessandro Di Federico 6e88f6b287 Merge branch 'feature/adopt-tupletreereference' 2023-12-05 16:19:52 +01:00
Alessandro Di Federico 10dcb7be11 Fix semantics of model::Types purging passes
One of the two passes to purge unused types from the model had a
ill-defined semantics.

This commit simplifies them in two passes: one that purges all the types
unreachable from outside `Binary::Types` and one that does the same but
also preserves types with a `CustomName` or an `OriginalName`.
2023-12-05 16:19:39 +01:00
Pietro Fezzardi bbcb4df839 TypeSystemPrinter: print segment types 2023-12-05 16:19:39 +01:00
Pietro Fezzardi 1285c6e7af Make RawFunctionType::StackArgumentsType a TTR
This is homogeneous with all other nullable references to types in the
model, such as the StackFrameType in model::Function, and others.
2023-12-05 16:19:38 +01:00
Pietro Fezzardi 48b27af795 Make Segment::Type a TupleTreeReference
This is homogeneous with all other nullable reference to types in the
model, like the StackFrameType in model::Function, and others.
2023-12-05 16:19:38 +01:00
Pietro Fezzardi ef8a9699f1 Improve model::Function::verify 2023-12-05 16:19:38 +01:00
Massimo Fioravanti aa1a3e6764 Unify pipeline pipes files
Pipeline files are now a single file where the decompilation pipeline is
the main branch
2023-12-05 15:47:52 +01:00
Pietro Fezzardi 10a4368a0f Merge branch 'feature/noreturn-promotion' 2023-11-28 16:24:14 +01:00
Andrea Gussoni 078cc1222a InlineDispatcherSwitch: introduce beautify pass
We introduce the `InlineDispatcherSwitch` beautify pass. Its goal is to
try and inline the body of some of the `case`s of a exit dispatcher, in
place of the `SetNode` corresponding to that `case`, if this doesn't
introduce duplication in the code (i.e., a single `SetNode` for that
specific case value is present).

Additionally, if the inlining procedure is able to completely remove the
necessity of an exit dispatcher altogether, the pass removes it.

The pass is able to handle chains of weaved dispatcher `switch`es
referring to the same original dispatcher `switch`, by handling the
inline operation and the possible simplification level-wise.

The inlining procedure, cannot take place if a `SetNode` is contained in
the body of the case we are trying to inline, since this can possibly
break the semantics of the state variable of a loop, by placing a
`SetNode` in a more internal loop.
2023-11-28 16:19:08 +01:00
Andrea Gussoni ca422bafd2 PromoteCallNoReturn: introduce beautify pass
We introduce the PromoteCallNoReturn beautification pass. Its goal is to
restructure sequence of statements, in order to have `call`s to
`noreturn` functions as _inlined_ in the middle of the statement
sequence, and leave _non local control flow statements_ at the end of
that scope. E.g., we prefer:
```
if (cond)
  call noreturnfunc();
return;
```
to
```
if (!cond)
  return;
call noreturnfunc();
```

In order to do this, contextually, we restructure the routine computing
the `fallthrough` property, in order to be able to differentiate between
the _non local control flow statements_, a call to a `noreturn`
function, or a generic mix of the two (useful when combining results
from the two situations above).

The new analysis is also used in the `promoteNoFallThrough` promotion
pass.
2023-11-28 16:19:08 +01:00
Andrea Gussoni 1b19e5a9ef ASTNode: introduce DispatcherKind attribute
Introduce the `DispatcherKind` attribute as a field in the
`BasicBlockNode` and `ASTNode` classes, in order to be able to
distinguish entry and exit dispatchers, and the related set nodes.
2023-11-28 16:19:08 +01:00
Andrea Gussoni a8634b385c BeautifyGHAST: debug graph counter
Automatically compute the debug graph serialization indexes in the
beautify phase.
2023-11-28 16:19:08 +01:00
Andrea Gussoni 1e8b37ce51 ASTNode: remove unused SwitchNode constructor 2023-11-28 16:19:08 +01:00
Pietro Fezzardi e22d66bcbf Merge branch 'feature/improve-loop-matching' 2023-11-28 15:48:24 +01:00
Pietro Fezzardi deddbda41d BeautifyGHAST: match do-while before while loops
This commits reorders the beautifiers so that do-while loops are matched
before while loops.
This has the effect that loops that can be matched both as while and
do-while loops end up matched as do-while.
The consequence is that we generate C code with a layout that is much
more similar to assembly, and prevents duplicating code to recompute the
condition of the while.
2023-11-28 15:46:19 +01:00
Pietro Fezzardi 24c9fbe3fb Fix emission of do-while loops 2023-11-28 15:46:19 +01:00
Pietro Fezzardi 3e49213b43 Drop needsTopScopeDeclaration hack
This commit drops `needsTopScopeDeclaration`.
Now all the LocalVariables are declared at the top of the function.
In DecompileFunction.cpp, the emission of LocalVariables if handled at
the scope of GHAST `ASTNode`s instead of being LLVM BasicBlock-based.
This enables in the future to design and implement an analysis that for
each LocalVariable decides the C scope (represented by an `ASTNode`)
where it's declaration should be emitted to always be visible in all its
uses.
2023-11-28 15:46:19 +01:00
Pietro Fezzardi 191373acdc MarkAssigmnents: avoid const variables 2023-11-27 18:02:24 +01:00
Pietro Fezzardi 83f0457a7f Add getNonConst helper
This function returns the first non-const qualified view of a
model::QualifiedType.
2023-11-27 18:02:21 +01:00
Pietro Fezzardi 69fe37dc2f Teach initModelTypes to traverse PHINodes 2023-11-23 16:38:30 +01:00
Andrea Gussoni 6f5968d1ff SimplifyImplicitStatement: handle empty ScsNodes 2023-11-22 13:53:54 +01:00
Pietro Fezzardi 2653a6ec6b DLA: don't link formal arguments with constant
The inter-procedural part of the DLA frontend connects actual arguments
of function calls with formal arguments of the callee functions, with
instance links at offset 0.
This represents the information that the type pointed-to by the actual
argument has an instance of the type pointed-to by the formal argument
at offset 0.

Before this commit, this was done even when the actual argument was an
integer constant leading to various problems:
1. Most of the times, small integer constants passed as actual
   cannot represent valid pointers, leading to graphs bigger than
   necessary.
2. Even when an integer constant might actually represent a pointer into
   valid memory, if it does it should fall into some segment.
   If it does, by the time DLA runs, those integer constants should have
   already converted into calls to the special SegmentRef opcode, which
   is already handled properly.
3. Finally, those constants end up being very connected in the DLA
   graph, creating connections between other nodes that are otherwise
   unrelated or very far from each other. This pollutes the graph and
   rapidly degrades the quality of the results.

This commit properly guards the code so that the instance-at-offset-0
link between actual and formal arguments is never injected if the actual
argument is an integer constant.
2023-11-22 10:16:06 +01:00
Pietro Fezzardi ebb2b6a45b DLA: enable Frontend to see through ZExt 2023-11-22 00:59:52 +01:00
Pietro Fezzardi 2bca392be3 Fix typo 2023-11-22 00:59:30 +01:00
Pietro Fezzardi 71b2e8e799 DLA: drop old Logger that was creating files 2023-11-22 00:59:08 +01:00
Ivan Krysak fa7bf17dae IRCanonicalization: introduce ternary-reduction 2023-11-21 17:05:18 +01:00
Pietro Fezzardi 59603cdafb HeadersGeneration: make type dependency stricter
This commit makes some of the dependencies among types in
HeadersGeneration stricter than they were before.

The patch is a temporary workaround to the problem of pointers to arrays
of struct/union in C.

Basically, in C, `struct X (*ptr_to_array)[2];` declares a variable
`ptr_to_array` that points to an array with two elements of type `struct
X`. The problem is that, because of a quirk of paragraph 6.7.6.2 of the
C11 standard (Array declarators), to declare `ptr_to_array` it is required
to see the copmlete definition of `struct X`.
Even if MSVC seems to compile it just fine, clang and gcc don't.

In principle this could be worked around by doing the following two
things:
1) introducing wrapper structs around arrays of struct/union that are used
as pointees
2) postpone the complete definition of the wrapper to after the element
type of the array is complete.

However for now we just inject a stronger dependency to enforce ordering.
This is actually stricter than necessary and can yield to be unable to
print valid C code for model that was otherwise perfectly valid and could
have been fixed by injecting the wrapper structs properly.

This particular handling of pointers to array is more strict than actually
necessary. It has been implemented as a workaround, instead of handling
the emission of wrapper structs. This latter solution of emitting structs
has already been used in other places but, in all the other places where we
currently do it, it is possible to do it on-the-fly, locally.
On the other hand, for dealing with this case properly we'd have to keep
track of dependencies between the forward declaration of the wrapper, and
the full definition of the element type of the wrapped array.
The emission of the full definition of the wrapper must be postponed until
the element type of the wrapped type is fully defined, otherwise it would
fail compilation. So fow now we've put this forced dependency, that could
be relaxed if we properly handle the array wrappers.
2023-11-21 11:34:57 +01:00
Pietro Fezzardi b81481ab2b MakeModelGEP: improve logging 2023-11-20 22:37:20 +01:00
Pietro Fezzardi 0572b8f972 Relax assertion on mismatching size of binary ops
This fixes a crash due to the fact that pointers on the model may have
different size than pointer on LLVM IR, due to revng always using
64-bits DataLayout, even when decompiling 32-bits architecture.

The assertion did not take this into account.
In principle the assertion could be removed, but it might become a
source of broken semantics, so it's better to keep an advanced verions
of the assertion in place, to make sure we only allow situations that we
fully understand, and that we can guarantee don't break semantics.
2023-11-20 22:36:59 +01:00
Pietro Fezzardi dd8049abcc Add LiteralPrintDecorator Tag
This Tag is used to tag all the funcitons that we use to decorate
integer literals to decide how to print them.
Using a single Tag shared among all the decorators enables more concise
code to handle it.
2023-11-20 22:36:39 +01:00
Pietro Fezzardi d8cc7091ee DLA: improve logging for StepManager 2023-11-20 22:35:06 +01:00
Pietro Fezzardi 5a0d11ec32 SimplifyInstanceAtOffset0.cpp: drop old comment 2023-11-20 22:34:46 +01:00
Alessandro Di Federico d19a7c0c5d DLA: handle scPtrToInt 2023-11-20 15:52:34 +01:00
Massimo Fioravanti fdf5cf0325 Fix incorrect usage of TaggedFunctionKinds
A bug in revng allowed to use any TaggedFunctionKinds in place of any
other one. That bug has been fixed and the wrong usages in revng-c have
been updated.
2023-11-06 14:35:06 +01:00
Massimo Fioravanti b0614c3a4b Fix: isolated functions targets overappresented
Isolated functions no longer show up in the content of the pipeline
multiple times equal to the number of tags.
2023-11-06 14:33:56 +01:00
Alessandro Di Federico 8549cc9346 LDDTree: parse ld.so.conf 2023-11-06 14:33:21 +01:00
Pietro Fezzardi 2b2b7e3123 RegionCFG: drop old unused PDT and IFDT fields 2023-11-06 13:50:54 +01:00
Alessandro Di Federico e7f9facfe2 Merge branch 'feature/tracing-improvements' 2023-11-03 18:29:31 +01:00
Giacomo Vercesi 56abe3b797 Tracing/Runner: improve pointer vector handling
Wrap vectors of pointers in the new `PointerVector` class which
automatically poisons the tail of the vector and unpoisons it on
destruction.
2023-11-03 18:29:10 +01:00
Giacomo Vercesi da144f1fee revng trace run: allow providing resume directory
Allow the `revng trace run` to provide a resume directory from the
command line to use when creating the first manager.
2023-11-03 18:29:10 +01:00
Giacomo Vercesi 6d03b5874e InitRevng: HideUnrelatedOptions only when provided
Avoid calling `HideUnrelatedOptions` in InitRevng if the
`CategoriesToHide` option is empty.
2023-11-03 18:29:10 +01:00
Alessandro Di Federico feef267b22 Merge branch 'feature/comment-edit-in-ui' 2023-11-03 17:44:52 +01:00
Giacomo Vercesi 68f71c3b39 Adopt new PTML tags
Change the emitted decompile code to include the
`data-action-context-location` and `data-name-of` tags.
2023-11-03 16:31:06 +01:00
Giacomo Vercesi 94efdc142a Drop model::editPath helpers
With the introduction of `data-action-context-location` the helpers in
the `model::editPath` namespace are no longer needed.
2023-11-03 16:31:02 +01:00
Giacomo Vercesi db1691b03f Adopt new PTML tags
Change the emitted disassembly to include the
`data-action-context-location` and `data-name-of` tags.
2023-11-03 16:30:40 +01:00