← Back to Pull requests

UCM: check sbrk failure in ucm_fire_mmap_events_internal #12519

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Dec 01, 2026 05:00 UTC
Author

What?


Check the result status of sbrk in ucm_fire_mmap_events_internal and avoid unmapping if it didn't succeed.

Why?


This PR fixes a rare but persistent failure we've seen in Legion CI for many years. We finally root caused and diagnosed the issue as an unchecked sbrk call in UCX. We are working around the failure in Legion, but meanwhile, the proper fix in UCX is ~2 lines and seems worth taking.

For posterity, the Legion CI workaround: https://gitlab.com/StanfordLegion/legion/-/merge_requests/2144

How?


The fix is about 2 lines of actual changes and hopefully self-evident enough, but a detailed root cause diagnosis is also available below.

Note: this bug was triaged and fixed by Claude Opus 5.5, but the fix is so simple that I think it's fairly self-evident even as a non-UCX expert. Claude's analysis also includes a reproducer for posterity.

Claude's analysis

UCM: Unchecked sbrk(sbrk_size) failure in ucm_fire_mmap_events_internal() unmaps live [heap] page and causes SIGSEGV in malloc

Summary

In src/ucm/mmap/install.c, ucm_fire_mmap_events_internal() tests UCM_EVENT_SBRK by calling sbrk(sbrk_size) and then sbrk(-sbrk_size), ignoring the return value of sbrk(sbrk_size).

If sbrk(sbrk_size) fails with ENOMEM (returns (void )-1 / MAP_FAILED)—for example because an existing VMA sits immediately adjacent to the end of [heap]—the unconditional sbrk(-sbrk_size) call still succeeds and shrinks the program break by one page below its original value, silently unmapping the top page of glibc's live [heap]. Subsequent malloc() calls dereferencing chunks or main_arena.top in that unmapped page crash with SIGSEGV.

Affected Versions

Confirmed in v1.14.1 and v1.20.1, and the bug is still present on master (and v1.23.0-rc4) at src/ucm/mmap/install.c lines 217–222.

Root Cause

In src/ucm/mmap/install.c (ucm_fire_mmap_events_internal()):

    if (exclusive && !RUNNING_ON_VALGRIND) {
#if HAVE_SBRK
        sbrk_size = ucm_get_page_size();
        if (events & (UCM_EVENT_BRK|UCM_EVENT_VM_MAPPED|UCM_EVENT_VM_UNMAPPED)) {
            p = ucm_get_current_brk();
            UCM_FIRE_EVENT(events, UCM_EVENT_BRK|UCM_EVENT_VM_MAPPED, data,
                           ucm_brk_checked(UCS_PTR_BYTE_OFFSET(p, sbrk_size)));
            UCM_FIRE_EVENT(events, UCM_EVENT_BRK|UCM_EVENT_VM_UNMAPPED, data,
                           ucm_brk_checked(p));
        }
        if (events & (UCM_EVENT_SBRK|UCM_EVENT_VM_MAPPED|UCM_EVENT_VM_UNMAPPED)) {
            UCM_FIRE_EVENT(events, UCM_EVENT_SBRK|UCM_EVENT_VM_MAPPED,
                           data, (void)sbrk(sbrk_size));
            UCM_FIRE_EVENT(events, UCM_EVENT_SBRK|UCM_EVENT_VM_UNMAPPED,
                           data, (void)sbrk(-sbrk_size));
        }
#endif
    }

  • When brk(p + sbrk_size) fails, the subsequent brk(p) is a no-op because the break is still p.

  • When sbrk(sbrk_size) fails ((void )-1), the break did not advance, yet sbrk(-sbrk_size) unconditionally moves the break backward by sbrk_size (4096 bytes), unmapping a live page owned by glibc's allocator.
  • Real-world trigger

    We hit this in CI (~1 in 20,000–30,000 process launches) in Legion/Regent (terra), which runs LuaJIT before dlopen()-ing a shared library linked against UCX. LuaJIT's mcode allocator maps 64 KB r-xp JIT code pages with an mmap(hint) within ±2 GB of the PIE binary (overlapping [heap]'s address range). Whenever [heap] ends immediately adjacent to a LuaJIT mcode VMA at the time libucs.so.0 initializes (ucs_init() → ucm_mmap_init() → ucm_mmap_install(events, 1)), sbrk(+4096) fails with ENOMEM, sbrk(-4096) unmaps the top 4 KB page of [heap], and the process segfaults inside glibc malloc (_int_malloc, unlink_chunk, or tcache pop).

    Minimal Reproducer

    The following standalone C program (requiring only libucs.so.0) reproduces the bug 100% deterministically on Linux x86_64:

    #define _GNU_SOURCE
    #include <dlfcn.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/mman.h>
    #include <unistd.h>
    
    int main(void) {
        /* 1. Initialize glibc's [heap] so sbrk(0) points to the top of an active heap. */
        void *init = malloc(1024);
        if (!init) {
            perror("malloc");
            return 1;
        }
    
        /* 2. Place a VMA immediately at the current program break so sbrk(+pagesize)
         *    fails with ENOMEM (simulating e.g. LuaJIT mcode or another mmap landing
         *    adjacent to [heap]). */
        size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
        void *brk_before = sbrk(0);
        void *blocker = mmap(brk_before, pagesize, PROT_READ | PROT_WRITE,
                             MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
                             -1, 0);
        if (blocker == MAP_FAILED) {
            perror("mmap(MAP_FIXED_NOREPLACE)");
            return 1;
        }
    
        /* 3. Load libucs.so.0, which triggers ucs_init() -> ucm_mmap_init() ->
         *    ucm_fire_mmap_events_internal(). Because sbrk(+pagesize) fails and its
         *    return value is ignored, the subsequent sbrk(-pagesize) shrinks [heap]
         *    below brk_before, unmapping the live top page of glibc's heap. */
        void *h = dlopen("libucs.so.0", RTLD_NOW);
        if (!h) {
            fprintf(stderr, "dlopen(libucs.so.0) failed: %s\n", dlerror());
            return 1;
        }
    
        void *brk_after = sbrk(0);
        fprintf(stderr, "brk_before = %p\n", brk_before);
        fprintf(stderr, "blocker    = %p\n", blocker);
        fprintf(stderr, "brk_after  = %p (delta = %td bytes)\n",
                brk_after, (char *)brk_after - (char *)brk_before);
    
        /* 4. Allocate from the remaining [heap] until glibc reaches the unmapped
         *    top page -> SIGSEGV inside malloc. */
        for (int i = 0; i < 64; i++) {
            void *p = malloc(4096);
            if (!p) {
                perror("malloc");
                return 1;
            }
            memset(p, 0x42, 4096);
        }
    
        fprintf(stderr, "OK (no crash)\n");
        free(init);
        return 0;
    }
    

    Build & Run

    gcc -O2 -g ucx_sbrk_repro.c -o ucx_sbrk_repro -ldl
    LD_LIBRARY_PATH=$UCX_ROOT/lib UCX_HANDLE_ERRORS=none UCX_MEM_LOG_LEVEL=diag ./ucx_sbrk_repro
    

    Observed Output (UCX 1.20.1, Ubuntu 22.04 / glibc 2.35, x86_64)

    [1795236027.542918] [4a8d2ee3708a:12:0]          install.c:143  UCX  DIAG brk(addr=0x5643b7623000) failed: Cannot allocate memory
    [1795236027.542962] [4a8d2ee3708a:12:0]          install.c:274  UCX  DIAG missing installed memory events: VM_MAPPED, VM_UNMAPPED
    brk_before = 0x5643b7622000
    blocker    = 0x5643b7622000
    brk_after  = 0x5643b7621000 (delta = -4096 bytes)
    Segmentation fault (core dumped)
    

    In gdb:

    brk_before = 0x55555557a000
    blocker    = 0x55555557a000
    brk_after  = 0x555555579000 (delta = -4096 bytes)
    
    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7e308b0 in _int_malloc (av=av@entry=0x7ffff7fa6c80 <main_arena>, bytes=bytes@entry=4096) at ./malloc/malloc.c:4382
    FAULT_ADDR = 0x555555579c58   # inside the unmapped [0x555555579000, 0x55555557a000) page
    

    With UCX_MEM_MMAP_HOOK_MODE=reloc or UCX_MEM_EVENTS=no:

    brk_before = 0x5626c383d000
    blocker    = 0x5626c383d000
    brk_after  = 0x5626c383d000 (delta = 0 bytes)
    OK (no crash)
    

    Suggested Fix

    Check the return value of sbrk(sbrk_size) before calling sbrk(-sbrk_size) (matching the if (p != MAP_FAILED) guard already used for mmap() a few lines below):

    --- a/src/ucm/mmap/install.c
    +++ b/src/ucm/mmap/install.c
    @@ -216,10 +216,12 @@ ucm_fire_mmap_events_internal(int events, ucm_mmap_test_events_data_t *data,
             }
             if (events & (UCM_EVENT_SBRK|UCM_EVENT_VM_MAPPED|UCM_EVENT_VM_UNMAPPED)) {
                 UCM_FIRE_EVENT(events, UCM_EVENT_SBRK|UCM_EVENT_VM_MAPPED,
    -                           data, (void)sbrk(sbrk_size));
    -            UCM_FIRE_EVENT(events, UCM_EVENT_SBRK|UCM_EVENT_VM_UNMAPPED,
    -                           data, (void)sbrk(-sbrk_size));
    +                           data, p = sbrk(sbrk_size));
    +            if (p != MAP_FAILED) {
    +                UCM_FIRE_EVENT(events, UCM_EVENT_SBRK|UCM_EVENT_VM_UNMAPPED,
    +                               data, (void)sbrk(-sbrk_size));
    +            }
             }
     #endif
         } else {
    

    We verified this patch against UCX 1.20.1: with the patch applied, brk_after == brk_before (delta = 0 bytes) and both the standalone C reproducer and our full terra/Regent reproducers exit cleanly with status 0.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: tvegas1 Review duration: 331m
    All Vouch review confidence checks passed
    Residual risk 0.33 = Change Risk (0.57) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #12519"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 32.5% · Safe to merge
    32.5%
    Repository