Skip to content

Commit 91efa9b

Browse files
committed
Enable the proxy library on Windows
Co-developed-by: Rafał Rudnicki <rafal.rudnicki@intel.com> Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent 08f89a5 commit 91efa9b

File tree

6 files changed

+68
-17
lines changed

6 files changed

+68
-17
lines changed

.github/workflows/basic.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ jobs:
232232
-B ${{env.BUILD_DIR}}
233233
${{matrix.toolset}}
234234
-DCMAKE_PREFIX_PATH="${{env.VCPKG_PATH}}"
235+
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
235236
-DCMAKE_C_COMPILER=${{matrix.compiler.c}}
236237
-DCMAKE_CXX_COMPILER=${{matrix.compiler.cxx}}
237238
-DUMF_BUILD_SHARED_LIBRARY=${{matrix.shared_library}}

src/CMakeLists.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,19 @@ install(TARGETS umf
166166

167167
add_subdirectory(pool)
168168

169-
# TODO: enable proxy_lib on Windows
170-
if(LINUX)
169+
if(LINUX OR WINDOWS)
171170
if(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL SCALABLE)
172-
set(PROXY_LIB_USES_SCALABLE_POOL ON)
173-
set(PROXY_LIBS umf scalable_pool)
174171
if(UMF_BUILD_LIBUMF_POOL_SCALABLE)
172+
set(PROXY_LIB_USES_SCALABLE_POOL ON)
173+
set(PROXY_LIBS umf scalable_pool)
175174
add_subdirectory(proxy_lib)
176175
else()
177176
message(STATUS "Disabling the proxy library, because UMF_PROXY_LIB_BASED_ON_POOL==SCALABLE but UMF_BUILD_LIBUMF_POOL_SCALABLE is OFF")
178177
endif()
179178
elseif(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL JEMALLOC)
180-
set(PROXY_LIB_USES_JEMALLOC_POOL ON)
181-
set(PROXY_LIBS umf jemalloc_pool)
182179
if(UMF_BUILD_LIBUMF_POOL_JEMALLOC)
180+
set(PROXY_LIB_USES_JEMALLOC_POOL ON)
181+
set(PROXY_LIBS umf jemalloc_pool)
183182
add_subdirectory(proxy_lib)
184183
else()
185184
message(STATUS "Disabling the proxy library, because UMF_PROXY_LIB_BASED_ON_POOL==JEMALLOC but UMF_BUILD_LIBUMF_POOL_JEMALLOC is OFF")

src/proxy_lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ add_umf_library(NAME umf_proxy
3737

3838
add_library(${PROJECT_NAME}::proxy ALIAS umf_proxy)
3939

40+
target_link_directories(umf_proxy PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
41+
4042
if(PROXY_LIB_USES_SCALABLE_POOL)
4143
target_compile_definitions(umf_proxy PRIVATE PROXY_LIB_USES_SCALABLE_POOL=1)
4244
elseif(PROXY_LIB_USES_JEMALLOC_POOL)

src/proxy_lib/proxy_lib.c

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
#endif
2929

3030
#include <assert.h>
31-
#include <stdlib.h>
32-
#include <string.h>
31+
#include <stdio.h>
3332

3433
#include <umf/memory_pool.h>
3534
#include <umf/memory_provider.h>
@@ -38,8 +37,27 @@
3837
#include "base_alloc_linear.h"
3938
#include "proxy_lib.h"
4039
#include "utils_common.h"
40+
41+
#ifdef _WIN32 /* Windows ***************************************/
42+
43+
#define _X86_
44+
#include <process.h>
45+
#include <synchapi.h>
46+
47+
#define UTIL_ONCE_FLAG INIT_ONCE
48+
#define UTIL_ONCE_FLAG_INIT INIT_ONCE_STATIC_INIT
49+
50+
void util_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
51+
52+
#else /* Linux *************************************************/
53+
54+
#include <stdlib.h>
55+
#include <string.h>
56+
4157
#include "utils_concurrency.h"
4258

59+
#endif /* _WIN32 ***********************************************/
60+
4361
/*
4462
* The UMF proxy library uses two memory allocators:
4563
* 1) the "LEAK" internal linear base allocator based on the anonymous mapped
@@ -100,9 +118,20 @@ void proxy_lib_create_common(void) {
100118
}
101119

102120
void proxy_lib_destroy_common(void) {
103-
// We cannot destroy 'Base_alloc_leak' nor 'Proxy_pool' nor 'OS_memory_provider',
104-
// because it could lead to use-after-free in the program's unloader
105-
// (for example _dl_fini() on Linux).
121+
if (util_is_running_in_proxy_lib()) {
122+
// We cannot destroy 'Base_alloc_leak' nor 'Proxy_pool' nor 'OS_memory_provider',
123+
// because it could lead to use-after-free in the program's unloader
124+
// (for example _dl_fini() on Linux).
125+
return;
126+
}
127+
128+
umf_memory_pool_handle_t pool = Proxy_pool;
129+
Proxy_pool = NULL;
130+
umfPoolDestroy(pool);
131+
132+
umf_memory_provider_handle_t provider = OS_memory_provider;
133+
OS_memory_provider = NULL;
134+
umfMemoryProviderDestroy(provider);
106135
}
107136

108137
/*****************************************************************************/
@@ -223,6 +252,13 @@ void free(void *ptr) {
223252
return;
224253
}
225254

255+
#ifdef _WIN32
256+
void _free_dbg(void *userData, int blockType) {
257+
(void)blockType; // unused
258+
free(userData);
259+
}
260+
#endif
261+
226262
void *realloc(void *ptr, size_t size) {
227263
if (ptr == NULL) {
228264
return malloc(size);
@@ -260,7 +296,11 @@ void *aligned_alloc(size_t alignment, size_t size) {
260296
return ba_leak_aligned_alloc(alignment, size);
261297
}
262298

299+
#ifdef _WIN32
300+
size_t _msize(void *ptr) {
301+
#else
263302
size_t malloc_usable_size(void *ptr) {
303+
#endif
264304
if (!was_called_from_umfPool && Proxy_pool) {
265305
was_called_from_umfPool = 1;
266306
size_t size = umfPoolMallocUsableSize(Proxy_pool, ptr);

src/proxy_lib/proxy_lib.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ EXPORTS
99
DllMain
1010
aligned_alloc
1111
calloc
12+
_free_dbg
1213
free
1314
malloc
14-
malloc_usable_size
15+
_msize
1516
realloc

test/test_installation.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,20 @@ def _create_match_list(self) -> List[str]:
6969
lib_ext_shared = "dylib"
7070
lib_prefix = "lib"
7171

72+
# Currently the proxy library uses and requires the scalable pool
73+
# The proxy library does not work in the Debug build on Windows yet.
74+
if ("scalable_pool" in self.pools) and not (platform.system() == "Windows" and self.build_type == "debug"):
75+
is_umf_proxy = True
76+
else:
77+
is_umf_proxy = False
78+
7279
bin = []
73-
if platform.system() == "Windows" and self.shared_library:
80+
if platform.system() == "Windows" and (self.shared_library or is_umf_proxy):
7481
bin.append("bin")
75-
bin.append("bin/umf.dll")
82+
if self.shared_library:
83+
bin.append("bin/umf.dll")
84+
if is_umf_proxy:
85+
bin.append("bin/umf_proxy.dll")
7686

7787
include_dir = Path(self.workspace_dir, "include")
7888
include = [
@@ -94,9 +104,7 @@ def _create_match_list(self) -> List[str]:
94104
lib.append(f"lib/{lib_prefix}{pool}.{lib_ext_static}")
95105
lib_ext = lib_ext_shared if self.shared_library else lib_ext_static
96106
lib.append(f"lib/{lib_prefix}umf.{lib_ext}")
97-
if platform.system() != "Windows" and (
98-
"jemalloc_pool" in self.pools or "scalable_pool" in self.pools
99-
):
107+
if is_umf_proxy:
100108
lib.append(f"lib/{lib_prefix}umf_proxy.{lib_ext_shared}")
101109
lib.append(f"lib/{lib_prefix}umf_utils.{lib_ext_static}")
102110

0 commit comments

Comments
 (0)