zoukankan      html  css  js  c++  java
  • 并发编程2-2

    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;
    }
    
  • 相关阅读:
    安全工具
    WebRTC媒体协商及实践
    流媒体协议介绍(rtp/rtcp/rtsp/rtmp/mms/hls)
    基于 WebRTC 技术的实时通信服务开发实践
    实时音视频互动系列(下):基于 WebRTC 技术的实战解析
    WebRTC基于浏览器的开发
    webRtc+websocket多人视频通话
    Android IOS WebRTC 音视频开发总结(四九) ffmpeg介绍
    Android IOS WebRTC 音视频开发总结(二五) webrtc优秀资源汇总
    The 3n + 1 problem UVA 100
  • 原文地址:https://www.cnblogs.com/sunshinekevin/p/13877429.html
Copyright © 2011-2022 走看看