Java反射权限错误

发布于 2024-12-13 05:46:09 字数 877 浏览 2 评论 0 原文

我试图通过 URLClassLoader 加载一个类(嗯,它不适用于普通的类加载器)并希望它们没有任何权限。

因此,我创建了自己的安全管理器,它在启动时生成一个密钥,只能请求一次(在主线程中)。 安全管理器有 2 个列表,applicationThread,它将被授予任何权限;temporaryList,将被授予一次权限(与反射有关)。

由于很难描述,我决定上传整个内容:查看下面的链接

好的,回来:我创建了一个 WatchDog 线程,它检查线程是否不需要太多时间时间。

当我现在开始从 URLClassLoader 实例化两个类时,我调用了 30 个方法,没有出现任何错误,但在第 31 次调用时,它尝试检查以下内容的权限,但这只是在第 30 次调用之后发生。

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

有谁知道那里发生了什么事吗?

编辑: 我有时间简化这个例子。 http://myxcode.at/securitymanager.zip 我发现 SecurityManager 没有要求同步。只需运行这一小段代码并查看红线即可。

如果红线出现在第一行,再运行一下程序,你会发现它似乎有点不受控制。

问题或多或少是,我需要同步安全管理器。 这是我为那些无法面对错误(错误?)的人提供的输出 http://pastebin.com/E9yLRLif

edit2:也许与控制台有关?也许控制台太慢了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2024-12-20 05:46:09

对我来说,检查发生在 i=15 时:

线程[main,5,main]的checkPermission ((java.lang.RuntimePermission accessClassInPackage.sun.reflect))

延迟权限检查的原因是 ReflectionFactoryinflationThreshold > 由 invoke 方法使用的类href="http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/reflect/sun/reflect/NativeMethodAccessorImpl.java.htm" rel="noreferrer"><代码> 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);
}

要禁用延迟,您可以使用 Reflection API :)

Field hack = Class.forName("sun.reflect.ReflectionFactory").getDeclaredField("inflationThreshold");
hack.setAccessible(true);
hack.set(null, 0);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文