Skip to content

Commit

Permalink
Fix ADD_CALL_CHAIN() placement to follow GC_store_debug_info_inner call
Browse files Browse the repository at this point in the history
(a cherry-pick of commit 70163b7 from 'release-8_0')

Issue #613 (bdwgc).

This might be important for the case of malloc redirection as
backtrace() need to be called without the allocator lock held.

* dbg_mlc.c (store_debug_info, GC_debug_generic_malloc_inner,
GC_debug_generic_malloc_inner_ignore_off_page): Call ADD_CALL_CHAIN()
right after GC_store_debug_info_inner() call (instead of before it).
* gcj_mlc.c (GC_debug_gcj_malloc): Likewise.
* dbg_mlc.c (GC_debug_generic_malloc_inner,
GC_debug_generic_malloc_inner_ignore_off_page): Define base local
variable.
* gcj_mlc.c (GC_debug_gcj_malloc): Likewise.
  • Loading branch information
ivmai committed Sep 5, 2024
1 parent 5624506 commit 36a48ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
22 changes: 13 additions & 9 deletions dbg_mlc.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ static void *store_debug_info(void *p, size_t lb,
LOCK();
if (!GC_debugging_started)
GC_start_debugging_inner();
ADD_CALL_CHAIN(p, ra);
result = GC_store_debug_info_inner(p, (word)lb, s, i);
ADD_CALL_CHAIN(p, ra);
UNLOCK();
return result;
}
Expand Down Expand Up @@ -552,37 +552,41 @@ GC_API GC_ATTR_MALLOC void * GC_CALL
/* We assume we already hold the GC lock. */
GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
{
void * result = GC_generic_malloc_inner(
void *base = GC_generic_malloc_inner(
SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
void *result;

if (result == 0) {
if (NULL == base) {
GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
(unsigned long) lb);
return(0);
}
if (!GC_debugging_started) {
GC_start_debugging_inner();
}
ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
result = GC_store_debug_info_inner(base, (word)lb, "INTERNAL", 0);
ADD_CALL_CHAIN(base, GC_RETURN_ADDR);
return result;
}

GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
int k)
{
void * result = GC_generic_malloc_inner_ignore_off_page(
void *base = GC_generic_malloc_inner_ignore_off_page(
SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
void *result;

if (result == 0) {
if (NULL == base) {
GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
(unsigned long) lb);
return(0);
}
if (!GC_debugging_started) {
GC_start_debugging_inner();
}
ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
result = GC_store_debug_info_inner(base, (word)lb, "INTERNAL", 0);
ADD_CALL_CHAIN(base, GC_RETURN_ADDR);
return result;
}
#endif /* DBG_HDRS_ALL */

Expand Down
14 changes: 7 additions & 7 deletions gcj_mlc.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,28 +209,28 @@ static void maybe_finalize(void)
GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_gcj_malloc(size_t lb,
void * ptr_to_struct_containing_descr, GC_EXTRA_PARAMS)
{
void * result;
void *base, *result;
DCL_LOCK_STATE;

/* We're careful to avoid extra calls, which could */
/* confuse the backtrace. */
LOCK();
maybe_finalize();
result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES),
GC_gcj_debug_kind);
if (result == 0) {
base = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES),
GC_gcj_debug_kind);
if (NULL == base) {
GC_oom_func oom_fn = GC_oom_fn;
UNLOCK();
GC_err_printf("GC_debug_gcj_malloc(%lu, %p) returning NULL (%s:%d)\n",
(unsigned long)lb, ptr_to_struct_containing_descr, s, i);
return((*oom_fn)(lb));
}
*((void **)((ptr_t)result + sizeof(oh))) = ptr_to_struct_containing_descr;
*((void **)((ptr_t)base + sizeof(oh))) = ptr_to_struct_containing_descr;
if (!GC_debugging_started) {
GC_start_debugging_inner();
}
ADD_CALL_CHAIN(result, ra);
result = GC_store_debug_info_inner(result, (word)lb, s, i);
result = GC_store_debug_info_inner(base, (word)lb, s, i);
ADD_CALL_CHAIN(base, ra);
UNLOCK();
GC_dirty(result);
REACHABLE_AFTER_DIRTY(ptr_to_struct_containing_descr);
Expand Down

0 comments on commit 36a48ec

Please sign in to comment.