Commit Graph

233 Commits

Author SHA1 Message Date
Alessandro Di Federico 5d559367a4 PTCDump.cpp: use strncpy as appropriate
In `PTCDump.cpp` we were using `strncpy` in an invalid fashion: the `n`
argument was exactly the same size of the buffer, which might lead to a
missing NUL-terminator. This commit increases the buffer size by one
byte and initializes it with '\0'.

This bug was reported by `-Wstringop-truncation` of GCC 9.1.0 on a
Release build.
2019-05-28 21:32:40 +02:00
Alessandro Di Federico 707ec7c1fa Introduce preliminary Mach-O support 2019-05-27 19:36:12 +02:00
AndreaJegher 8492c56333 Enable AArch64 support (no tests)
This commit enables AArch64 as an input architecture. However, tests are
not enabled yet due to some known bugs.
2019-05-27 19:36:12 +02:00
Alessandro Di Federico c7e1c992de DropMarkerCalls: fix dangling pointer refs
`DropMarkerCalls` used to take an `ArrayRef<StringRef>` as an argument,
however the `ArrayRef` was copied in a class field, leading to a
reference to a temporary array.

This commit switches from `ArrayRef` to `SmallVector`.
2019-05-27 19:36:12 +02:00
Alessandro Di Federico b9ef70c8dc AdvancedValueInfo: handle pointer types 2019-05-27 19:36:12 +02:00
Alessandro Di Federico 877ba895af Rewrite get{BasicBlockPC,PrevPCWrite}
This commit reimplements `getBasicBlockPC` and `getPrevPCWrite` using
`BackwardBFSVisitor`, making them much more effective.
2019-05-27 19:36:12 +02:00
Alessandro Di Federico d0e5f61b20 CSAA: allow recursion in untainted paths 2019-05-27 19:36:12 +02:00
Alessandro Di Federico 4b542de82f Drop unnecessary masking loading CSVs
When loading a CSV, we sometimes need to mask part of the loaded
value. However, this happened also when the source and target types had
the same size, leading to complex code doing nothing.

This bug has been discovered due to a problem in i386: all the function
calls were identified as indirect.
2019-05-27 19:36:12 +02:00
Alessandro Di Federico 1c6f8ee0f0 Fix disassembly printing too many instructions
This commit fixes a bug that lead to print 4096 disassembly instructions
at a time instead of one. This restore `--debug-log ptc` into a useful
state.
2019-05-27 19:36:12 +02:00
Alessandro Di Federico 5eee55ff11 Whitespace and other minor changes 2019-05-27 19:34:44 +02:00
Alessandro Di Federico 378d6366fe Let CpuLoopFunctionPass handle exception_index
`CpuLoopFunctionPass` is a pass performing some changes to
`cpu_loop`. Among these, calls to `cpu_*_exec` are replaced with the
value of `exception_index`.

This commit does not directly loads it but computes its address as an
offset from `CPUState`. This commit goes in tandem with the
corresponding QEMU commit which provide `exception_index` as an offset
from `CPUState` instead than from `env`. All of this makes CSAA more
effective.
2019-05-22 21:30:29 +02:00
Alessandro Di Federico 5009074e9e Introduce AdvancedValueInfo for JT discovery
* Introduce `ShrinkInstructionOperandsPass`: a transformation shrinking
  operands and the results of instructions if they are
  zero/sign-extended immediately before and after the instruction.
* Introduce `ConstantRangeSet`: similar to `ConstantRange` but allows
  disjoint ranges.
* Introduce `MaterializedValue`: a class that can represent a constant
  value or a symbol plus offset pair.
* Introduce `DropHelperCallsPass`: a transformation removing calls to
  helpers and replacing them with a function call reading the CSVs that
  the helper reads and writing the CSVs that the helper writes
  (according to CSAA).
* Introduce `DropRangeMetadataPass`: a transformation dropping the
  `range` metadata, which, in certain situations, lowers the quality of
  the results provided by `LazyValueInfo`.
* Introduce `AdvancedValueInfo`: an analysis exploiting results of
  `LazyValueInfo` but collecting them as `ConstantRangeSet` with a
  monotone framework. It produces `MaterializedValue`.
* Anticipate linking of helpers: `AVI` requires `CSAA`, which requires
  helper functions to be linked in.
* Drop `--no-link`.
* Force x86-64 `DataLayout`.
* Reorganize harvesting to either collect simple literals or go with
  (incremental) `AVI`.
* Drop `SET`, `OSRA`, the reaching definition analysis, the
  `SimplifyComparisonsPass` and all the sumjump-related code: e now
  clone `root`, optimize it and analyze it with `AVI`.
* Temporarily drop the `NoReturnAnalysis`.
* Link `libLLVMInstCombine`, `libLLVMCodeGen` and `libLLVMPasses`.
* Introduce tests for `AdvancedValueInfo`,
  `ShrinkInstructionOperandsPass` and `ConstantRangeSet`.
* Fix test results.
* Add `llvm.bswap.i64` and `@pc` to the LLVM template module for unit
  tests.
2019-05-22 21:30:29 +02:00
Alessandro Di Federico 3bef125a54 PTCInstructionList: release memory after free
To release memory of a `PTCInstructionList`, we used to call
`ptc_instruction_list_free` only. However, that function does not
release memory. This commit calls `delete` to actually release the
memory and prevent memory from leaking.
2019-05-22 18:42:32 +02:00
Alessandro Di Federico 8627a32b4e dlopen: use RTLD_NODELETE to make ASan happy 2019-05-22 18:42:32 +02:00
Alessandro Di Federico ddb23e7147 CSAA: use more conventional metadata names 2019-05-22 18:42:32 +02:00
Alessandro Di Federico 00d5a00d86 CSAA: handle casted users of env 2019-05-22 18:42:32 +02:00
Pietro Fezzardi f205a28d97 CPUStateAccessAnalysis: handle recursion 2019-05-22 18:42:32 +02:00
Pietro Fezzardi 90bdfb07c8 Make CPUStateAccessAnalysisPass lazy on demand
CPUStateAccessAnalysisPass can now run lazily and accumulate partial
results. Actual fixes to the accesses to the CPU State (to make them
explicit) are only applied when the analysis is run in non-lazy mode.
2019-05-21 16:46:20 +02:00
Alessandro Di Federico 6ae3b6f22f Whitespace and other minor changes
* `GeneratedCodeBasicInfo::getCSVUsedByHelperCall` and
  `GeneratedCodeBasicInfo::extractCSVs`: make the call argument an
  `Instruction`.
* Introduce `blockByName`
* Introduce `getUniqueUser`.
* Fix linking issues.
2019-05-14 16:27:13 +02:00
Alessandro Di Federico d75e5f3b6a Improve detection of external function calls
This commit enlarges the set of external function calls we
detect. Basically, instead of assuming that the instruction jumping to
the external function is the call, we now also consider instructions in
previous basic blocks, as long as there's only one possible path
backward.
2019-04-11 19:51:32 +02:00
Alessandro Di Federico 2a5081328c NRA: do not iterate while deleting calls 2019-04-11 18:16:53 +02:00
Alessandro Di Federico d77ad08917 Drop leftover references to revamb 2019-04-11 17:44:39 +02:00
Alain Carlucci 51a58a5513 Add support for PE32+ (x86-64) 2019-04-04 22:55:45 +02:00
Alessandro Di Federico 02273d58e6 Add ABI registers for architectures missing them 2019-03-08 15:00:59 +01:00
Alessandro Di Federico 57693f3653 NRA: compute PredecessorsMap internally 2019-03-08 15:00:59 +01:00
Alessandro Di Federico a61c744bd8 CSAA: fix bug leading to ignore stores 2019-03-06 09:04:58 +01:00
Alessandro Di Federico 17b1eb19a9 Introduce revng.csv for listing CSVs
Previously, to enumerate all the CSVs we had to go through the
`GlobalVariable` of a `Module` and see if the were being used in
rev.ng-generated code.

Now we have a named metadata for that: `revng.csv`.
2019-03-06 09:04:58 +01:00
Alessandro Di Federico 82412a5a05 CSAA: use GlobalVariable *, not names
The CPUStateAccessAnalysis used to record its results in terms of the
names of GlobalVariable, however, metadata can be constants, and,
specifically, `GlobalVariable`.

This commit serializes the results as references to the `GlobalVariable`
object instead of using their names.
2019-03-06 09:04:58 +01:00
Alessandro Di Federico 75914742d7 Use BlockType by name in metadata
We used to save the type of a block in the `revng.block.type` metadata
as a number. This commit serializes it as a string.

In order to do this, the `BlockType` enum has been promoted to a
namespace with the usual `getName` and `fromName` functions.
2019-03-06 09:04:58 +01:00
Pietro Fezzardi 50dd2b7b11 Remove buildEmptyExecutableSegmentList method
ExternalJumpsHandler::buildEmptyExecutableSegmentLIst() was used to
initialize an empty lists of segments for targets that did not
support dynamic libraries.
However, having an empty segment list triggered assertions in the
handling of jumps to invalid jump targets providing misleading error
messages on the segments_count that were hiding the real cause of the
error (unknown pc, invalid jump target).
This commit properly initializes the segment list to avoid the
misleading assertion messages
2019-03-06 09:04:58 +01:00
Alessandro Di Federico 53db84f7d6 s/revamb/revng/g 2019-02-11 16:09:23 +01:00
Pietro Fezzardi e594de5b3e Remove unused data members from VariableManager 2019-01-18 15:18:47 +01:00
Alessandro Di Federico 2e241acae3 Introduce SA-based ABIDetection and FBD passes
This commit drops the old FunctionBoundariesDetectionPass and introduces
a new one based on the results provided by the StackAnalysis. A very
similar pass, the ABIDetectionPass, is now available to offer the
results of the ABI analysis too.

These two new passes are a thin shim depending on the appropriate
version of the StackAnalysis (with or withour ABI anlysis) and simply
call `serializeMetadata`, which decorates the LLVM IR with the requested
information.

In addition to drop the old analysis, this commit also isolates the
function boundaries detection pass from `revamb` making it available as
a library only.
2019-01-18 15:18:47 +01:00
Alessandro Di Federico 7a045d0c0d Drop revamb-dump in favor of plain passes
This commit does the following:

* It drops `revamb-dump` and transforms all the passes it featured in
  passes that can be used directly from `opt`.
* It rename `revamb` to `revng-lift`.
* It introduces a script called `revng` which acts as a driver for the
  whole rev.ng project. It replaces `translate`, `revcc`,
  `csv-to-ld-options` and `revamb-dump`, since it offers an `opt`
  subcommand which allows to easily invoke all the analysis passes.
* It makes the project a CMake package that can be easily used
  externally.
* It allows to easily create libraries of analysis to use through
  `revng-opt`.
2019-01-18 15:18:47 +01:00
Alessandro Di Federico 05b1833ee6 Add ExternalJumpsHandlerBlock to BlockType 2019-01-18 15:18:47 +01:00
Alessandro Di Federico ef708e9912 Let --debug-log work without main involvement 2019-01-18 15:18:47 +01:00
Alessandro Di Federico bd09716534 FCI: provide a custom CFG
The `FunctionCallIdentification` analysis now provides a custom view on
the CFG where 1) dispatcher-related basic blocks are absent, 2) nodes
performing functions calls have an edge to their return address and 3)
nodes ending with a return instruction have no successor.

This CFG is now employed by the reaching definitions analysis and OSRA.

Additionally, the implementation of the `visitSuccessors` and
`visitPredecessors` method has been reviewed. It now consists in a class
that needs to be inherited and for which two methods should be
implemented, one to perform the visit of a block and another one to
enumerate the successors.

In addition, all the users of `visitSuccessors`/`visitPredecessors` have
been updated, a simple set of tests has been introduced and
`GeneratedCodeBasicInfo::visitPredecessors` has been dropped.
2019-01-18 15:18:46 +01:00
Alessandro Di Federico 7ead8d68cc Collect simple literals (return addresses) as JTs
This commit fixes a problem that led to use SET to discover very very
simple jump targets: the return addresses of functions call.

Since in this situation, for each call, we needed to run SET, this
commit leads to a *huge* improvements in terms of translation
performance.

This commit also enforces renaming of the translated basic blocks after
they get split and update the ground truth for several analysis tests.
2018-12-17 08:15:45 +01:00
Pietro Fezzardi dbb25bef2c Apply information about CSV aliasing
This commit introduces alias information to state that memory accesses
relative to CSVs do not alias memory accesses of the original program.
2018-12-17 08:15:45 +01:00
Alessandro Di Federico ef2d16db15 Install headers and libraries 2018-12-15 08:34:36 +01:00
Alessandro Di Federico 8877946be3 Handle codeless programs
Programs without code (e.g., no executable segment) led to miss the
`env` variable, making the `CpuLoopExitPass` fail. This commit, fixes
the problem by simply populating the `cpu_loop_exit` function with a
`ret`.
2018-12-15 08:34:36 +01:00
Alessandro Di Federico ac5dea43b4 Reduce exceptions thrown (e.g., due to tail calls)
In function isolation, every time we met a jump to an unexpected basic
block (i.e., a basic block that is not part of the current function), we
used to throw an exception. However this is unnecessary since oftentimes
it is sufficient to call the `function_dispatcher` or even perform a
regular function call.

The most obvious example is the case of a direct tail call. In this
situation performing a function call to the corresponding isolated
function is the most appopriate thing to do.
2018-12-15 08:34:36 +01:00
Alessandro Di Federico 2003b5c79f Being a callee is more important than being read
We used to black list memory portions target of memory read, so that
they couldn't become function entry points. However, this led to false
positives, in particular with functions that are explicitly called.

This commit ensures that we black list those addresses only if they are
not targets of function calls.
2018-12-13 18:03:11 +01:00
Andrea Gussoni af6f963d77 Improve exception handling
When optimizing a module to which function isolation has been applied the
`prune-eh` pass lead to several issues, specifically:

* `support.c` is now compiled with support for exceptions, to prevent
  `raise_exception_helper` from being marked `nounwind` during the
  link phase.
* Added a fake `ret` at the end of the `catchblock` to avoid promotion
  of `invoke`s to regular `call`s.
* Marked the `invoke` instructions as `noinline`.
2018-12-13 18:03:11 +01:00
Alessandro Di Federico 9d13ce343d OSRA: handle computations on constants
This commit fixes an issue in OSRA with computations on constants. If a
constant was added to another constant, a new constant bounded value
would be created. However, such bounded value wouldn't get propagated
further. This commit fixes the problem by creating an OSRA relative to
the original constant plus an offset.
2018-12-13 18:03:11 +01:00
Alessandro Di Federico ecfb842073 Force FCI during basic block harvesting
This commit forces the function call identification analysis during
harvesting of new basic blocks. Additionally, the appropriate jump
target reasons are associated to the involved basic blocks.
2018-12-13 18:03:11 +01:00
Alessandro Di Federico 2669d78206 Add some dump methods 2018-12-13 18:03:11 +01:00
Alessandro Di Federico 5cc504e117 Whitespace and other minor changes 2018-12-13 18:03:11 +01:00
Alessandro Di Federico f2dd01cea2 Handle [p_filesz, p_memsz] segment portion
`BinaryFile::readRawValue` scans the segment list to identify which
segment contains a certain address. However, it was failing if the
target address was in `.bss`, i.e., the portion of a segment after
`p_filesz` but `before `p_memsz`.

This commit lets `BinaryFile::readRawValue` return 0 in that situation.
2018-11-30 17:47:03 +01:00
Alessandro Di Federico 823f277d2d Do not assert on calls targeting multiple symbols 2018-11-30 17:47:03 +01:00