zoukankan      html  css  js  c++  java
  • 关于通过Unsafe.getUnsafe()方法拿Unsafe对象抛出SecurityException异常的原因

    众所周知,Java中的Unsafe对象可以直接调用内存,而且CAS等原子性操作中,也都是使用Unsafe对象,但当我们自己准备去使用Unsafe.getUnsafe()函数获取Unsafe对象时,却会抛出SecurityException:Unsafe异常,原因是因为双亲委派制的保护机制
    我们看一下Unsafe.getUnsafe()函数的源码:
    @CallerSensitive
    public static Unsafe getUnsafe() {
        // ----- 这里去获取当前类的ClassLoader加载器
        Class var0 = Reflection.getCallerClass();
        // ----- 判断var0是不是BootstrapClassLoader
        if (!VM.isSystemDomainLoader(var0.getClassLoader())) {
            // ----- 否:抛出SecurityException异常
            throw new SecurityException("Unsafe");
        } else {
            // ----- 是:返回unsafe对象
            return theUnsafe;
        }
    }
    
    Class.getClassLoader()源码
    @CallerSensitive
    public ClassLoader getClassLoader() {
        ClassLoader cl = getClassLoader0();
        if (cl == null)
            return null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
        }
        return cl;
    }
    
    ClassLoader.checkClassLoaderPermission()源码
    static void checkClassLoaderPermission(ClassLoader cl, Class<?> caller) {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            // caller can be null if the VM is requesting it
            ClassLoader ccl = getClassLoader(caller);
            if (needsClassLoaderPermissionCheck(ccl, cl)) {
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
            }
        }
    }
    
    VM.isSystemDomainLoader()源码
    public static boolean isSystemDomainLoader(ClassLoader var0) {
        // ----- 重点是在这里:
        // --- 当结果为true时:说明var0是Bootstrap类加载器,
        // -- 当结果为false时:说明var0是Extension || App || Custom 等类加载器
        // ----- 所以回到getUnsafe()函数,当这个函数返回false时,会直接抛异常,不允许加载Unsafe
        return var0 == null;
    }
    
  • 相关阅读:
    TensorFlow学习笔记之四——源码分析之基本操作
    TensorFlow学习笔记之三——适合入门的一些资源
    AW286 选课(背包类树形DP)
    AW303 运输小猫
    AW302 任务安排3
    AW301 任务安排2 (斜率优化DP)
    AW300 任务安排1
    AW311 月之谜
    AW310 启示录
    AW292 炮兵阵地
  • 原文地址:https://www.cnblogs.com/yanwu0527/p/13036330.html
Copyright © 2011-2022 走看看