Commit Graph

67 Commits

Author SHA1 Message Date
Pietro Fezzardi 6a4ce00ce9 InitModelTypes: improve types of void * variables
This commit adds a post-processing step to initModelTypes, to enable it
to better discover types of void * variables, by looking at their uses.

This is useful in scenarios like e.g.:

```c
struct s {
  int32_t x;
  int32_t y;
};

struct s* init() {
  void *result = calloc(SIZE, 1);
  *((int32_t *)(result)) = SOME_INIT_VALUE;
  *((int32_t *)( (uin64_t)(result) + 4) = OTHER_INIT_VALUE;
  return result;
```

This patch enables initModelTypes to look at uses of `result`, and to
enable other transformations that depend on initModelTypes (such as
MakeModelGEP, SwitchToStatements) to turn the above code into:

```c
struct s {
  int32_t x;
  int32_t y;
};

struct s* init() {
  struct s *result = calloc(SIZE, 1);
  result->x = SOME_INIT_VALUE;
  result->y = OTHER_INIT_VALUE;
  return result;
```
2025-03-05 12:22:30 +01:00
Ivan Krysak 705e4a5955 auto [...] -> auto &&[...] 2025-02-13 13:10:51 +02:00
Alessandro Di Federico 36edf810ef Introduce support for function pointers 2025-01-28 15:20:45 +01:00
Alessandro Di Federico 143c315196 Merge revng-c into revng 2024-11-21 10:50:55 +01:00
Alessandro Di Federico 4e670ac8e6 Either link revngcSupport or revngSupport 2024-11-04 15:09:56 +01:00
Alessandro Di Federico 2e4f4d09b9 Remaining FunctionTags have been moved to revng 2024-11-04 15:09:56 +01:00
Alessandro Di Federico 800340d6e8 IRHelpers.cpp has been moved to revng 2024-11-04 15:09:56 +01:00
Alessandro Di Federico 36e2faad3d Introduce -Wunreachable-code-break 2024-10-31 17:19:55 +01:00
Alessandro Di Federico 0cb3eedd37 Do not use CallBase::getCalledFunction
Use our wrapper, which does not return nullptr if the `FunctionType`s do
not match.
2024-08-07 15:40:56 +02:00
Ivan Krysak 94a0ad6b93 Adopt reworked model::Type 2024-06-27 11:07:01 +02:00
Ivan Krysak 504928d886 Minor improvements 2024-06-27 11:07:01 +02:00
Ivan Krysak 3705906074 Model: rename Type into TypeDefinition 2024-06-27 11:07:01 +02:00
Alessandro Di Federico 5186a58053 s/FunctionMetadata/ControlFlowGraph/ 2024-06-18 17:56:24 +02:00
Pietro Fezzardi 4531b9ff5d InitModelTypes: fix IntToPtr
Before this commit, initModelTypes could return integers of the wrong
size for IntToPtrInst, whenever the integer being casted to pointer did
not have the exact same size of the pointer on the model.

This commit fixes the problem. Now initModelTypes, even if it might be
forced to return an integer type for IntToPtr, it makes sure that the
size of that integer matches the size of the pointer on the model.
2024-03-19 09:44:00 +01:00
Alessandro Di Federico 0c212b66d9 Relicense to MIT 2024-02-29 17:03:36 +01:00
Pietro Fezzardi fdf1720433 Drop ValueManipulationAnalysis (VMA)
This commit drops ValueManipulationAnalysis, which in its original
design based on MinCut and Karger, was never enabled in the
decompilation pipeline.

Until now, VMA was only used in a severely weakened form in
initModelTypes. That for was so weakened that it barely did anything.

We already have a new design for VMA so that it can work before
DataLayoutAnalysis, and on Clift.
At this point, the old VMA is basically useless anyway, and the very few
occasions where it can do something will simply be solved by the
upcoming work on making some of the remaining casts implicit.

At this point it does not make sense to keep VMA alive anymore.
2024-02-07 00:41:30 +01:00
Alessandro Di Federico 2de10213d4 Segregate: improve returning aggregates support
This commit heavily reworks how we handle returned values, making things
a bit more elegant.

Apart from this, it fixes how were handling types that on the model are
aggregates but were being returned via registers on the IR.
2023-12-12 12:04:21 +01:00
Pietro Fezzardi 69fe37dc2f Teach initModelTypes to traverse PHINodes 2023-11-23 16:38:30 +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 48a708c07c Support printing NULL in decompiled C code 2023-10-19 16:18:41 +02:00
Alessandro Di Federico cb8aa3d929 Adopt .prototype()
Also adopt `QualifiedType::getFunctionType` where appropriate in order
to unwrap typedefs.
2023-10-05 19:18:38 +02:00
Pietro Fezzardi c94d0cb626 InitModelTypes: traverse BitCast and Freeze 2023-07-26 15:10:23 +02:00
Pietro Fezzardi f04dea7636 InitModelTypes: detect type for isolated callees
Before this commit initModelTypes wasn't able to detect the type of the
callee operand for calls to isolated functions.
This commit properly supports this case.
2023-07-20 20:30:49 +02:00
Pietro Fezzardi e2791443fb Remove extractvalue instructions in decompilation
Now extractvalue instruction are replaced by dedicated
OpaqueExtractValue custom opcode, that prevents LLVM from doing strange
things with extractvalues during optimizations (such as e.g. sinking).

This is important since extractvalue instructions and struct-typed
values in general in our LLVM IR are not real first-class citizens, but
only a byproduct of the binary lifting process, and they actually
represent bundles of registers that are returned from isolated
functions.
2023-06-30 10:48:59 +02:00
Pietro Fezzardi a9128fb72b InitModelTypes: improve const-correctness 2023-06-30 10:48:59 +02:00
Valentina Sona 8dfd7c2d96 Add custom opcode BooleanNot 2023-06-12 14:17:34 +02:00
Pietro Fezzardi fceaa57f80 InitModelTypes: UnaryMinus is considered Signed 2023-06-09 09:23:55 +02:00
Giacomo Vercesi a4ad571e61 rcc: Fix typos
Fix the typos detected by `codespell`
2023-05-11 10:04:32 +02:00
Pietro Fezzardi 8413e6e872 InitModelType: fix type of cstringLiterals 2023-04-14 14:54:14 +02:00
Pietro Fezzardi 118929b8fd InitModelTypes: don't run VMA if PointerOnly 2023-04-14 14:54:14 +02:00
Alessandro Di Federico 886e1696a2 llvmIntToModelType: adapt to opaque pointers
This commit introduces modelType, which supersedes `llvmIntToModelType`
in order to better handle translation of LLVM types into model types
after introduction of opaque pointer types.  The main differences is
that `modelType` accepts the `Value` (instead of just the `Type`), so it
can better handle `AllocaInst` and `GlobalVariable`, which provide
information about the pointee.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico 340f64e150 Drop usages of PointerType::getElementType 2023-04-14 14:54:14 +02:00
Alessandro Di Federico e2ee3f5495 Switch from llvm::Optional to std::optional 2023-04-14 14:54:14 +02:00
Kacper Kołodziej 3dd647cb28 Handle StringLiteral tag in InitModelTypes
InitModelTypes: use argument type as return type for cstringLiteral
2023-04-04 09:31:10 +02:00
Ivan Krysak c2f0fc19e5 ABI: FunctionType.h -> FunctionType/Layout.h 2023-03-15 10:28:24 +01:00
Pietro Fezzardi f3e466c989 DecompileFunction.cpp: fix format for int literals 2023-03-01 12:22:35 +01:00
Kacper Kołodziej 5b94a5a649 HelpersToHeaders: skip unnecessary helpers 2023-02-07 17:43:05 +01:00
Pietro Fezzardi 97b23935de Handle FunctionTags::Exceptional
This is necessary for e.g. the abort function.
2023-01-17 11:14:47 +01:00
Kacper Kołodziej 568bd24900 Add UnaryMinus and BinaryNot 2023-01-09 15:01:16 +01:00
Pietro Fezzardi e9d84264ff Reduce the number of emitted local variables in C
This commit does various things oriented at reducing the number of local
variables emitted in C:
- MarkAssignments now know that @Copy and @Assign involving
  @LocalVariable only have side effects that affect the local variable
  itself; this enables to reduce the number of times we're forced to
  emit a local variable due to interfering side effects
- Drop the @AssignmentMarker FunctionTag; AddAssignmentMarkerPass now
  doesn't emit @AssignmentMarker anymore; instead it emits groups of
  @LocalVariable, @Copy, and @Assign, which benefit from the previous
  point
- Drop 2 MarkAssignments::Reasons: HasManyUses and HasUsesOutsideOfBB;
  both these have now been aggregated into the AlwaysAssign reason for
  simplicity, representing all reasons non involving side effects
- Update BeautifyGHAST and how it reasons about side effects when
  beautifying; before this commit it used @AssignmentMarker, now it
  looks at @Assign
- Simplify ExitSSA; before this commit it was trying hard to be smart on
  where it emitted the store instructions representing the incoming
  values of the PHI that was being destroyed; this seemed smart when we
  originally did it but it generated C code that was not really better
  to read, so this useless complexity is finally gone
2022-12-24 02:47:10 +01:00
Massimo Fioravanti 87aef3fbad revng-c now uses Model accessors 2022-12-12 11:35:52 +01:00
Kacper Kołodziej 83ab07d7fb Enable printing integers as bool, hex or char
This commit adds a new pass, PrettyIntFormatting, that injects calls to
decorator functions print_hex, print_char, and print_bool around
llvm::ConstantInt in various situations.

It also updates the rest of passes of the decompilation pipelin to
understand these new decorator functions and to properly emit decorated
integer literals in the decompiled C code.
2022-12-02 10:08:00 +01:00
Pietro Fezzardi 05c96a1a3a Adopt abi::FunctionType::ArgumentKind 2022-11-30 18:16:13 +01:00
Pietro Fezzardi 2ab5b5d9bf Fix handling of UndefValue and PoisonValue 2022-11-30 18:16:13 +01:00
Ivan Krysak 033ccbe51a Adopt FunctionType::Layout to improve CABI support 2022-11-30 18:16:13 +01:00
Ivan Krysak 654afbef0c Move pointer creation away from model::Binary 2022-11-30 18:16:13 +01:00
Massimo Fioravanti 335d402245 Change signatures to forward metadata cache. 2022-11-22 12:27:02 +01:00
Pietro Fezzardi eef93f2a85 InitModelTypes: handle CABIFunctionType
Before this commit, the code assumed that whenever we had multiple
return types on the model we had an aggregate return type also in LLVM
IR. This is not true anymore with CABIFunctionType, where many model
types can be stuffed into a single wide integer in LLVM IR.

This commit fixes the code to be able to handle that situation.
2022-11-09 11:59:51 +01:00
Pietro Fezzardi 25119c4377 Drop bugged addPointerQualifier for getPointerTo
This commit removes the bugged addPointerQualifier helper function,
that was wrongly pushing the pointer qualifier at the end.

Instead, we now use the correct model::Binary::getPointerTo method.
2022-10-21 09:45:28 +02:00
Pietro Fezzardi 887701e6f0 InitModelTypes: better support CABIFunctionType 2022-10-20 10:22:20 +02:00