jbonofre commented on code in PR #2870:
URL: https://github.com/apache/karaf/pull/2870#discussion_r3991000916
##########
services/interceptor/impl/src/main/java/org/apache/karaf/service/interceptor/impl/runtime/proxy/AsmProxyFactory.java:
##########
@@ -114,19 +114,31 @@ private boolean hasSameSignature(Method a, Method b) {
private void createConstructor(final ClassWriter cw, final String
proxyClassFileName, final Class<?> classToProxy,
final String classFileName) {
- Constructor superDefaultCt;
- String parentClassFileName = classFileName;
- String descriptor = "()V";
-
- try {
- if (classToProxy.isInterface()) {
- parentClassFileName = Type.getInternalName(Object.class);
- superDefaultCt = Object.class.getConstructor(null);
- descriptor = Type.getConstructorDescriptor(superDefaultCt);
+ // the proxy extends the proxied class, or Object when proxying an
interface; either way
+ // the super constructor it invokes is the no-arg one
+ final String parentClassFileName;
+ if (classToProxy.isInterface()) {
+ parentClassFileName = Type.getInternalName(Object.class);
+ } else {
+ parentClassFileName = classFileName;
+ // without these checks the generated INVOKESPECIAL would only
fail once the proxy is
+ // instantiated. The proxy is defined by its own class loader, so
it lands in a different
+ // runtime package than the proxied class: only a public or
protected constructor is
+ // reachable from it, a package-private one is not.
+ final Constructor<?> superCt;
+ try {
+ superCt = classToProxy.getDeclaredConstructor();
+ } catch (final NoSuchMethodException nsme) {
+ throw new IllegalArgumentException("Cannot proxy " +
classToProxy.getName()
+ + ", it has no no-arg constructor", nsme);
+ }
+ final int modifiers = superCt.getModifiers();
+ if (!Modifier.isPublic(modifiers) &&
!Modifier.isProtected(modifiers)) {
Review Comment:
This only checks the constructor own modifiers, not whether `classToProxy`
itself is accessible to a class defined by a different `ClassLoader`. A
package-private class (common for OSGi impl classes) with a public/protected
no-arg constructor passes this check, but the proxy (defined by its own
`ProxyClassLoader`, hence a different runtime package even with an identical
package name) still can't legally extend it.
That fails later with an uncaught `IllegalAccessError` at class-link time,
which is exactly the failure mode this PR set out to prevent.
I suggest also checking `Modifier.isPublic(classToProxy.getModifiers())`
here. Thoughts?
##########
services/interceptor/impl/src/main/java/org/apache/karaf/service/interceptor/impl/runtime/proxy/AsmProxyFactory.java:
##########
@@ -114,19 +114,31 @@ private boolean hasSameSignature(Method a, Method b) {
private void createConstructor(final ClassWriter cw, final String
proxyClassFileName, final Class<?> classToProxy,
final String classFileName) {
- Constructor superDefaultCt;
- String parentClassFileName = classFileName;
- String descriptor = "()V";
-
- try {
- if (classToProxy.isInterface()) {
- parentClassFileName = Type.getInternalName(Object.class);
- superDefaultCt = Object.class.getConstructor(null);
- descriptor = Type.getConstructorDescriptor(superDefaultCt);
+ // the proxy extends the proxied class, or Object when proxying an
interface; either way
+ // the super constructor it invokes is the no-arg one
+ final String parentClassFileName;
+ if (classToProxy.isInterface()) {
+ parentClassFileName = Type.getInternalName(Object.class);
+ } else {
+ parentClassFileName = classFileName;
+ // without these checks the generated INVOKESPECIAL would only
fail once the proxy is
+ // instantiated. The proxy is defined by its own class loader, so
it lands in a different
+ // runtime package than the proxied class: only a public or
protected constructor is
+ // reachable from it, a package-private one is not.
+ final Constructor<?> superCt;
+ try {
+ superCt = classToProxy.getDeclaredConstructor();
Review Comment:
`getDeclaredConstructor()` forces the JVM to resolve **all** declared
constructors of `classToProxy`, not just the no-arg one: previously this branch
never reflected on constructors at all.
If `classToProxy` has an unrelated constructor whose parameter type lives in
an optional/unresolvable OSGi bundle, this now throws an uncaught
`NoClassDefFoundError` where proxying used to just work.
That's a regression risk in exactly the split-classpath environment Karaf
runs in. Worth maybe a test with such a class before merging. Thoughts?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]