Skip to content

Commit

Permalink
Avoid segfaults in j9vmem_testOverlappingSegments
Browse files Browse the repository at this point in the history
Since the keepCycles array is zero-initialized, if the test is aborted
in the very first cycle (i == 0) the cleanup procedure at the end cannot
rely on the fact that keepCycles[j] >= i implies that segment j must
be freed. Before these changes, such a scenario would cause the test
to segfault during cleanup.

Signed-off-by: Christian Despres <[email protected]>
  • Loading branch information
cjjdespres committed May 17, 2024
1 parent 64d4ef4 commit 9d8a2f6
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion runtime/tests/port/j9vmemTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -3415,6 +3415,9 @@ j9vmem_testOverlappingSegments(struct J9PortLibrary *portLibrary)
if (NULL == memPtr) {
outputComment(PORTLIB, "Failed to get memory. Error: %s.\n", strerror(errno));
outputComment(PORTLIB, "Ignoring memory allocation failure(%d of %d loops finished).\n", i, CYCLES);
if (0 == i) {
keepCycles[0] = -1; /* This segment would otherwise look like it needed to be freed during cleanup */
}
goto exit;
}
/* Determine how long to keep the segment */
Expand Down Expand Up @@ -3457,7 +3460,8 @@ j9vmem_testOverlappingSegments(struct J9PortLibrary *portLibrary)

/* Free remaining segments */
freed = 0;
for (j = 0; j < CYCLES; j++) {
i = (CYCLES == i) ? i : i + 1;
for (j = 0; j < i; j++) {
if (keepCycles[j] >= i) {
I_32 rc = j9vmem_free_memory(vmemID[j].address, vmemParams[j].byteAmount, &vmemID[j]);
if (0 == rc) {
Expand Down

0 comments on commit 9d8a2f6

Please sign in to comment.