Commit Graph

6113 Commits

Author SHA1 Message Date
Alessandro Di Federico 13beb4490d Whitespace changes and doc additions 2016-01-04 21:47:55 +01:00
Alessandro Di Federico 8ca9156be3 Add a simple test for the getuid syscall 2016-01-04 21:47:34 +01:00
Alessandro Di Federico 3c0fce0ba0 Transform calls to cpu_loop_exit to cpu_loop
`cpu_loop_exit` is used by QEMU to get back control from the translated
code while running an helper. Here we complete the inversion of the
hierarchy by transforming calls to `cpu_loop_exit` into calls to our
customized `cpu_loop`, which will handle syscalls and the like.

Since originally `cpu_loop_exit` was a noreturn function, we also need to
ensure that the semantic of its usage is preserved. We do this by setting
a global variable (`cpu_loop_exiting`) right after the call to
`cpu_loop_exit` and forcing the whole call stack to return immediately
if it's true.

* Introduce `CpuLoopExitPass`: replace all the calls to
  `cpu_loop_exit` with a call to `cpu_loop`, a store true to
  `cpu_loop_exiting` and a return. Then take all the callers and make
  them return if `cpu_loop_exiting` is set. Once we get to the translated
  function just reset `cpu_loop_exiting` to false.
* Introduce `VariableManager::computeEnvAddress`: generate code to
  compute the offset of env . This computation is useful when
  reference to the CPU state must be arificially introduced. This code
  will be flattened by `CorrectCPUStateUsagePass` later.
2016-01-04 21:47:26 +01:00
Alessandro Di Federico dc23749e0e Transform cpu_loop according to our requirements
`cpu_loop` is the main program loop used by QEMU during emulation. Here
we are interested in transforming it in a simple handler of exceptions
(e.g. signals and syscalls).

* Introduce `CpuLoopFunctionPass`: remove the outermost backedge in
  `cpu_loop` and replace the call to cpu_*_exec with the exception index.
* Implement the support function `find_unique` which returns the only
  element in a range satisfying a predicate, or, otherwise, asserts.
2016-01-04 21:46:14 +01:00
Alessandro Di Federico 810e477e2d Improve linking for libtinycode's helpers support
* Expand the function to replace with no-op or abort
* Factor out the code to replace function bodies (replaceFunction and
  replaceFunctionWithRet)
* Replace functions before linking, directly in the helper module
2016-01-04 21:33:45 +01:00
Alessandro Di Federico 8e3268c9c0 Handle post-helper indirect jumps
In certain cases we have a call to `exitTB` right after an helper, in
particular in x86, after a syscall. We cannot know what the target
address will be, so we have to handle this as an indirect jump.

* `JumpTargetManager::getPrevPCWrite`: clean up.
* `JumpTargetManager::getPrevPCWrite`: while searching for stores to the
  PC, also check for call instructions. If one is met, return nullptr.
* `TranslateDirectBranchesPass::runOnFunction` and
  `JumpTargetManager::translateIndirectJumps`: insert new code before
  `exitTB`, not the write to the PC.
2016-01-04 21:29:03 +01:00
Alessandro Di Federico 581be847ce Abort on unsupported accesses to the CPU state
Currently we do not support access to pointer data types in the CPU state
structure. Now, since their usage is marginal, instead of failing at
compile-time we put an abort instruction when the code reaches that
point.

The same is done in case an access to an array member using a
non-constant index is performed.
2016-01-04 21:26:43 +01:00
Alessandro Di Federico 046947da14 CorrectCPUStateUsagePass: add support to IntToPtr 2016-01-04 21:24:31 +01:00
Alessandro Di Federico 46cb955bfa Replace existing globals if a new name is given 2016-01-04 21:23:36 +01:00
Alessandro Di Federico 1ca2e57f00 Allow unsupported but dead usages of the CPU state 2016-01-04 21:22:40 +01:00
Alessandro Di Federico d684ea7ab1 Sync with the updated libtinycode interface 2016-01-04 21:20:38 +01:00
Alessandro Di Federico c9221fe8a0 Handle termination of a basic block after a call 2016-01-04 21:20:18 +01:00
Alessandro Di Federico f36bc6b42f Introduce tests for floaing point operations 2015-12-04 23:45:50 +01:00
Alessandro Di Federico 6338f3b0b8 Link with helpers and adjust their CPU state usage
* Move initialization and management of the structure describing the CPU
  state (CPUStateType) into variablemanager.cpp.
* Support parts of CPU state outside "env" (e.g. the MIPSCPU
  structure). Now "env" has an offset into the possibly larger CPU state
  which we have to take into account where appropriate (see
  VariableManager::envOffset).
* Link the helpers module into the generated module, including only what
  is needed.
* Create some "no-op" or "abort" function corresponding to QEMU functions
  not included in the helper module (e.g. logging and abort functions).
* Implement the CorrectCPUStateUsagePass pass, which starts from the
  "env" global variable and looks for all its usages recursively, keeping
  track of where pointers are pointing into the CPU state data structure,
  and replaces all the load/stores with the global variable corresponding
  to that specific field of the CPU state.
* After the linking phase, run SROA, the pass to adjust the CPU usage and
  DCE.
* Let global variables have common linkage.
2015-12-04 23:44:46 +01:00
Alessandro Di Federico 9fd83ea336 Implement the exit_tb PTC instruction
* Emit a call to an helper function (ExitTB) corresponding to each
  exit_tb PTC instruction.
* Update TranslateDirectBranchesPass and
  JumpTargetManager::translateIndirectJumps to look for the last write
  to the PC before calls to ExitTB.
* Exposing the PC with JumpTargetManager::PC is not needed anymore. Users
  from outside should only be interested in finding the the previous
  write to the PC (using JumpTargetManager::getPrevPCWrite).
2015-12-03 18:02:52 +01:00
Alessandro Di Federico a8075fcf4f Remove code to ignore first few PTC instructions
libtinycode no more emits initialization and finalization code that we
don't need.
2015-12-03 18:01:02 +01:00
Alessandro Di Federico 4416ef84d1 getTypeAtOffset: handle arrays of structs 2015-12-03 18:00:30 +01:00
Alessandro Di Federico 4e326f5799 Factorize LLVM-related helper functions 2015-12-03 17:59:32 +01:00
Alessandro Di Federico 9bf830c8ed Add a terminator before splitting a basic block 2015-12-03 17:57:23 +01:00
Alessandro Di Federico 1bddfbca1a PTC dump now properly prints constant args in hex 2015-12-03 17:56:14 +01:00
Alessandro Di Federico 5d130b7072 Split ptctollvmir.cpp into multiple files 2015-11-24 15:21:17 +01:00
Alessandro Di Federico 38b79e944b Add support to load from CPU state 2015-11-23 20:14:16 +01:00
Alessandro Di Federico 3f25b17091 Handle explicitly the CPU state pointer 2015-11-23 20:14:16 +01:00
Alessandro Di Federico 15cc08304c Zero extend before storing into CPU state 2015-11-23 20:14:16 +01:00
Alessandro Di Federico a091b1b675 Don't remove nodes while iterating over them 2015-11-23 20:14:16 +01:00
Alessandro Di Federico 3e8bed2ce8 Decouple PTC::* from InstructionTranslator 2015-11-23 20:14:09 +01:00
Alessandro Di Federico 37615bb6e6 Fix UAF in DebugHelper (OutputPath) 2015-11-23 20:04:55 +01:00
Alessandro Di Federico 1d4e3169eb Add support for indirect jumps and function calls
The main aim of these changes is to support `ret` instructions (that are
indirect jumps), so that function calls can now work properly.

* Emit a dummy call to a `@newPC` function to record where a new
  instruction in the original assembly started, what is its PC and its
  size. The `Size` parameter for these function call is initially zero,
  and it's updated later when a new instruction is met or the current
  basic block is terminated. All these calls, before the final emission
  of the IR are removed `InstructionTranslator::removeNewPCMarkers`.
* Transform translateMoveToPC into an LLVM pass
  (`TranslateDirectBranchesPass`).
* Implement `getNextPC`, which returns the next program counter by
  looking for a call to `@newpc` in the current basic block, and,
  recursively, in all the basic blocks dominating the current one. This
  function is used to force exploration of the basic block coming after
  a direct jump, which is paritcularly useful in case of a function call.
* Introduce `translateIndirectJumps`, which translates all the leftover
  writes to the PC, i.e. all the indirect ones, by diverting execution
  to a large switch statement mapping addresses in the original program
  to the corresponding basic blocks in the translated program.
* Introduce `function_call`, a simple test for function calls.
* Other minor changes and whitespace fixes.
2015-11-15 12:06:17 +01:00
Alessandro Di Federico e2efe161e0 Add support for MIPS
First time all tests pass!
2015-11-10 09:22:58 +01:00
Alessandro Di Federico 94864e49b5 Introduce support for virtual addresses
* mmap on the PTC side before doing any translation
* Update usages of virtual addresses
* Refactor options to have --load-at and --entry
* Update tests according to the changes
2015-11-10 00:05:21 +01:00
Alessandro Di Federico 91a01937cf Let support.c know about the target architecture 2015-11-07 15:02:12 +01:00
Alessandro Di Federico 1d56c32667 Make writes to PC explicit
* Introduce PassManager running SROA
* Import the ScalarOpts LLVM module
2015-11-07 15:02:11 +01:00
Alessandro Di Federico c232c01238 Improve dump of PTC helper call instructions 2015-11-07 15:02:11 +01:00
Alessandro Di Federico ba950d26f2 Split CodeGenerator::translate and use CPUState from the QEMU helpers
* Implement an handler for PTC_INSTRUCTION_op_debug_insn_start
* Implement an handler for calls to helpers
* Implement an handler for all the remaining instructions
* Discover automatically path of the helpers module
* Import the IRReader module
* Remove support for predefined global variables
* Implement getByCPUStateOffset which returns or creates a global
  variable from an offset in the CPUState structure
* Remove the VariableManager::createGlobal function
* Implement getTypeAtOffset which searches for the data type at the
  specified offset, recursively exploring sub-structs
* Autodetect the CPUState structure by election on the struct parameters
  of the helper functions
* CodeGenerator::translate returns void
* Multiplication should sign-extend, not zero-extend
* Fix wrong update of alloca insertion point
* Implement PTC_INSTRUCTION_op_mul{u,s}2_i{32,64} instructions
2015-11-07 15:02:11 +01:00
Alessandro Di Federico 711b798be6 Refactoring
* Fix whitespaces
* Fix case of method names
2015-11-07 15:02:11 +01:00
Alessandro Di Federico ee850e39bc Introduce C++ wrapper of the PTC library
* s/PTC/PTCInterface/
* Implement InstructionArgumentsIterator
* Implement Instrcution and CallInstruction
2015-11-07 15:02:11 +01:00
Alessandro Di Federico 4a151eaa82 Fix handling of debug information
* Don't output a source line if it's identical to the last printed one
* Invoke DIBuilder::finalize as appropriate
* Fix off-by-one in line number computation
2015-11-07 15:02:05 +01:00
Alessandro Di Federico f6ae68516d Use llc from the given LLVM installation 2015-11-07 15:01:34 +01:00
Alessandro Di Federico bd898de98f Fix propagation of OutputPath and DebugPath 2015-11-07 15:01:34 +01:00
Alessandro Di Federico 282fa2b40b Import functional-style iterators 2015-11-07 15:01:34 +01:00
Alessandro Di Federico 9795191f11 Add support for 64-bit local variables 2015-11-07 15:01:16 +01:00
Alessandro Di Federico fd1070b708 Disable RTTI 2015-11-07 14:24:20 +01:00
Alessandro Di Federico c5d510f4d6 Introduce using namespace std in ptctollvmir.cpp 2015-10-27 17:44:25 +01:00
Alessandro Di Federico 9007943839 Refactor the Translate function
* Create the CodeGenerator class from most of the logic that was in the
  Translate function.
* Extract debug information handling logic from the Translate function and move
  it into the DebugManager class.
* Add include guards.
* Remove some dead code and add and fix documentation.
2015-10-27 17:19:52 +01:00
Alessandro Di Federico 15cfbee422 Make reference to QEMU install path and dlopen an absolute path 2015-10-27 11:56:08 +01:00
Alessandro Di Federico 9cf82f36d8 Introduce testing framework
* Import single-function calculator
* Create a puppet CMake project for cross-compiling
* Create a CMake manager for the tests
2015-10-27 09:13:14 +01:00
Alessandro Di Federico 2b5823c6bc Dump useful information in case an unsupported helper is called 2015-10-27 08:54:05 +01:00
Alessandro Di Federico a4fdf0f1b5 Improve debug info handling
* Add support to specify a path for debug source
* Annotate generated LLVM with original assembly and PTC
* As debug source, use the same .ll as the compiled one
2015-10-27 08:54:05 +01:00
Alessandro Di Federico cb3ef7b263 Make global names case-insensitive 2015-10-27 08:54:05 +01:00
Alessandro Di Federico 7f9b86787c Minor refactoring of CMakeLists.txt 2015-10-27 08:54:05 +01:00