Java Reflection permission error
Asked Answered
P

1

6

I'm trying to load a class via an URLClassLoader (well, it neither works with an normal class loader) and want them to not have any permission.

Therefore, i created my own security manager, which genereates a key on startup, which can only by requested once (in main thread). The security manager has 2 lists, the applicationThread, which will be granted any right and the temporaryList, which will be granted one right just once (it's about the reflection).

As it is very hard to descripe, i decided to upload the whole thing: look at the link below

Ok, coming back: I created a WatchDog thread, which checks if the thread doesn't take too much time.

When i now start to instance two classes from an URLClassLoader, I call exactly 30 methods without getting any errors, but on the 31st call, it tries to check Permissions for the following but this is just happaning after the 30th call.

java.lang.RuntimePermission accessClassInPackage.sun.reflect),

Does anyone know what's going on there?

edit: I had time to strip down the example. http://myxcode.at/securitymanager.zip I found out, that the SecurityManager is not asked synchronous. Just run this small piece of code and have a look at the red lines.

If the red lines come in the very first line, just run the program again, you will find out that it seems a little bit uncontrolled.

The problem more or less is, that i need the security manager to be synchronized. Here is my output for those who cannot face the error(bug?) http://pastebin.com/E9yLRLif

edit2: maybe its about the console? maybe the console is too slow?

Pyretic answered 31/10, 2011 at 0:36 Comment(1)
Boil your problem down to a small, concise, complete example and post it here.Cooncan
S
5

For me the check occurs when i=15:

checkPermission ( (java.lang.RuntimePermission accessClassInPackage.sun.reflect) ) for Thread[main,5,main]

The reason for the delayed permission check is an inflationThreshold of the ReflectionFactory class which is used by the invoke method of NativeMethodAccessorImpl.java:

public Object invoke(Object obj, Object[] args)
        throws IllegalArgumentException, InvocationTargetException {
    if (++numInvocations > ReflectionFactory.inflationThreshold()) {
        MethodAccessorImpl acc = (MethodAccessorImpl) new MethodAccessorGenerator()
                .generateMethod(method.getDeclaringClass(), method
                        .getName(), method.getParameterTypes(),
                        method.getReturnType(), method
                                .getExceptionTypes(), method
                                .getModifiers());
        parent.setDelegate(acc);
    }

    return invoke0(method, obj, args);
}

To disable the delay you could use Reflection API :)

Field hack = Class.forName("sun.reflect.ReflectionFactory").getDeclaredField("inflationThreshold");
hack.setAccessible(true);
hack.set(null, 0);
Seeger answered 2/11, 2011 at 23:18 Comment(1)
My JDK version is 1.6.0_23 btwSeeger

© 2022 - 2024 — McMap. All rights reserved.