mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:42:43 +00:00 
			
		
		
		
	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! :^)
			
			
This commit is contained in:
		
							parent
							
								
									f2d3cc7325
								
							
						
					
					
						commit
						be5f42adea
					
				
					 14 changed files with 1088 additions and 752 deletions
				
			
		|  | @ -28,6 +28,7 @@ | |||
| #include "Emulator.h" | ||||
| #include "MmapRegion.h" | ||||
| #include <AK/LogStream.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| //#define REACHABLE_DEBUG
 | ||||
| 
 | ||||
|  | @ -41,6 +42,13 @@ MallocTracer::MallocTracer() | |||
| 
 | ||||
| void MallocTracer::target_did_malloc(Badge<SoftCPU>, FlatPtr address, size_t size) | ||||
| { | ||||
|     auto* region = Emulator::the().mmu().find_region({ 0x20, address }); | ||||
|     ASSERT(region); | ||||
|     ASSERT(region->is_mmap()); | ||||
|     auto& mmap_region = static_cast<MmapRegion&>(*region); | ||||
|     auto* shadow_bits = mmap_region.shadow_data() + address - mmap_region.base(); | ||||
|     memset(shadow_bits, 0, size); | ||||
| 
 | ||||
|     if (auto* existing_mallocation = find_mallocation(address)) { | ||||
|         ASSERT(existing_mallocation->freed); | ||||
|         existing_mallocation->size = size; | ||||
|  | @ -151,7 +159,7 @@ bool MallocTracer::is_reachable(const Mallocation& mallocation) const | |||
|         size_t pointers_in_mallocation = other_mallocation.size / sizeof(u32); | ||||
|         for (size_t i = 0; i < pointers_in_mallocation; ++i) { | ||||
|             auto value = Emulator::the().mmu().read32({ 0x20, other_mallocation.address + i * sizeof(u32) }); | ||||
|             if (value == mallocation.address) { | ||||
|             if (value.value() == mallocation.address && !value.is_uninitialized()) { | ||||
| #ifdef REACHABLE_DEBUG | ||||
|                 dbgprintf("mallocation %p is reachable from other mallocation %p\n", mallocation.address, other_mallocation.address); | ||||
| #endif | ||||
|  | @ -176,7 +184,7 @@ bool MallocTracer::is_reachable(const Mallocation& mallocation) const | |||
|         size_t pointers_in_region = region.size() / sizeof(u32); | ||||
|         for (size_t i = 0; i < pointers_in_region; ++i) { | ||||
|             auto value = region.read32(i * sizeof(u32)); | ||||
|             if (value == mallocation.address) { | ||||
|             if (value.value() == mallocation.address && !value.is_uninitialized()) { | ||||
| #ifdef REACHABLE_DEBUG | ||||
|                 dbgprintf("mallocation %p is reachable from region %p-%p\n", mallocation.address, region.base(), region.end() - 1); | ||||
| #endif | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling