Skip to content

Commit b7bbc1d

Browse files
committed
Move patching code to LinuxImageHeapProvider from CEntryPointSnippets.
1 parent 76f0980 commit b7bbc1d

File tree

2 files changed

+55
-63
lines changed

2 files changed

+55
-63
lines changed

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/linux/LinuxImageHeapProvider.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import com.oracle.svm.core.util.UnsignedUtils;
8383
import com.oracle.svm.core.util.VMError;
8484

85+
import jdk.graal.compiler.nodes.NamedLocationIdentity;
8586
import jdk.graal.compiler.nodes.PauseNode;
8687
import jdk.graal.compiler.word.Word;
8788

@@ -122,6 +123,14 @@ public class LinuxImageHeapProvider extends AbstractImageHeapProvider {
122123
*/
123124
static final CGlobalData<WordPointer> CACHED_LAYERED_IMAGE_HEAP_ADDRESS_SPACE_SIZE = CGlobalDataFactory.createWord();
124125

126+
private static final class ImageHeapPatchingState {
127+
static final Word UNINITIALIZED = Word.zero();
128+
static final Word IN_PROGRESS = Word.unsigned(1);
129+
static final Word SUCCESSFUL = Word.unsigned(2);
130+
}
131+
132+
private static final CGlobalData<Word> IMAGE_HEAP_PATCHING_STATE = CGlobalDataFactory.createWord();
133+
125134
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
126135
private static UnsignedWord getLayeredImageHeapAddressSpaceSize() {
127136
// check if value is cached
@@ -167,6 +176,8 @@ protected int initializeLayeredImage(Pointer firstHeapStart, Pointer selfReserve
167176
int result = -1;
168177
UnsignedWord remainingSize = initialRemainingSize;
169178

179+
patchLayeredImageHeap();
180+
170181
int layerCount = 0;
171182
Pointer currentSection = ImageLayerSection.getInitialLayerSection().get();
172183
Pointer currentHeapStart = firstHeapStart;
@@ -213,6 +224,47 @@ protected int initializeLayeredImage(Pointer firstHeapStart, Pointer selfReserve
213224
return result;
214225
}
215226

227+
@Uninterruptible(reason = "Thread state not yet set up.")
228+
public static void patchLayeredImageHeap() {
229+
Word heapPatchStateAddr = IMAGE_HEAP_PATCHING_STATE.get();
230+
boolean firstIsolate = heapPatchStateAddr.logicCompareAndSwapWord(0, ImageHeapPatchingState.UNINITIALIZED, ImageHeapPatchingState.IN_PROGRESS, NamedLocationIdentity.OFF_HEAP_LOCATION);
231+
232+
if (!firstIsolate) {
233+
// spin-wait for first isolate
234+
Word state = heapPatchStateAddr.readWordVolatile(0, LocationIdentity.ANY_LOCATION);
235+
while (state.equal(ImageHeapPatchingState.IN_PROGRESS)) {
236+
PauseNode.pause();
237+
state = heapPatchStateAddr.readWordVolatile(0, LocationIdentity.ANY_LOCATION);
238+
}
239+
240+
/* Patching has already been successfully completed, nothing needs to be done. */
241+
return;
242+
}
243+
244+
Pointer currentSection = ImageLayerSection.getInitialLayerSection().get();
245+
Word heapBegin = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_BEGIN));
246+
247+
Word patches = ImageLayerSection.getHeapRelativeRelocationsStart().get();
248+
int endOffset = Integer.BYTES + (patches.readInt(0) * Integer.BYTES);
249+
250+
int referenceSize = ConfigurationValues.getObjectLayout().getReferenceSize();
251+
int offset = Integer.BYTES;
252+
while (offset < endOffset) {
253+
int heapOffset = patches.readInt(offset);
254+
int referenceEncoding = patches.readInt(offset + Integer.BYTES);
255+
256+
if (referenceSize == 4) {
257+
heapBegin.writeInt(heapOffset, referenceEncoding);
258+
} else {
259+
heapBegin.writeLong(heapOffset, referenceEncoding);
260+
}
261+
262+
offset += 2 * Integer.BYTES;
263+
}
264+
265+
heapPatchStateAddr.writeWordVolatile(0, ImageHeapPatchingState.SUCCESSFUL);
266+
}
267+
216268
@Override
217269
@Uninterruptible(reason = "Called during isolate initialization.")
218270
public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, WordPointer basePointer, WordPointer endPointer) {
@@ -262,7 +314,9 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
262314
basePointer.write(heapBase);
263315
Pointer imageHeapStart = heapBase.add(imageHeapOffsetInAddressSpace);
264316
remainingSize = remainingSize.subtract(imageHeapOffsetInAddressSpace);
265-
if (!ImageLayerBuildingSupport.buildingImageLayer()) {
317+
if (ImageLayerBuildingSupport.buildingImageLayer()) {
318+
return initializeLayeredImage(imageHeapStart, selfReservedHeapBase, remainingSize, endPointer);
319+
} else {
266320
int result = initializeImageHeap(imageHeapStart, remainingSize, endPointer,
267321
CACHED_IMAGE_FD.get(), CACHED_IMAGE_HEAP_OFFSET.get(), CACHED_IMAGE_HEAP_RELOCATIONS.get(), MAGIC.get(),
268322
IMAGE_HEAP_BEGIN.get(), IMAGE_HEAP_END.get(),
@@ -272,8 +326,6 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
272326
freeImageHeap(selfReservedHeapBase);
273327
}
274328
return result;
275-
} else {
276-
return initializeLayeredImage(imageHeapStart, selfReservedHeapBase, remainingSize, endPointer);
277329
}
278330
}
279331

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CEntryPointSnippets.java

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static com.oracle.svm.core.graal.nodes.WriteCurrentVMThreadNode.writeCurrentVMThread;
3030
import static com.oracle.svm.core.graal.nodes.WriteHeapBaseNode.writeCurrentVMHeapBase;
3131
import static com.oracle.svm.core.heap.RestrictHeapAccess.Access.NO_ALLOCATION;
32-
import static com.oracle.svm.core.imagelayer.ImageLayerSection.SectionEntries.HEAP_BEGIN;
3332
import static com.oracle.svm.core.util.VMError.shouldNotReachHereUnexpectedInput;
3433
import static jdk.graal.compiler.core.common.spi.ForeignCallDescriptor.CallSideEffect.HAS_SIDE_EFFECT;
3534

@@ -73,7 +72,6 @@
7372
import com.oracle.svm.core.c.locale.LocaleSupport;
7473
import com.oracle.svm.core.code.CodeInfoTable;
7574
import com.oracle.svm.core.code.ImageCodeInfo;
76-
import com.oracle.svm.core.config.ConfigurationValues;
7775
import com.oracle.svm.core.container.Container;
7876
import com.oracle.svm.core.graal.meta.SubstrateForeignCallsProvider;
7977
import com.oracle.svm.core.graal.nodes.CEntryPointEnterNode;
@@ -85,8 +83,6 @@
8583
import com.oracle.svm.core.heap.ReferenceHandlerThread;
8684
import com.oracle.svm.core.heap.ReferenceInternals;
8785
import com.oracle.svm.core.heap.RestrictHeapAccess;
88-
import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport;
89-
import com.oracle.svm.core.imagelayer.ImageLayerSection;
9086
import com.oracle.svm.core.jdk.PlatformNativeLibrarySupport;
9187
import com.oracle.svm.core.jdk.RuntimeSupport;
9288
import com.oracle.svm.core.layeredimagesingleton.MultiLayeredImageSingleton;
@@ -117,7 +113,6 @@
117113
import jdk.graal.compiler.graph.Node;
118114
import jdk.graal.compiler.graph.Node.ConstantNodeParameter;
119115
import jdk.graal.compiler.graph.Node.NodeIntrinsic;
120-
import jdk.graal.compiler.nodes.NamedLocationIdentity;
121116
import jdk.graal.compiler.nodes.PauseNode;
122117
import jdk.graal.compiler.nodes.extended.ForeignCallNode;
123118
import jdk.graal.compiler.nodes.spi.LoweringTool;
@@ -260,57 +255,6 @@ public static int createIsolateSnippet(CEntryPointCreateIsolateParameters parame
260255
return runtimeCallInitializeIsolate(INITIALIZE_ISOLATE, parameters);
261256
}
262257

263-
private static final CGlobalData<Word> IMAGE_HEAP_PATCHING_STATE = CGlobalDataFactory.createWord();
264-
265-
private static final class ImageHeapPatchingState {
266-
static final Word UNINITIALIZED = Word.zero();
267-
static final Word IN_PROGRESS = Word.unsigned(1);
268-
static final Word SUCCESSFUL = Word.unsigned(2);
269-
}
270-
271-
@Uninterruptible(reason = "Thread state not yet set up.")
272-
private static void layeredPatchHeapRelativeRelocations() {
273-
Word heapPatchStateAddr = IMAGE_HEAP_PATCHING_STATE.get();
274-
boolean firstIsolate = heapPatchStateAddr.logicCompareAndSwapWord(0, ImageHeapPatchingState.UNINITIALIZED, ImageHeapPatchingState.IN_PROGRESS, NamedLocationIdentity.OFF_HEAP_LOCATION);
275-
276-
if (!firstIsolate) {
277-
// spin-wait for first isolate
278-
Word state = heapPatchStateAddr.readWordVolatile(0, LocationIdentity.ANY_LOCATION);
279-
while (state.equal(ImageHeapPatchingState.IN_PROGRESS)) {
280-
PauseNode.pause();
281-
state = heapPatchStateAddr.readWordVolatile(0, LocationIdentity.ANY_LOCATION);
282-
}
283-
284-
/*
285-
* Patching has already been successfully completed, nothing needs to be done.
286-
*/
287-
return;
288-
}
289-
290-
Pointer currentSection = ImageLayerSection.getInitialLayerSection().get();
291-
Word heapBegin = currentSection.readWord(ImageLayerSection.getEntryOffset(HEAP_BEGIN));
292-
293-
Word patches = ImageLayerSection.getHeapRelativeRelocationsStart().get();
294-
int endOffset = Integer.BYTES + (patches.readInt(0) * Integer.BYTES);
295-
296-
int referenceSize = ConfigurationValues.getObjectLayout().getReferenceSize();
297-
int offset = Integer.BYTES;
298-
while (offset < endOffset) {
299-
int heapOffset = patches.readInt(offset);
300-
int referenceEncoding = patches.readInt(offset + Integer.BYTES);
301-
302-
if (referenceSize == 4) {
303-
heapBegin.writeInt(heapOffset, referenceEncoding);
304-
} else {
305-
heapBegin.writeLong(heapOffset, referenceEncoding);
306-
}
307-
308-
offset += 2 * Integer.BYTES;
309-
}
310-
311-
heapPatchStateAddr.writeWordVolatile(0, ImageHeapPatchingState.SUCCESSFUL);
312-
}
313-
314258
/**
315259
* After parsing the isolate arguments in
316260
* {@link IsolateArgumentParser#parse(CEntryPointCreateIsolateParameters, IsolateArguments)} the
@@ -332,10 +276,6 @@ private static int createIsolate(CEntryPointCreateIsolateParameters providedPara
332276
return CEntryPointErrors.PAGE_SIZE_CHECK_FAILED;
333277
}
334278

335-
if (ImageLayerBuildingSupport.buildingImageLayer()) {
336-
layeredPatchHeapRelativeRelocations();
337-
}
338-
339279
LocaleSupport.initialize();
340280

341281
CEntryPointCreateIsolateParameters parameters = providedParameters;

0 commit comments

Comments
 (0)