Skip to content

Commit

Permalink
Cleanups and fixes.
Browse files Browse the repository at this point in the history
Removed all JVMTI code that uses JNI object handles.
  • Loading branch information
christianhaeubl committed Mar 5, 2024
1 parent 3b9bc76 commit 31a084e
Show file tree
Hide file tree
Showing 76 changed files with 729 additions and 5,384 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import java.lang.ref.Reference;

import com.oracle.svm.core.jvmti.JvmtiPostEvents;
import org.graalvm.nativeimage.CurrentIsolate;
import org.graalvm.nativeimage.IsolateThread;
import org.graalvm.nativeimage.Platform;
Expand Down Expand Up @@ -170,8 +169,6 @@ public void collectionHint(boolean fullGC) {

private void collect(GCCause cause, boolean forceFullGC) {
if (!hasNeverCollectPolicy()) {
JvmtiPostEvents.postGarbageCollectionStart();
JvmtiPostEvents.postGarbageCollectionFinish();
boolean outOfMemory = collectWithoutAllocating(cause, forceFullGC);
if (outOfMemory) {
throw OutOfMemoryUtil.heapSizeExceeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private static boolean walkPartitionInline(Object firstObject, Object lastObject

@Uninterruptible(reason = "Bridge between uninterruptible and potentially interruptible code.", mayBeInlined = true, calleeMustBe = false)
private static boolean visitObject(ObjectVisitor visitor, Object currentObject) {
return visitor. visitObject(currentObject);
return visitor.visitObject(currentObject);
}

@Uninterruptible(reason = "Bridge between uninterruptible and potentially interruptible code.", mayBeInlined = true, calleeMustBe = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,13 @@ public static void updateMaxJavaStackTraceDepth(EconomicMap<OptionKey<?>, Object
@Option(help = "Enable JVM Tool Interface (JVMTI) support.", type = OptionType.User)//
public static final HostedOptionKey<Boolean> JVMTI = new HostedOptionKey<>(false);

// TEMP (chaeubl): we need to support multiple values
@Option(help = "Loads the specified native agent library. " +
"After the library name, a comma-separated list of options specific to the library can be used.", type = OptionType.User)//
public static final RuntimeOptionKey<String> AgentLib = new RuntimeOptionKey<>(null);
public static final RuntimeOptionKey<String> JVMTIAgentLib = new RuntimeOptionKey<>(null);

// TEMP (chaeubl): we need to support multiple values
@Option(help = "Loads the specified native agent library specified by the absolute path name. " +
"After the library path, a comma-separated list of options specific to the library can be used.", type = OptionType.User)//
public static final RuntimeOptionKey<String> AgentPath = new RuntimeOptionKey<>(null);
public static final RuntimeOptionKey<String> JVMTIAgentPath = new RuntimeOptionKey<>(null);

@Option(help = "Alignment of AOT and JIT compiled code in bytes.")//
public static final HostedOptionKey<Integer> CodeAlignment = new HostedOptionKey<>(16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@

import java.util.Map;

import com.oracle.svm.core.jvmti.JvmtiEnvManager;
import com.oracle.svm.core.jvmti.JvmtiManager;
import com.oracle.svm.core.jvmti.JvmtiPostEvents;
import org.graalvm.nativeimage.CurrentIsolate;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Isolate;
Expand Down Expand Up @@ -80,7 +77,6 @@
import com.oracle.svm.core.heap.RestrictHeapAccess;
import com.oracle.svm.core.jdk.PlatformNativeLibrarySupport;
import com.oracle.svm.core.jdk.RuntimeSupport;
import com.oracle.svm.core.jvmti.JvmtiAgents;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.option.RuntimeOptionParser;
import com.oracle.svm.core.os.CommittedMemoryProvider;
Expand Down Expand Up @@ -402,13 +398,6 @@ private static int initializeIsolateInterruptibly1(CEntryPointCreateIsolateParam
/* Initialize the physical memory size. */
PhysicalMemory.size();

if (SubstrateOptions.JVMTI.getValue()) {
JvmtiAgents.singleton().load();

//JvmtiPostEvents.postVMInit();
JvmtiPostEvents.postVMStart();
}

assert !isolateInitialized;
isolateInitialized = true;

Expand Down Expand Up @@ -643,14 +632,6 @@ private static boolean initiateTearDownIsolateInterruptibly() {
}

VMThreads.singleton().threadExit();

if (SubstrateOptions.JVMTI.getValue()) {
JvmtiPostEvents.postVMDeath();
JvmtiManager.freeAllJvmtiClassesUnmanagedMemory();
JvmtiAgents.singleton().unload();
}


return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.oracle.svm.core.memory.NativeMemory;

/** Platform-independent LibC support. Don't use this class directly, use {@link LibC} instead. */
public interface LibCSupport {
public interface LibCSupport {
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
int errno();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,6 @@ private ClassRepository getGenericInfo() {
return companion.getGenericInfo(this);
}


ClassRepository computeGenericInfo() {
String genericSignature = getGenericSignature0();
if (genericSignature == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,21 +602,6 @@ public static int modifiedUTF8Length(java.lang.String string, boolean addNullTer
return result + (addNullTerminator ? 1 : 0);
}

//TODO @dprcci FINISH THAT T艂OMORROW
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static int modifiedUTF8LengthCharArray(char[] buffer, int bufferLength, boolean addNullTerminator, CharReplacer replacer) {
int result = 0;
for (int index = 0; index < bufferLength; index++) {
char ch = buffer[index];
if (replacer != null) {
ch = replacer.replace(ch);
}
result += modifiedUTF8Length(ch);
}

return result + (addNullTerminator ? 1 : 0);
}

/**
* Writes the encoded {@code string} into the given {@code buffer} using the modified UTF8
* encoding (null characters that are present in the input will be encoded in a way that
Expand Down Expand Up @@ -652,29 +637,6 @@ public static Pointer toModifiedUTF8(java.lang.String string, int stringLength,
VMError.guarantee(pos.belowOrEqual(bufferEnd), "Must not write out of bounds.");
return pos;
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static Pointer toModifiedUTF8FromCharArray(char[] charArray, int endIndex, Pointer buffer, Pointer bufferEnd, boolean addNullTerminator) {
return toModifiedUTF8FromCharArray(charArray, endIndex, buffer, bufferEnd, addNullTerminator, null);
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static Pointer toModifiedUTF8FromCharArray(char[] charArray, int endIndex, Pointer buffer, Pointer bufferEnd, boolean addNullTerminator, CharReplacer replacer) {
Pointer pos = buffer;
for (int i = 0; i < endIndex; i++) {
char ch = charArray[i];
if (replacer != null) {
ch = replacer.replace(ch);
}
pos = writeModifiedUTF8(pos, ch);
}
if (addNullTerminator) {
pos.writeByte(0, (byte) 0);
pos = pos.add(1);
}
VMError.guarantee(pos.belowOrEqual(bufferEnd), "Must not write out of bounds.");
return pos;
}
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
/**
* Information on a field that can be looked up and accessed via JNI.
*/
public final class JNIAccessibleField extends JNIAccessibleMember {
public final class JNIAccessibleField extends JNIAccessibleMember {
/* 10000000...0 */
private static final UnsignedWord ID_STATIC_FLAG = WordFactory.unsigned(-1L).unsignedShiftRight(1).add(1);
/* 01000000...0 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ boolean isPublic() {
return Modifier.isPublic(modifiers);
}

boolean isNative() {
public boolean isNative() {
return Modifier.isNative(modifiers);
}

boolean isStatic() {return Modifier.isStatic(modifiers);}
boolean isStatic() {
return Modifier.isStatic(modifiers);
}

@Platforms(HOSTED_ONLY.class)
public void finishBeforeCompilation(EconomicSet<Class<?>> hidingSubclasses, int vtableEntryOffset, CodePointer nonvirtualEntry, PointerBase newObjectEntry, CodePointer callWrapperEntry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
import static com.oracle.svm.core.SubstrateOptions.JNIVerboseLookupErrors;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.Function;

import org.graalvm.collections.EconomicMap;
Expand Down Expand Up @@ -234,75 +230,6 @@ private JNIAccessibleMethod getDeclaredMethod(Class<?> classObject, JNIAccessibl
return method;
}

public void printall(){
classesByClassObject.getKeys().forEach(System.out::println);
}
// TODO @dprcci REMOVE!
public JNIMethodId getRandomMethodID() {
Iterable<JNIAccessibleClass> accessibleClass = classesByClassObject.getValues();
ArrayList<JNIAccessibleClass> classList = new ArrayList<>();
accessibleClass.forEach(classList::add);

// Get random class
Random random = new Random();
JNIAccessibleClass randomClass = null;
if (!classList.isEmpty()) {
int randomIndex = random.nextInt(classList.size());
randomClass = classList.get(randomIndex);
}
if (randomClass == null) {
return WordFactory.nullPointer();
}
MapCursor<JNIAccessibleMethodDescriptor, JNIAccessibleMethod> cursor = randomClass.getMethods();
if (cursor.advance()) {
return toMethodID(cursor.getValue());
} else {
return WordFactory.nullPointer();
}
}

// TODO @dprcci JVMTI is this correct or should reflection be used? It is assumed the internals
// should not be exposed to the user
public JNIMethodId toMethodID(Class<?> clazz, String methodName) {
//Log.log().string(clazz.getName());
JNIAccessibleClass accessibleClass = classesByClassObject.get(clazz);
if (accessibleClass == null) {
return WordFactory.nullPointer();
}
boolean found = false;
MapCursor<JNIAccessibleMethodDescriptor, JNIAccessibleMethod> cursor = accessibleClass.getMethods();
while (!found && cursor.advance()) {
found = cursor.getKey().getName().equals(methodName);
}
return toMethodID(found ? cursor.getValue() : null);
}

// TODO @dprcci
@Platforms(HOSTED_ONLY.class)
public Set<Class<?>> getRegisteredClasses() {
Set<Class<?>> res = new HashSet<>();
classesByClassObject.getKeys().forEach(res::add);
return res;
}

// TODO @dprcci
public static boolean isMethodNative(JNIMethodId methodId) {
return getMethodByID(methodId).isNative();
}

public boolean isValidMethodIdSlow(JNIMethodId methodId){
Iterable<JNIAccessibleClass> classIterator = classesByClassObject.getValues();
for (JNIAccessibleClass c : classIterator){
MapCursor<JNIAccessibleMethodDescriptor, JNIAccessibleMethod> cursor = c.getMethods();
while(cursor.advance()){
if(toMethodID(cursor.getValue()).equal(methodId)){
return true;
}
}
}
return false;
}

public JNIMethodId getMethodID(Class<?> classObject, CharSequence name, CharSequence signature, boolean isStatic) {
JNIAccessibleMethod method = findMethod(classObject, new JNIAccessibleMethodDescriptor(name, signature), "getMethodID");
boolean match = (method != null && method.isStatic() == isStatic && method.isDiscoverableIn(classObject));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
import com.oracle.svm.core.jni.headers.JNIJavaVMOption;
import com.oracle.svm.core.jni.headers.JNIJavaVMPointer;
import com.oracle.svm.core.jni.headers.JNIVersion;
import com.oracle.svm.core.jvmti.JvmtiEnvManager;
import com.oracle.svm.core.jvmti.JvmtiEnvs;
import com.oracle.svm.core.jvmti.headers.JvmtiExternalEnv;
import com.oracle.svm.core.jvmti.headers.JvmtiVersion;
import com.oracle.svm.core.log.FunctionPointerLogHandler;
import com.oracle.svm.core.memory.UntrackedNullableNativeMemory;
Expand Down Expand Up @@ -302,14 +303,12 @@ static int DestroyJavaVM(JNIJavaVM vm) {
@SuppressWarnings("unused")
static int GetEnv(JNIJavaVM vm, WordPointer env, int version) {
if (SubstrateOptions.JVMTI.getValue() && JvmtiVersion.isSupported(version)) {
JvmtiEnvManager envManager = JvmtiEnvManager.singleton();
envManager.createJvmtiEnv(env);
JvmtiExternalEnv jvmtiEnv = JvmtiEnvs.singleton().create();
env.write(jvmtiEnv);
return JNIErrors.JNI_OK();

} else if (JNIVersion.isSupported(version)) {
env.write(JNIThreadLocalEnvironment.getAddress());
return JNIErrors.JNI_OK();

} else {
env.write(WordFactory.nullPointer());
return JNIErrors.JNI_EVERSION();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.io.Serial;
import java.util.ArrayList;

import jdk.graal.compiler.api.replacements.Fold;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand All @@ -50,6 +49,8 @@
import com.oracle.svm.core.log.Log;
import com.oracle.svm.util.StringUtil;

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

public class JvmtiAgents {
private static final String AGENT_ON_LOAD = "Agent_OnLoad";
private static final String AGENT_ON_UNLOAD = "Agent_OnUnload";
Expand All @@ -65,15 +66,13 @@ public static JvmtiAgents singleton() {
return ImageSingletons.lookup(JvmtiAgents.class);
}

// TEMP (chaeubl): we could call `Agent_OnAttach` instead - would be closer in terms of the
// semantics.
public void load() {
String agentLib = SubstrateOptions.AgentLib.getValue();
String agentLib = SubstrateOptions.JVMTIAgentLib.getValue();
if (agentLib != null) {
loadAgent(agentLib, true);
}

String agentPath = SubstrateOptions.AgentPath.getValue();
String agentPath = SubstrateOptions.JVMTIAgentPath.getValue();
if (agentPath != null) {
loadAgent(agentPath, false);
}
Expand Down Expand Up @@ -129,8 +128,6 @@ private static String getAgentFile(String agent, boolean relative) {
String libname = System.mapLibraryName(agent);
file = new File(sysPath, libname);
if (!file.exists()) {
// TEMP (chaeubl): is this really accurate? What if someone uses a custom
// LD_LIBRARY_PATH?
throw new AgentInitException("Could not find agent library '" + agent + "' on the library path.");
}
} else {
Expand Down

0 comments on commit 31a084e

Please sign in to comment.