diff options
| author | Pali Rohár <pali.rohar@gmail.com> | 2020-07-14 21:14:56 +0200 |
|---|---|---|
| committer | Pali Rohár <pali.rohar@gmail.com> | 2020-07-14 21:14:56 +0200 |
| commit | 8ec5ffef2ecabd2474706b11e0238e8fca40b800 (patch) | |
| tree | 7462316b9e8a743d034bd8ee482a3f55a22d03bd | |
| parent | 61ad60f591700cc083b8a310c020554a59374a0a (diff) | |
| download | dlfcn-win32-8ec5ffef2ecabd2474706b11e0238e8fca40b800.tar.gz dlfcn-win32-8ec5ffef2ecabd2474706b11e0238e8fca40b800.tar.bz2 dlfcn-win32-8ec5ffef2ecabd2474706b11e0238e8fca40b800.zip | |
Replace VirtualQueryEx() call by GetModuleHandleExA() call
Usage of VirtualQueryEx() call is needed for retrieving HMODULE of passed
function address and it is just an undocumented hack. Based on @rhabacker's
tests it is unstable and does not work correctly.
https://github.com/dlfcn-win32/dlfcn-win32/pull/72#issuecomment-656581418
So replace VirtualQueryEx() call by standard GetModuleHandleExA() function
with special GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS flag which should do
the same thing but it is documented and it is working correctly.
| -rw-r--r-- | dlfcn.c | 18 |
1 files changed, 2 insertions, 16 deletions
| @@ -369,24 +369,10 @@ void *dlsym( void *handle, const char *name ) | |||
| 369 | * The next object is the one found upon the application of a load | 369 | * The next object is the one found upon the application of a load |
| 370 | * order symbol resolution algorithm. To get caller function of dlsym() | 370 | * order symbol resolution algorithm. To get caller function of dlsym() |
| 371 | * use _ReturnAddress() intrinsic. To get HMODULE of caller function | 371 | * use _ReturnAddress() intrinsic. To get HMODULE of caller function |
| 372 | * use undocumented hack from https://stackoverflow.com/a/2396380 | 372 | * use standard GetModuleHandleExA() function. |
| 373 | * The HMODULE of a DLL is the same value as the module's base address. | ||
| 374 | */ | 373 | */ |
| 375 | MEMORY_BASIC_INFORMATION info; | 374 | if( !GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) _ReturnAddress( ), &hCaller ) ) |
| 376 | size_t sLen; | ||
| 377 | sLen = VirtualQueryEx( hCurrentProc, _ReturnAddress(), &info, sizeof( info ) ); | ||
| 378 | if( sLen != sizeof( info ) ) | ||
| 379 | { | ||
| 380 | if( sLen != 0 ) | ||
| 381 | SetLastError( ERROR_INVALID_PARAMETER ); | ||
| 382 | goto end; | 375 | goto end; |
| 383 | } | ||
| 384 | hCaller = (HMODULE) info.AllocationBase; | ||
| 385 | if( !hCaller ) | ||
| 386 | { | ||
| 387 | SetLastError( ERROR_INVALID_PARAMETER ); | ||
| 388 | goto end; | ||
| 389 | } | ||
| 390 | } | 376 | } |
| 391 | 377 | ||
| 392 | if( handle != RTLD_NEXT ) | 378 | if( handle != RTLD_NEXT ) |
