Commit graph

33 commits

Author SHA1 Message Date
Andreas Kling
1873b8f3e4 UserspaceEmulator: Fix bogus use of "errno" in shbuf related syscalls
When we're making direct syscalls, there's no "errno" involved.
Thanks to Sergey for spotting these.
2020-07-15 23:43:38 +02:00
Andreas Kling
c314292319 UserspaceEmulator: Catch use-after-frees by tracking malloc/free :^)
This patch introduces a "MallocTracer" to the UserspaceEmulator.
If this object is present on the Emulator, it can be notified whenever
the emulated program does a malloc() or free().

The notifications come in via a magic instruction sequence that we
embed in the LibC malloc() and free() functions. The sequence is:

    "salc x2, push reg32 x2, pop reg32 x3"

The data about the malloc/free operation is in the three pushes.
We make sure the sequence is harmless when running natively.

Memory accesses on MmapRegion are then audited to see if they fall
inside a known-to-be-freed malloc chunk. If so, we complain loud
and red in the debugger output. :^)

This is very, very cool! :^)

It's also a whole lot slower than before, since now we're auditing
memory accesses against a new set of metadata. This will need to be
optimized (and running in this mode should be opt-in, perhaps even
a separate program, etc.)
2020-07-15 23:25:20 +02:00
Andreas Kling
c8b496162d UserspaceEmulator: Add some more syscalls :^)
Here's set_process_icon(), gettimeofday() and clock_gettime().
2020-07-15 18:47: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
339f12e8a2 UserspaceEmulator: Implement an assortment of system calls
Here goes mkdir(), unlink(), socket(), getsockopt(), fchmod()
bind(), connect(), listen(), select() and recvfrom().

They're not perfect but they seem to work. :^)
2020-07-15 18:47:45 +02:00
Sergey Bugaev
e12b591509 UserspaceEmulator: Implement virt$pipe() 2020-07-15 13:41:46 +02:00
Andreas Kling
a27473cbc2 UserspaceEmulator+LibX86: Turn on -O3 optimization for emulation code
Since this code is performance-sensitive, let's have the compiler do
whatever it can to help us with the most important files.

This yields a ~8% speedup.
2020-07-13 20:23:00 +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
9b6464010f UserspaceEmulator: Add basic support for memory-mapped files
MmapRegion now supports using an mmap'ed file descriptor as backing.
2020-07-13 13:50:22 +02:00
Andreas Kling
f6ad5edab0 UserspaceEmulator: Make mmap'ed memory track read/write protection
Here's the first time we get a taste of better information than the
real hardware can give us: unlike x86 CPUs, we can actually support
write-only memory, so now we do!

While this isn't immediately useful, it's still pretty cool. :^)
2020-07-13 13:50:22 +02:00
Andreas Kling
27c1690504 UserspaceEmulator: Pass arguments through to emulated process
Ultimately we'll want to support passing some options to the emulator
as well, but for now just pass all arguments (except argv[0] of course)
through to the emulated process.

This is still not perfect, but slightly better than what we had before.
2020-07-13 13:50:22 +02:00
Andreas Kling
3d42b85969 UserspaceEmulator: Move SimpleRegion to its own files 2020-07-13 13:50:22 +02:00
Andreas Kling
4d3787ae33 UserspaceEmulator: Support the fstat() and get_process_name() syscalls
For now, we just pretend that the process name is "EMULATED". We can
probably do better though. :^)
2020-07-13 13:50:22 +02:00
Andreas Kling
617655db0d UserspaceEmulator: Move exit() syscall logging to debug output
We want the emulated program to appear without noise in the terminal.
2020-07-12 21:37:54 +02:00
Andreas Kling
1d32c66dde UserspaceEmulator: Put some syscall logging behind DEBUG_SPAM 2020-07-12 21:37:54 +02:00
Andreas Kling
95a42efc62 UserspaceEmulator: Implement enough syscalls to get /bin/id running :^) 2020-07-12 21:37:54 +02:00
Andreas Kling
1b196df4c4 UserspaceEmulator: Implement/stub out various syscalls
Moving forward on getting /bin/id to run inside the emulator. :^)
2020-07-12 21:37:54 +02:00
Andreas Kling
56d3a949e6 UserspaceEmulator: Disable per-instruction trace dumps for now
With tracing turned on, it's just too slow when doing big operations
like initializing malloc freelists.
2020-07-12 21:37:54 +02:00
Andreas Kling
079021a607 UserspaceEmulator: Put the executable name in argv[0] :^)
The emulated program can now find its own name in argv[0]. Very cool!
2020-07-12 21:37:54 +02:00
Andreas Kling
ddf7b817df UserspaceEmulator: Add Emulator::dump_backtrace()
This gives you a nice, symbolicated backtrace at the current EIP. :^)
2020-07-12 21:37:54 +02:00
Andreas Kling
584923445c UserspaceEmulator: "Add" a couple of syscalls
This patch adds gettid() and stubs out pledge() and unveil() for now.
2020-07-12 21:37:54 +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
ce51cf90c6 UserspaceEmulator: Make sure ELF data segments are zero-initialized
(And all other memory, too.) This will mutate later when we add shadow
memory etc, but for now just zero-initialize it since that's expected
by the emulated program.
2020-07-12 01:36:45 +02:00
Andreas Kling
463afa69a7 UserspaceEmulator: Improve the initial program stack a tiny bit
Instead of starting with argv=nullptr, envp=nullptr, programs now
start with both pointing to a null terminated array (that immediately
terminates.) :^)
2020-07-11 23:57:14 +02:00
Andreas Kling
0f63d8c9b4 UserspaceEmulator: Symbolicate disassembly output :^)
Since we have the ELF executable handy, we can actually symbolicate the
disassembly trace output really easily. Very cool! :^)
2020-07-11 17:18:07 +02:00
Andreas Kling
dc66d70369 UserspaceEmulator: Don't exit the emulation loop on "RET" :^) 2020-07-11 17:13:15 +02:00
Andreas Kling
c4ec38ddb5 UserspaceEmulator: Print out the current EIP as we execute instructions 2020-07-11 16:48:27 +02:00
Andreas Kling
ae1d14bc7a UserspaceEmulator: Load the target executable ELF semi-properly :^)
This patch adds a basic ELF program loader to the UserspaceEmulator and
creates MMU regions for each PT_LOAD header. (Note that we don't yet
respect the R/W/X flags etc.)

We also turn the SoftCPU into an X86::InstructionStream and give it an
EIP register so we can actually execute code by fetching memory through
our MMU abstraction.
2020-07-11 16:45:48 +02:00
Andreas Kling
0eab5659f8 UserspaceEmulator: Set up a very basic program entry stack 2020-07-11 16:30:17 +02:00
Andreas Kling
6f27770cea UserspaceEmulator: Add 8/16 bit memory read/write operations 2020-07-10 20:20:27 +02:00
Andreas Kling
d10765bec3 UserspaceEmulator: Add an initial stack and implement PUSH/POP reg32
Programs now start out with a 64 KB stack at 0x10000000. :^)
2020-07-09 16:20:08 +02:00
Andreas Kling
d0dbf92c8d UserspaceEmulator: Break out of emulation when hitting a RET
Until we learn more instructions, we'll have to exit somewhere, so let
us exit when we hit a RET instruction for now.
2020-07-07 22:44:58 +02:00
Andreas Kling
8d8bb07476 UserspaceEmulator: Start building a userspace X86 emulator :^)
This introduces a new X86 CPU emulator for running SerenityOS userspace
programs in a virtualized interpreter environment.

The main goal is to be able to instrument memory accesses and catch
interesting bugs that are very hard to find otherwise. But before we
can do fancy things like that, we have to build a competent emulator
able to actually run programs.

This initial version is able to run a very small program that makes
some tiny syscalls, but nothing more.
2020-07-07 22:44:58 +02:00