Commit Graph

39 Commits

Author SHA1 Message Date
Alessandro Di Federico 0dc77e104f Introduce statistics
This class introduces the `RunningStatistics` class, which allows to
compute the mean and standard deviation of a set of numbers. These
values are computed incrementally and can be associated to a name. The
values computed by `RunningStatistics` can be dumped upon regular
program termination, `SIGABRT` and `SIGINT`. In practice they are
printed at the end of the program execution, even in case of asserts and
`Ctrl + C`. Moreover, `SIGUSR1` is used to trigger printing the
statistics without crashing the program.
2018-05-30 12:45:45 +02:00
Alessandro Di Federico e407fc11a1 Various minor cleanups 2018-05-30 12:45:45 +02:00
Alessandro Di Federico 61cfbdfc56 Introduce support for dynamic binaries
This commit introduces support for dynamic programs. The current
implementation translate the main binary and uses native libraries. This
works only if the target architecture is the same as the source
one. Currently we only handle x86-64.

* The `ExternalJumpsHandler` class has been introduced. It basically
  takes care of extending the dispatcher handling the case in which the
  program counter is an address outside the range of executable
  addresses of the input program. In this case, a `setjmp` is perfomed,
  the CPU state is serialized to physical registers and jump to the
  value of the program counter is performed.

  Once the target code will try to return to the translated program, a
  segmentation fault will be triggered, a `longjmp` is performed and the
  CPU state is deserialized so that the execution can resume (from the
  dispatcher).

* `early-linked.c` has been introduced. Its purposes is to provide
  declarations of variables and functions defined in `support.c`. In the
  past, we had to manually create these definitions, a cumbersome and
  error prone we now avoid by letting `clang` compile `early-linked.c`
  and then linking it in.

* The old `support.h` is now known as `commonconstants.h`. `support.h`
  now contains declarations that have to be consumed by
  `early-linked.c`.

* Each architecture now provides additional information:

  1. Which registers are part of the ABI and have to be preserved. If
     necessary the QEMU name can be provided. For each register it's
     also possible to provide their position within the `mcontext_t`
     structure, provided by the signal handler.
  2. Three assembly snippets, one to write a register, one to read it
     and one perform an indirect jump.

  Some of this information is also exposed in the output module as
  metadata.

* `support.c` now installs a SIGSEGV signal handler. Since pages that
  were originally executable are no longer executable, jumping there
  (typically, from a library) will trigger a SIGSEGV that we will
  handle. This allows us to properly deserialize the CPU state and
  resume execution of the translate code.

* Now also a dynamic version of each test program is translated and
  tested.

* The `merge-dynamic.py` script has been introduced: it takes case of
  rewriting the translated binary so to tell the linker to performe both
  the relocations of the translate program and the relocations of the
  original program. It does so by rewriting a large portion of the
  sections employed by the dynamic linker such as `.dynamic`, `.dynsym`
  and so on.

* The `compile-time-constants.py` script has been introduced: it a
  user-specified compiler on a source file producing an object
  file. This object file is inspected and the value of global read-only
  variables is produced in a CSV.
2018-05-29 15:10:51 +02:00
Alessandro Di Federico 470ca264ad Convert bool arguments to int
The `argparse` library treats boolean arguments as
integers. Specifically, each time a boolean argument is meet the
associated variable is incremented. This led to weird behaviors having
`2` being converted to `false`. Using `int` as a type solves this issue.
2018-05-29 08:35:24 +02:00
Pietro Fezzardi 5d9aa31c50 Remove useless type GenericFunctor
`GenericFunctor` is substituted with the `std::integral_constant`
template. This also allows us to remove code that requires C++14.

It also removes the now useless cmake tests on the compiler flag
`-Wno-error=noexcept-type` that was introduced to disable fatal warnings
on the type `GenericFunctor`.
Now that this type has been removed the check is not necessary anymore,
because the `std::integral_constant` template used now does not cause
the warning.
So we can go back to enabling the fatal warnings.
2018-01-18 18:21:33 +01:00
Alessandro Di Federico e031583308 Set CSVs linkage to internal
This simple commit should improve performance of the generated program
sensibly. Basically all the global variables will have internal linkage
from now on (unless the `--external` parameter is specified on the
command line). This way, the compiler will be able to avoid load/store
instructions when leaving code in the current translation unit.
2017-04-03 15:57:50 +02:00
Alessandro Di Federico dae2f7e696 Compile support.c to LLVM IR
`support.c` used to be compiled using the system compiler and then
linked to the module generated by `revamb` as a separate translation
unit. This commit introduces a change that lets `clang` compile
`support.c`. This will allow us to make the CSV static, which should
enable more aggressive optimizations.

* Change the signature of the `root` function so that it accepts an
  argument: the initial value of the stack pointer, which the main is
  supposed to set up. QEMU now provides us with the offset of the stack
  pointer.
* Let the build system compile `support.c` for each supported
  architecture, both in normal and "tracing" mode.
* Remove the `--tracing` option, this is now handled by `support.c`, in
  particular depending on which version of `support.c` you link, you can
  have tracing enabled or not.
* In `support.c` drop global variables representing the stack pointer,
  we no longer need them.
* In `support.c` fix some warnings while handling the stack on 32-bit
  architectures.
* Extende the `translate` script to handle the new way we link the final
  binary and the tracing mechanism.
2017-03-02 08:21:11 +01:00
Alessandro Di Federico 1a3950a3ea Force root function's name 2017-02-20 12:13:11 +01:00
Alessandro Di Federico 4501dad2c0 Introduce the --no-link option
Introduce an option to prevent `revamb` from linking in all the QEMU
helpers. This is useful if the output doesn't need to be compiled, but
just analyzed.
2017-01-11 15:25:53 +01:00
Alessandro Di Federico 3e77bb443f s/function boundaries/functions boundaries/ 2016-12-04 00:28:57 +01:00
Alessandro Di Federico 5ca3200114 Update command line description of revamb 2016-12-03 08:46:12 +01:00
Alessandro Di Federico bc6a732ad7 Don't compute function boundaries by default 2016-12-03 08:46:12 +01:00
Alessandro Di Federico 83ea2caacd Isolate ELF code and remove architecture parameter
This commit removes all the ELF-specific code from the `CodeGenerator`
class by creating a new class, `BinaryFile` which contains all the
information about the program that might be needed in an image format
independent way. However, `BinaryFile` has some fields which are
specific to ELF, we might want to address this when additional file
formats are supported.

A key benefit of isolating this code is that we can anticipate the
parsing of the input file, so that we have its architecture available
earlier than when `CodeGenerator` is instantiated, therefore we can drop
the `--architecture` parameter.
2016-12-03 08:46:12 +01:00
Alessandro Di Federico 59c871afc5 Make revamb portable
Add different search paths for QEMU components, in paritcular relative
to the program's path.
Also, install the revamb.
2016-09-22 18:45:59 +02:00
Alessandro Di Federico d01ee1f437 Copyright notices, license and credits 2016-09-21 01:45:26 +02:00
Alessandro Di Federico 6c5c0ad8f7 Add support for using section information 2016-09-17 15:33:54 +02:00
Alessandro Di Federico 1a5fc0f519 Introduce collection of basic block statistics
Let revamb produce a CSV file containing statistics about the translated
input basic blocks for further analysis (e.g., identify false
positives).
2016-08-20 03:10:46 +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 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 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 0d58bc3cf7 Introduce logging framework 2016-01-12 22:43:04 +01:00
Alessandro Di Federico d510fea8c7 Simplify command line usage exploiting ELF info
* s/`importGlobalData`/`parseELF`/
* Save the entry point specified in the ELF header, which will be used
  if the user doesn't provide an address.
* Let parse `parseELF` take care of informing libtinycode about what
  has to be mmap'd and where.
* Remove some support scripts used during testing, now no longer
  necessary.
* Various cleanups
2016-01-09 13:37:03 +01:00
Alessandro Di Federico ac316cd758 Add support for ELF and import its global data
* Use `llvm::object` framework to obtain useful information from the ELF
  binary such as pointer size and endianess.
* Introduce `CodeGenerator::importGlobalData`: import global (read-only
  and writeable data) from the input binary directly into the generated
  module.
* Introduce the `--linking-info` parameter: path to a CSV file where
  sections containing global data extracted from the input binary are
  listed with their name, start and end address.
* Expand the `Architecture` class with constructors and support accessor
  methods.
2016-01-07 14:16:05 +01:00
Alessandro Di Federico 0f2ddd80a0 Remove -o parameter: use the second argument 2016-01-05 15:35:44 +01:00
Alessandro Di Federico 5d130b7072 Split ptctollvmir.cpp into multiple files 2015-11-24 15:21:17 +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 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 bd898de98f Fix propagation of OutputPath and DebugPath 2015-11-07 15:01:34 +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 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 089bfd2461 Introduce support for LLVM IR debugging information 2015-10-27 08:54:05 +01:00
Alessandro Di Federico 3b0588b75d Add support for PTC debug info 2015-10-27 08:54:04 +01:00
Alessandro Di Federico 8f88625e94 Add metadata with original and PTC instructions to each new instruction
* Convert ptcdump.cpp to use streams and export more fine-grained functions
* Add documentation to ptcdump.h
* Create metadata for the original instruction when a
  PTC_INSTRUCTION_op_debug_insn_start instruction is met.
2015-10-17 22:44:42 +02:00
Alessandro Di Federico 5ee7b1dd8a Initial import 2015-09-26 15:17:32 +02:00