Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up off-heap memory usages #7669

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,14 @@ public static <T extends PointerBase> T realloc(T ptr, UnsignedWord size) {
public static void free(PointerBase ptr) {
ImageSingletons.lookup(UnmanagedMemorySupport.class).free(ptr);
}

/**
* Temporarily the same as {@link UnmanagedMemory#free(PointerBase)}. Will later be different
* because it will not attempt to perform any NMT operations. This is crucial for releasing
* memory allocated by C libraries which will not have NMT "malloc headers". If
* {@link UnmanagedMemory#free(PointerBase)} is used instead, a segfault will occur.
*/
public static void untrackedFree(PointerBase ptr) {
ImageSingletons.lookup(UnmanagedMemorySupport.class).free(ptr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
import org.graalvm.nativeimage.UnmanagedMemory;
import org.graalvm.word.PointerBase;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.BaseProcessPropertiesSupport;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.posix.headers.Dlfcn;
import com.oracle.svm.core.posix.headers.Signal;
import com.oracle.svm.core.posix.headers.Stdlib;
Expand Down Expand Up @@ -103,7 +103,7 @@ public String getObjectFile(PointerBase symbolAddress) {
try {
return CTypeConversion.toJavaString(realpath);
} finally {
LibC.free(realpath);
UnmanagedMemory.untrackedFree(realpath);
}
}

Expand Down Expand Up @@ -164,7 +164,7 @@ protected static String realpath(String path) {
} else {
/* Success */
final String result = CTypeConversion.toJavaString(realpathPointer);
LibC.free(realpathPointer);
UnmanagedMemory.untrackedFree(realpathPointer);
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,46 @@
*/
package com.oracle.svm.core.posix;

import jdk.graal.compiler.api.replacements.Fold;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.impl.UnmanagedMemorySupport;
import org.graalvm.word.PointerBase;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.headers.LibCSupport;

@AutomaticallyRegisteredImageSingleton(UnmanagedMemorySupport.class)
class UnmanagedMemorySupportImpl implements UnmanagedMemorySupport {
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public <T extends PointerBase> T malloc(UnsignedWord size) {
return LibC.malloc(size);
return libc().malloc(size);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public <T extends PointerBase> T calloc(UnsignedWord size) {
return LibC.calloc(WordFactory.unsigned(1), size);
return libc().calloc(WordFactory.unsigned(1), size);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public <T extends PointerBase> T realloc(T ptr, UnsignedWord size) {
return LibC.realloc(ptr, size);
return libc().realloc(ptr, size);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void free(PointerBase ptr) {
LibC.free(ptr);
libc().free(ptr);
}

@Fold
static LibCSupport libc() {
return ImageSingletons.lookup(LibCSupport.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
import org.graalvm.nativeimage.UnmanagedMemory;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
import com.oracle.svm.core.feature.InternalFeature;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.jdk.SystemPropertiesSupport;
import com.oracle.svm.core.posix.PosixSystemPropertiesSupport;
import com.oracle.svm.core.posix.headers.Limits;
Expand Down Expand Up @@ -105,7 +105,7 @@ protected String osVersionValue() {
CCharPointer osVersionStr = Foundation.systemVersionPlatform();
if (osVersionStr.isNonNull()) {
osVersionValue = CTypeConversion.toJavaString(osVersionStr);
LibC.free(osVersionStr);
UnmanagedMemory.untrackedFree(osVersionStr);
return osVersionValue;
}
} else {
Expand All @@ -120,7 +120,7 @@ protected String osVersionValue() {
CCharPointer osVersionStr = Foundation.systemVersionPlatformFallback();
if (osVersionStr.isNonNull()) {
osVersionValue = CTypeConversion.toJavaString(osVersionStr);
LibC.free(osVersionStr);
UnmanagedMemory.untrackedFree(osVersionStr);
return osVersionValue;
}
return osVersionValue = "Unknown";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
import org.graalvm.nativeimage.c.type.VoidPointer;
import org.graalvm.nativeimage.c.type.WordPointer;
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
import org.graalvm.nativeimage.impl.UnmanagedMemorySupport;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.c.NonmovableArrays;
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
import com.oracle.svm.core.feature.InternalFeature;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.jdk.SystemPropertiesSupport;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.core.windows.headers.FileAPI;
Expand Down Expand Up @@ -245,29 +245,30 @@ public Pair<String, String> getOsNameAndVersion() {
break;
}

VoidPointer versionInfo = LibC.malloc(WordFactory.unsigned(versionSize));
VoidPointer versionInfo = ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(WordFactory.unsigned(versionSize));
if (versionInfo.isNull()) {
break;
}
try {

if (WinVer.GetFileVersionInfoW(kernel32Path, 0, versionSize, versionInfo) == 0) {
LibC.free(versionInfo);
break;
}
if (WinVer.GetFileVersionInfoW(kernel32Path, 0, versionSize, versionInfo) == 0) {
break;
}

WindowsLibC.WCharPointer rootPath = NonmovableArrays.addressOf(NonmovableArrays.fromImageHeap(ROOT_PATH), 0);
WordPointer fileInfoPointer = UnsafeStackValue.get(WordPointer.class);
CIntPointer lengthPointer = UnsafeStackValue.get(CIntPointer.class);
if (WinVer.VerQueryValueW(versionInfo, rootPath, fileInfoPointer, lengthPointer) == 0) {
LibC.free(versionInfo);
break;
}
WindowsLibC.WCharPointer rootPath = NonmovableArrays.addressOf(NonmovableArrays.fromImageHeap(ROOT_PATH), 0);
WordPointer fileInfoPointer = UnsafeStackValue.get(WordPointer.class);
CIntPointer lengthPointer = UnsafeStackValue.get(CIntPointer.class);
if (WinVer.VerQueryValueW(versionInfo, rootPath, fileInfoPointer, lengthPointer) == 0) {
break;
}

VerRsrc.VS_FIXEDFILEINFO fileInfo = fileInfoPointer.read();
majorVersion = (short) (fileInfo.dwProductVersionMS() >> 16); // HIWORD
minorVersion = (short) fileInfo.dwProductVersionMS(); // LOWORD
buildNumber = (short) (fileInfo.dwProductVersionLS() >> 16); // HIWORD
LibC.free(versionInfo);
VerRsrc.VS_FIXEDFILEINFO fileInfo = fileInfoPointer.read();
majorVersion = (short) (fileInfo.dwProductVersionMS() >> 16); // HIWORD
minorVersion = (short) fileInfo.dwProductVersionMS(); // LOWORD
buildNumber = (short) (fileInfo.dwProductVersionLS() >> 16); // HIWORD
} finally {
ImageSingletons.lookup(UnmanagedMemorySupport.class).free(versionInfo);
}
} while (false);

String osVersion = majorVersion + "." + minorVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,46 @@
*/
package com.oracle.svm.core.windows;

import jdk.graal.compiler.api.replacements.Fold;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.impl.UnmanagedMemorySupport;
import org.graalvm.word.PointerBase;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.headers.LibCSupport;

@AutomaticallyRegisteredImageSingleton(UnmanagedMemorySupport.class)
class WindowsUnmanagedMemorySupportImpl implements UnmanagedMemorySupport {
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public <T extends PointerBase> T malloc(UnsignedWord size) {
return LibC.malloc(size);
return libc().malloc(size);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public <T extends PointerBase> T calloc(UnsignedWord size) {
return LibC.calloc(WordFactory.unsigned(1), size);
return libc().calloc(WordFactory.unsigned(1), size);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public <T extends PointerBase> T realloc(T ptr, UnsignedWord size) {
return LibC.realloc(ptr, size);
return libc().realloc(ptr, size);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void free(PointerBase ptr) {
LibC.free(ptr);
libc().free(ptr);
}

@Fold
static LibCSupport libc() {
return ImageSingletons.lookup(LibCSupport.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,6 @@ public static <T extends PointerBase> T memset(T s, SignedWord c, UnsignedWord n
return libc().memset(s, c, n);
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static <T extends PointerBase> T malloc(UnsignedWord size) {
return libc().malloc(size);
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static <T extends PointerBase> T calloc(UnsignedWord nmemb, UnsignedWord size) {
return libc().calloc(nmemb, size);
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static <T extends PointerBase> T realloc(PointerBase ptr, UnsignedWord size) {
return libc().realloc(ptr, size);
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static void free(PointerBase ptr) {
libc().free(ptr);
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static void exit(int status) {
libc().exit(status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static String getSystemTimeZoneID(String javaHome) {
CCharPointer tzId = LibCHelper.SVM_FindJavaTZmd(tzMappingsPtr, contentLen);
String result = CTypeConversion.toJavaString(tzId);
// SVM_FindJavaTZmd returns a newly allocated string
UnmanagedMemory.free(tzId);
UnmanagedMemory.untrackedFree(tzId);
return result;
} finally {
if (refContent != null) {
Expand Down