8000 [Stalker]Unable to allocate code slab near 0x7c4f333000 with max_distance=2138779647 · Issue #793 · frida/frida-gum · GitHub
[go: up one dir, main page]

Skip to content
7FFF

[Stalker]Unable to allocate code slab near 0x7c4f333000 with max_distance=2138779647 #793

Open

Description

@DiamondHunters

When I tried to perform tracing by stalker like itrace, I got this error.

It works fine on a single function, but when I use it to trace a heavily obfuscated algorithm, the exception occurs.

The issue seems to be that the stalker is unable to allocate memory within a ±2GB address space.

However, my code would only cause the rewritten code size to expand by several times.

I checked the maps file, and many libraries were mapped near that address, which might be the reason why the stalker was unable to allocate space.

Is there a way to control the code generation location of the stalker to make it start at a less crowded memory position?

map:

maps.txt

Activity

  1. DiamondHunters commented on Apr 12, 2024

    @DiamondHunters
    Author

    I think the root cause of the issue lies in the ctx address selected by mmap. When the system allocates in a crowded address space, this significantly reduces the likelihood of successfully allocating near the ctx address.

    near_address at:

    spec->near_address = ctx;

    ctx at:

    ctx = (GumExecCtx *) base;

    A possible solution is requesting a large block of memory to ensure ample free address space, then unmap that area and use its address to request memory. This approach might introduce some race condition issues. A more comprehensive solution is to mmap a large block of memory and then allocate new memory within it using remap / fixed mmap, involving considerable modifications. Let me give it a try.

  2. DiamondHunters commented on Apr 16, 2024

    @DiamondHunters
    Author

    This is a modification I tested that is available but not complete.

    gum_exec_ctx_new (GumStalker * stalker,
                      GumThreadId thread_id,
                      GumStalkerTransformer * transformer,
                      GumEventSink * sink)
    {
      GumExecCtx * ctx;
      guint8 * base;
      GumCodeSlab * code_slab;
      GumSlowSlab * slow_slab;
      GumDataSlab * data_slab;
      base = gum_memory_allocate (NULL, INT32_MAX, stalker->page_size, GUM_PAGE_RW);
      g_warning("Alloc big memory at base %p:",base);
      gum_memory_free(base,INT32_MAX);
      base = gum_memory_allocate (base + INT32_MAX / 2, stalker->ctx_size, stalker->page_size,
          stalker->is_rwx_supported ? GUM_PAGE_RWX : GUM_PAGE_RW);
      g_warning("target base: %p, real base %p:",base + INT32_MAX / 2,base);
      ctx = (GumExecCtx *) base;
    

    It can temporarily solve the problem, but I think that parts of the memory allocation mechanism should be refactored.

  3. v-p-b commented on Apr 20, 2026

    @v-p-b
    SponsorContributor

    I'm also hitting this and started to work on a PR with @DiamondHunters approach but it's still probabilistic. I wonder if this is the optimal approach, or would it be possible to use far jumps on 64-bit platforms in the injected assembly stubs (and provide a larger address spec for the "near" allocations)? If the latter approach is preferred I'd be happy to work on a PR as well, but I'd need some pointers about where these jumps are relevant.

    (paging @oleavr )

  4. DiamondHunters commented on Apr 21, 2026

    @DiamondHunters
    Author

    I'm also hitting this and started to work on a PR with @DiamondHunters approach but it's still probabilistic. I wonder if this is the optimal approach, or would it be possible to use far jumps on 64-bit platforms in the injected assembly stubs (and provide a larger address spec for the "near" allocations)? If the latter approach is preferred I'd be happy to work on a PR as well, but I'd need some pointers about where these jumps are relevant.

    (paging @oleavr )

    A more robust and standard solution is to reserve a sufficiently large virtual address range with mmap() and then remap or commit portions of it on demand. This avoids the race where the region is released and subsequently claimed by unrelated allocations before it can be reused.

    Another approach, which does not rely on OS support for remapping, is to reserve one large virtual address range up front and manage sub-allocations within it manually. On systems with page-based virtual memory, the range can then be populated lazily on a page-by-page basis as pages are actually accessed, rather than backing the entire range up front.

    My understanding is that avoiding far jumps as the default strategy is an intentional design choice, likely due to a combination of performance, code-size, and register-management tradeoffs. Frida does use indirect branches when necessary, but for hot paths it seems to prefer keeping helpers reachable and duplicating them when needed. If so, then improving near address-space reservation and slab placement is probably the most relevant direction for this particular failure mode, even if it is not the only bottleneck in Stalker overall.

  5. v-p-b commented on May 8, 2026

    @v-p-b
    SponsorContributor

    @DiamondHunters Thanks, this really seems the "right" way to handle these allocations. I think an implementation would need a new (OS independent) pair of APIs along the lines of:

    void* large_buf = allocate_large(..., &large_alloc_details); // We allocate memory and reserve extra space for near jumps
    void* near_buf = allocate_near(..., large_alloc_details); // Near allocations reference the previous large allocation so returned pointer is within reserved space
    

    Would you agree with such design?

  6. DiamondHunters commented on May 8, 2026

    @DiamondHunters
    Author

    @DiamondHunters Thanks, this really seems the "right" way to handle these allocations. I think an implementation would need a new (OS independent) pair of APIs along the lines of:

    void* large_buf = allocate_large(..., &large_alloc_details); // We allocate memory and reserve extra space for near jumps
    void* near_buf = allocate_near(..., large_alloc_details); // Near allocations reference the previous large allocation so returned pointer is within reserved space
    

    Would you agree with such design?

    Yes, 100%. I really like this approach.

  7. mnalmahmud commented on Aug 7, 2026

    @mnalmahmud
    Contributor

    Stalker: NULL deref in gum_data_slab_new when gum_memory_allocate_near fails

    gum_memory_allocate_near returns NULL when it cannot satisfy the address spec, but
    all three slab constructors in the Stalker backends pass the result straight into their
    *_slab_init helper, which immediately stores through it. The process dies with
    SIGSEGV at address 0 instead of surfacing the allocation failure.

    This is the same underlying address-space exhaustion as #793, but on the data slab
    path, where there is no diagnostic at all — just a crash.

    Where

    gum/backend-x86/gumstalker-x86.c (main, and identically in 17.17.0):

    static GumDataSlab *
    gum_data_slab_new (GumExecCtx * ctx)
    {
      ...
      slab = gum_memory_allocate_near (&spec, slab_size, stalker->page_size,
          GUM_PAGE_RW);          // may return NULL
    
      gum_data_slab_init (slab, slab_size);   // -> gum_slab_init -> slab->data = ...
    
      return slab;
    }

    gum_slab_init does slab->data = (guint8 *) slab + header_size; — an unconditional
    store at offset 0.

    gum_code_slab_new (:6152) and gum_slow_slab_new (:6170) have the same shape, so the
    same fault is reachable through them. gum/backend-arm/gumstalker-arm.c and
    gum/backend-arm64/gumstalker-arm64.c carry the identical pattern.

    For reference, the contract being violated, from
    gum/backend-posix/gummemory-posix.c:118:

      ctx.result = NULL;
      gum_enumerate_free_ranges (gum_try_alloc_in_range_if_near_enough, &ctx);
      return ctx.result;      // NULL when no range is near enough

    Observed crash

    Android 16 (API 36) and Android 15 (API 35), x86_64 emulator, frida-gumjs devkit
    17.17.0, statically linked. Both crashed at a byte-identical PC from a byte-identical
    binary.

    signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
    Cause: null pointer dereference
        rax 0000000000000000  rbx 00000000000cd000  rcx 0000000000000018
        #00 pc 892aed  gum_slab_init                 gumstalker-x86.c:6289
                       gum_data_slab_init            gumstalker-x86.c:6262
                       gum_data_slab_new             gumstalker-x86.c:6244
        #01 pc 8926e0  gum_exec_block_new            gumstalker-x86.c:4054
        #02 pc 8923dd  gum_exec_ctx_build_block      gumstalker-x86.c:2843
        #03 pc 88eb82  gum_exec_ctx_obtain_block_for gumstalker-x86.c:2790
        #04 pc 8917ab  gum_exec_ctx_switch_block     gumstalker-x86.c:2671
        #05 pc 0333f2  <anonymous:7798fc43a000>
    

    The registers pin it down without ambiguity:

    892ae4: call gum_memory_allocate_near
    892ae9: lea  0x18(%rax),%rcx     ; rcx = 0x18 = sizeof (GumDataSlab), so rax == 0
    892aed: mov  %rcx,(%rax)         ; faults

    and rbx = 0xcd000 is exactly GUM_ALIGN_SIZE (GUM_DATA_SLAB_SIZE_DYNAMIC, 4096)
    (4 MiB / 5), the slab_size that could not be placed.

    Reproducing

    Not reliably — it depends on the process map at the moment the second data slab is
    needed, so it is intermittent on the same image and same binary. What triggers it here
    is stalking a full ART runtime:

    Stalker.exclude(Process.getModuleByName('runner'));
    Stalker.follow({ events: { call: true }, onCallSummary (summary) { /* ... */ } });
    // run a workload that reaches a lot of distinct code
    Stalker.unfollow();
    Stalker.flush();

    Across four CI runs of the same suite it fired in roughly half of them, on API 35 and 36
    alike. The spec is near_address = ctx->code_slab with
    max_distance = G_MAXINT32 - code_slab_size_dynamic - slab_size, so it needs ~820 KiB
    free within ±2 GiB of the code slab; the emulator maps enough near there that this
    sometimes cannot be met.

    Suggested fix

    Minimally, make the failure explicit rather than a wild store — e.g. have the three
    *_slab_new functions return NULL on a failed allocation and propagate it, or fail
    loudly with the same kind of message #793 gets. Either would turn this from an
    unattributable SIGSEGV into something a user can act on.

    The broader question in #793 — whether Stalker can be given room to place slabs when the
    ±2 GiB window is crowded — is the real cure, and this issue is just the case where
    missing it is silent.

  8. v-p-b commented on Oct 5, 2026

    @v-p-b
    SponsorContributor

    #1168 seems related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0