Commit Graph

6113 Commits

Author SHA1 Message Date
Pietro Fezzardi 36a525314a Detect isolated functions using FunctionTags 2021-04-27 21:19:06 +02:00
Alessandro Di Federico 96a3cb83d3 Merge branch 'feature/function-tags' 2021-04-23 09:37:26 +02:00
Alessandro Di Federico 53f6328507 Introduce PromoteCSVs
This commit takes out of EnforceABI the part taking care of creating
wrappers for calls to helpers and promoting CSV to local variables.
This decoupling, enables to run -promote-csvs multiple times, for
instance after inlining.
2021-04-22 18:07:24 +02:00
Alessandro Di Federico 2e9c2ee275 Introduce FunctionTags
FunctionTags goal is to solve the long-standing problem of identifying
what type of function are we dealing with. Is it a lifted function? An
helper?

Now we have a sane way to determine this using Metadata and a proper
API.
2021-04-22 18:07:24 +02:00
Alessandro Di Federico e166ccf860 Merge branch 'feature/llvm-11-preparation' 2021-04-22 18:06:55 +02:00
Alessandro Di Federico 2ea5723a53 s/getCalledValue/getCalledOperand/g 2021-04-22 18:06:27 +02:00
Alessandro Di Federico aeb81c4218 Don't implicitly use StringRef as std::string 2021-04-22 18:06:27 +02:00
Alessandro Di Federico d6df9b682f Use MaybeAlign 2021-04-22 18:06:27 +02:00
Alessandro Di Federico bd3e048647 CPUStateAccessFixer: do not call delete directly 2021-04-22 18:06:27 +02:00
Alessandro Di Federico e92cbd91a1 LoadInst::LoadInst: pass pointee type 2021-04-22 18:06:27 +02:00
Alessandro Di Federico 93ef716311 Module::getFunction returns Function * 2021-04-22 18:06:27 +02:00
Alessandro Di Federico 90c93bfc26 Do not include PassSupport.h directly 2021-04-22 18:06:27 +02:00
Alessandro Di Federico 1e8c43b1d5 PCH::store: pass IRBuilder by reference 2021-04-22 18:06:27 +02:00
Alessandro Di Federico fee270b64e Minor changes 2021-04-22 18:06:27 +02:00
Alessandro Di Federico 0fcaf7f065 Merge branch 'feature/improve-performance' 2021-04-22 18:06:09 +02:00
Alessandro Di Federico 3ce9acf198 AVI: ignore function calls
This commit further reduces the time spent in AVI by considering all the
function calls as indirect. In fact, we currently don't handle indirect
jumps whose target is affected by computation happening before a
function call.
2021-04-22 18:05:39 +02:00
Alessandro Di Federico 33fbcd7ac2 AVI: stop backward exploration at dispatcher
This commit greatly improves the performance by ensuring that, when
computing the set of nodes we want to consider for AVI, we do not
traverse the dispatcher.

Doing so, means including *a lot* of irrelevant nodes and wasting a lot
of computation, since the CFG usually is not influenced by stuff
happening before an indirect jump.

In at least a situation the speedup is in the order of 20x, however this
depends on the size of the binary, since traversing the dispatcher means
including all the binary in the computations (as opposed to just the set
of blocks involved in the dataflow to compute a certain expression).
2021-04-22 18:05:39 +02:00
Alessandro Di Federico 84a4aef1ee nodesBetween: introduce ignore list 2021-04-22 18:05:39 +02:00
Alessandro Di Federico a27ee2faa6 Merge branch 'feature/prepare-helpers-inlining' 2021-04-22 18:05:10 +02:00
Alessandro Di Federico 0e21755eb0 Handle more count-leading-zeros helpers 2021-04-22 18:03:53 +02:00
Alessandro Di Federico 618dbf3673 s/CSVsUsedByHelperCall/CSVsUsage/g 2021-04-22 18:03:53 +02:00
Alessandro Di Federico 8c27f2bfcd OpaqueFunctionsPool: enumeration and registration 2021-04-22 18:03:53 +02:00
Alessandro Di Federico 694b76a8e2 GenericGraph: propagate graph properties to nodes 2021-04-22 18:03:53 +02:00
Alessandro Di Federico e3157ffdd4 GenericGraph: initialize EntryNode 2021-04-22 18:03:53 +02:00
Alessandro Di Federico 2a93ef2257 IRHelpers: introduce callers() 2021-04-22 18:03:53 +02:00
Alessandro Di Federico 4f7be5a966 IRHelpers: generalize getContext to GlobalObjects 2021-04-22 18:03:53 +02:00
Alessandro Di Federico b29fc4c59c Ignore nulls in revng.csvs 2021-04-22 18:03:53 +02:00
Alessandro Di Federico 02f3bb4f88 QEMU helpers no longer have optnone
QEMU helpers are compiled with -O0. This led the functions in the QEMU
helper module to have the `optnone` attribute, preventing any
optimization.

This has now been fixed on the QEMU side with the following flags:

    clang -Xclang -disable-O0-optnone
2021-04-22 18:03:53 +02:00
Alessandro Di Federico f5e8ac21c0 Merge branch 'feature/invoke-isolated-functions' 2021-04-22 18:02:57 +02:00
Alessandro Di Federico 651282a85a Introduce constructors
Isolated functions return structs. These structs used to be constructed
using `IRBuilder::CreateAggregateRet`, which is implemented using
`insertvalue` instructions. However, this approach led to ugly
decompiled code.

This commit introduces "constructor" functions that can be easily
pattern matched down the pipeline.
2021-04-22 14:45:52 +02:00
Alessandro Di Federico 2b9cbb98ec Introduce --invoke-isolated-functions
This commit ensures that FunctionIsolation and EnforceABI do only
thing. This means that they no longer modify `root`.

Instead, we have a new pass, `invoke-isolated-functions` that needs to
be run after them and replaces the entry point of the functions with
invokes to the isolated functions, possibly with the appropriate
arguments.
2021-04-22 14:45:52 +02:00
Alessandro Di Federico 081809ec38 Adopt EliminateUnreachableBlocks 2021-04-22 14:45:52 +02:00
Alessandro Di Federico c11065c7fd scripts/revng: handle same library multiple times
The `revng` script looks in several paths for analysis
libraries. However, before this commit, in case multiple versions of the
same library were available, you'd get unpredictable results.

This commit ensures that each library is considered at most once, in the
right order.
2021-04-22 14:45:52 +02:00
Alessandro Di Federico 71044a9277 Drop setNoInline from invokes
This is no longer necessary, since we now use invoke instructions
properly and it's no longer possible to demote them to calls.
2021-04-22 14:45:52 +02:00
Alessandro Di Federico 40ecee6482 Make SA tests more deterministic
When we compare StackAnalysis test results, the order in which things
appear in the JSON is relevant. However, the output was
non-deterministic due to a `std::map` using a pointer as key.

This commit improves the situation by sorting the elements by name
before dumping them in JSON.
2021-04-22 14:45:52 +02:00
Pietro Fezzardi fc7a8cac1a RestructureCFG: optimize updating Backedges 2021-04-15 11:16:57 +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 56ed3f3904 RestructureCFG: fix typo in comments 2021-04-15 09:47:53 +02:00
Pietro Fezzardi 11906b8977 libIsolatedFunctions: fix CMakeLists.txt 2021-04-15 09:46:55 +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
Pietro Fezzardi d4fe6f1a26 Merge branch 'feature/promote-nofallthrough' 2021-04-07 11:56:57 +02:00
Andrea Gussoni 4bdfb2713a Number the beautification passes debug graphs 2021-04-07 11:56:06 +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
Andrea Gussoni ddbb10f3fa Fix flipEmptyThen beautify
Do not invoke the `flipEmptyThen` beautify if we are in presence of a
`ScsNode` without body.
2021-04-07 10:42:17 +02:00
Pietro Fezzardi 77fb07926b Add recompilation tests without DLA 2021-04-01 12:56:33 +02:00
Pietro Fezzardi f49a8ce1e2 CDecompilerAction: fix order of type declarations 2021-04-01 12:51:02 +02:00
Pietro Fezzardi e4dbdeaf99 CDecompilerPass: refactor per-function ostreams 2021-04-01 12:47:48 +02:00
Pietro Fezzardi 42531f1701 Decompiler: avoid code duplication in do-while
When printing do-while loops in C, we wrongly emitted redundant
statements before the `do`, representing computation necessary for
evaluating the exit condition from the loop.

These statements were duplicated at end of the loop body, and were
entirely redundant before the `do`.
This commit removes them.
2021-03-29 17:49:25 +02:00
Pietro Fezzardi 52e29ccdbf PromoteStackPointer: do nothing with missing sp
The PromoteStackPointerPass now trivially handles running on modules
where the global variable for the stack pointer is missing.
When the global is not found, the pass just does nothing and returns
false.
This is necessary to handle very small functions, typically from tests.
2021-03-29 17:21:34 +02:00
Pietro Fezzardi 45c586d0b3 MarkForSerialization: better model side-effects
Before this commit, whenever an Instruction needed to be serialized, it
forced to serialize all the pending Instructions.
Now this happens only for Instructions with side effect, that may
interfere with the pending Instructions if they are not serialized as
well.
In all the other cases, serializing an Instruction does not force
serialization of other Instructions.
2021-03-29 17:20:43 +02:00