Commit Graph

216 Commits

Author SHA1 Message Date
Alessandro Di Federico 2e0266ed05 mv {JumpTargetManager,BinaryFile}::nameForAddress 2020-06-02 21:14:56 +02:00
Alessandro Di Federico 3f79683c18 Drop CFG suffix from CFGForm::Values 2020-06-02 21:14:55 +02:00
Alessandro Di Federico 23f1a7b342 Reduce blocks analyzed by AVI
AVI and InstCombine are the bottleneck of the lifting process.
This commit introduces a whitelist of jump targets that are considered
by AVI during harvesting.
The whitelist is initialized by the jump targets that are new with
respect to the last run of AVI. Then, it's expanded with all the jump
targets that can reach the initial set of jump targets through direct
jumps.
2020-06-02 21:14:50 +02:00
Alessandro Di Federico b27beecc0f Preliminary harvesting: s/InstCombine/ConstProp/ 2020-06-02 10:57:02 +02:00
Alessandro Di Federico c202b989af AVI: drop one InstCombinePass
Preparing for AVI we used to run InstCombine twice in the
pipeline. Apparently, this was not acutally necessary.
2020-06-02 10:57:02 +02:00
Alessandro Di Federico 1f7b23bb5a Introduce harvesting statistics 2020-06-02 10:57:02 +02:00
Alessandro Di Federico 773e386f22 Prevent SIGSEGV in translation with guard pages
When translation of code overflows into an unmapped page, we can get a
SIGSEGV. To avoid this, for each set of contiguous pages, we add "guard
page" containing an architecture-specific pattern that ensure basic
block termination.
2020-06-02 10:57:02 +02:00
Alessandro Di Federico d10178483d Introduce the new MetaAddress
Unlike the previous iteration of `MetaAddress`, which tried to stuff all
the parts of `MetaAddress` within the existing `PC` CSV, this
implementation adds a set of new CSVs (or marks some existing ones as) to
represent the four portions of the current PC's `MetaAddress`.

* Introduce `ProgramCounterHandler`: a class responsible to maintain the
  PC-related CSVs. This class is also used to manipulate the new
  dispatcher.
* `AdvancedValueInfo`: update for new MetaAddress.
* External jump handler: do not clobber registers.
  When introducing support for dynamic binaries, we didn't realize that
  in x86-64 we were clobbering `r11`. To avoid this, we have to jump to
  an address stored in memory. However, due to the new `MetaAddress`,
  obtaining a *jumpable* address from the PC-related CSVs might require
  some computations (and it does in ARM). Therefore, we introduce a new
  global variable, `jumpablepc`, whose only role is to contain the
  jumpable version of the program counter and then be the target of the
  memory-indirect jump instruction.
* Labels care only about absolute addresses.
* CSAA: mark call site, even if no accesses.
2020-06-02 10:57:02 +02:00
Alessandro Di Federico 10435feb59 Whitespace and other minor changes 2020-05-14 22:55:18 +02:00
Alessandro Di Federico 89258b832d Add ARM's PCMContextIndex
`PCMContextIndex` is the index of the PC register within the `mcontex_t`
`struct`.
2020-05-14 22:55:18 +02:00
Alessandro Di Federico 1e5fd5cc25 BinaryFile::readRawValue: prevent invalid reads
It might happend that, while parsing an input binary, we are request to
read at a valid address, but a for a size leading outside of a valid
page.

This commit, copies as much data as possible from the valid page into a
temporary buffer of the appropriate size, and then performs the read on
that temporary buffer.
2020-05-14 22:22:31 +02:00
Alessandro Di Federico 429567b885 Handle binaries without ELF program headers
Not all binaries have ELF program headers, e.g., Mach-O binaries.
2020-05-14 22:22:31 +02:00
Alessandro Di Federico 20d346aaf7 Fix handling of Mach-O LC_UNIXTHREAD 2020-05-14 22:22:31 +02:00
Alessandro Di Federico 9cfb96c18c Run InstCombine on generated code
We now run InstCombine on the generated code in order to simplify
further analyses. In particular this is useful to handle code such as
the following:

    %pc = load @pc
    %is_thumb = and %pc, 1
    %address = and %pc, ~1
    %pc2 = or %is_thumb, %pc

After InstCombine `%pc` and `%pc2` are collapsed.
2020-05-14 11:59:05 +02:00
Pietro Fezzardi 1c02dfd2a5 CSAA: strip bitcasts on pointers to env 2020-05-14 11:59:02 +02:00
Alessandro Di Federico dc48188f37 Introduce MetaAddress
`MetaAddress` replaces all the `uint64_t` used to represent a virtual
address. Its main features are:

* It has a non-zero representation of invalid addresses.
* It supports tags to represent code that has different interpretations but
  resides at the same address in memory (namely ARM vs Thumb).
* Arithmetic operations cannot overflow.
* It supports epochs, a way we intend to employ to handle self-modifying code
  (i.e., different code at the same address at different times).
* It supports "address spaces", which enable handling architectures with
  multiple address spaces.
* It fits in two 64-bit registers.
2020-05-14 11:58:18 +02:00
Alessandro Di Federico 58a070349b Fixes in handling DWARF information 2020-05-14 11:58:18 +02:00
Alessandro Di Federico 35171a789d Sort translated basic blocks in reverse post-order
After translation, `revng-lift` now sorts all the basic blocks in
reverse post-order to ease manual reading of the generated modules.
2020-05-14 11:58:18 +02:00
Alessandro Di Federico 20c367b88f Add ArchitectureName to revng.input.architecture 2020-05-14 11:58:18 +02:00
Alessandro Di Federico da9e6094d3 Introduce Label::hasValue 2020-05-14 11:58:18 +02:00
Alessandro Di Federico cb803dce70 BinaryFile::parseELF: fix labels creation
The loop creating the labes in `BinaryFile::parseELF` was badly indent
and resulted in the creation of labels only if the `.dynamic` section
was available.
2020-05-14 11:58:18 +02:00
Alessandro Di Federico 72fc95bda9 Purge OriginalInstructionAddresses when deleting
`JumpTargetManager::purgeTranslation` deletes some basic blocks that
need to re-translated, however references to some instructions remained
in `OriginalInstructionAddresses`.
2020-05-14 11:58:18 +02:00
Alessandro Di Federico 8deab9c7ea Whitespace and other minor changes
* Drop unused argument names from function prototypes
* Make `static` some methods
* Disable some copy constructors
* Fix casing of Doxygen `\file` directives
* Add some casts to make the compiler happy
* Initialize `hasRelocationAddend` for AArch64
* Use references in range-for where possible
* Drop default for `switch` statements covering all the entries of an `enum`
* Make some global variables `static`
* Drop dead functions
2020-05-14 11:58:18 +02:00
Alessandro Di Federico c1a768ff21 Drop InstructionTranslator::ForceNewPC
It was a leftover for a problem (marking post-helper addresses as jump
targets) that we appear to have solved in another way.
2020-03-02 12:38:01 +01:00
Pietro Fezzardi 5d0f5fef71 Drop TransformIterator
LLVM already implements an analogous `mapped_iterator` type in
`STLExtras.h` header.  `llvm::mapped_iterator` has been used to
substitute `TransformIterator` in the project so that we don't need to
reinvent the wheel.
2020-02-27 12:28:28 +01:00
Andrea Gussoni 2896664cab JTM::readFromPointer: handle ConstantNullPointer 2020-02-27 11:57:34 +01:00
Pietro Fezzardi ae948500eb Move LLVM header where it's really needed
Before this commit, the header `revng/Support/DebugHelper.h` could only
be included after explicitly including
`llvm/IR/AssemblyAnnotationWriter.h`, since `DebugHelper.h` used the
LLVM class `AssemblyAnnotationWriter`, that is not defined in
`DebugHelper.h`.

This commit includes `llvm/IR/AssemblyAnnotationWriter.h` directly into
`revng/Support/DebugHelper.h`, which can now be included alone without
compilation errors.
2020-02-27 10:47:07 +01:00
Alessandro Di Federico b6658df052 Fix various warnings 2019-11-24 22:52:23 +01:00
Alessandro Di Federico 229a93367c Switch to C++20 2019-11-24 22:47:06 +01:00
Alessandro Di Federico 7b2744f0ab Register ModuleAnalysisManager in new PassManager 2019-11-21 23:35:42 +01:00
Alessandro Di Federico ae8da14d46 Adopt FunctionCallee
`getOrInsertFunction` in LLVM 9 now returns `FunctionCallee`.
2019-11-21 20:52:55 +01:00
Alessandro Di Federico 0eb1bb583f Remove TerminatorInst
LLVM 9 drops the `TerminatorInst` class. This commit replaces it with
`Instruction` where possible and asserts
`Instruction::isTerminator()`. It also switches from
`TerminatorInst::successors` to `successors(TerminatorInst *)`.
2019-11-21 20:50:55 +01:00
Alessandro Di Federico 8d664c1588 Fix RPATHs
This commit replicates the structure of the install directory into the
build directory in order to simplify the management of RPATHs.

This commit also drop `QEMU_INSTALL_PATH`.
2019-09-04 11:52:07 +02:00
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