Commit Graph

3168 Commits

Author SHA1 Message Date
Pietro Fezzardi b434e10f92 ASTBuildAnalysis: enable returning const aggregate 2020-11-19 17:11:03 +01:00
Pietro Fezzardi 584faf5097 Add MakeEnvNullPass to decompilation pipeline 2020-11-18 18:04:14 +01:00
Pietro Fezzardi d4d8430553 Add MakeEnvNull pass
This pass substitutes the `@env` global variable with a null pointer
`@env` is a pointer to the CPU state in QEMU, and is not meant to be
visible in the decompiled code.
This pass is used to remove all references to it before decompilation.
2020-11-17 14:33:19 +01:00
Pietro Fezzardi 9869f057b9 Use #pragma once for header include guards 2020-11-13 14:12:18 +01:00
Pietro Fezzardi cd9bc34d9d Enforce new include conventions 2020-11-13 10:00:24 +01:00
Pietro Fezzardi 429633b627 Use #pragma once for header include guards 2020-11-12 18:00:45 +01:00
Pietro Fezzardi 93d9cbb67b Enforce new include conventions 2020-11-12 18:00:45 +01:00
Alessandro Di Federico 14cf2a399d Make assertion messages prettier 2020-11-12 16:39:03 +01:00
Alessandro Di Federico 5e7d288530 Introduce PruneRetSuccessors pass
This commit introduces the PruneRetSuccessors pass, whose role is to
identify all the indirect jumps whose devirtualized destinations
correspond to return addresses. In fact, they are most likely to be
return instructions and devirtualizing them is always detrimental.
2020-11-12 15:28:31 +01:00
Alessandro Di Federico d609c2e74d ABI Analysis: more verbose logging 2020-11-12 15:15:27 +01:00
Alessandro Di Federico 5f25194ec8 IncoherentCallsAnalysis: introduce Logger 2020-11-12 14:56:05 +01:00
Antonio Frighetto cd4a8dcf79 StackAnalysis: remove cleanup in mergeASState
After combining two AS objects, removing an ASOContent in the cleanup
phase may lead to a mismatch in the CSVs promoted to function argument,
with some registers actually being used. In most cases, this results in
promoting only one or few alive registers as function argument.
2020-11-12 14:56:03 +01:00
Pietro Fezzardi fc537a2fc7 Add missing MIT license headers 2020-11-12 14:55:57 +01:00
Alessandro Di Federico 29080a2f29 StackAnalysis: resetCacheMustHit on right entry
We used to accidentaly call `resetCacheMustHit` on the just popped
element of the set, resulting in an out-of-bounds access within the
container.

This bug has been spotted thanks to AddressSanitizer.
2020-11-12 14:55:57 +01:00
Alessandro Di Federico cf5fca85a1 Whitespace changes 2020-11-12 14:55:56 +01:00
Alessandro Di Federico 37fde04594 Move licensing details to LICENSE.md 2020-11-09 10:03:33 +01:00
Andrea Gussoni 9c910604fd EnforceABI: add opaque store to PC after call
Add an additional opaque store the the `pc` after each function call in
the enforced functions. In this way, when the `pc` will be promoted to
an alloca, we won't have an inconsistent state of the IR (the callee
cannot propagate the correct value of pc, the safety check introduced by
the function isolation will always be false, and llvm will place a
`llvm.assume` which will in turn make the rest of the code dead and
purged by an additional `-simplifycfg`).
2020-11-08 20:52:03 +01:00
Pietro Fezzardi 5de14227cf Improve generation of valid C identifiers
This commit does not implement real mangling yet, but it should at least
get most low-hanging fruits, to prevent generation of invalid C
identifiers.
2020-11-06 15:08:42 +01:00
Andrea Gussoni 1bade3fd88 ASTTree: delete sequence nodes in simplification
When performing `atomicSequenceSimplification`, actually remove the
nodes from the ASTTree.
2020-11-06 15:08:00 +01:00
Andrea Gussoni 5afaa7a90b ASTTree: AST debug printing is now iterative
Change the AST debug printing from a recursive descent on the AST nodes
in the graph, to an iterative printing of respectively nodes and their
outgoing edges.

Also, we now print the edge going to the `Successor` field, if present.
2020-11-06 15:08:00 +01:00
Andrea Gussoni f3f11533b8 ASTTree: Reorganize AST dot printing
Reorganize the AST dot printing in order to use LLVM facilities for
files handling. In addition, clean up the code, and add a function
(useful in debug) in order to dump the dot of and AST from GDB (accepts
a `char *` instead of a `std::string`.
2020-11-06 15:08:00 +01:00
Andrea Gussoni df0985f1fc RegionCFG: dumpOnFile to dumpASTOnFile 2020-11-06 15:08:00 +01:00
Andrea Gussoni eb1ddab86c RegionCFG: dumpDotOnFile to dumpCFGOnFile 2020-11-06 15:08:00 +01:00
Andrea Gussoni 7362e7beeb RegionCFG: Reorganize graph debug
Now, when the `debug-log=restructure` flag is active, debug graphs are
organized in a more rational way.

First of all, all the graphs are now put in a single directory, and are
divided first by function name and then by type.

Also, as in the case of the tiling debug graphs, their name is more
consistent with the rest of the codebase.
2020-11-06 15:08:00 +01:00
Pietro Fezzardi 217a18edb1 Update copyright notice 2020-11-06 15:07:45 +01:00
Alessandro Di Federico 10d3561212 StackAnalysis: disable expensive assertions 2020-11-06 10:59:00 +01:00
Alessandro Di Federico d0a61e7947 Add logger to dump CFEPs 2020-11-06 10:59:00 +01:00
Pietro Fezzardi 890d60833b Drop old obsolete RemovePCStoresPass 2020-11-04 09:42:13 +01:00
Pietro Fezzardi 8bfbb153ca libDecompiler: new public API decompileFunction 2020-11-04 09:32:36 +01:00
Pietro Fezzardi f127764e0b CDecompilerBeautify: fix beautification of IfNodes
This commit fixes two interacting problems in the beautification of if
statements.

First, in `simplifyShortCircuit`, there were two assertion on null
pointers that were too strict. These assertions didn't take into
consideration that one among the "then" and the "else" branch of an
`IfNode` could be simplified away _during_ the beautification itself.

Second, a previous commit had moved the execution `flipEmptyThen` after
the two beautification of short-circuit, leading to assertions failed
in `simplifyTrivialShortCircuit`.
This was caused by the fact that `simplifyTrivialShortCircuit` only
never expected `IfNode`s with empty "then".
This was fixed by running `flipEmptyThen` also before
`simplifyTrivialShortCircuit`.
2020-11-04 09:32:36 +01:00
Pietro Fezzardi 90ff17f107 Fix handling of switch default in GHAST 2020-11-04 09:32:36 +01:00
Pietro Fezzardi 508719ad97 HACK: remove assertion on NDuplicates
This commit is a hack, that removes an assertion on NDuplicates.
For now we can remove this check, because we declare all the local
variables at the beginning of the function body.
Whenever we start emitting the declarations of local variables
as-late-as-possible, we'll need to take care of this.
2020-11-03 13:50:20 +01:00
Pietro Fezzardi 87611643ad Fix names of struct types declarations
Before this commit, we assigned names to struct types without looking at
the function names of which they were return types.
Now we do, so that the name of the generated type for the function `f`
is `f_ret_type`.

This also fixes errors when reparsing the generated C code, where the
names of the struct types did not match.
2020-11-03 12:53:11 +01:00
Andrea Gussoni 371dc7ea97 Remove exceptions before decompilation in pipeline
We now craft a new decompilation pipeline. We remove the exceptions
introduced as fallbacks to preserve the semantics, and we perform passes
of `dce` and `simplifycfg` to remove all the superfluos basic block
remaining after this change. To do this, we need an additional pass that
removes also the calls to the `llvm.assume` intrinsic, and another pass
of `dce` to remove all the dead uses.

In addition, we also remove dead stores to the `cpu_loop_exiting` global
variable in order to improve the decompiled code.

As a byproduct of this, we remove also the dependency from the
`-remove-pc-stores` pass, since its jobs is included by the changes
mentioned above.
2020-11-02 18:33:57 +01:00
Pietro Fezzardi 2e7b6d171f CDecompilerAction: use logical not in if stmts
This commit fixes some warnings due to the wrong emission of bitwise not
instead of logical not in condition of if statements.
2020-11-02 18:33:57 +01:00
Pietro Fezzardi ecd9ac991f CDecompilerAction: fix print of conditional blocks
This commit fixes an issue that caused to emit duplicated statements
for BasicBlocks that terminated with a conditional branch.
This was caused by a redundant call to `buildStmtsForBasicBlocks()`
inside `createCondExpr`, that has now been removed.
2020-11-02 18:33:57 +01:00
Pietro Fezzardi 2bdff9bbaf MarkForSerialization: fix ExtractValueInst
With this commit `ExtractValueInst`s are handled so that they don't have
side effects.
This significantly improves the quality of the decompiled output,
preventing the forced serialization of all the extraction of struct
fields after calls to function that return structs, except for when it's
really needed.
2020-11-02 12:52:58 +01:00
Pietro Fezzardi 37f43f7e83 Use Value names for C declarations if possible
With this commit, the declarations of local variables and function
parameters now use the name of the associated `llvm::Value` if present,
instead of always using the prefix "param_" or "var_".
2020-11-02 12:32:22 +01:00
Alessandro Di Federico 6bfae0409b FunctionIsolation: drop isolate-no-safety-checks
This commit drops the `--isolate-no-safety-checks` flags since we
adopted a new approach later on in the decompilation pipeline, i.e.,
dropping calls to `raise_exception_helper` and running dead code
elimination.
2020-11-02 11:21:32 +01:00
Alessandro Di Federico ccb0b183a5 FunctionIsolation: member.type, not function_call
FunctionIsolation used to base its work on calls to the marker function
`function_call`, as opposed to information provided by the StackAnalysis
(i.e., `revng.member.type` along with `func.call`).

This also affected EnforceABI, which took care of finishing the work
left over by FunctionIsolation. This was hackish and inelegant.

This commit makes FunctionIsolation work exclusively employing
information from StackAnalysis and purges away code that is no longer
necessary from EnforceABI.
2020-11-02 11:21:07 +01:00
Pietro Fezzardi 99a6a18f26 createAST(): fix size of stack-allocated arrays
This commit fixes a wrong computation that caused stack-allocated arrays
to use the wrong size in bits instead of the correct size in bytes.
2020-11-02 10:43:06 +01:00
Alessandro Di Federico 7f90bc4fe0 StackAnalysis: callee saved must pass by the stack
We used to detect callee-saved registers only by checking if their final
value was identical to the initial one. However, the need for a more
precise heuristic emerged: we now also check if at least on of the stack
slots contains that same value.
2020-11-02 09:05:12 +01:00
Alessandro Di Federico dd774af288 ABIAnalysis: merge DefaultMap::Default last
This commit fixes a subtle bug that was preventing us from correctly
considering the effects of function calls during ABI analysis.

Specifically, when merging information from the call site to the caller,
in case the caller did not provide any information about a certain
slots, we used the `DefaultMap::Default` field, which is the correct
thing to do, except for the fact that, in two methods, the `Default`
field was being updated to early.

This basically had the effect of not considering arguments of function
calls as used, e.g., in URAOF.
2020-11-02 09:05:12 +01:00
Alessandro Di Federico dd29699aed FunctionsSummary: print the callee too 2020-11-02 09:05:12 +01:00
Alessandro Di Federico 51b29afc30 Make ABIAnalysis mandatory 2020-11-02 09:05:12 +01:00
Andrea Gussoni 02a4970fa6 EnforceABI: outline function_dispatcher on flag
When the `--enforce-no-safety-checks` flag is passed during the
enforcing, we avoid emitting the inlined `function_dispatcher` and we
emit instead a call to an `indirect_handle` disposable call created on
the fly to respect the ABI information on the callsite.
2020-10-30 14:17:57 +01:00
Alessandro Di Federico bf2ac34b38 FunctionSymbol blocks are tier-1 CFEPs 2020-10-30 09:10:00 +01:00
Pietro Fezzardi 2b599d0ebc RestructureCFG: fix NDuplicates 2020-10-30 01:12:03 +01:00
Pietro Fezzardi 27f850e433 GlobalDeclCreationAction: fix short literals
Before this commit, when GlobalDeclCreationAction needed to emit
literals for initialization of global variables, it did it using
custom code.
This was not working properly, an in some cases it emitted short
literals which are not allowed in C.
Hence the generated C code that was impossible to recompile without
syntax errors.

This commit fixes this problem, using the getLiteralFromConstant method
of StmtBuilder.
In order to do this, we need to make the StmtBuilder available inside
the GlobalDeclCreationAction, which is not a very clean design.
However, we are already planning to merge the GlobalDeclCreationAction
and the StmtBuilder class, so this issue will be taken care of in the
future.
2020-10-29 14:27:38 +01:00
Pietro Fezzardi b2437ddabc Drop old unused pass RemoveUnexpectedPCPass 2020-10-29 09:34:05 +01:00