Skip to content

Commit d50a711

Browse files
Label call sites and clean up
mtThread Remove virtual memory tracking code wip remove more vmem tracking code comments and refactor more small fixes, refactore, comments comment unchecked cast label Unsafe callsite. Clean up. javadoc
1 parent d16ed75 commit d50a711

27 files changed

+222
-705
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/UnmanagedMemory.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public static <T extends PointerBase> T malloc(UnsignedWord size) {
7676
return result;
7777
}
7878

79+
public static <T extends PointerBase> T malloc(UnsignedWord size, int flag) {
80+
T result = ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(size, flag);
81+
if (result.isNull()) {
82+
throw new OutOfMemoryError("malloc of unmanaged memory");
83+
}
84+
return result;
85+
}
86+
7987
/**
8088
* Allocates {@code size} bytes of unmanaged memory. The content of the memory is undefined.
8189
* <p>
@@ -138,6 +146,14 @@ public static <T extends PointerBase> T realloc(T ptr, UnsignedWord size) {
138146
return result;
139147
}
140148

149+
public static <T extends PointerBase> T realloc(T ptr, UnsignedWord size, int flag) {
150+
T result = ImageSingletons.lookup(UnmanagedMemorySupport.class).realloc(ptr, size, flag);
151+
if (result.isNull()) {
152+
throw new OutOfMemoryError("realloc of unmanaged memory");
153+
}
154+
return result;
155+
}
156+
141157
/**
142158
* Frees unmanaged memory that was previously allocated using methods of this class.
143159
*
@@ -148,8 +164,8 @@ public static void free(PointerBase ptr) {
148164
}
149165

150166
/**
151-
* Will not attempt to perform any NMT operations. This is crucial for releasing
152-
* memory allocated by C libraries which will not have NMT "malloc headers". If
167+
* Will not attempt to perform any NMT operations. This is crucial for releasing memory
168+
* allocated by C libraries which will not have NMT "malloc headers". If
153169
* {@link UnmanagedMemory#free(PointerBase)} is used instead, a segfault will occur.
154170
*/
155171
public static void untrackedFree(PointerBase ptr) {

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

Lines changed: 5 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454
import com.oracle.svm.core.c.CGlobalDataFactory;
5555
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
5656
import com.oracle.svm.core.feature.InternalFeature;
57-
import com.oracle.svm.core.nmt.NativeMemoryTracking;
58-
import com.oracle.svm.core.nmt.NmtFlag;
59-
import com.oracle.svm.core.nmt.NmtVirtualMemoryData;
6057
import com.oracle.svm.core.os.VirtualMemoryProvider;
6158
import com.oracle.svm.core.posix.headers.Unistd;
6259
import com.oracle.svm.core.util.PointerUtils;
@@ -113,23 +110,6 @@ public UnsignedWord getGranularity() {
113110
@Override
114111
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
115112
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable) {
116-
return reserve(nbytes, alignment, executable, WordFactory.nullPointer());
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, NmtFlag.mtNone.ordinal());
123-
}
124-
125-
@Override
126-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
127-
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, int flag) {
128-
return reserve0(nbytes, alignment, executable, WordFactory.nullPointer(), flag);
129-
}
130-
131-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
132-
private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtVirtualMemoryData nmtData, int flag) {
133113
if (nbytes.equal(0)) {
134114
return WordFactory.nullPointer();
135115
}
@@ -148,56 +128,25 @@ private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean ex
148128
return nullPointer();
149129
}
150130
if (!customAlignment) {
151-
if (nmtData.isNull()) {
152-
NativeMemoryTracking.recordReserve(mappingSize, flag);
153-
} else {
154-
nmtData.setReserved(nmtData.getReserved().add(mappingSize));
155-
}
156131
return mappingBegin;
157132
}
158-
UnsignedWord unmappedSize = WordFactory.zero();
159133
Pointer begin = PointerUtils.roundUp(mappingBegin, alignment);
160134
UnsignedWord clippedBegin = begin.subtract(mappingBegin);
161135
if (clippedBegin.aboveOrEqual(granularity)) {
162-
UnsignedWord unmapSize = UnsignedUtils.roundDown(clippedBegin, granularity);
163-
munmap(mappingBegin, unmapSize);
164-
unmappedSize.add(unmapSize);
136+
munmap(mappingBegin, UnsignedUtils.roundDown(clippedBegin, granularity));
165137
}
166138
Pointer mappingEnd = mappingBegin.add(mappingSize);
167139
UnsignedWord clippedEnd = mappingEnd.subtract(begin.add(nbytes));
168140
if (clippedEnd.aboveOrEqual(granularity)) {
169141
UnsignedWord rounded = UnsignedUtils.roundDown(clippedEnd, granularity);
170142
munmap(mappingEnd.subtract(rounded), rounded);
171-
unmappedSize.add(rounded);
172-
}
173-
if (nmtData.isNull()) {
174-
NativeMemoryTracking.recordReserve(mappingSize.subtract(unmappedSize), flag);
175-
} else {
176-
nmtData.setReserved(nmtData.getReserved().add(mappingSize.subtract(unmappedSize)));
177143
}
178144
return begin;
179145
}
180146

181147
@Override
182148
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
183149
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access) {
184-
return mapFile(start, nbytes, fileHandle, offset, access, WordFactory.nullPointer());
185-
}
186-
187-
@Override
188-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
189-
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtVirtualMemoryData nmtData) {
190-
return mapFile0(start, nbytes, fileHandle, offset, access, nmtData, NmtFlag.mtNone.ordinal());
191-
}
192-
193-
@Override
194-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
195-
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, int flag) {
196-
return mapFile0(start, nbytes, fileHandle, offset, access, WordFactory.nullPointer(), flag);
197-
}
198-
199-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
200-
private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtVirtualMemoryData nmtData, int flag) {
201150
if ((start.isNonNull() && !isAligned(start)) || nbytes.equal(0)) {
202151
return WordFactory.nullPointer();
203152
}
@@ -208,33 +157,12 @@ private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHa
208157
}
209158
int fd = (int) fileHandle.rawValue();
210159
Pointer result = mmap(start, nbytes, accessAsProt(access), flags, fd, offset.rawValue());
211-
if (result.notEqual(MAP_FAILED())) {
212-
commitAndMaybeReserve(start, nbytes, nmtData, flag);
213-
return result;
214-
}
215-
return WordFactory.nullPointer();
160+
return result.notEqual(MAP_FAILED()) ? result : WordFactory.nullPointer();
216161
}
217162

218163
@Override
219164
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
220165
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access) {
221-
return commit(start, nbytes, access, WordFactory.nullPointer());
222-
}
223-
224-
@Override
225-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
226-
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, NmtVirtualMemoryData nmtData) {
227-
return commit0(start, nbytes, access, nmtData, NmtFlag.mtNone.ordinal());
228-
}
229-
230-
@Override
231-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
232-
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, int flag) {
233-
return commit0(start, nbytes, access, WordFactory.nullPointer(), flag);
234-
}
235-
236-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
237-
private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtVirtualMemoryData nmtData, int flag) {
238166
if ((start.isNonNull() && !isAligned(start)) || nbytes.equal(0)) {
239167
return WordFactory.nullPointer();
240168
}
@@ -249,27 +177,7 @@ private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtV
249177
}
250178
/* The memory returned by mmap is guaranteed to be zeroed. */
251179
final Pointer result = mmap(start, nbytes, accessAsProt(access), flags, NO_FD, NO_FD_OFFSET);
252-
if (result.notEqual(MAP_FAILED())) {
253-
commitAndMaybeReserve(start, nbytes, nmtData, flag);
254-
return result;
255-
}
256-
return nullPointer();
257-
}
258-
259-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
260-
private void commitAndMaybeReserve(PointerBase start, UnsignedWord nbytes, NmtVirtualMemoryData nmtData, int flag) {
261-
if (nmtData.isNull()) {
262-
NativeMemoryTracking.recordCommit(nbytes, flag);
263-
} else {
264-
nmtData.setCommitted(nmtData.getCommitted().add(nbytes));
265-
}
266-
if (start.isNull()) {
267-
if (nmtData.isNull()) {
268-
NativeMemoryTracking.recordReserve(nbytes, flag);
269-
} else {
270-
nmtData.setReserved(nmtData.getReserved().add(nbytes));
271-
}
272-
}
180+
return result.notEqual(MAP_FAILED()) ? result : nullPointer();
273181
}
274182

275183
@Override
@@ -285,41 +193,25 @@ public int protect(PointerBase start, UnsignedWord nbytes, int access) {
285193
@Override
286194
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
287195
public int uncommit(PointerBase start, UnsignedWord nbytes) {
288-
return uncommit(start, nbytes, NmtFlag.mtNone.ordinal());
289-
}
290-
291-
@Override
292-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
293-
public int uncommit(PointerBase start, UnsignedWord nbytes, int flag) {
294196
if (start.isNull() || !isAligned(start) || nbytes.equal(0)) {
295197
return -1;
296198
}
297-
NativeMemoryTracking.recordUncommit(nbytes, flag);
199+
298200
final Pointer result = mmap(start, nbytes, PROT_NONE(), MAP_FIXED() | MAP_ANON() | MAP_PRIVATE() | MAP_NORESERVE(), NO_FD, NO_FD_OFFSET);
299201
return result.notEqual(MAP_FAILED()) ? 0 : -1;
300202
}
301203

302204
@Override
303205
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
304206
public int free(PointerBase start, UnsignedWord nbytes) {
305-
return free(start, nbytes, NmtFlag.mtNone.ordinal());
306-
}
307-
308-
@Override
309-
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
310-
public int free(PointerBase start, UnsignedWord nbytes, int flag) {
311207
if (start.isNull() || !isAligned(start) || nbytes.equal(0)) {
312208
return -1;
313209
}
314210

315211
UnsignedWord granularity = getGranularity();
316212
Pointer mappingBegin = PointerUtils.roundDown(start, granularity);
317213
UnsignedWord mappingSize = UnsignedUtils.roundUp(nbytes, granularity);
318-
int ret = munmap(mappingBegin, mappingSize);
319-
if (ret == 0) {
320-
NativeMemoryTracking.recordFree(mappingSize, flag);
321-
}
322-
return ret;
214+
return munmap(mappingBegin, mappingSize);
323215
}
324216

325217
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public <T extends PointerBase> T malloc(UnsignedWord size) {
5050
}
5151

5252
@Override
53+
@SuppressWarnings("unchecked")
5354
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
5455
public <T extends PointerBase> T malloc(UnsignedWord size, int flag) {
5556
ReturnAddress ra = StackValue.get(ReturnAddress.class);
@@ -67,6 +68,7 @@ public <T extends PointerBase> T calloc(UnsignedWord size) {
6768
}
6869

6970
@Override
71+
@SuppressWarnings("unchecked")
7072
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
7173
public <T extends PointerBase> T calloc(UnsignedWord size, int flag) {
7274
ReturnAddress ra = StackValue.get(ReturnAddress.class);
@@ -84,6 +86,7 @@ public <T extends PointerBase> T realloc(T ptr, UnsignedWord size) {
8486
}
8587

8688
@Override
89+
@SuppressWarnings("unchecked")
8790
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
8891
public <T extends PointerBase> T realloc(T ptr, UnsignedWord size, int flag) {
8992

@@ -105,7 +108,7 @@ public <T extends PointerBase> T realloc(T ptr, UnsignedWord size, int flag) {
105108
NativeMemoryTracking.deaccountMalloc(oldSize, oldCategory);
106109
}
107110

108-
// Account the new block
111+
// Account the new block and overwrite the header with the new tracking data
109112
return (T) NativeMemoryTracking.recordMalloc(newOuterPointer, size, flag);
110113
}
111114

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@
5858
import com.oracle.svm.core.config.ConfigurationValues;
5959
import com.oracle.svm.core.headers.LibC;
6060
import com.oracle.svm.core.heap.Heap;
61-
import com.oracle.svm.core.nmt.NmtFlag;
62-
import com.oracle.svm.core.nmt.NmtVirtualMemoryData;
6361
import com.oracle.svm.core.os.AbstractImageHeapProvider;
6462
import com.oracle.svm.core.os.CopyingImageHeapProvider;
6563
import com.oracle.svm.core.os.VirtualMemoryProvider;
@@ -106,7 +104,7 @@ public boolean guaranteesHeapPreferredAddressSpaceAlignment() {
106104

107105
@Override
108106
@Uninterruptible(reason = "Called during isolate initialization.")
109-
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) {
110108
// If we are the first isolate, we might be able to use the existing image heap (see below)
111109
SignedWord fd = CACHED_IMAGE_FD.get().read();
112110
boolean firstIsolate = false;
@@ -142,7 +140,7 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
142140

143141
// If we cannot find or open the image file, fall back to copy it from memory.
144142
if (fd.equal(CANNOT_OPEN_FD)) {
145-
return fallbackCopyingProvider.initialize(reservedAddressSpace, reservedSize, basePointer, endPointer, nmtData);
143+
return fallbackCopyingProvider.initialize(reservedAddressSpace, reservedSize, basePointer, endPointer);
146144
}
147145

148146
boolean haveDynamicMethodResolution = DynamicMethodAddressResolutionHeapSupport.isEnabled();
@@ -200,7 +198,7 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
200198
Pointer heapBase;
201199
Pointer allocatedMemory = WordFactory.nullPointer();
202200
if (reservedAddressSpace.isNull()) {
203-
heapBase = allocatedMemory = VirtualMemoryProvider.get().reserve(totalAddressSpaceSize, alignment, false, nmtData);
201+
heapBase = allocatedMemory = VirtualMemoryProvider.get().reserve(totalAddressSpaceSize, alignment, false);
204202
if (allocatedMemory.isNull()) {
205203
return CEntryPointErrors.RESERVE_ADDRESS_SPACE_FAILED;
206204
}
@@ -228,24 +226,16 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
228226
// Create memory mappings from the image file.
229227
UnsignedWord fileOffset = CACHED_IMAGE_HEAP_OFFSET.get().read();
230228
Pointer imageHeap = heapBase.add(imageHeapOffsetInAddressSpace);
231-
imageHeap = VirtualMemoryProvider.get().mapFile(imageHeap, imageHeapSizeInFile, fd, fileOffset, Access.READ, nmtData);
229+
imageHeap = VirtualMemoryProvider.get().mapFile(imageHeap, imageHeapSizeInFile, fd, fileOffset, Access.READ);
232230
if (imageHeap.isNull()) {
233231
freeImageHeap(allocatedMemory);
234232
return CEntryPointErrors.MAP_HEAP_FAILED;
235233
}
236234

237235
Pointer relocPointer = IMAGE_HEAP_A_RELOCATABLE_POINTER.get();
238236
ComparableWord relocatedValue = relocPointer.readWord(0);
239-
ComparableWord mappedValue = imageHeap.readWord(relocPointer.subtract(imageHeapBegin)); // ***
240-
// What
241-
// is
242-
// this
243-
// mappedValue?
237+
ComparableWord mappedValue = imageHeap.readWord(relocPointer.subtract(imageHeapBegin));
244238
if (relocatedValue.notEqual(mappedValue)) {
245-
// TODO what is actually happening in this block?
246-
// *** this is checking to see if they've been mapped correctly? [GDB: this doesn't
247-
// actually get entered it seems]
248-
249239
/*
250240
* Addresses were relocated by dynamic linker, so copy them, but first remap the pages
251241
* to avoid swapping them in from disk.
@@ -256,7 +246,7 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
256246
freeImageHeap(allocatedMemory);
257247
return CEntryPointErrors.PROTECT_HEAP_FAILED;
258248
}
259-
Pointer committedRelocsBegin = VirtualMemoryProvider.get().commit(relocsBegin, relocsSize, Access.READ | Access.WRITE, nmtData);
249+
Pointer committedRelocsBegin = VirtualMemoryProvider.get().commit(relocsBegin, relocsSize, Access.READ | Access.WRITE);
260250
if (committedRelocsBegin.isNull() || committedRelocsBegin != relocsBegin) {
261251
freeImageHeap(allocatedMemory);
262252
return CEntryPointErrors.PROTECT_HEAP_FAILED;
@@ -370,15 +360,15 @@ public int freeImageHeap(PointerBase heapBase) {
370360
*/
371361
assert Heap.getHeap().getImageHeapOffsetInAddressSpace() == 0;
372362
UnsignedWord beforeRelocSize = IMAGE_HEAP_RELOCATABLE_BEGIN.get().subtract((Pointer) heapBase);
373-
Pointer newHeapBase = VirtualMemoryProvider.get().commit(heapBase, beforeRelocSize, Access.READ, NmtFlag.mtJavaHeap.ordinal());
363+
Pointer newHeapBase = VirtualMemoryProvider.get().commit(heapBase, beforeRelocSize, Access.READ);
374364

375365
if (newHeapBase.isNull() || newHeapBase.notEqual(heapBase)) {
376366
return CEntryPointErrors.MAP_HEAP_FAILED;
377367
}
378368

379369
Word relocEnd = IMAGE_HEAP_RELOCATABLE_END.get();
380370
Word afterRelocSize = IMAGE_HEAP_END.get().subtract(relocEnd);
381-
Pointer newRelocEnd = VirtualMemoryProvider.get().commit(relocEnd, afterRelocSize, Access.READ, NmtFlag.mtJavaHeap.ordinal());
371+
Pointer newRelocEnd = VirtualMemoryProvider.get().commit(relocEnd, afterRelocSize, Access.READ);
382372

383373
if (newRelocEnd.isNull() || newRelocEnd.notEqual(relocEnd)) {
384374
return CEntryPointErrors.MAP_HEAP_FAILED;
@@ -392,7 +382,7 @@ public int freeImageHeap(PointerBase heapBase) {
392382
addressSpaceStart = addressSpaceStart.subtract(preHeapRequiredBytes);
393383
}
394384

395-
if (VirtualMemoryProvider.get().free(addressSpaceStart, totalAddressSpaceSize, NmtFlag.mtJavaHeap.ordinal()) != 0) {
385+
if (VirtualMemoryProvider.get().free(addressSpaceStart, totalAddressSpaceSize) != 0) {
396386
return CEntryPointErrors.FREE_IMAGE_HEAP_FAILED;
397387
}
398388
}

0 commit comments

Comments
 (0)