Commit Graph

1642 Commits

Author SHA1 Message Date
Alessandro Di Federico 0ae9999be6 Improve LLVM IR annotations
Reduce the amount of "\n" and do not print again the original
instruction unless the last instruction that had a decoration, if any,
was associated with a different instruction.
2016-08-20 03:10:45 +02:00
Alessandro Di Federico ff52a06c74 Unify return values for opcode translations
Now `CodeGenerator::translate`, `CodeGenerator::translateCall` and
`CodeGenerator::newInstruction` all return
`CodeGenerator::TranslationResult` which covers all the possible results
that the caller needs to handle such abort, stop translation, force a
new basic block or simply proceed.

This patch also prevents reading a PTC temporary that has never been
written (typically due to a mistranslation) by emitting an abort.
2016-08-20 03:10:45 +02:00
Alessandro Di Federico 329fcb3707 Introduce tracing support 2016-08-20 03:10:45 +02:00
Alessandro Di Federico 0d035a93e1 Introduce clang support: fixes and cleanup 2016-08-20 03:10:45 +02:00
Alessandro Di Federico 6acc701b54 Documentation and some refactoring 2016-08-20 03:10:45 +02:00
Alessandro Di Federico 57721ff851 Isolate SET
* Rename `JumpTargetsFromConstantsPass` to `SET`
* Move `SET` to set.{cpp,h}
* Remove some useless includes
2016-08-20 03:10:45 +02:00
Alessandro Di Federico fbca5bba2e Import OSRA and update SET
* Import OSRA
* Improve the SET (aka `JumpTargetFromConstants`) by introducing the
  `OperationsStack` class.
* Review `harvest` logic
* Allow to disable OSRA (along with the sumjump heuristic)
* Take the core of `getNextPC` out of it and move it to `getPC`, a
  function returning both the current and the next PC. Also, fix a bug
  when reaching the beginning of a basic block.
* Detect "reliable" jump targets: a "reliable" jump target is a jump
  target obtained from a store to a PC but it's not a fallthrough jump.
2016-08-20 03:10:39 +02:00
Alessandro Di Federico 670ca9d990 Remove the specila handling of null-jumps 2016-04-14 16:10:13 +02:00
Alessandro Di Federico 1ca682e3d1 Implement "unvisit" logic for SET 2016-04-14 16:10:13 +02:00
Alessandro Di Federico b74f09cad4 support.c: introduce unknownPC plus fixes
`unknownPC` is an extern function we expect to be linked to the output
which is called when we have to crash due to an unexpected jump target.

* Remove unused references to register variables, now only need the
  stack pointer
* Fix bug in how the auxiliary values were pushed on the stack.
* Push 0 HW_CAPs
* Implement some glib's functions
2016-04-14 16:10:13 +02:00
Alessandro Di Federico 7fa391381b Perform aggressive specialization on helpers
Since one of our requirements is to have all the accesses to the CPU
state explicit, we used to modify the helper functions depending on the
parameters used to call them. This was fine when we were supporting a
reduced set of helpers, but now this is not acceptable since the calling
code can call helpers in different ways. We circumvent the problem by
creating a distinct function specialization for call.
2016-04-14 16:10:13 +02:00
Alessandro Di Federico cda7bbd94e Extend support for partial CPU state reads/writes
This patch implements `VariableManager::storeToCPUStateOffset` and
`VariableManager::loadFromCPUStateOffset`, which handle in a single
point all the accesses by offset to the CPU state.

* `getTypeAtOffset`: introduce a feature to easily debug how we compute
  which field is at a specified offset in the CPU state (`--debug
  type-at-offset`).
* Let `getTypeAtOffset` and its wrappers return the offset inside a
  field of the CPU state (useful when accessing the third byte of an
  integer).
* Use a dedicated class for the `CorrectCPUStateUsage` worklist
2016-04-14 16:10:13 +02:00
Alessandro Di Federico 74ed2abd53 Implement a set of helpers for Constant unboxing 2016-04-14 16:10:13 +02:00
Alessandro Di Federico 2a2306c0ae Improve new basic block positioning 2016-04-14 16:10:13 +02:00
Alessandro Di Federico 3daa285640 Fix bug in translation of rotate 2016-04-14 16:10:13 +02:00
Alessandro Di Federico 938057c4d8 Handle basic blocks with no terminator 2016-04-14 16:10:13 +02:00
Alessandro Di Federico 0f255102cd Handle helpers that might change the PC
If before a call to an heper the PC is saved, it means that the helper
can change the PC, therefore we need to go the dispatcher on return. In
this case, we also have to force the PC after the call to be a jump
target.
2016-04-14 16:10:13 +02:00
Alessandro Di Federico 3eb8caed40 Introduce instruction alignment information 2016-04-14 16:10:13 +02:00
Alessandro Di Federico bbab7763ce At the end, remove all dead blocks 2016-04-14 16:10:13 +02:00
Alessandro Di Federico 6814f0c595 Compute explicitly the next PC while translating
When we meet a new PC from the PTC input, find immediately the next PC.
This makes the information available early and removes the necessity for
`closeLastInstruction`.
2016-04-14 16:10:13 +02:00
Alessandro Di Federico 4d9e5684e9 Update --entry parameter
Give a new, useful, meaning to the `--entry` parameter: it's new purpose
is to be able to easily try to translate the code at a certain address.
In this sense, prevent global data harvesting if `--entry` is specified.
The handling of GVN options has also been improved.
2016-04-14 16:10:13 +02:00
Alessandro Di Federico 298915ac6d Remove noreturn attribute from helpers 2016-04-14 16:10:13 +02:00
Alessandro Di Federico 6837959a16 Fix emission of program header address 2016-04-14 16:10:13 +02:00
Alessandro Di Federico c94fad95e4 Produce coverage CSV
Implement producing a CSV file containing information about the which
PCs have been translated. For each PC it is specified whether its a jump
target or not.
2016-04-14 16:10:13 +02:00
Alessandro Di Federico 7a1ed8826b Coverage: explore PC after syscall 2016-04-14 16:10:13 +02:00
Alessandro Di Federico 8a34adb1ed Soft fail when calling a pointer using CPU state 2016-02-02 16:52:28 +01:00
Alessandro Di Federico 3f2ce746ec Bug: do not sign extend composed values in add2 2016-02-02 16:51:07 +01:00
Alessandro Di Federico 8df95158fb Bug: missing breaks in bswap PTC operation 2016-02-02 16:50:39 +01:00
Alessandro Di Federico 318b478a92 Attach local temporaries to newpc calls
Pass local temporaries' allocas as parameters to newpc, so they can't be
promoted to SSA values by SROA, which creates problem when a basic block
must be split between a use and a def of such a value.
2016-02-02 16:48:32 +01:00
Alessandro Di Federico 04a2374ef5 Make support variable external and named
Assigning support variables (such as those for original segments and the
ELF header helper) external linkage and giving them a name prevents the
optimizer from dropping them.
2016-02-02 16:03:34 +01:00
Alessandro Di Federico c8274af360 Let tests run from _start 2016-01-30 11:15:50 +01:00
Alessandro Di Federico 32b756510f Prepare stack for the translated program
* Create 3 constant global variables (`phdr_address`, `e_phentsize` and
  `e_phnum`) in the IR which will be used to populate the auxiliary
  vectors at run-time.
* Update compile options for `support.c` to ignore useless warnings and
  enable debug information
* Implement in `support.c` some functions required by QEMU run-time and
  other cleanups to make it compatible with programs translated from
  `_start`, not `root`
* Implement in `support.c` the `prepare_stack` function, which
  initializes the base of the stack with environment variables,
  arguments and auxiliary vectors
* Improve syscall support
2016-01-30 11:15:04 +01:00
Alessandro Di Federico e59edd4109 Improve code pointer harvesting using GVN
If EarlyCSE didn't produce any new code pointer, we use
GlobalValueNumbering which usually leads to better results, in
particular if we remove `newpc` markers and if it can make use of alias
information, which we introduce to let the compiler know that
loads/stores to the CPU state will never alias loads/stores to normal
memory.

* Before generating any load/store instruction mark it with the
  appropriate aliasing information.
* Update `JumpTargetManager::harvest` to run GVN
* Move the `Visited` set of `JumpTargetsFromConstantsPass` in
  `JumpTargetManager`, even if currently we clear it at each invocation
  of the pass
2016-01-30 11:14:20 +01:00
Alessandro Di Federico 224fd3d93c Handle the PC += arg case
Some hand crafted assembly code perform a PC-relative jump of an
non-statically known amount. This patch introduces a simple hack to
handle such a situation by simply detecting it and marking as potential
jump targets all the instructions to come until the next jump.

This is implemented by the `JumpTargetManager::handleSumJump` and
`isSumJump` functions.

This commit also introduces a new implementation of `getNextPC` not
requiring the dominator tree.
2016-01-30 11:14:15 +01:00
Alessandro Di Federico e2e7593fa6 Coverage: explore PC after syscall 2016-01-30 11:03:36 +01:00
Alessandro Di Federico cb7096b076 Start execution from the specified virtual address 2016-01-30 11:03:09 +01:00
Alessandro Di Federico de2621165f Abort if helper module's loading fails 2016-01-30 11:02:24 +01:00
Alessandro Di Federico 475adf963c Update VariableManager's DataLayout after linking 2016-01-30 11:02:07 +01:00
Alessandro Di Federico c3138d5db6 Force the linker to import non-static functions 2016-01-30 11:00:52 +01:00
Alessandro Di Federico 25f540a8c6 Minor improvements to DebugHelper
* Ignore support functions (i.e. everything outside the `root` function)
* Update DWARF version to 4
* Update subrprogram creation interface to LLVM 3.8
2016-01-30 10:59:29 +01:00
Alessandro Di Federico f20c8e8839 Take initial values for CPU state from libtinycode 2016-01-30 10:59:00 +01:00
Alessandro Di Federico a0f89a4269 Delay Env + non-const failures to run-time 2016-01-30 10:57:57 +01:00
Alessandro Di Federico 71f919675e Speedup compilation by using llc -O0 2016-01-30 10:57:15 +01:00
Alessandro Di Federico 3746bcbd78 Move harvesting of code pointers in JTM
The logic to implement harvesting of new code pointers when we're out of
them during translation, has been moved to `JumpTargetManager`. Its
interface has also been reduced and some logging has been introduced.

At the current stage, if there's nothing to `peek`, we first give a shot
of `SROA` and `TranslateDirectBranchesPass`, and then, if nothing came
out, we go for `EarlyCSE` and `JumpTargetsFromConstantsPass`.
2016-01-12 23:28:23 +01:00
Alessandro Di Federico 8c381090a7 Move TranslateDirectBranchesPass to JTM 2016-01-12 23:05:29 +01:00
Alessandro Di Federico 0d58bc3cf7 Introduce logging framework 2016-01-12 22:43:04 +01:00
Alessandro Di Federico e5c037e6f6 Introduce global data harvesting for code pointers
The `JumpTargetManager` constructor now collects everything looks like a
code pointer in read-only and writeable data.
2016-01-12 19:44:01 +01:00
Alessandro Di Federico ef98afc1e3 Insert an unreachable after exit_tb
Introduce an unreachable instruction after each emitted call to
`exit_tb` to terminate properly basic blocks. This patch also removes it
when appropropriate (i.e. in `TranslateDirectBranchesPass` and
`JumpTargetManager::translateIndirectJumps`).
2016-01-12 19:41:37 +01:00
Alessandro Di Federico f8dcd566ae Collect information about ELF segments
Instead of taking note of the executable ranges exclusively, keep track
of all the segments in `CodeGenerator`. `JumpTargetManager` instead will
keep track of executable areas only.

* Introduce the `SegmentInfo` struct, which simply holds essential
  information about the segment such as start and end address,
  permissions and a reference to the global variable holding its content.
* Update `CodeGenerator` to keep a vector of `SegmentInfo`.
* `JumpTargetManager`: polish the constructor and make it take the vector
  of `SegmentInfo`, from which the executable ranges are then extracted.
2016-01-12 18:47:15 +01:00
Alessandro Di Federico 3420b0f85f Filter instructions while inspecting immediates
In `JumpTargetsFromConstantsPass` ignore calls to `newpc` and stores to
the PC, since they are harmful or simply don't carry any useful
information.
2016-01-12 18:47:15 +01:00