From 88e9cbf09681c60a742d3d1141f1bb7cfbcc40d6 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 24 Jan 2022 17:05:03 +0100 Subject: [PATCH] fix(profiling/heap): check correctly maximum number of tracked allocs There's a possibility that the number of tracked allocs overflow the maximum if, e.g., 65k allocs are stored in the alloc tracker and then 1k allocs are stored in the freezer: when the freezer is merged back in the alloc tracker, then it will overflow. --- ddtrace/profiling/collector/_memalloc_heap.c | 6 ++++-- .../profiler-heap-max-alloc-bugfix-646423cdd7f7811f.yaml | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/profiler-heap-max-alloc-bugfix-646423cdd7f7811f.yaml diff --git a/ddtrace/profiling/collector/_memalloc_heap.c b/ddtrace/profiling/collector/_memalloc_heap.c index 414bb1e8cca..f11adaf50b5 100644 --- a/ddtrace/profiling/collector/_memalloc_heap.c +++ b/ddtrace/profiling/collector/_memalloc_heap.c @@ -174,8 +174,10 @@ memalloc_heap_track(uint16_t max_nframe, void* ptr, size_t size) if (global_heap_tracker.allocated_memory < global_heap_tracker.current_sample_size) return false; - /* Cannot add more sample */ - if (global_heap_tracker.allocs.count >= TRACEBACK_ARRAY_MAX_COUNT) + /* Check if we can add more samples: the sum of the freezer + alloc tracker + cannot be greater than what the alloc tracker can handle: when the alloc + tracker is thawed, all the allocs in the freezer will be moved there!*/ + if ((global_heap_tracker.freezer.allocs.count + global_heap_tracker.allocs.count) >= TRACEBACK_ARRAY_MAX_COUNT) return false; traceback_t* tb = memalloc_get_traceback(max_nframe, ptr, global_heap_tracker.allocated_memory); diff --git a/releasenotes/notes/profiler-heap-max-alloc-bugfix-646423cdd7f7811f.yaml b/releasenotes/notes/profiler-heap-max-alloc-bugfix-646423cdd7f7811f.yaml new file mode 100644 index 00000000000..d7353a096d8 --- /dev/null +++ b/releasenotes/notes/profiler-heap-max-alloc-bugfix-646423cdd7f7811f.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fix a possible bug in the heap memory profiler that could trigger an + overflow when too many allocations were being tracked.