Skip to content

Commit ce79b10

Browse files
committed
Build the self monitor reference with term_from_ref_ticks
Monitoring self installs no alias, so the returned reference is always a short ref. The path reserved TERM_BOXED_SHORT_REF_SIZE but built the term with term_from_ref_data, whose allocation size depends on the embedded pid. At runtime this is safe (process_id is INVALID_PROCESS_ID, so term_from_ref_data returns a short ref of exactly that size), but the static allocation check cannot prove the pid stays invalid and sizes it as a process ref, reporting a 3-vs-2 term over-allocation. Call term_from_ref_ticks directly: it allocates exactly TERM_BOXED_SHORT_REF_SIZE, matching the reservation, and is identical to what term_from_ref_data does for an invalid pid. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 288bd8a commit ce79b10

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/libAtomVM/nifs.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5069,11 +5069,14 @@ static term nif_erlang_monitor(Context *ctx, int argc, term argv[])
50695069
// with {alias, _}, no alias either -- sends to the returned reference are dropped,
50705070
// unalias/1 and demonitor(Ref, [info]) return false.
50715071
if (UNLIKELY(local_process_id == ctx->process_id)) {
5072-
RefData ref_data = { .ref_ticks = globalcontext_get_ref_ticks(ctx->global), .process_id = INVALID_PROCESS_ID };
50735072
if (UNLIKELY(memory_ensure_free_opt(ctx, TERM_BOXED_SHORT_REF_SIZE, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
50745073
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
50755074
}
5076-
return term_from_ref_data(&ref_data, &ctx->heap);
5075+
// No alias is installed, so this is always a short ref. term_from_ref_ticks allocates
5076+
// exactly TERM_BOXED_SHORT_REF_SIZE, matching the reservation; term_from_ref_data sizes
5077+
// by pid, which the allocation checker can't prove stays short here.
5078+
uint64_t ref_ticks = globalcontext_get_ref_ticks(ctx->global);
5079+
return term_from_ref_ticks(ref_ticks, &ctx->heap);
50775080
}
50785081

50795082
target = globalcontext_get_process_lock(ctx->global, local_process_id);

0 commit comments

Comments
 (0)