Skip to content

Commit c3bedd6

Browse files
refactor code so it's closer to Hotspot implementation
1 parent cadcc00 commit c3bedd6

24 files changed

+704
-553
lines changed

substratevm/src/com.oracle.svm.core.foreign/src/com/oracle/svm/core/foreign/TrampolineSet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.graalvm.word.WordFactory;
3838

3939
import com.oracle.svm.core.heap.OutOfMemoryUtil;
40+
import com.oracle.svm.core.nmt.NmtCategory;
4041
import com.oracle.svm.core.os.VirtualMemoryProvider;
4142
import com.oracle.svm.core.util.VMError;
4243

@@ -112,7 +113,7 @@ private Pointer prepareTrampolines(PinnedObject mhsArray, PinnedObject stubsArra
112113
VirtualMemoryProvider memoryProvider = VirtualMemoryProvider.get();
113114
UnsignedWord pageSize = allocationSize();
114115
/* We request a specific alignment to guarantee correctness of getAllocationBase */
115-
Pointer page = memoryProvider.commit(WordFactory.nullPointer(), pageSize, VirtualMemoryProvider.Access.WRITE | VirtualMemoryProvider.Access.FUTURE_EXECUTE);
116+
Pointer page = memoryProvider.commit(WordFactory.nullPointer(), pageSize, VirtualMemoryProvider.Access.WRITE | VirtualMemoryProvider.Access.FUTURE_EXECUTE, NmtCategory.Internal);
116117
if (page.isNull()) {
117118
throw OutOfMemoryUtil.reportOutOfMemoryError(new OutOfMemoryError("Could not allocate memory for trampolines."));
118119
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixVirtualMemoryProvider.java

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
import com.oracle.svm.core.feature.InternalFeature;
5757
import com.oracle.svm.core.nmt.NativeMemoryTracking;
5858
import com.oracle.svm.core.nmt.NmtCategory;
59-
import com.oracle.svm.core.nmt.NmtVirtualMemoryData;
59+
import com.oracle.svm.core.nmt.NmtPreImageHeapData;
60+
import com.oracle.svm.core.nmt.NmtPreImageHeapDataAccess;
6061
import com.oracle.svm.core.os.VirtualMemoryProvider;
6162
import com.oracle.svm.core.posix.headers.Unistd;
6263
import com.oracle.svm.core.util.PointerUtils;
@@ -112,14 +113,8 @@ public UnsignedWord getGranularity() {
112113

113114
@Override
114115
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
115-
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable) {
116-
return reserve0(nbytes, alignment, executable, WordFactory.nullPointer(), NmtCategory.Internal);
117-
}
118-
119-
@Override
120-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
121-
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtVirtualMemoryData nmtData) {
122-
return reserve0(nbytes, alignment, executable, nmtData, NmtCategory.Internal);
116+
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtPreImageHeapData nmtData) {
117+
return reserve0(nbytes, alignment, executable, nmtData, NmtCategory.ImageHeap);
123118
}
124119

125120
@Override
@@ -129,7 +124,7 @@ public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean exec
129124
}
130125

131126
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
132-
private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtVirtualMemoryData nmtData, NmtCategory category) {
127+
private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtPreImageHeapData nmtData, NmtCategory category) {
133128
if (nbytes.equal(0)) {
134129
return WordFactory.nullPointer();
135130
}
@@ -151,8 +146,7 @@ private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean ex
151146
if (nmtData.isNull()) {
152147
NativeMemoryTracking.trackReserve(mappingBegin, mappingSize, category);
153148
} else {
154-
nmtData.setReserved(nmtData.getReserved().add(mappingSize));
155-
nmtData.setBaseAddr(mappingBegin);
149+
NmtPreImageHeapDataAccess.enqueueReserve(nmtData, mappingBegin, mappingSize, category);
156150
}
157151
return mappingBegin;
158152
}
@@ -173,24 +167,16 @@ private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean ex
173167
}
174168
if (nmtData.isNull()) {
175169
NativeMemoryTracking.trackReserve(begin, mappingSize.subtract(unmappedSize), category);
176-
// NativeMemoryTracking.trackReserve(begin, nbytes, category); // *** didn't fix problem.
177170
} else {
178-
nmtData.setReserved(nmtData.getReserved().add(mappingSize.subtract(unmappedSize)));
179-
nmtData.setBaseAddr(begin);
171+
NmtPreImageHeapDataAccess.enqueueReserve(nmtData, begin, mappingSize.subtract(unmappedSize), category);
180172
}
181173
return begin;
182174
}
183175

184176
@Override
185177
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
186-
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access) {
187-
return mapFile0(start, nbytes, fileHandle, offset, access, WordFactory.nullPointer(), NmtCategory.Internal);
188-
}
189-
190-
@Override
191-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
192-
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtVirtualMemoryData nmtData) {
193-
return mapFile0(start, nbytes, fileHandle, offset, access, nmtData, NmtCategory.Internal);
178+
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtPreImageHeapData nmtData) {
179+
return mapFile0(start, nbytes, fileHandle, offset, access, nmtData, NmtCategory.ImageHeap);
194180
}
195181

196182
@Override
@@ -200,7 +186,7 @@ public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHand
200186
}
201187

202188
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
203-
private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtVirtualMemoryData nmtData, NmtCategory category) {
189+
private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtPreImageHeapData nmtData, NmtCategory category) {
204190
if ((start.isNonNull() && !isAligned(start)) || nbytes.equal(0)) {
205191
return WordFactory.nullPointer();
206192
}
@@ -212,22 +198,16 @@ private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHa
212198
int fd = (int) fileHandle.rawValue();
213199
Pointer result = mmap(start, nbytes, accessAsProt(access), flags, fd, offset.rawValue());
214200
if (result.notEqual(MAP_FAILED())) {
215-
commitAndMaybeReserve(result, start, nbytes, nmtData, category);
201+
trackCommitAndMaybeReserve(result, start, nbytes, nmtData, category);
216202
return result;
217203
}
218204
return WordFactory.nullPointer();
219205
}
220206

221207
@Override
222208
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
223-
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access) {
224-
return commit0(start, nbytes, access, WordFactory.nullPointer(), NmtCategory.Internal);
225-
}
226-
227-
@Override
228-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
229-
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, NmtVirtualMemoryData nmtData) {
230-
return commit0(start, nbytes, access, nmtData, NmtCategory.Internal);
209+
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, NmtPreImageHeapData nmtData) {
210+
return commit0(start, nbytes, access, nmtData, NmtCategory.ImageHeap);
231211
}
232212

233213
@Override
@@ -237,7 +217,7 @@ public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, NmtCat
237217
}
238218

239219
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
240-
private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtVirtualMemoryData nmtData, NmtCategory category) {
220+
private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtPreImageHeapData nmtData, NmtCategory category) {
241221
if ((start.isNonNull() && !isAligned(start)) || nbytes.equal(0)) {
242222
return WordFactory.nullPointer();
243223
}
@@ -253,28 +233,26 @@ private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtV
253233
/* The memory returned by mmap is guaranteed to be zeroed. */
254234
final Pointer result = mmap(start, nbytes, accessAsProt(access), flags, NO_FD, NO_FD_OFFSET);
255235
if (result.notEqual(MAP_FAILED())) {
256-
commitAndMaybeReserve(result, start, nbytes, nmtData, category);
236+
trackCommitAndMaybeReserve(result, start, nbytes, nmtData, category);
257237
return result;
258238
}
259239
return nullPointer();
260240
}
261241

262242
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
263-
private void commitAndMaybeReserve(PointerBase baseAddr, PointerBase start, UnsignedWord nbytes, NmtVirtualMemoryData nmtData, NmtCategory category) {
243+
private static void trackCommitAndMaybeReserve(PointerBase baseAddr, PointerBase start, UnsignedWord nbytes, NmtPreImageHeapData nmtData, NmtCategory category) {
264244
// Account for possible reserve before commit
265245
if (start.isNull()) {
266246
if (nmtData.isNull()) {
267247
NativeMemoryTracking.trackReserve(baseAddr, nbytes, category);
268248
} else {
269-
nmtData.setReserved(nmtData.getReserved().add(nbytes));
270-
nmtData.setBaseAddr(baseAddr);
249+
NmtPreImageHeapDataAccess.enqueueReserve(nmtData, baseAddr, nbytes, category);
271250
}
272251
}
273252
if (nmtData.isNull()) {
274253
NativeMemoryTracking.trackCommit(baseAddr, nbytes, category);
275254
} else {
276-
nmtData.setCommitted(nmtData.getCommitted().add(nbytes));
277-
nmtData.setBaseAddr(baseAddr);
255+
NmtPreImageHeapDataAccess.enqueueCommit(nmtData, baseAddr, nbytes, category);
278256
}
279257
}
280258

@@ -305,6 +283,12 @@ public int uncommit(PointerBase start, UnsignedWord nbytes) {
305283
@Override
306284
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
307285
public int free(PointerBase start, UnsignedWord nbytes) {
286+
return free(start, nbytes, WordFactory.nullPointer());
287+
}
288+
289+
@Override
290+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
291+
public int free(PointerBase start, UnsignedWord nbytes, NmtPreImageHeapData nmtData) {
308292
if (start.isNull() || !isAligned(start) || nbytes.equal(0)) {
309293
return -1;
310294
}
@@ -313,9 +297,8 @@ public int free(PointerBase start, UnsignedWord nbytes) {
313297
Pointer mappingBegin = PointerUtils.roundDown(start, granularity);
314298
UnsignedWord mappingSize = UnsignedUtils.roundUp(nbytes, granularity);
315299
int ret = munmap(mappingBegin, mappingSize);
316-
if (ret == 0) {
317-
NativeMemoryTracking.trackFree(start, mappingSize); // *** use start here, not
318-
// mappingBegin
300+
if (ret == 0 && nmtData.isNull()) {
301+
NativeMemoryTracking.trackFree(start);
319302
}
320303
return ret;
321304
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import com.oracle.svm.core.config.ConfigurationValues;
5858
import com.oracle.svm.core.headers.LibC;
5959
import com.oracle.svm.core.heap.Heap;
60-
import com.oracle.svm.core.nmt.NmtVirtualMemoryData;
60+
import com.oracle.svm.core.nmt.NmtPreImageHeapData;
6161
import com.oracle.svm.core.os.AbstractImageHeapProvider;
6262
import com.oracle.svm.core.os.VirtualMemoryProvider;
6363
import com.oracle.svm.core.os.VirtualMemoryProvider.Access;
@@ -104,7 +104,7 @@ public boolean guaranteesHeapPreferredAddressSpaceAlignment() {
104104

105105
@Override
106106
@Uninterruptible(reason = "Called during isolate initialization.")
107-
public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, WordPointer basePointer, WordPointer endPointer, NmtVirtualMemoryData nmtData) {
107+
public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, WordPointer basePointer, WordPointer endPointer, NmtPreImageHeapData nmtData) {
108108
Pointer selfReservedMemory = WordFactory.nullPointer();
109109
UnsignedWord requiredSize = getTotalRequiredAddressSpaceSize();
110110
if (reservedAddressSpace.isNull()) {
@@ -133,13 +133,13 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
133133

134134
int error = DynamicMethodAddressResolutionHeapSupport.get().initialize();
135135
if (error != CEntryPointErrors.NO_ERROR) {
136-
freeImageHeap(selfReservedHeapBase);
136+
freeImageHeap(selfReservedHeapBase, nmtData);
137137
return error;
138138
}
139139

140140
error = DynamicMethodAddressResolutionHeapSupport.get().install(heapBase);
141141
if (error != CEntryPointErrors.NO_ERROR) {
142-
freeImageHeap(selfReservedHeapBase);
142+
freeImageHeap(selfReservedHeapBase, nmtData);
143143
return error;
144144
}
145145
} else {
@@ -157,15 +157,15 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
157157
IMAGE_HEAP_RELOCATABLE_BEGIN.get(), IMAGE_HEAP_A_RELOCATABLE_POINTER.get(), IMAGE_HEAP_RELOCATABLE_END.get(),
158158
IMAGE_HEAP_WRITABLE_BEGIN.get(), IMAGE_HEAP_WRITABLE_END.get(), nmtData);
159159
if (result != CEntryPointErrors.NO_ERROR) {
160-
freeImageHeap(selfReservedHeapBase);
160+
freeImageHeap(selfReservedHeapBase, nmtData);
161161
}
162162
return result;
163163
}
164164

165165
@Uninterruptible(reason = "Called during isolate initialization.")
166166
private static int initializeImageHeap(Pointer imageHeap, UnsignedWord reservedSize, WordPointer endPointer, WordPointer cachedFd, WordPointer cachedOffsetInFile,
167167
Pointer magicAddress, Word heapBeginSym, Word heapEndSym, Word heapRelocsSym, Pointer heapAnyRelocPointer, Word heapRelocsEndSym, Word heapWritableSym, Word heapWritableEndSym,
168-
NmtVirtualMemoryData nmtData) {
168+
NmtPreImageHeapData nmtData) {
169169
assert heapBeginSym.belowOrEqual(heapWritableSym) && heapWritableSym.belowOrEqual(heapWritableEndSym) && heapWritableEndSym.belowOrEqual(heapEndSym);
170170
assert heapBeginSym.belowOrEqual(heapRelocsSym) && heapRelocsSym.belowOrEqual(heapRelocsEndSym) && heapRelocsEndSym.belowOrEqual(heapEndSym);
171171
assert heapAnyRelocPointer.isNull() || (heapRelocsSym.belowOrEqual(heapAnyRelocPointer) && heapAnyRelocPointer.belowThan(heapRelocsEndSym));
@@ -257,7 +257,7 @@ private static int initializeImageHeap(Pointer imageHeap, UnsignedWord reservedS
257257

258258
@Uninterruptible(reason = "Called during isolate initialization.")
259259
private static int initializeImageHeapByCopying(Pointer imageHeap, UnsignedWord imageHeapSize, UnsignedWord pageSize, Word heapBeginSym, Word heapWritableSym, Word heapWritableEndSym,
260-
NmtVirtualMemoryData nmtData) {
260+
NmtPreImageHeapData nmtData) {
261261
Pointer committedBegin = VirtualMemoryProvider.get().commit(imageHeap, imageHeapSize, Access.READ | Access.WRITE, nmtData);
262262
if (committedBegin.isNull()) {
263263
return CEntryPointErrors.MAP_HEAP_FAILED;
@@ -351,6 +351,11 @@ private static boolean checkImageFileMagic(int mapfd, int imagefd, CCharPointer
351351
@Override
352352
@Uninterruptible(reason = "Called during isolate tear-down.")
353353
public int freeImageHeap(PointerBase heapBase) {
354+
return freeImageHeap(heapBase, WordFactory.nullPointer());
355+
}
356+
357+
@Uninterruptible(reason = "Called during isolate tear-down.")
358+
private int freeImageHeap(PointerBase heapBase, NmtPreImageHeapData nmtData) {
354359
if (heapBase.isNull()) { // no memory allocated
355360
return CEntryPointErrors.NO_ERROR;
356361
}
@@ -360,7 +365,7 @@ public int freeImageHeap(PointerBase heapBase) {
360365
if (DynamicMethodAddressResolutionHeapSupport.isEnabled()) {
361366
addressSpaceStart = addressSpaceStart.subtract(getPreHeapAlignedSizeForDynamicMethodAddressResolver());
362367
}
363-
if (VirtualMemoryProvider.get().free(addressSpaceStart, getTotalRequiredAddressSpaceSize()) != 0) {
368+
if (VirtualMemoryProvider.get().free(addressSpaceStart, getTotalRequiredAddressSpaceSize(), nmtData) != 0) {
364369
return CEntryPointErrors.FREE_IMAGE_HEAP_FAILED;
365370
}
366371
return CEntryPointErrors.NO_ERROR;

substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/WindowsImageHeapProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
import com.oracle.svm.core.c.function.CEntryPointActions;
4747
import com.oracle.svm.core.c.function.CEntryPointErrors;
4848
import com.oracle.svm.core.headers.LibC;
49+
import com.oracle.svm.core.nmt.NmtPreImageHeapData;
4950
import com.oracle.svm.core.os.AbstractCopyingImageHeapProvider;
50-
import com.oracle.svm.core.nmt.NmtVirtualMemoryData;
5151
import com.oracle.svm.core.os.VirtualMemoryProvider;
5252
import com.oracle.svm.core.os.VirtualMemoryProvider.Access;
5353
import com.oracle.svm.core.windows.headers.FileAPI;
@@ -65,7 +65,7 @@
6565
public class WindowsImageHeapProvider extends AbstractCopyingImageHeapProvider {
6666
@Override
6767
@Uninterruptible(reason = "Called during isolate initialization.")
68-
protected int commitAndCopyMemory(Pointer loadedImageHeap, UnsignedWord imageHeapSize, Pointer newImageHeap, NmtVirtualMemoryData nmtData) {
68+
protected int commitAndCopyMemory(Pointer loadedImageHeap, UnsignedWord imageHeapSize, Pointer newImageHeap, NmtPreImageHeapData nmtData) {
6969
HANDLE imageHeapFileMapping = getImageHeapFileMapping();
7070
if (imageHeapFileMapping.isNull()) {
7171
/* Fall back to copying from memory. */

0 commit comments

Comments
 (0)