Commit graph

13 commits

Author SHA1 Message Date
Nico Weber
f1c0f661f4
UserspaceEmulator+LibX86: Add support for 64-bit memory reads and writes (#3584)
This is useful for reading and writing doubles for #3329.
It is also useful for emulating 64-bit binaries.

MemoryOrRegisterReference assumes that 64-bit values are always
memory references since that's enough for fpu support. If we
ever want to emulate 64-bit binaries, that part will need minor
updating.
2020-09-23 20:45:43 +02:00
Andreas Kling
be5f42adea UserspaceEmulator+LibX86: Start tracking uninitialized memory :^)
This patch introduces the concept of shadow bits. For every byte of
memory there is a corresponding shadow byte that contains metadata
about that memory.

Initially, the only metadata is whether the byte has been initialized
or not. That's represented by the least significant shadow bit.

Shadow bits travel together with regular values throughout the entire
CPU and MMU emulation. There are two main helper classes to facilitate
this: ValueWithShadow and ValueAndShadowReference.

ValueWithShadow<T> is basically a struct { T value; T shadow; } whereas
ValueAndShadowReference<T> is struct { T& value; T& shadow; }.

The latter is used as a wrapper around general-purpose registers, since
they can't use the plain ValueWithShadow memory as we need to be able
to address individual 8-bit and 16-bit subregisters (EAX, AX, AL, AH.)

Whenever a computation is made using uninitialized inputs, the result
is tainted and becomes uninitialized as well. This allows us to track
this state as it propagates throughout memory and registers.

This patch doesn't yet keep track of tainted flags, that will be an
important upcoming improvement to this.

I'm sure I've messed up some things here and there, but it seems to
basically work, so we have a place to start! :^)
2020-07-21 02:37:29 +02:00
Andreas Kling
e50874621a UserspaceEmulator: Don't scan text segment for malloc leaks
There will be no (true positive) malloc addresses in the text segment.
2020-07-16 19:27:03 +02:00
Andreas Kling
c13da77e85 UserspaceEmulator: Add TLS regions to reachability checking 2020-07-16 19:21:45 +02:00
Andreas Kling
f6584bfc36 UserspaceEmulator: Implement very basic leak checking :^)
Upon exit, the emulator will now print a leak report of any malloc
allocations that are still live and don't have pointers to their base
address anywhere in either another live mallocation, or in one of the
non-malloc-block memory regions.

Note that the malloc-block memory region check is not fully functional
and this will work even better once we get that fixed.

This is pretty cool. :^)
2020-07-16 19:21:45 +02:00
Andreas Kling
7e13244238 UserspaceEmulator: Add ways to check if a Region is stack/mmap 2020-07-16 19:21:45 +02:00
Andreas Kling
2da44dba44 UserspaceEmulator: Add support for shared buffers (shbuf)
We track these separately from regular mmap() regions, as they have
slightly different behaviors.
2020-07-15 18:47:45 +02:00
Andreas Kling
8656835935 UserspaceEmulator: Add a very simple instruction fetch cache
To avoid MMU region lookup on every single instruction fetch, we now
cache a raw pointer to the current instruction. This gets automatically
invalidated when we jump somewhere, but as long as we're executing
sequentially, instruction fetches will hit the cache and bypass all
the region lookup stuff.

This is about a ~2x speedup. :^)
2020-07-13 20:14:14 +02:00
Andreas Kling
e0580e2975 UserspaceEmulator: Add some more syscalls
We can now unmap mapped memory, among other things. This is all very
ad-hoc as I'm trying to run UserspaceEmulator inside itself. :^)
2020-07-13 13:50:22 +02:00
Andreas Kling
94f07660e9 UserspaceEmulator: Add some convenient SoftMMU APIs for copying data
We'll soon want to copy data in and out of the SoftMMU memory space.
2020-07-12 17:42:57 +02:00
Andreas Kling
734f63d522 UserspaceEmulator: Add basic TLS (thread-local storage) support
The SoftMMU now receives full X86::LogicalAddress values from SoftCPU.
This allows the MMU to reroute TLS accesses to a special memory region.

The ELF executable's PT_TLS header tells us how to allocate the TLS.

Basically, the GS register points to a magical 4-byte area which has
a pointer to the TCB (thread control block). The TCB lives in normal
flat memory space and is accessed through the DS register.
2020-07-12 01:36:45 +02:00
Andreas Kling
6f27770cea UserspaceEmulator: Add 8/16 bit memory read/write operations 2020-07-10 20:20:27 +02:00
Andreas Kling
d5c46cf528 UserspaceEmulator: Start sketching out a SoftMMU class :^)
This Emulator sub-object will keep track of all active memory regions
and handle memory read/write operations from the CPU.

A memory region is currently represented by a virtual Region object
that can implement arbitrary behavior by overriding read/write ops.
2020-07-09 16:18:47 +02:00