Skip to content

Commit

Permalink
Avoid usage of freed memory (#428)
Browse files Browse the repository at this point in the history
Ensure there can not be a free between the unwinding and aggregation steps
  • Loading branch information
r1viollet authored Oct 8, 2024
1 parent 09d59ab commit 1fa3fc8
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/ddprof_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ DDRes worker_update_stats(DDProfWorkerContext &worker_context,
}

DDRes ddprof_unwind_sample(DDProfContext &ctx, perf_event_sample *sample,
int watcher_pos) {
int watcher_pos, bool &inconsistent_pid_state) {
inconsistent_pid_state = false;
struct UnwindState *us = ctx.worker_ctx.us;
PerfWatcher *watcher = &ctx.watchers[watcher_pos];

Expand Down Expand Up @@ -229,7 +230,7 @@ DDRes ddprof_unwind_sample(DDProfContext &ctx, perf_event_sample *sample,
if (us->_dwfl_wrapper && us->_dwfl_wrapper->_inconsistent) {
// Loaded modules were inconsistent, assume we should flush everything.
LG_WRN("(Inconsistent DWFL/DSOs)%d - Free associated objects", us->pid);
DDRES_CHECK_FWD(worker_pid_free(ctx, us->pid));
inconsistent_pid_state = true;
}
return res;
}
Expand Down Expand Up @@ -413,7 +414,9 @@ DDRes ddprof_pr_sample(DDProfContext &ctx, perf_event_sample *sample,
}

auto ticks0 = TscClock::cycles_now();
DDRes const res = ddprof_unwind_sample(ctx, sample, watcher_pos);
bool inconsistent_pid_state = false;
DDRes const res =
ddprof_unwind_sample(ctx, sample, watcher_pos, inconsistent_pid_state);
auto unwind_ticks = TscClock::cycles_now();
ddprof_stats_add(STATS_UNWIND_AVG_TIME, unwind_ticks - ticks0, nullptr);

Expand Down Expand Up @@ -454,7 +457,10 @@ DDRes ddprof_pr_sample(DDProfContext &ctx, perf_event_sample *sample,
ctx.worker_ctx.symbolizer, pprof));
}
}

// We need to free the PID only after any aggregation operations
if (inconsistent_pid_state) {
DDRES_CHECK_FWD(worker_pid_free(ctx, ctx.worker_ctx.us->pid));
}
ddprof_stats_add(STATS_AGGREGATION_AVG_TIME,
TscClock::cycles_now() - unwind_ticks, nullptr);

Expand Down

0 comments on commit 1fa3fc8

Please sign in to comment.