Skip to content

Commit 1c769ac

Browse files
committed
[GR-60879] Revert "Define both native and Java breakpoints to trace Class#getSigners."
PullRequest: graal/19746
2 parents 622bf5d + fe4e328 commit 1c769ac

File tree

1 file changed

+22
-58
lines changed

1 file changed

+22
-58
lines changed

substratevm/src/com.oracle.svm.agent/src/com/oracle/svm/agent/BreakpointInterceptor.java

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -317,13 +317,9 @@ private static boolean getSigners(JNIEnvironment jni, JNIObjectHandle thread, Br
317317
return handleGetClasses(jni, thread, bp, state);
318318
}
319319

320-
private static boolean handleGetClasses(JNIEnvironment jni, JNIObjectHandle thread, AbstractBreakpoint<?> bp, InterceptedState state) {
321-
JNIObjectHandle self = getReceiver(thread);
322-
return handleGetClassesWithReceiver(jni, self, bp, state);
323-
}
324-
325-
private static boolean handleGetClassesWithReceiver(JNIEnvironment jni, JNIObjectHandle self, AbstractBreakpoint<?> bp, InterceptedState state) {
320+
private static boolean handleGetClasses(JNIEnvironment jni, JNIObjectHandle thread, Breakpoint bp, InterceptedState state) {
326321
JNIObjectHandle callerClass = state.getDirectCallerClass();
322+
JNIObjectHandle self = getReceiver(thread);
327323
traceReflectBreakpoint(jni, self, nullHandle(), callerClass, bp.specification.methodName, null, state.getFullStackTraceOrNull());
328324
return true;
329325
}
@@ -370,10 +366,10 @@ private interface AllocateInstanceFunctionPointer extends CFunctionPointer {
370366
@CEntryPoint
371367
@CEntryPointOptions(prologue = AgentIsolate.Prologue.class)
372368
static long nativeAllocateInstance(JNIEnvironment jni, JNIObjectHandle self, JNIObjectHandle clazz) {
373-
NativeBreakpoint breakpoint = NATIVE_ALLOCATE_INSTANCE_BREAKPOINT_SPEC.installed;
374-
VMError.guarantee(breakpoint != null && breakpoint.replacedFunction.isNonNull(), "incompletely installed");
369+
VMError.guarantee(NATIVE_ALLOCATE_INSTANCE_BREAKPOINT_SPEC.installed != null &&
370+
NATIVE_ALLOCATE_INSTANCE_BREAKPOINT_SPEC.installed.replacedFunction.isNonNull(), "incompletely installed");
375371

376-
AllocateInstanceFunctionPointer original = (AllocateInstanceFunctionPointer) breakpoint.replacedFunction;
372+
AllocateInstanceFunctionPointer original = (AllocateInstanceFunctionPointer) NATIVE_ALLOCATE_INSTANCE_BREAKPOINT_SPEC.installed.replacedFunction;
377373
long result = original.invoke(jni, self, clazz);
378374
if (!Support.isInitialized()) { // in case of a (very) late call
379375
return result;
@@ -396,36 +392,6 @@ private static void traceAllocateInstance(JNIEnvironment jni, JNIObjectHandle cl
396392
}
397393
}
398394

399-
private static final CEntryPointLiteral<AllocateInstanceFunctionPointer> nativeGetSigners = CEntryPointLiteral.create(
400-
BreakpointInterceptor.class, "nativeGetSigners", JNIEnvironment.class, JNIObjectHandle.class);
401-
private static final NativeBreakpointSpecification NATIVE_GET_SIGNERS_BREAKPOINT_SPEC = new NativeBreakpointSpecification(
402-
"java/lang/Class", "getSigners", "()[Ljava/lang/Object;", nativeGetSigners);
403-
404-
private interface GetSignersFunctionPointer extends CFunctionPointer {
405-
@InvokeCFunctionPointer
406-
JNIObjectHandle invoke(JNIEnvironment jni, JNIObjectHandle self);
407-
}
408-
409-
/**
410-
* Before Java 24, {@code java/lang/Class#getSigners} was declared as a native method, so we use
411-
* a native breakpoint to trace it. Either this breakpoint or the Java breakpoint will be set
412-
* (but not both).
413-
*/
414-
@CEntryPoint
415-
@CEntryPointOptions(prologue = AgentIsolate.Prologue.class)
416-
static JNIObjectHandle nativeGetSigners(JNIEnvironment jni, JNIObjectHandle self) {
417-
NativeBreakpoint breakpoint = NATIVE_GET_SIGNERS_BREAKPOINT_SPEC.installed;
418-
VMError.guarantee(breakpoint != null && breakpoint.replacedFunction.isNonNull(), "incompletely installed");
419-
GetSignersFunctionPointer original = (GetSignersFunctionPointer) breakpoint.replacedFunction;
420-
JNIObjectHandle result = original.invoke(jni, self);
421-
if (!Support.isInitialized()) {
422-
return result;
423-
}
424-
InterceptedState state = interceptedStateSupplier.get();
425-
handleGetClassesWithReceiver(jni, self, breakpoint, state);
426-
return result;
427-
}
428-
429395
private static boolean objectFieldOffsetByName(JNIEnvironment jni, JNIObjectHandle thread, Breakpoint bp, InterceptedState state) {
430396
JNIObjectHandle callerClass = state.getDirectCallerClass();
431397
JNIObjectHandle self = getReceiver(thread);
@@ -1360,13 +1326,17 @@ public static void onLoad(JvmtiEnv jvmti, JvmtiEventCallbacks callbacks, Tracer
13601326
BreakpointInterceptor.experimentalClassDefineSupport = exptlClassDefineSupport;
13611327
BreakpointInterceptor.experimentalUnsafeAllocationSupport = exptlUnsafeAllocationSupport;
13621328
BreakpointInterceptor.trackReflectionMetadata = trackReflectionData;
1363-
BreakpointInterceptor.boundNativeMethods = new HashMap<>();
13641329

13651330
JvmtiCapabilities capabilities = UnmanagedMemory.calloc(SizeOf.get(JvmtiCapabilities.class));
13661331
check(jvmti.getFunctions().GetCapabilities().invoke(jvmti, capabilities));
13671332
capabilities.setCanGenerateBreakpointEvents(1);
13681333
capabilities.setCanAccessLocalVariables(1);
1369-
capabilities.setCanGenerateNativeMethodBindEvents(1);
1334+
1335+
if (exptlUnsafeAllocationSupport) {
1336+
capabilities.setCanGenerateNativeMethodBindEvents(1);
1337+
callbacks.setNativeMethodBind(onNativeMethodBindLiteral.getFunctionPointer());
1338+
BreakpointInterceptor.boundNativeMethods = new HashMap<>();
1339+
}
13701340

13711341
if (exptlClassLoaderSupport) {
13721342
capabilities.setCanGetBytecodes(1);
@@ -1378,10 +1348,8 @@ public static void onLoad(JvmtiEnv jvmti, JvmtiEventCallbacks callbacks, Tracer
13781348
}
13791349
check(jvmti.getFunctions().AddCapabilities().invoke(jvmti, capabilities));
13801350
UnmanagedMemory.free(capabilities);
1381-
check(jvmti.getFunctions().SetEventNotificationMode().invoke(jvmti, JvmtiEventMode.JVMTI_ENABLE, JVMTI_EVENT_NATIVE_METHOD_BIND, nullHandle()));
13821351

13831352
callbacks.setBreakpoint(onBreakpointLiteral.getFunctionPointer());
1384-
callbacks.setNativeMethodBind(onNativeMethodBindLiteral.getFunctionPointer());
13851353

13861354
if (exptlClassDefineSupport) {
13871355
callbacks.setClassFileLoadHook(onClassFileLoadHookLiteral.getFunctionPointer());
@@ -1391,6 +1359,9 @@ public static void onLoad(JvmtiEnv jvmti, JvmtiEventCallbacks callbacks, Tracer
13911359
callbacks.setClassPrepare(onClassPrepareLiteral.getFunctionPointer());
13921360
}
13931361

1362+
if (exptlUnsafeAllocationSupport) {
1363+
Support.check(jvmti.getFunctions().SetEventNotificationMode().invoke(jvmti, JvmtiEventMode.JVMTI_ENABLE, JVMTI_EVENT_NATIVE_METHOD_BIND, nullHandle()));
1364+
}
13941365
}
13951366

13961367
public static void onVMInit(JvmtiEnv jvmti, JNIEnvironment jni) {
@@ -1441,7 +1412,9 @@ public static void onVMInit(JvmtiEnv jvmti, JNIEnvironment jni) {
14411412
}
14421413
installedBreakpoints = breakpoints;
14431414

1444-
setupNativeBreakpoints(jni, lastClass, lastClassName);
1415+
if (experimentalUnsafeAllocationSupport) {
1416+
setupNativeBreakpoints(jni, lastClass, lastClassName);
1417+
}
14451418

14461419
if (experimentalClassDefineSupport) {
14471420
setupClassLoadEvent(jvmti, jni);
@@ -1458,12 +1431,8 @@ private static void setupNativeBreakpoints(JNIEnvironment jni, JNIObjectHandle p
14581431
String lastClassName = previousClassName;
14591432
nativeBreakpointsInitLock.lock();
14601433
try {
1461-
List<NativeBreakpointSpecification> nativeBreakpointsList = new ArrayList<>(Arrays.asList(NATIVE_BREAKPOINT_SPECIFICATIONS));
1462-
if (experimentalUnsafeAllocationSupport) {
1463-
nativeBreakpointsList.addAll(Arrays.asList(EXPERIMENTAL_UNSAFE_ALLOCATION_NATIVE_BREAKPOINT_SPECIFICATIONS));
1464-
}
1465-
nativeBreakpoints = new HashMap<>(nativeBreakpointsList.size());
1466-
for (NativeBreakpointSpecification br : nativeBreakpointsList) {
1434+
nativeBreakpoints = new HashMap<>(NATIVE_BREAKPOINT_SPECIFICATIONS.length);
1435+
for (NativeBreakpointSpecification br : NATIVE_BREAKPOINT_SPECIFICATIONS) {
14671436
JNIObjectHandle clazz;
14681437
if (lastClassName != null && lastClassName.equals(br.className)) {
14691438
clazz = lastClass;
@@ -1477,8 +1446,7 @@ private static void setupNativeBreakpoints(JNIEnvironment jni, JNIObjectHandle p
14771446
NativeBreakpoint bp = new NativeBreakpoint(br, clazz, method);
14781447
nativeBreakpoints.put(method.rawValue(), bp);
14791448
Long original = boundNativeMethods.get(method.rawValue());
1480-
// only bind a native breakpoint if method was bound as a native method
1481-
if (original != null) {
1449+
if (original != null) { // already bound, replace
14821450
bindNativeBreakpoint(jni, bp, WordFactory.pointer(original), nullPointer());
14831451
}
14841452
}
@@ -1742,7 +1710,7 @@ private interface BreakpointHandler {
17421710
optionalBrk("java/lang/Class", "getNestMembers", "()[Ljava/lang/Class;",
17431711
BreakpointInterceptor::getNestMembers),
17441712
optionalBrk("java/lang/Class", "getSigners", "()[Ljava/lang/Object;",
1745-
BreakpointInterceptor::getSigners),
1713+
BreakpointInterceptor::getSigners)
17461714
};
17471715

17481716
private static boolean allocateInstance(JNIEnvironment jni, JNIObjectHandle thread, @SuppressWarnings("unused") Breakpoint bp, InterceptedState state) {
@@ -1756,10 +1724,6 @@ private static boolean allocateInstance(JNIEnvironment jni, JNIObjectHandle thre
17561724
"(Ljava/lang/String;)Ljava/lang/Class;", BreakpointInterceptor::loadClass);
17571725

17581726
private static final NativeBreakpointSpecification[] NATIVE_BREAKPOINT_SPECIFICATIONS = {
1759-
NATIVE_GET_SIGNERS_BREAKPOINT_SPEC,
1760-
};
1761-
1762-
private static final NativeBreakpointSpecification[] EXPERIMENTAL_UNSAFE_ALLOCATION_NATIVE_BREAKPOINT_SPECIFICATIONS = {
17631727
NATIVE_ALLOCATE_INSTANCE_BREAKPOINT_SPEC
17641728
};
17651729

0 commit comments

Comments
 (0)