Skip to content

Commit d98d498

Browse files
authored
Merge pull request #20611 from tajila/jfr2
Prevent writes to free'd JFR buffer
2 parents 77f153d + ee35ed5 commit d98d498

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

runtime/vm/JFRConstantPoolTypes.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ VM_JFRConstantPoolTypes::addThreadEntry(J9VMThread *vmThread)
774774
entry->vmThread = vmThread;
775775
_buildResult = OK;
776776
omrthread_t osThread = vmThread->osThread;
777+
j9object_t threadObject = vmThread->threadObject;
777778

778779
entry = (ThreadEntry *) hashTableFind(_threadTable, entry);
779780
if (NULL != entry) {
@@ -784,19 +785,22 @@ VM_JFRConstantPoolTypes::addThreadEntry(J9VMThread *vmThread)
784785
}
785786

786787
entry->osTID = ((J9AbstractThread*)osThread)->tid;
787-
entry->javaTID = J9VMJAVALANGTHREAD_TID(_currentThread, vmThread->threadObject);
788+
if (NULL != threadObject) {
789+
entry->javaTID = J9VMJAVALANGTHREAD_TID(_currentThread, threadObject);
788790

789-
entry->javaThreadName = copyStringToJ9UTF8WithMemAlloc(_currentThread, J9VMJAVALANGTHREAD_NAME(_currentThread, vmThread->threadObject), J9_STR_NONE, "", 0, NULL, 0);
791+
entry->javaThreadName = copyStringToJ9UTF8WithMemAlloc(_currentThread, J9VMJAVALANGTHREAD_NAME(_currentThread, threadObject), J9_STR_NONE, "", 0, NULL, 0);
790792

791-
/* TODO is this always true? */
792-
entry->osThreadName = entry->javaThreadName;
793-
if (isResultNotOKay()) goto done;
793+
if (isResultNotOKay()) goto done;
794794
#if JAVA_SPEC_VERSION >= 19
795-
entry->threadGroupIndex = addThreadGroupEntry(J9VMJAVALANGTHREADFIELDHOLDER_GROUP(_currentThread, (J9VMJAVALANGTHREAD_HOLDER(_currentThread, vmThread->threadObject))));
795+
entry->threadGroupIndex = addThreadGroupEntry(J9VMJAVALANGTHREADFIELDHOLDER_GROUP(_currentThread, (J9VMJAVALANGTHREAD_HOLDER(_currentThread, threadObject))));
796796
#else /* JAVA_SPEC_VERSION >= 19 */
797-
entry->threadGroupIndex = addThreadGroupEntry(J9VMJAVALANGTHREAD_GROUP(_currentThread, vmThread->threadObject));
797+
entry->threadGroupIndex = addThreadGroupEntry(J9VMJAVALANGTHREAD_GROUP(_currentThread, threadObject));
798798
#endif /* JAVA_SPEC_VERSION >= 19 */
799-
if (isResultNotOKay()) goto done;
799+
if (isResultNotOKay()) goto done;
800+
}
801+
802+
/* TODO is this always true? */
803+
entry->osThreadName = entry->javaThreadName;
800804

801805
entry->index = _threadCount;
802806
_threadCount++;

runtime/vm/jfr.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void initializeEventFields(J9VMThread *currentThread, J9JFREvent *jfrEven
5858
static int J9THREAD_PROC jfrSamplingThreadProc(void *entryArg);
5959
static void jfrExecutionSampleCallback(J9VMThread *currentThread, IDATA handlerKey, void *userData);
6060
static void jfrThreadCPULoadCallback(J9VMThread *currentThread, IDATA handlerKey, void *userData);
61-
61+
static bool areJFRBuffersReadyForWrite(J9VMThread *currentThread);
6262
/**
6363
* Calculate the size in bytes of a JFR event.
6464
*
@@ -129,6 +129,22 @@ jfrBufferNextDo(J9JFRBufferWalkState *walkState)
129129
return (J9JFREvent*)next;
130130
}
131131

132+
static bool
133+
areJFRBuffersReadyForWrite(J9VMThread *currentThread)
134+
{
135+
bool result = true;
136+
J9JavaVM *vm = currentThread->javaVM;
137+
138+
if ((!vm->jfrState.isStarted)
139+
|| (NULL == currentThread->jfrBuffer.bufferStart)
140+
|| (NULL == vm->jfrBuffer.bufferCurrent)
141+
) {
142+
result = false;
143+
}
144+
145+
return result;
146+
}
147+
132148
/**
133149
* Write out the contents of the global JFR buffer.
134150
*
@@ -148,16 +164,18 @@ writeOutGlobalBuffer(J9VMThread *currentThread, bool finalWrite)
148164
j9tty_printf(PORTLIB, "\n!!! writing global buffer %p of size %p\n", currentThread, vm->jfrBuffer.bufferSize - vm->jfrBuffer.bufferRemaining);
149165
#endif /* defined(DEBUG) */
150166

151-
VM_JFRWriter::flushJFRDataToFile(currentThread, finalWrite);
167+
if (areJFRBuffersReadyForWrite(currentThread)) {
168+
VM_JFRWriter::flushJFRDataToFile(currentThread, finalWrite);
152169

153-
/* Reset the buffer */
154-
vm->jfrBuffer.bufferRemaining = vm->jfrBuffer.bufferSize;
155-
vm->jfrBuffer.bufferCurrent = vm->jfrBuffer.bufferStart;
170+
/* Reset the buffer */
171+
vm->jfrBuffer.bufferRemaining = vm->jfrBuffer.bufferSize;
172+
vm->jfrBuffer.bufferCurrent = vm->jfrBuffer.bufferStart;
156173

157174

158175
#if defined(DEBUG)
159-
memset(vm->jfrBuffer.bufferStart, 0, J9JFR_GLOBAL_BUFFER_SIZE);
176+
memset(vm->jfrBuffer.bufferStart, 0, J9JFR_GLOBAL_BUFFER_SIZE);
160177
#endif /* defined(DEBUG) */
178+
}
161179

162180
return true;
163181
}
@@ -180,7 +198,7 @@ flushBufferToGlobal(J9VMThread *currentThread, J9VMThread *flushThread)
180198
UDATA bufferSize = flushThread->jfrBuffer.bufferCurrent - flushThread->jfrBuffer.bufferStart;
181199
bool success = true;
182200

183-
if (NULL == flushThread->jfrBuffer.bufferStart) {
201+
if (areJFRBuffersReadyForWrite(currentThread)) {
184202
goto done;
185203
}
186204

@@ -284,7 +302,9 @@ reserveBuffer(J9VMThread *currentThread, UDATA size)
284302
Assert_VM_true(((currentThread)->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS)
285303
|| ((J9_XACCESS_EXCLUSIVE == vm->exclusiveAccessState) || (J9_XACCESS_EXCLUSIVE == vm->safePointState)));
286304

287-
305+
if (areJFRBuffersReadyForWrite(currentThread)) {
306+
goto done;
307+
}
288308

289309
/* If the event is larger than the buffer, fail without attemptiong to flush */
290310
if (size <= currentThread->jfrBuffer.bufferSize) {

0 commit comments

Comments
 (0)