Skip to content

Commit c698e43

Browse files
authored
UCM: gate brk/sbrk hooks on configure-time availability (#11198)
Some platforms do not provide brk()/sbrk(). UCM currently builds and registers hooks for these symbols unconditionally, which can break the build or lead to undefined behavior when the functions are absent.
1 parent 49a0d4c commit c698e43

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Peter Rudenko <peterr@mellanox.com>
9191
Peter-Jan Gootzen <pgootzen@nvidia.com>
9292
Qiang Yu <Qiang.Yu@amd.com>
9393
Raul Akhmetshin <rakhmetshin@nvidia.com>
94+
Rikka Göring <rikka.goering@outlook.de>
9495
Robert Dietrich <rdietrich@nvidia.com>
9596
Rohit Zambre <rzambre@uci.edu>
9697
Roie Danino <rdanino@nvidia.com>

config/m4/ucm.m4

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#
99
# Memory allocator selection
1010
#
11+
AC_CHECK_FUNCS([brk sbrk])
12+
1113
AC_ARG_WITH([allocator],
1214
[AS_HELP_STRING([--with-allocator=NAME],
1315
[Build UCX with predefined memory allocator. The supported values are:
@@ -18,13 +20,18 @@ AC_ARG_WITH([allocator],
1820
case ${with_allocator} in
1921
ptmalloc286)
2022
AC_MSG_NOTICE(Memory allocator is ptmalloc-2.8.6 version)
21-
AC_DEFINE([HAVE_UCM_PTMALLOC286], 1, [Use ptmalloc-2.8.6 version])
23+
AC_DEFINE([HAVE_UCM_PTMALLOC286], [1], [Use ptmalloc-2.8.6 version])
2224
HAVE_UCM_PTMALLOC286=yes
25+
AS_IF([test "x$ac_cv_func_sbrk" != xyes], [
26+
AC_DEFINE([HAVE_MORECORE], [0],
27+
[Disable MORECORE in ptmalloc (no sbrk available)])
28+
])
2329
;;
2430
*)
2531
AC_MSG_ERROR(Cannot continue. Unsupported memory allocator name
2632
in --with-allocator=[$with_allocator])
2733
;;
34+
2835
esac
2936

3037
AM_CONDITIONAL([HAVE_UCM_PTMALLOC286],[test "x$HAVE_UCM_PTMALLOC286" = "xyes"])

src/ucm/malloc/malloc_hook.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,29 @@ ucs_status_t ucm_malloc_install(int events)
836836
#endif
837837
}
838838

839+
#if !HAVE_BRK
840+
if (events & UCM_EVENT_BRK) {
841+
ucm_debug("brk event requested, but brk() is not available");
842+
events &= ~UCM_EVENT_BRK;
843+
}
844+
#endif
845+
846+
#if !HAVE_SBRK
847+
if (events & UCM_EVENT_SBRK) {
848+
ucm_debug("sbrk event requested, but sbrk() is not available");
849+
events &= ~UCM_EVENT_SBRK;
850+
}
851+
#endif
852+
853+
#if HAVE_SBRK
839854
if (!(ucm_malloc_hook_state.install_state & UCM_MALLOC_INSTALLED_SBRK_EVH)) {
840855
ucm_debug("installing malloc-sbrk event handler");
841856
ucm_event_handler_add(&sbrk_handler);
842857
ucm_malloc_hook_state.install_state |= UCM_MALLOC_INSTALLED_SBRK_EVH;
843858
}
859+
#else
860+
(void)sbrk_handler;
861+
#endif
844862

845863
/* When running on valgrind, don't even try malloc hooks.
846864
* We want to release original blocks to silence the leak check, so we must

src/ucm/mmap/install.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ static ucm_mmap_func_t ucm_mmap_funcs[] = {
8686
#endif
8787
{ UCM_MMAP_RELOC_ENTRY(shmat), UCM_EVENT_SHMAT, UCM_EVENT_NONE},
8888
{ UCM_MMAP_RELOC_ENTRY(shmdt), UCM_EVENT_SHMDT, UCM_EVENT_SHMAT},
89+
#if HAVE_SBRK
8990
{ UCM_MMAP_RELOC_ENTRY(sbrk), UCM_EVENT_SBRK, UCM_EVENT_NONE},
91+
#endif
92+
#if HAVE_BRK
9093
{ UCM_MMAP_RELOC_ENTRY(brk), UCM_EVENT_BRK, UCM_EVENT_NONE},
94+
#endif
9195
{ UCM_MMAP_RELOC_ENTRY(madvise), UCM_EVENT_MADVISE, UCM_EVENT_NONE},
9296
{ {NULL, NULL, NULL}, UCM_EVENT_NONE}
9397
};
@@ -138,10 +142,12 @@ static void ucm_mmap_event_test_callback(ucm_event_type_t event_type,
138142
/* Call brk() and check return value, to avoid compile error of unused result */
139143
static void ucm_brk_checked(void *addr)
140144
{
145+
#if HAVE_BRK
141146
int ret = brk(addr);
142147
if ((ret != 0) && (addr != NULL)) {
143148
ucm_diag("brk(addr=%p) failed: %m", addr);
144149
}
150+
#endif
145151
}
146152

147153
/* Fire events with pre/post action. The problem is in call sequence: we
@@ -199,6 +205,7 @@ ucm_fire_mmap_events_internal(int events, ucm_mmap_test_events_data_t *data,
199205
}
200206

201207
if (exclusive && !RUNNING_ON_VALGRIND) {
208+
#if HAVE_SBRK
202209
sbrk_size = ucm_get_page_size();
203210
if (events & (UCM_EVENT_BRK|UCM_EVENT_VM_MAPPED|UCM_EVENT_VM_UNMAPPED)) {
204211
p = ucm_get_current_brk();
@@ -213,6 +220,7 @@ ucm_fire_mmap_events_internal(int events, ucm_mmap_test_events_data_t *data,
213220
UCM_FIRE_EVENT(events, UCM_EVENT_SBRK|UCM_EVENT_VM_UNMAPPED,
214221
data, (void)sbrk(-sbrk_size));
215222
}
223+
#endif
216224
} else {
217225
/* To avoid side effects on other threads and valgrind heap corruption,
218226
* pass invalid parameters. We assume that if the natives events are

src/ucm/util/replace.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ UCM_DEFINE_REPLACE_FUNC(mremap, void*, MAP_FAILED, void*, size_t, size_t, int,
5555
#endif
5656
UCM_DEFINE_REPLACE_FUNC(shmat, void*, MAP_FAILED, int, const void*, int)
5757
UCM_DEFINE_REPLACE_FUNC(shmdt, int, -1, const void*)
58+
#if HAVE_SBRK
5859
UCM_DEFINE_REPLACE_FUNC(sbrk, void*, MAP_FAILED, intptr_t)
60+
#endif
61+
#if HAVE_BRK
5962
UCM_DEFINE_REPLACE_FUNC(brk, int, -1, void*)
63+
#endif
6064
UCM_DEFINE_REPLACE_FUNC(madvise, int, -1, void*, size_t, int)
6165

6266
UCM_DEFINE_SELECT_FUNC(mmap, void*, SYS_mmap, void*, size_t, int, int, int,
@@ -122,7 +126,9 @@ int ucm_orig_shmdt(const void *shmaddr)
122126

123127
#endif
124128

129+
#if HAVE_BRK
125130
_UCM_DEFINE_DLSYM_FUNC(brk, ucm_orig_dlsym_brk, ucm_override_brk, int, void*)
131+
#endif
126132

127133
int ucm_orig_brk(void *addr)
128134
{
@@ -141,8 +147,10 @@ int ucm_orig_brk(void *addr)
141147
}
142148
}
143149

150+
#if HAVE_SBRK
144151
_UCM_DEFINE_DLSYM_FUNC(sbrk, ucm_orig_dlsym_sbrk, ucm_override_sbrk, void*,
145152
intptr_t)
153+
#endif
146154

147155
void *ucm_orig_sbrk(intptr_t increment)
148156
{

src/ucm/util/sys.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,16 @@ char *ucm_concat_path(char *buffer, size_t max, const char *dir, const char *fil
377377

378378
void *ucm_brk_syscall(void *addr)
379379
{
380+
#if HAVE_DECL_SYS_BRK
380381
/* Return type is equivalent to full pointer size */
381382
UCS_STATIC_ASSERT(sizeof(syscall(0)) == sizeof(void*));
382383

383384
return (void*)syscall(SYS_brk, addr);
385+
#else
386+
(void)addr;
387+
errno = ENOSYS;
388+
return NULL;
389+
#endif
384390
}
385391

386392
pid_t ucm_get_tid()

0 commit comments

Comments
 (0)