You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Stalker]Unable to allocate code slab near 0x7c4f333000 with max_distance=2138779647 #793
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.
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.
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.
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.
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.
@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
@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
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):
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);
returnctx.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>
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 codeStalker.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.
Activity
DiamondHunters commentedon Apr 12, 2024
I think the root cause of the issue lies in the
ctxaddress selected bymmap. When the system allocates in a crowded address space, this significantly reduces the likelihood of successfully allocating near the ctx address.near_addressat:frida-gum/gum/backend-arm64/gumstalker-arm64.c
Line 2318 in 8000 ffda5fc
ctxat:frida-gum/gum/backend-arm64/gumstalker-arm64.c
Line 2155 in ffda5fc
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
mmapa large block of memory and then allocate new memory within it using remap / fixed mmap, involving considerable modifications. Let me give it a try.DiamondHunters commentedon Apr 16, 2024
This is a modification I tested that is available but not complete.
It can temporarily solve the problem, but I think that parts of the memory allocation mechanism should be refactored.
v-p-b commentedon Apr 20, 2026
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 )
DiamondHunters commentedon Apr 21, 2026
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.
v-p-b commentedon May 8, 2026
@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:
Would you agree with such design?
DiamondHunters commentedon May 8, 2026
Yes, 100%. I really like this approach.
mnalmahmud commentedon Aug 7, 2026
Stalker: NULL deref in
gum_data_slab_newwhengum_memory_allocate_nearfailsgum_memory_allocate_nearreturnsNULLwhen it cannot satisfy the address spec, butall three slab constructors in the Stalker backends pass the result straight into their
*_slab_inithelper, which immediately stores through it. The process dies withSIGSEGVat address0instead 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):gum_slab_initdoesslab->data = (guint8 *) slab + header_size;— an unconditionalstore at offset 0.
gum_code_slab_new(:6152) andgum_slow_slab_new(:6170) have the same shape, so thesame fault is reachable through them.
gum/backend-arm/gumstalker-arm.candgum/backend-arm64/gumstalker-arm64.ccarry the identical pattern.For reference, the contract being violated, from
gum/backend-posix/gummemory-posix.c:118:Observed crash
Android 16 (API 36) and Android 15 (API 35),
x86_64emulator, frida-gumjs devkit17.17.0, statically linked. Both crashed at a byte-identical PC from a byte-identical
binary.
The registers pin it down without ambiguity:
and
rbx = 0xcd000is exactlyGUM_ALIGN_SIZE (GUM_DATA_SLAB_SIZE_DYNAMIC, 4096)(
4 MiB / 5), theslab_sizethat 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:
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_slabwithmax_distance = G_MAXINT32 - code_slab_size_dynamic - slab_size, so it needs ~820 KiBfree 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_newfunctions returnNULLon a failed allocation and propagate it, or failloudly 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.
v-p-b commentedon Oct 5, 2026
#1168 seems related