Skip to content

Commit

Permalink
Fix code cache segment race condition
Browse files Browse the repository at this point in the history
The first two fields of a TR::CodeCache data structures are
`_warmCodeAlloc` and `_coldCodeAlloc` and they used by a
third party (e.g. the JVM) to compute the amount of free
space in the code cache. During the TR::CodeCache initialization
we populate the _warmCodeAlloc and _coldCodeAlloc fields and then
write a pointer to the TR::CodeCache structure at the beginning
of the segment that provides memory for the code cache itself.
Just before writing this pointer we need to issue a write memory
barrier to ensure that the values of _warmCodeAlloc and _coldCodeAlloc
are seen by another thread at the time when the pointer to
the TR::CodeCache is written at the begining of the memory segment.

Signed-off-by: Marius <[email protected]>
  • Loading branch information
mpirvu committed Oct 3, 2023
1 parent 366c6e8 commit 24685fe
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions compiler/runtime/OMRCodeCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "AtomicSupport.hpp"
#include "env/FrontEnd.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
Expand Down Expand Up @@ -309,9 +310,6 @@ OMR::CodeCache::initialize(TR::CodeCacheManager *manager,
_sizeOfLargestFreeWarmBlock = 0;
_lastAllocatedBlock = NULL; // MP

omrthread_jit_write_protect_disable();
*((TR::CodeCache **)(_segment->segmentBase())) = self(); // Write a pointer to this cache at the beginning of the segment
omrthread_jit_write_protect_enable();
_warmCodeAlloc = _segment->segmentBase() + sizeof(this);

_warmCodeAlloc = (uint8_t *)align((size_t)_warmCodeAlloc, config.codeCacheAlignment());
Expand Down Expand Up @@ -417,6 +415,13 @@ OMR::CodeCache::initialize(TR::CodeCacheManager *manager,
size_t spaceLost = (_warmCodeAlloc - _segment->segmentBase()) + (_segment->segmentTop() - _trampolineBase);
_manager->increaseCurrTotalUsedInBytes(spaceLost);

// Now that we have initialized the code cache, (including _warmCodeAlloc and _coldCodeAlloc)
// write a pointer to this cache at the beginning of the segment
VM_AtomicSupport::writeBarrier();
omrthread_jit_write_protect_disable();
*((TR::CodeCache **)(_segment->segmentBase())) = self();
omrthread_jit_write_protect_enable();

return true;
}

Expand Down

0 comments on commit 24685fe

Please sign in to comment.