Commit 29c46a54ffa31513be1a2cdcd9e8a55938d6e549

Pali Rohár 2019-01-29T22:38:42

Fix resolving global symbols when LoadLibrary() is called after dlopen() Usage of first_automatic_object cache is wrong. This cache is filled by all loaded DLL files (either implicitly or explicitly with LoadLibrary() call) by EnumProcessModules() call at first usage of dlopen(). So dlsym() can resolve global symbols only if they were loaded prior to dlopen() call. Any future usage of LoadLibrary() does not include newly loaded DLLs into first_automatic_object cache. To fix this problem, first_automatic_object cache is fully removed and EnumProcessModules() call is issued directly in dlsym() call. As EnumProcessModules() returns all DLLs, included those which were loaded by dlopen() with RTLD_LOCAL, it may break RTLD_LOCAL support. To address this problem switch linked-list of all loaded DLLs with RTLD_GLOBAL to linked-list of all loaded DLLs with RTLD_LOCAL flag. And then skip modules from EnumProcessModules() which are in linked-list. Also in WinAPI all DLLs loaded by LoadLibrary() behaves like RTLD_GLOBAL. So above change is compatible with this behavior. There may be another problem. Before retrieving HMODULE for DLL filename (which is done by LoadLibrary()), it is not possible to detect if DLL was already loaded by RTLD_LOCAL or not. And after calling LoadLibrary() it is not possible to know if DLL was loaded either by dlsym() with RTLD_LOCAL or by LoadLibrary() (which is equivalent to RTLD_GLOBAL). To address this problem, compare number of loaded modules (counted by EnumProcessModules()) before and after LoadLibrary() called from dlsym(). If number does not change it means that DLL was already loaded. So based on this result either add or remove HMODULE from linked-list of RTLD_LOCAL modules. Added test demonstrate usage of: global = dlopen(NULL, RTLD_GLOBAL); /* global handle */ LoadLibrary("library.dll"); /* this provides function */ function = dlsym(global, "function"); /* resolve function from library.dll */

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 48945b2..a13cf99 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -63,6 +63,8 @@ if (BUILD_TESTS)
   enable_testing()
   add_library(testdll SHARED testdll.c)
   set_target_properties(testdll PROPERTIES PREFIX "")
+  add_library(testdll3 SHARED testdll3.c)
+  set_target_properties(testdll3 PROPERTIES PREFIX "")
   add_executable(t_dlfcn test.c)
   target_link_libraries(t_dlfcn dl)
   add_test (NAME t_dlfcn COMMAND t_dlfcn)
diff --git a/Makefile b/Makefile
index c02dce0..a0f5cb9 100644
--- a/Makefile
+++ b/Makefile
@@ -66,7 +66,10 @@ test.exe: test.o $(TARGETS)
 testdll.dll: testdll.c
 	$(CC) -shared -o $@ $^
 
-test: $(TARGETS) test.exe testdll.dll
+testdll3.dll: testdll3.c
+	$(CC) -shared -o $@ $^
+
+test: $(TARGETS) test.exe testdll.dll testdll3.dll
 	$(WINE) test.exe
 
 clean::
@@ -74,7 +77,7 @@ clean::
 		dlfcn.o \
 		libdl.dll libdl.a libdl.def libdl.dll.a libdl.lib libdl.exp \
 		tmptest.c tmptest.dll \
-		test.exe testdll.dll
+		test.exe testdll.dll testdll3.dll
 
 distclean: clean
 	rm -f config.mak
diff --git a/cmake-test/CMakeLists.txt b/cmake-test/CMakeLists.txt
index 532f2b8..b853920 100644
--- a/cmake-test/CMakeLists.txt
+++ b/cmake-test/CMakeLists.txt
@@ -8,6 +8,8 @@ find_package(dlfcn-win32 REQUIRED)
 
 add_library(testdll SHARED ../testdll.c)
 set_target_properties(testdll PROPERTIES PREFIX "")
+add_library(testdll3 SHARED ../testdll3.c)
+set_target_properties(testdll3 PROPERTIES PREFIX "")
 add_executable(t_dlfcn ../test.c)
 target_link_libraries(t_dlfcn dlfcn-win32::dl)
 enable_testing()
diff --git a/dlfcn.c b/dlfcn.c
index b356558..1e706f0 100644
--- a/dlfcn.c
+++ b/dlfcn.c
@@ -2,6 +2,7 @@
  * dlfcn-win32
  * Copyright (c) 2007 Ramiro Polla
  * Copyright (c) 2015 Tiancheng "Timothy" Gu
+ * Copyright (c) 2019 Pali Rohár <pali.rohar@gmail.com>
  *
  * dlfcn-win32 is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -52,57 +53,48 @@
  * any kind of thread safety.
  */
 
-typedef struct global_object {
+typedef struct local_object {
     HMODULE hModule;
-    struct global_object *previous;
-    struct global_object *next;
-} global_object;
+    struct local_object *previous;
+    struct local_object *next;
+} local_object;
 
-static global_object first_object;
-static global_object first_automatic_object;
-static int auto_ref_count = 0;
+static local_object first_object;
 
-/* These functions implement a double linked list for the global objects. */
-static global_object *global_search( global_object *start, HMODULE hModule )
+/* These functions implement a double linked list for the local objects. */
+static local_object *local_search( HMODULE hModule )
 {
-    global_object *pobject;
+    local_object *pobject;
 
     if( hModule == NULL )
         return NULL;
 
-    for( pobject = start; pobject; pobject = pobject->next )
+    for( pobject = &first_object; pobject; pobject = pobject->next )
         if( pobject->hModule == hModule )
             return pobject;
 
     return NULL;
 }
 
-static void global_add( global_object *start, HMODULE hModule )
+static void local_add( HMODULE hModule )
 {
-    global_object *pobject;
-    global_object *nobject;
+    local_object *pobject;
+    local_object *nobject;
 
     if( hModule == NULL )
         return;
 
-    pobject = global_search( start, hModule );
+    pobject = local_search( hModule );
 
     /* Do not add object again if it's already on the list */
     if( pobject )
         return;
 
-    if( start == &first_automatic_object )
-    {
-        pobject = global_search( &first_object, hModule );
-        if( pobject )
-            return;
-    }
-
-    for( pobject = start; pobject->next; pobject = pobject->next );
+    for( pobject = &first_object; pobject->next; pobject = pobject->next );
 
-    nobject = (global_object*) malloc( sizeof( global_object ) );
+    nobject = (local_object*) malloc( sizeof( local_object ) );
 
-    /* Should this be enough to fail global_add, and therefore also fail
+    /* Should this be enough to fail local_add, and therefore also fail
      * dlopen?
      */
     if( !nobject )
@@ -114,14 +106,14 @@ static void global_add( global_object *start, HMODULE hModule )
     nobject->hModule = hModule;
 }
 
-static void global_rem( global_object *start, HMODULE hModule )
+static void local_rem( HMODULE hModule )
 {
-    global_object *pobject;
+    local_object *pobject;
 
     if( hModule == NULL )
         return;
 
-    pobject = global_search( start, hModule );
+    pobject = local_search( hModule );
 
     if( !pobject )
         return;
@@ -224,10 +216,6 @@ void *dlopen( const char *file, int mode )
 
     if( file == 0 )
     {
-        HMODULE hAddtnlMods[1024]; // Already loaded modules
-        HANDLE hCurrentProc = GetCurrentProcess( );
-        DWORD cbNeeded;
-
         /* POSIX says that if the value of file is 0, a handle on a global
          * symbol object must be provided. That object must be able to access
          * all symbols from the original program file, and any objects loaded
@@ -242,27 +230,13 @@ void *dlopen( const char *file, int mode )
 
         if( !hModule )
             save_err_ptr_str( file );
-
-
-        /* GetModuleHandle( NULL ) only returns the current program file. So
-         * if we want to get ALL loaded module including those in linked DLLs,
-         * we have to use EnumProcessModules( ).
-         */
-        if( EnumProcessModules( hCurrentProc, hAddtnlMods,
-            sizeof( hAddtnlMods ), &cbNeeded ) != 0 )
-        {
-            DWORD i;
-            for( i = 0; i < cbNeeded / sizeof( HMODULE ); i++ )
-            {
-                global_add( &first_automatic_object, hAddtnlMods[i] );
-            }
-        }
-        auto_ref_count++;
     }
     else
     {
+        HANDLE hCurrentProc;
+        DWORD dwProcModsBefore, dwProcModsAfter;
         CHAR lpFileName[MAX_PATH];
-        int i;
+        size_t i;
 
         /* MSDN says backslashes *must* be used instead of forward slashes. */
         for( i = 0 ; i < sizeof(lpFileName) - 1 ; i ++ )
@@ -276,6 +250,11 @@ void *dlopen( const char *file, int mode )
         }
         lpFileName[i] = '\0';
 
+        hCurrentProc = GetCurrentProcess( );
+
+        if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 )
+            dwProcModsBefore = 0;
+
         /* POSIX says the search path is implementation-defined.
          * LOAD_WITH_ALTERED_SEARCH_PATH is used to make it behave more closely
          * to UNIX's search paths (start with system folders instead of current
@@ -284,17 +263,24 @@ void *dlopen( const char *file, int mode )
         hModule = LoadLibraryEx(lpFileName, NULL, 
                                 LOAD_WITH_ALTERED_SEARCH_PATH );
 
-        /* If the object was loaded with RTLD_GLOBAL, add it to list of global
-         * objects, so that its symbols may be retrieved even if the handle for
+        if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 )
+            dwProcModsAfter = 0;
+
+        /* If the object was loaded with RTLD_LOCAL, add it to list of local
+         * objects, so that its symbols cannot be retrieved even if the handle for
          * the original program file is passed. POSIX says that if the same
          * file is specified in multiple invocations, and any of them are
          * RTLD_GLOBAL, even if any further invocations use RTLD_LOCAL, the
-         * symbols will remain global.
+         * symbols will remain global. If number of loaded modules was not
+         * changed after calling LoadLibraryEx(), it means that library was
+         * already loaded.
          */
         if( !hModule )
             save_err_str( lpFileName );
-        else if( (mode & RTLD_GLOBAL) )
-            global_add( &first_object, hModule );
+        else if( (mode & RTLD_LOCAL) && dwProcModsBefore != dwProcModsAfter )
+            local_add( hModule );
+        else if( !(mode & RTLD_LOCAL) && dwProcModsBefore == dwProcModsAfter )
+            local_rem( hModule );
     }
 
     /* Return to previous state of the error-mode bit flags. */
@@ -303,21 +289,6 @@ void *dlopen( const char *file, int mode )
     return (void *) hModule;
 }
 
-static void free_auto( )
-{
-    global_object *pobject = first_automatic_object.next;
-    if( pobject )
-    {
-        global_object *next;
-        for ( ; pobject; pobject = next )
-        {
-            next = pobject->next;
-            free( pobject );
-        }
-        first_automatic_object.next = NULL;
-    }
-}
-
 int dlclose( void *handle )
 {
     HMODULE hModule = (HMODULE) handle;
@@ -327,22 +298,11 @@ int dlclose( void *handle )
 
     ret = FreeLibrary( hModule );
 
-    /* If the object was loaded with RTLD_GLOBAL, remove it from list of global
+    /* If the object was loaded with RTLD_LOCAL, remove it from list of local
      * objects.
      */
     if( ret )
-    {
-        HMODULE cur = GetModuleHandle( NULL );
-        global_rem( &first_object, hModule );
-        if( hModule == cur )
-        {
-            auto_ref_count--;
-            if( auto_ref_count < 0 )
-                auto_ref_count = 0;
-            if( !auto_ref_count )
-                free_auto( );
-        }
-    }
+        local_rem( hModule );
     else
         save_err_ptr_str( handle );
 
@@ -356,6 +316,7 @@ void *dlsym( void *handle, const char *name )
 {
     FARPROC symbol;
     HMODULE hModule;
+    HANDLE hCurrentProc;
 
 #ifdef UNICODE
     wchar_t namew[MAX_PATH];
@@ -363,6 +324,7 @@ void *dlsym( void *handle, const char *name )
 #endif
 
     current_error = NULL;
+    hCurrentProc = GetCurrentProcess( );
 
     symbol = GetProcAddress( (HMODULE) handle, name );
 
@@ -377,25 +339,33 @@ void *dlsym( void *handle, const char *name )
 
     if( hModule == handle )
     {
-        global_object *pobject;
-
-        for( pobject = &first_object; pobject; pobject = pobject->next )
-        {
-            if( pobject->hModule )
-            {
-                symbol = GetProcAddress( pobject->hModule, name );
-                if( symbol != NULL )
-                    goto end;
-            }
-        }
+        HMODULE *modules;
+        DWORD cbNeeded;
+        DWORD dwSize;
+        size_t i;
 
-        for( pobject = &first_automatic_object; pobject; pobject = pobject->next )
+        /* GetModuleHandle( NULL ) only returns the current program file. So
+         * if we want to get ALL loaded module including those in linked DLLs,
+         * we have to use EnumProcessModules( ).
+         */
+        if( EnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 )
         {
-            if( pobject->hModule )
+            modules = malloc( dwSize );
+            if( modules )
             {
-                symbol = GetProcAddress( pobject->hModule, name );
-                if( symbol != NULL )
-                    goto end;
+                if( EnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded )
+                {
+                    for( i = 0; i < dwSize / sizeof( HMODULE ); i++ )
+                    {
+                        if( local_search( modules[i] ) )
+                            continue;
+                        symbol = GetProcAddress( modules[i], name );
+                        if( symbol != NULL )
+                            goto end;
+                    }
+
+                }
+                free( modules );
             }
         }
     }
@@ -472,18 +442,8 @@ char *dlerror( void )
 BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
 {
     (void) hinstDLL;
-    /*
-     * https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx 
-     *
-     *     When handling DLL_PROCESS_DETACH, a DLL should free resources such as heap
-     *     memory only if the DLL is being unloaded dynamically (the lpReserved
-     *     parameter is NULL).
-     */
-    if( fdwReason == DLL_PROCESS_DETACH && !lpvReserved )
-    {
-        auto_ref_count = 0;
-        free_auto( );
-    }
+    (void) fdwReason;
+    (void) lpvReserved;
     return TRUE;
 }
 #endif
diff --git a/test.c b/test.c
index ba440da..10c655e 100644
--- a/test.c
+++ b/test.c
@@ -2,6 +2,7 @@
  * dlfcn-win32
  * Copyright (c) 2007-2009 Ramiro Polla
  * Copyright (c) 2014      Tiancheng "Timothy" Gu
+ * Copyright (c) 2019      Pali Rohár <pali.rohar@gmail.com>
  *
  * dlfcn-win32 is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -25,6 +26,7 @@
 #endif
 #include <stdio.h>
 #include <string.h>
+#include <windows.h>
 #include "dlfcn.h"
 
 /* If these dlclose's fails, we don't care as the handles are going to be
@@ -77,6 +79,7 @@ int main()
     size_t (*fwrite_local) ( const void *, size_t, size_t, FILE * );
     int (*nonexistentfunction)( void );
     int ret;
+    HMODULE library3;
 
 #ifdef _DEBUG
     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
@@ -335,6 +338,15 @@ int main()
         printf("SUCCESS\tGot symbol from global handle: %p\n", function);
     
 
+    library3 = LoadLibraryA("testdll3.dll");
+    if (!library3)
+    {
+        printf( "ERROR\tCould not open library3 via WINAPI\n" );
+        RETURN_ERROR;
+    }
+    else
+        printf( "SUCCESS\tOpened library3 via WINAPI: %p\n", library3 );
+
     ret = dlclose( library );
     if( ret )
     {
@@ -346,6 +358,21 @@ int main()
     else
         printf( "SUCCESS\tClosed library.\n" );
 
+    function = dlsym(global, "function3");
+    if (!function)
+    {
+        error = dlerror();
+        printf("ERROR\tCould not get symbol from global handle: %s\n",
+            error ? error : "");
+        CLOSE_LIB;
+        CLOSE_GLOBAL;
+        RETURN_ERROR;
+    }
+    else
+        printf("SUCCESS\tGot symbol from global handle: %p\n", function);
+
+    RUNFUNC;
+
     ret = dlclose( global );
     if( ret )
     {
diff --git a/testdll3.c b/testdll3.c
new file mode 100644
index 0000000..3906e48
--- /dev/null
+++ b/testdll3.c
@@ -0,0 +1,38 @@
+/*
+ * dlfcn-win32
+ * Copyright (c) 2007 Ramiro Polla
+ * Copyright (c) 2019 Pali Rohár <pali.rohar@gmail.com>
+ *
+ * dlfcn-win32 is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * dlfcn-win32 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with dlfcn-win32; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifdef _DEBUG
+#define _CRTDBG_MAP_ALLOC
+#include <stdlib.h>
+#include <crtdbg.h>
+#endif
+#include <stdio.h>
+
+#if defined(_WIN32)
+#define EXPORT __declspec(dllexport)
+#else
+#define EXPORT
+#endif
+
+EXPORT int function3( void )
+{
+    printf( "Hello, world!\n" );
+    return 0;
+}