Module: wine Branch: master Commit: bcc717174c54017e0cb5dfdb8c0a8e4f521ea42a URL: http://source.winehq.org/git/wine.git/?a=commit;h=bcc717174c54017e0cb5dfdb8c...
Author: Mike McCormack mike@codeweavers.com Date: Mon Jan 22 19:24:14 2007 +0900
ntdll: Implement RtlDecodePointer and RtlEncodePointer.
---
dlls/ntdll/ntdll.spec | 4 ++-- dlls/ntdll/rtl.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/dlls/ntdll/ntdll.spec b/dlls/ntdll/ntdll.spec index 803ed53..b3e3540 100644 --- a/dlls/ntdll/ntdll.spec +++ b/dlls/ntdll/ntdll.spec @@ -501,7 +501,7 @@ @ stub RtlDeactivateActivationContext @ stub RtlDeactivateActivationContextUnsafeFast @ stub RtlDebugPrintTimes -# @ stub RtlDecodePointer +@ stdcall RtlDecodePointer(ptr) # @ stub RtlDecodeSystemPointer @ stub RtlDecompressBuffer @ stub RtlDecompressFragment @@ -543,7 +543,7 @@ @ stdcall RtlDuplicateUnicodeString(long ptr ptr) @ stdcall RtlEmptyAtomTable(ptr long) # @ stub RtlEnableEarlyCriticalSectionEventCreation -# @ stub RtlEncodePointer +@ stdcall RtlEncodePointer(ptr) # @ stub RtlEncodeSystemPointer @ stdcall -ret64 RtlEnlargedIntegerMultiply(long long) @ stdcall RtlEnlargedUnsignedDivide(double long ptr) diff --git a/dlls/ntdll/rtl.c b/dlls/ntdll/rtl.c index 8ddab6a..449e91a 100644 --- a/dlls/ntdll/rtl.c +++ b/dlls/ntdll/rtl.c @@ -886,3 +886,39 @@ NTSTATUS WINAPI RtlIpv4AddressToStringEx
return STATUS_SUCCESS; } + +static DWORD_PTR get_pointer_obfuscator( void ) +{ + static DWORD_PTR pointer_obfuscator; + + if (!pointer_obfuscator) + { + ULONG seed = NtGetTickCount(); + ULONG_PTR rand; + + /* generate a random value for the obfuscator */ + rand = RtlUniform( &seed ); + + /* handle 64bit pointers */ + rand ^= RtlUniform( &seed ) << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8); + + /* set the high bits so dereferencing obfuscated pointers will (usually) crash */ + rand |= 0xc0000000 << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8); + + interlocked_cmpxchg_ptr( (void**) &pointer_obfuscator, (void*) rand, NULL ); + } + + return pointer_obfuscator; +} + +PVOID WINAPI RtlEncodePointer( PVOID ptr ) +{ + DWORD_PTR ptrval = (DWORD_PTR) ptr; + return (PVOID)(ptrval ^ get_pointer_obfuscator()); +} + +PVOID WINAPI RtlDecodePointer( PVOID ptr ) +{ + DWORD_PTR ptrval = (DWORD_PTR) ptr; + return (PVOID)(ptrval ^ get_pointer_obfuscator()); +}