Coverage Report

Created: 2025-08-26 06:37

/src/FreeRDP/winpr/libwinpr/library/library.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * Library Loader
4
 *
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <winpr/config.h>
21
22
#include <winpr/crt.h>
23
#include <winpr/platform.h>
24
25
#include <winpr/library.h>
26
27
#include "../log.h"
28
#define TAG WINPR_TAG("library")
29
30
/**
31
 * api-ms-win-core-libraryloader-l1-1-1.dll:
32
 *
33
 * AddDllDirectory
34
 * RemoveDllDirectory
35
 * SetDefaultDllDirectories
36
 * DisableThreadLibraryCalls
37
 * EnumResourceLanguagesExA
38
 * EnumResourceLanguagesExW
39
 * EnumResourceNamesExA
40
 * EnumResourceNamesExW
41
 * EnumResourceTypesExA
42
 * EnumResourceTypesExW
43
 * FindResourceExW
44
 * FindStringOrdinal
45
 * FreeLibrary
46
 * FreeLibraryAndExitThread
47
 * FreeResource
48
 * GetModuleFileNameA
49
 * GetModuleFileNameW
50
 * GetModuleHandleA
51
 * GetModuleHandleExA
52
 * GetModuleHandleExW
53
 * GetModuleHandleW
54
 * GetProcAddress
55
 * LoadLibraryExA
56
 * LoadLibraryExW
57
 * LoadResource
58
 * LoadStringA
59
 * LoadStringW
60
 * LockResource
61
 * QueryOptionalDelayLoadedAPI
62
 * SizeofResource
63
 */
64
65
#if !defined(_WIN32) || defined(_UWP)
66
67
#ifndef _WIN32
68
69
#include <dlfcn.h>
70
#include <stdio.h>
71
#include <stdlib.h>
72
#include <unistd.h>
73
#include <sys/types.h>
74
#include <sys/stat.h>
75
76
#ifdef __MACOSX__
77
#include <mach-o/dyld.h>
78
#endif
79
80
#if defined(__FreeBSD__)
81
#include <sys/sysctl.h>
82
#endif
83
84
#endif
85
86
DLL_DIRECTORY_COOKIE AddDllDirectory(WINPR_ATTR_UNUSED PCWSTR NewDirectory)
87
0
{
88
  /* TODO: Implement */
89
0
  WLog_ERR(TAG, "not implemented");
90
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
91
0
  return NULL;
92
0
}
93
94
BOOL RemoveDllDirectory(WINPR_ATTR_UNUSED DLL_DIRECTORY_COOKIE Cookie)
95
0
{
96
  /* TODO: Implement */
97
0
  WLog_ERR(TAG, "not implemented");
98
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
99
0
  return FALSE;
100
0
}
101
102
BOOL SetDefaultDllDirectories(WINPR_ATTR_UNUSED DWORD DirectoryFlags)
103
0
{
104
  /* TODO: Implement */
105
0
  WLog_ERR(TAG, "not implemented");
106
0
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
107
0
  return FALSE;
108
0
}
109
110
HMODULE LoadLibraryA(LPCSTR lpLibFileName)
111
0
{
112
0
  if (!lpLibFileName)
113
0
    return NULL;
114
115
#if defined(_UWP)
116
  int status;
117
  HMODULE hModule = NULL;
118
  WCHAR* filenameW = NULL;
119
120
  filenameW = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
121
  if (filenameW)
122
    return NULL;
123
124
  hModule = LoadLibraryW(filenameW);
125
  free(filenameW);
126
  return hModule;
127
#else
128
0
  HMODULE library = dlopen(lpLibFileName, RTLD_LOCAL | RTLD_LAZY);
129
130
0
  if (!library)
131
0
  {
132
    // NOLINTNEXTLINE(concurrency-mt-unsafe)
133
0
    const char* err = dlerror();
134
0
    WLog_ERR(TAG, "failed with %s", err);
135
0
    return NULL;
136
0
  }
137
138
0
  return library;
139
0
#endif
140
0
}
141
142
HMODULE LoadLibraryW(LPCWSTR lpLibFileName)
143
0
{
144
#if defined(_UWP)
145
  return LoadPackagedLibrary(lpLibFileName, 0);
146
#else
147
0
  char* name = NULL;
148
149
0
  if (lpLibFileName)
150
0
    name = ConvertWCharToUtf8Alloc(lpLibFileName, NULL);
151
152
0
  HMODULE module = LoadLibraryA(name);
153
0
  free(name);
154
0
  return module;
155
0
#endif
156
0
}
157
158
HMODULE LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
159
0
{
160
0
  if (dwFlags != 0)
161
0
    WLog_WARN(TAG, "does not support dwFlags 0x%08" PRIx32, dwFlags);
162
163
0
  if (hFile)
164
0
    WLog_WARN(TAG, "does not support hFile != NULL");
165
166
0
  return LoadLibraryA(lpLibFileName);
167
0
}
168
169
HMODULE LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
170
0
{
171
0
  if (dwFlags != 0)
172
0
    WLog_WARN(TAG, "does not support dwFlags 0x%08" PRIx32, dwFlags);
173
174
0
  if (hFile)
175
0
    WLog_WARN(TAG, "does not support hFile != NULL");
176
177
0
  return LoadLibraryW(lpLibFileName);
178
0
}
179
180
#endif
181
182
#if !defined(_WIN32) && !defined(__CYGWIN__)
183
184
FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
185
0
{
186
0
  FARPROC proc = NULL;
187
0
  proc = dlsym(hModule, lpProcName);
188
189
0
  if (proc == NULL)
190
0
  {
191
    // NOLINTNEXTLINE(concurrency-mt-unsafe)
192
0
    WLog_ERR(TAG, "GetProcAddress: could not find procedure %s: %s", lpProcName, dlerror());
193
0
    return (FARPROC)NULL;
194
0
  }
195
196
0
  return proc;
197
0
}
198
199
BOOL FreeLibrary(HMODULE hLibModule)
200
0
{
201
0
  int status = 0;
202
0
  status = dlclose(hLibModule);
203
204
0
  if (status != 0)
205
0
    return FALSE;
206
207
0
  return TRUE;
208
0
}
209
210
HMODULE GetModuleHandleA(LPCSTR lpModuleName)
211
0
{
212
0
  return dlopen(lpModuleName, RTLD_NOLOAD | RTLD_LOCAL | RTLD_LAZY);
213
0
}
214
215
HMODULE GetModuleHandleW(LPCWSTR lpModuleName)
216
0
{
217
0
  char* name = NULL;
218
0
  if (lpModuleName)
219
0
    name = ConvertWCharToUtf8Alloc(lpModuleName, NULL);
220
0
  HANDLE hdl = GetModuleHandleA(name);
221
0
  free(name);
222
0
  return hdl;
223
0
}
224
225
/**
226
 * GetModuleFileName:
227
 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197/
228
 *
229
 * Finding current executable's path without /proc/self/exe:
230
 * http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
231
 */
232
233
DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
234
0
{
235
0
  DWORD status = 0;
236
0
  if (!lpFilename)
237
0
  {
238
0
    SetLastError(ERROR_INTERNAL_ERROR);
239
0
    return 0;
240
0
  }
241
242
0
  char* name = calloc(nSize, sizeof(char));
243
0
  if (!name)
244
0
  {
245
0
    SetLastError(ERROR_INTERNAL_ERROR);
246
0
    return 0;
247
0
  }
248
0
  status = GetModuleFileNameA(hModule, name, nSize);
249
250
0
  if ((status > INT_MAX) || (nSize > INT_MAX))
251
0
  {
252
0
    SetLastError(ERROR_INTERNAL_ERROR);
253
0
    status = 0;
254
0
  }
255
256
0
  if (status > 0)
257
0
  {
258
0
    if (ConvertUtf8NToWChar(name, status, lpFilename, nSize) < 0)
259
0
    {
260
0
      free(name);
261
0
      SetLastError(ERROR_INTERNAL_ERROR);
262
0
      return 0;
263
0
    }
264
0
  }
265
266
0
  free(name);
267
0
  return status;
268
0
}
269
270
#if defined(__linux__) || defined(__NetBSD__) || defined(__DragonFly__)
271
static DWORD module_from_proc(const char* proc, LPSTR lpFilename, DWORD nSize)
272
0
{
273
0
  char buffer[8192] = { 0 };
274
0
  ssize_t status = readlink(proc, buffer, ARRAYSIZE(buffer) - 1);
275
276
0
  if ((status < 0) || ((size_t)status >= ARRAYSIZE(buffer)))
277
0
  {
278
0
    SetLastError(ERROR_INTERNAL_ERROR);
279
0
    return 0;
280
0
  }
281
282
0
  const size_t length = strnlen(buffer, ARRAYSIZE(buffer));
283
284
0
  if (length < nSize)
285
0
  {
286
0
    CopyMemory(lpFilename, buffer, length);
287
0
    lpFilename[length] = '\0';
288
0
    return (DWORD)length;
289
0
  }
290
291
0
  CopyMemory(lpFilename, buffer, nSize - 1);
292
0
  lpFilename[nSize - 1] = '\0';
293
0
  SetLastError(ERROR_INSUFFICIENT_BUFFER);
294
0
  return nSize;
295
0
}
296
#endif
297
298
DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
299
0
{
300
0
  if (hModule)
301
0
  {
302
0
    WLog_ERR(TAG, "is not implemented");
303
0
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
304
0
    return 0;
305
0
  }
306
307
0
#if defined(__linux__)
308
0
  return module_from_proc("/proc/self/exe", lpFilename, nSize);
309
#elif defined(__FreeBSD__)
310
  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
311
  size_t cb = nSize;
312
313
  {
314
    const int rc = sysctl(mib, ARRAYSIZE(mib), NULL, &cb, NULL, 0);
315
    if (rc != 0)
316
    {
317
      SetLastError(ERROR_INTERNAL_ERROR);
318
      return 0;
319
    }
320
  }
321
322
  char* fullname = calloc(cb + 1, sizeof(char));
323
  if (!fullname)
324
  {
325
    SetLastError(ERROR_INTERNAL_ERROR);
326
    return 0;
327
  }
328
329
  {
330
    size_t cb2 = cb;
331
    const int rc = sysctl(mib, ARRAYSIZE(mib), fullname, &cb2, NULL, 0);
332
    if ((rc != 0) || (cb2 != cb))
333
    {
334
      SetLastError(ERROR_INTERNAL_ERROR);
335
      free(fullname);
336
      return 0;
337
    }
338
  }
339
340
  if (nSize > 0)
341
  {
342
    strncpy(lpFilename, fullname, nSize - 1);
343
    lpFilename[nSize - 1] = '\0';
344
  }
345
  free(fullname);
346
347
  if (nSize < cb)
348
    SetLastError(ERROR_INSUFFICIENT_BUFFER);
349
350
  return (DWORD)MIN(nSize, cb);
351
#elif defined(__NetBSD__)
352
  return module_from_proc("/proc/curproc/exe", lpFilename, nSize);
353
#elif defined(__DragonFly__)
354
  return module_from_proc("/proc/curproc/file", lpFilename, nSize);
355
#elif defined(__MACOSX__)
356
  char path[4096] = { 0 };
357
  char buffer[4096] = { 0 };
358
  uint32_t size = sizeof(path);
359
  const int status = _NSGetExecutablePath(path, &size);
360
361
  if (status != 0)
362
  {
363
    /* path too small */
364
    SetLastError(ERROR_INTERNAL_ERROR);
365
    return 0;
366
  }
367
368
  /*
369
   * _NSGetExecutablePath may not return the canonical path,
370
   * so use realpath to find the absolute, canonical path.
371
   */
372
  realpath(path, buffer);
373
  const size_t length = strnlen(buffer, sizeof(buffer));
374
375
  if (length < nSize)
376
  {
377
    CopyMemory(lpFilename, buffer, length);
378
    lpFilename[length] = '\0';
379
    return (DWORD)length;
380
  }
381
382
  CopyMemory(lpFilename, buffer, nSize - 1);
383
  lpFilename[nSize - 1] = '\0';
384
  SetLastError(ERROR_INSUFFICIENT_BUFFER);
385
  return nSize;
386
#else
387
  WLog_ERR(TAG, "is not implemented");
388
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
389
  return 0;
390
#endif
391
0
}
392
393
#endif
394
395
HMODULE LoadLibraryX(LPCSTR lpLibFileName)
396
0
{
397
#if defined(_WIN32)
398
  HMODULE hm = NULL;
399
  WCHAR* wstr = NULL;
400
401
  if (lpLibFileName)
402
    wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
403
404
  hm = LoadLibraryW(wstr);
405
  free(wstr);
406
  return hm;
407
#else
408
0
  return LoadLibraryA(lpLibFileName);
409
0
#endif
410
0
}
411
412
HMODULE LoadLibraryExX(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
413
0
{
414
0
  if (!lpLibFileName)
415
0
    return NULL;
416
#if defined(_WIN32)
417
  HMODULE hm = NULL;
418
  WCHAR* wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
419
  if (wstr)
420
    hm = LoadLibraryExW(wstr, hFile, dwFlags);
421
  free(wstr);
422
  return hm;
423
#else
424
0
  return LoadLibraryExA(lpLibFileName, hFile, dwFlags);
425
0
#endif
426
0
}