Coverage Report

Created: 2026-03-04 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/library/library.c
Line
Count
Source
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 nullptr;
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 nullptr;
114
115
#if defined(_UWP)
116
  int status;
117
  HMODULE hModule = nullptr;
118
  WCHAR* filenameW = nullptr;
119
120
  filenameW = ConvertUtf8ToWCharAlloc(lpLibFileName, nullptr);
121
  if (filenameW)
122
    return nullptr;
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 nullptr;
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 = nullptr;
148
149
0
  if (lpLibFileName)
150
0
    name = ConvertWCharToUtf8Alloc(lpLibFileName, nullptr);
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 != nullptr");
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 != nullptr");
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 = nullptr;
187
0
  proc = dlsym(hModule, lpProcName);
188
189
0
  if (proc == nullptr)
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) nullptr;
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
  return (status == 0);
205
0
}
206
207
HMODULE GetModuleHandleA(LPCSTR lpModuleName)
208
0
{
209
0
  return dlopen(lpModuleName, RTLD_NOLOAD | RTLD_LOCAL | RTLD_LAZY);
210
0
}
211
212
HMODULE GetModuleHandleW(LPCWSTR lpModuleName)
213
0
{
214
0
  char* name = nullptr;
215
0
  if (lpModuleName)
216
0
    name = ConvertWCharToUtf8Alloc(lpModuleName, nullptr);
217
0
  HANDLE hdl = GetModuleHandleA(name);
218
0
  free(name);
219
0
  return hdl;
220
0
}
221
222
/**
223
 * GetModuleFileName:
224
 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197/
225
 *
226
 * Finding current executable's path without /proc/self/exe:
227
 * http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
228
 */
229
230
DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
231
0
{
232
0
  DWORD status = 0;
233
0
  if (!lpFilename)
234
0
  {
235
0
    SetLastError(ERROR_INTERNAL_ERROR);
236
0
    return 0;
237
0
  }
238
239
0
  char* name = calloc(nSize, sizeof(char));
240
0
  if (!name)
241
0
  {
242
0
    SetLastError(ERROR_INTERNAL_ERROR);
243
0
    return 0;
244
0
  }
245
0
  status = GetModuleFileNameA(hModule, name, nSize);
246
247
0
  if ((status > INT_MAX) || (nSize > INT_MAX))
248
0
  {
249
0
    SetLastError(ERROR_INTERNAL_ERROR);
250
0
    status = 0;
251
0
  }
252
253
0
  if (status > 0)
254
0
  {
255
0
    if (ConvertUtf8NToWChar(name, status, lpFilename, nSize) < 0)
256
0
    {
257
0
      free(name);
258
0
      SetLastError(ERROR_INTERNAL_ERROR);
259
0
      return 0;
260
0
    }
261
0
  }
262
263
0
  free(name);
264
0
  return status;
265
0
}
266
267
#if defined(__linux__) || defined(__NetBSD__) || defined(__DragonFly__)
268
static DWORD module_from_proc(const char* proc, LPSTR lpFilename, DWORD nSize)
269
0
{
270
0
  char buffer[8192] = WINPR_C_ARRAY_INIT;
271
0
  ssize_t status = readlink(proc, buffer, ARRAYSIZE(buffer) - 1);
272
273
0
  if ((status < 0) || ((size_t)status >= ARRAYSIZE(buffer)))
274
0
  {
275
0
    SetLastError(ERROR_INTERNAL_ERROR);
276
0
    return 0;
277
0
  }
278
279
0
  const size_t length = strnlen(buffer, ARRAYSIZE(buffer));
280
281
0
  if (length < nSize)
282
0
  {
283
0
    CopyMemory(lpFilename, buffer, length);
284
0
    lpFilename[length] = '\0';
285
0
    return (DWORD)length;
286
0
  }
287
288
0
  CopyMemory(lpFilename, buffer, nSize - 1);
289
0
  lpFilename[nSize - 1] = '\0';
290
0
  SetLastError(ERROR_INSUFFICIENT_BUFFER);
291
0
  return nSize;
292
0
}
293
#endif
294
295
DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
296
0
{
297
0
  if (hModule)
298
0
  {
299
0
    WLog_ERR(TAG, "is not implemented");
300
0
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
301
0
    return 0;
302
0
  }
303
304
0
#if defined(__linux__)
305
0
  return module_from_proc("/proc/self/exe", lpFilename, nSize);
306
#elif defined(__FreeBSD__)
307
  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
308
  size_t cb = nSize;
309
310
  {
311
    const int rc = sysctl(mib, ARRAYSIZE(mib), nullptr, &cb, nullptr, 0);
312
    if (rc != 0)
313
    {
314
      SetLastError(ERROR_INTERNAL_ERROR);
315
      return 0;
316
    }
317
  }
318
319
  char* fullname = calloc(cb + 1, sizeof(char));
320
  if (!fullname)
321
  {
322
    SetLastError(ERROR_INTERNAL_ERROR);
323
    return 0;
324
  }
325
326
  {
327
    size_t cb2 = cb;
328
    const int rc = sysctl(mib, ARRAYSIZE(mib), fullname, &cb2, nullptr, 0);
329
    if ((rc != 0) || (cb2 != cb))
330
    {
331
      SetLastError(ERROR_INTERNAL_ERROR);
332
      free(fullname);
333
      return 0;
334
    }
335
  }
336
337
  if (nSize > 0)
338
  {
339
    strncpy(lpFilename, fullname, nSize - 1);
340
    lpFilename[nSize - 1] = '\0';
341
  }
342
  free(fullname);
343
344
  if (nSize < cb)
345
    SetLastError(ERROR_INSUFFICIENT_BUFFER);
346
347
  return (DWORD)MIN(nSize, cb);
348
#elif defined(__NetBSD__)
349
  return module_from_proc("/proc/curproc/exe", lpFilename, nSize);
350
#elif defined(__DragonFly__)
351
  return module_from_proc("/proc/curproc/file", lpFilename, nSize);
352
#elif defined(__MACOSX__)
353
  char path[4096] = WINPR_C_ARRAY_INIT;
354
  char buffer[4096] = WINPR_C_ARRAY_INIT;
355
  uint32_t size = sizeof(path);
356
  const int status = _NSGetExecutablePath(path, &size);
357
358
  if (status != 0)
359
  {
360
    /* path too small */
361
    SetLastError(ERROR_INTERNAL_ERROR);
362
    return 0;
363
  }
364
365
  /*
366
   * _NSGetExecutablePath may not return the canonical path,
367
   * so use realpath to find the absolute, canonical path.
368
   */
369
  realpath(path, buffer);
370
  const size_t length = strnlen(buffer, sizeof(buffer));
371
372
  if (length < nSize)
373
  {
374
    CopyMemory(lpFilename, buffer, length);
375
    lpFilename[length] = '\0';
376
    return (DWORD)length;
377
  }
378
379
  CopyMemory(lpFilename, buffer, nSize - 1);
380
  lpFilename[nSize - 1] = '\0';
381
  SetLastError(ERROR_INSUFFICIENT_BUFFER);
382
  return nSize;
383
#else
384
  WLog_ERR(TAG, "is not implemented");
385
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
386
  return 0;
387
#endif
388
0
}
389
390
#endif
391
392
HMODULE LoadLibraryX(LPCSTR lpLibFileName)
393
0
{
394
#if defined(_WIN32)
395
  HMODULE hm = nullptr;
396
  WCHAR* wstr = nullptr;
397
398
  if (lpLibFileName)
399
    wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, nullptr);
400
401
  hm = LoadLibraryW(wstr);
402
  free(wstr);
403
  return hm;
404
#else
405
0
  return LoadLibraryA(lpLibFileName);
406
0
#endif
407
0
}
408
409
HMODULE LoadLibraryExX(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
410
0
{
411
0
  if (!lpLibFileName)
412
0
    return nullptr;
413
#if defined(_WIN32)
414
  HMODULE hm = nullptr;
415
  WCHAR* wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, nullptr);
416
  if (wstr)
417
    hm = LoadLibraryExW(wstr, hFile, dwFlags);
418
  free(wstr);
419
  return hm;
420
#else
421
0
  return LoadLibraryExA(lpLibFileName, hFile, dwFlags);
422
0
#endif
423
0
}