Skip to content

UCM: gate brk/sbrk hooks on configure-time availability#11198

Merged
tvegas1 merged 4 commits intoopenucx:masterfrom
GenericRikka:brk-sbrk-allocator
Mar 16, 2026
Merged

UCM: gate brk/sbrk hooks on configure-time availability#11198
tvegas1 merged 4 commits intoopenucx:masterfrom
GenericRikka:brk-sbrk-allocator

Conversation

@GenericRikka
Copy link
Contributor

What?

This PR makes UCM’s brk()/sbrk() hooking and related tests conditional on the functions being present on the target platform. Specifically:

  • Detect brk() and sbrk() at configure time.
  • Only build/enable the brk()/sbrk() replace layer when both are available.
  • Skip brk()/sbrk() mmap event hook registration and tests when they are not available.
  • Provide ENOSYS stubs for internal brk()/sbrk() wrappers when disabled.

Why?

Some supported/non-Linux platforms do not provide brk()/sbrk() (or do not expose them in a way compatible with UCM’s hooking approach). UCM currently builds and registers these hooks unconditionally, which can:

  • break the build due to missing symbols/headers, or
  • lead to undefined behavior if hook registration or wrappers are compiled in despite the functions not being usable.

How?

  • Add AC_CHECK_FUNCS([brk sbrk]) in the UCM configure logic and define HAVE_BRK_SBRK only when both are present.
  • Guard hook registration, event firing/tests, and the replace-layer definitions with #ifdef HAVE_BRK_SBRK.
  • When disabled, provide ENOSYS stub implementations so code paths remain well-defined and compilation remains clean.

Linux behavior is unchanged because brk()/sbrk() are detected as available and the existing code remains enabled.

Testing

  • Tested: Linux (build)
  • Tested: FreeBSD (build assumed via downstream port; runtime pending)

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.

Detect brk/sbrk during configure and:
- build/enable the brk/sbrk replace layer only when available
- skip brk/sbrk mmap event tests and hook registration otherwise
- provide ENOSYS stubs for the internal brk/sbrk wrappers when disabled

This keeps Linux behavior unchanged and avoids enabling unsupported hooks on
non-Linux targets.

Tested: Linux (build)
Tested: FreeBSD (build assumed via downstream port; runtime pending)
@yosefe
Copy link
Contributor

yosefe commented Feb 20, 2026

@GenericRikka thank you for your contribution!
Contributions to OpenUCX require a signed individual or organization contributor license agreement (CLA).
Can you pls review and sign a copy of our CLA (https://openucx.org/license) and email the signed CLA to admin at ucfconsortium.org?

@GenericRikka
Copy link
Contributor Author

@GenericRikka thank you for your contribution! Contributions to OpenUCX require a signed individual or organization contributor license agreement (CLA). Can you pls review and sign a copy of our CLA (https://openucx.org/license) and email the signed CLA to admin at ucfconsortium.org?

Thanks for pointing that out! I sent the email and am currently awaiting confirmation. Will report back once I got the backsigned version / confirmation!

Meanwhile I’ll continue iterating on the review feedback.

@GenericRikka
Copy link
Contributor Author

I should have addressed all review feedback. Thanks again for the effort and if anything else needs adjustment, I am happy to iterate / adjust further!

@GenericRikka GenericRikka force-pushed the brk-sbrk-allocator branch 2 times, most recently from 28a302e to f8f3451 Compare February 20, 2026 23:38
AC_CHECK_FUNCS([brk sbrk])

AS_IF([test "x$ac_cv_func_brk" = xyes], [
AC_DEFINE([HAVE_BRK], [1], [Define if brk() is available])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's not needed since AC_CHECK_FUNCS already defines HAVE_xx macros

config/m4/ucm.m4 Outdated
[], [mmap_hooks_happy=no], [#include <sys/syscall.h>])

AS_IF([test "x$ac_cv_func_brk" = xyes],
[AC_CHECK_DECLS([SYS_brk], [], [mmap_hooks_happy=no],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why needed? we can check SYS_brk regardless of brk function, as before

config/m4/ucm.m4 Outdated
Comment on lines +88 to +89
AS_IF([test "x$ac_cv_func_brk" != xyes -o "x$ac_cv_func_sbrk" != xyes],
[bistro_hooks_happy=no])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need it during compile time

Comment on lines +561 to +567
#if !defined(HAVE_BRK)
events &= ~UCM_EVENT_BRK;
#endif
#if !defined(HAVE_SBRK)
events &= ~UCM_EVENT_SBRK;
#endif
return events;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need this
usually users would request VM_UNMAP. And if they reuest BRK/SBRK explicitly and they are not available, why not fail?


ucs_status_t ucm_malloc_install(int events)
{
#ifdef HAVE_SBRK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need ifdef here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need the #ifdef HAVE_SBRK here: ucm_malloc_sbrk itself is only built when HAVE_SBRK is defined, so referencing it unconditionally would fail to compile/link on platforms without sbrk().
If it is preferred to reduce #ifdefs, I can instead provide a stub ucm_malloc_sbrk implementation under !HAVE_SBRK that returns UCS_ERR_UNSUPPORTED, and then keep the handler definition unconditional.


#endif

#ifdef HAVE_BRK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need this ifdef? i don't see we access brk() in the disabled code here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real sbrk implementation in this file depends on ucm_orig_brk() and ucm_get_current_brk(), so some HAVE_BRK guarding is still needed.
That said, I agree the current guard is broader than ideal: the sbrk path should really be conditioned on HAVE_SBRK && HAVE_BRK, while the brk path should stay under HAVE_BRK alone. I will tighten that so the preprocessor logic matches the actual dependency more precisely.


void *ucm_get_current_brk()
{
#ifdef HAVE_BRK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why needed? maybe check SYS_brk instead inside ucm_brk_syscall?


void *ucm_brk_syscall(void *addr)
{
#ifdef HAVE_BRK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we heck HAVE_DECL_SYS_BRK?

configure.ac Outdated
AM_CONDITIONAL([DOCS_ONLY], [false])
m4_include([config/m4/compiler.m4])
m4_include([config/m4/sysdep.m4])
UCX_CHECK_BRK_SBRK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we do it directly in ucm.m4 ?

@GenericRikka
Copy link
Contributor Author

Hi,

Thanks for the detailed review and comments on the PR — I appreciate the time.

I’m currently working through the feedback items and will push an updated revision once I’ve addressed them and re-tested. It may take me a little time due to a tight schedule this week, but it’s on my active list and I’ll follow up with concrete updates.

@GenericRikka
Copy link
Contributor Author

I have updated the differential according to your review feedback. Thank you again for taking the time to provide such thorough comments.

Where I reacted with 👍, I fully agreed with the suggestion and updated the code accordingly. In the few places where I thought additional context would be useful, I left inline comments to explain my reasoning and adjusted the patch accordingly.

@GenericRikka GenericRikka requested a review from yosefe March 9, 2026 21:50
@GenericRikka
Copy link
Contributor Author

GenericRikka commented Mar 10, 2026

Update on the CLA process: I received the backsigned copy of my CLA form from the UCF admins.

Copy link
Contributor

@tvegas1 tvegas1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last comment

@yosefe
Copy link
Contributor

yosefe commented Mar 14, 2026

Update on the CLA process: I received the backsigned copy of my CLA form from the UCF admins.

The CLA was accepted, thanks!

@GenericRikka
Copy link
Contributor Author

It looks like the Azure UCX snapshot check may be stuck rather than failing normally.
It started on 2026-03-13 21:51 and is still running more than 16 hours later, whereas these checks usually finish for me within ~2–3 hours.
Since the PR is already approved, could someone with the needed permissions cancel and re-run the pipeline?

@tvegas1 tvegas1 enabled auto-merge (squash) March 16, 2026 14:29
@tvegas1 tvegas1 merged commit c698e43 into openucx:master Mar 16, 2026
152 checks passed
@GenericRikka
Copy link
Contributor Author

Thanks for merging and the good review feedback!

jeynmann pushed a commit to jeynmann/ucx that referenced this pull request Mar 17, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants