Skip to content

Commit 1788096

Browse files
committed
Fix crash on J9/Zing: add NULL checks before freeing buffers
Initialize check before freeing _calltrace_buffer and _remote_frame_pool to prevent crashes on first start when pointers contain uninitialized values. Also move remote frame pool allocation outside conditional block to ensure it's reset on every profiler start for clean state.
1 parent 3b409aa commit 1788096

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

.claude/settings.local.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"WebFetch(domain:mvnrepository.com)",
4141
"WebFetch(domain:central.sonatype.com)",
4242
"Bash(jfr-shell:*)",
43-
"Bash(java -cp:*)"
43+
"Bash(java -cp:*)",
44+
"Bash(git commit:*)"
4445
],
4546
"deny": [],
4647
"ask": []

ddprof-lib/src/main/cpp/profiler.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,22 +1302,28 @@ Error Profiler::start(Arguments &args, bool reset) {
13021302
size_t nelem = _max_stack_depth + RESERVED_FRAMES;
13031303

13041304
for (int i = 0; i < CONCURRENCY_LEVEL; i++) {
1305-
free(_calltrace_buffer[i]);
1305+
if (_calltrace_buffer[i] != NULL) {
1306+
free(_calltrace_buffer[i]);
1307+
}
13061308
_calltrace_buffer[i] = (CallTraceBuffer*)calloc(nelem, sizeof(CallTraceBuffer));
13071309
if (_calltrace_buffer[i] == NULL) {
13081310
_max_stack_depth = 0;
13091311
return Error("Not enough memory to allocate stack trace buffers (try "
13101312
"smaller jstackdepth)");
13111313
}
1314+
}
1315+
}
13121316

1313-
// Allocate pre-allocated pool for RemoteFrameInfo (signal-safe storage)
1317+
// Allocate remote frame pool on every start to ensure clean state
1318+
for (int i = 0; i < CONCURRENCY_LEVEL; i++) {
1319+
if (_remote_frame_pool[i] != NULL) {
13141320
free(_remote_frame_pool[i]);
1315-
_remote_frame_pool[i] = (RemoteFrameInfo*)calloc(MAX_NATIVE_FRAMES, sizeof(RemoteFrameInfo));
1316-
if (_remote_frame_pool[i] == NULL) {
1317-
return Error("Not enough memory to allocate remote frame pool");
1318-
}
1319-
_remote_frame_count[i] = 0; // Reset allocation counter
13201321
}
1322+
_remote_frame_pool[i] = (RemoteFrameInfo*)calloc(MAX_NATIVE_FRAMES, sizeof(RemoteFrameInfo));
1323+
if (_remote_frame_pool[i] == NULL) {
1324+
return Error("Not enough memory to allocate remote frame pool");
1325+
}
1326+
_remote_frame_count[i] = 0; // Reset allocation counter
13211327
}
13221328

13231329
_features = args._features;

0 commit comments

Comments
 (0)