Java Code Examples for sun.misc.SharedSecrets

The following examples show how to use sun.misc.SharedSecrets. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jdk8u60   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void runAllFinalizers() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f;
                synchronized (lock) {
                    f = unfinalized;
                    if (f == null) break;
                    unfinalized = f.next;
                }
                f.runFinalizer(jla);
            }}});
}
 
Example 2
Source Project: jdk8u-dev-jdk   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void runFinalization() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f = (Finalizer)queue.poll();
                if (f == null) break;
                f.runFinalizer(jla);
            }
        }
    });
}
 
Example 3
Source Project: jdk8u_jdk   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void runFinalization() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            // in case of recursive call to run()
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f = (Finalizer)queue.poll();
                if (f == null) break;
                f.runFinalizer(jla);
            }
        }
    });
}
 
Example 4
Source Project: openjdk-jdk8u   Source File: PriorityQueue.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reconstitutes the {@code PriorityQueue} instance from a stream
 * (that is, deserializes it).
 *
 * @param s the stream
 */
private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException {
    // Read in size, and any hidden stuff
    s.defaultReadObject();

    // Read in (and discard) array length
    s.readInt();

    SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, size);
    queue = new Object[size];

    // Read in all elements.
    for (int i = 0; i < size; i++)
        queue[i] = s.readObject();

    // Elements are guaranteed to be in "proper order", but the
    // spec has never explained what that might be.
    heapify();
}
 
Example 5
Source Project: jdk-1.7-annotated   Source File: Finalizer.java    License: Apache License 2.0 6 votes vote down vote up
static void runFinalization() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f = (Finalizer)queue.poll();
                if (f == null) break;
                f.runFinalizer(jla);
            }
        }
    });
}
 
Example 6
public static void main(String[]args) throws Exception {
    final DomainCombiner dc = new DomainCombiner() {
        @Override
        public ProtectionDomain[] combine(ProtectionDomain[] currentDomains, ProtectionDomain[] assignedDomains) {
            return currentDomains; // basically a no-op
        }
    };

    // Get an instance of the saved ACC
    AccessControlContext saved = AccessController.getContext();
    // Simulate the stack ACC with a DomainCombiner attached
    AccessControlContext stack = new AccessControlContext(AccessController.getContext(), dc);

    // Now try to run JavaSecurityAccess.doIntersectionPrivilege() and assert
    // whether the DomainCombiner from the stack ACC is preserved
    boolean ret = SharedSecrets.getJavaSecurityAccess().doIntersectionPrivilege(new PrivilegedAction<Boolean>() {
        @Override
        public Boolean run() {
            return dc == AccessController.getContext().getDomainCombiner();
        }
    }, stack, saved);

    if (!ret) {
        System.exit(1);
    }
}
 
Example 7
Source Project: dragonwell8_jdk   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void forkSecondaryFinalizer(final Runnable proc) {
    AccessController.doPrivileged(
        new PrivilegedAction<Void>() {
            public Void run() {
                ThreadGroup tg = Thread.currentThread().getThreadGroup();
                for (ThreadGroup tgn = tg;
                     tgn != null;
                     tg = tgn, tgn = tg.getParent());
                Thread sft = new Thread(tg, proc, "Secondary finalizer");

                if (TenantGlobals.isDataIsolationEnabled() && TenantContainer.current() != null) {
                    SharedSecrets.getTenantAccess()
                            .registerServiceThread(TenantContainer.current(), sft);
                }

                sft.start();
                try {
                    sft.join();
                } catch (InterruptedException x) {
                    Thread.currentThread().interrupt();
                }
                return null;
            }});
}
 
Example 8
Source Project: jdk8u-jdk   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void runAllFinalizers() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f;
                synchronized (lock) {
                    f = unfinalized;
                    if (f == null) break;
                    unfinalized = f.next;
                }
                f.runFinalizer(jla);
            }}});
}
 
Example 9
/**
 * Reconstitutes the {@code PriorityQueue} instance from a stream
 * (that is, deserializes it).
 *
 * @param s the stream
 */
private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException {
    // Read in size, and any hidden stuff
    s.defaultReadObject();

    // Read in (and discard) array length
    s.readInt();

    SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, size);
    queue = new Object[size];

    // Read in all elements.
    for (int i = 0; i < size; i++)
        queue[i] = s.readObject();

    // Elements are guaranteed to be in "proper order", but the
    // spec has never explained what that might be.
    heapify();
}
 
Example 10
static void runFinalization() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            // in case of recursive call to run()
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f = (Finalizer)queue.poll();
                if (f == null) break;
                f.runFinalizer(jla);
            }
        }
    });
}
 
Example 11
public static void main(String[]args) throws Exception {
    final DomainCombiner dc = new DomainCombiner() {
        @Override
        public ProtectionDomain[] combine(ProtectionDomain[] currentDomains, ProtectionDomain[] assignedDomains) {
            return currentDomains; // basically a no-op
        }
    };

    // Get an instance of the saved ACC
    AccessControlContext saved = AccessController.getContext();
    // Simulate the stack ACC with a DomainCombiner attached
    AccessControlContext stack = new AccessControlContext(AccessController.getContext(), dc);

    // Now try to run JavaSecurityAccess.doIntersectionPrivilege() and assert
    // whether the DomainCombiner from the stack ACC is preserved
    boolean ret = SharedSecrets.getJavaSecurityAccess().doIntersectionPrivilege(new PrivilegedAction<Boolean>() {
        @Override
        public Boolean run() {
            return dc == AccessController.getContext().getDomainCombiner();
        }
    }, stack, saved);

    if (!ret) {
        System.exit(1);
    }
}
 
Example 12
/**
 * Reconstitutes the <tt>IdentityHashMap</tt> instance from a stream (i.e.,
 * deserializes it).
 */
private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException  {
    // Read in any hidden stuff
    s.defaultReadObject();

    // Read in size (number of Mappings)
    int size = s.readInt();
    if (size < 0)
        throw new java.io.StreamCorruptedException
            ("Illegal mappings count: " + size);
    int cap = capacity(size);
    SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, cap);
    init(cap);

    // Read the keys and values, and put the mappings in the table
    for (int i=0; i<size; i++) {
        @SuppressWarnings("unchecked")
            K key = (K) s.readObject();
        @SuppressWarnings("unchecked")
            V value = (V) s.readObject();
        putForCreate(key, value);
    }
}
 
Example 13
Source Project: openjdk-jdk8u   Source File: PolicyFile.java    License: GNU General Public License v2.0 6 votes vote down vote up
PolicyInfo(int numCaches) {
    policyEntries = new ArrayList<>();
    identityPolicyEntries =
        Collections.synchronizedList(new ArrayList<PolicyEntry>(2));
    aliasMapping = Collections.synchronizedMap(new HashMap<>(11));

    pdMapping = new ProtectionDomainCache[numCaches];
    JavaSecurityProtectionDomainAccess jspda
        = SharedSecrets.getJavaSecurityProtectionDomainAccess();
    for (int i = 0; i < numCaches; i++) {
        pdMapping[i] = jspda.getProtectionDomainCache();
    }
    if (numCaches > 1) {
        random = new java.util.Random();
    }
}
 
Example 14
/**
 * Reconstitutes this list from a stream (that is, deserializes it).
 * @param s the stream
 * @throws ClassNotFoundException if the class of a serialized object
 *         could not be found
 * @throws java.io.IOException if an I/O error occurs
 */
private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException {

    s.defaultReadObject();

    // bind to new lock
    resetLock();

    // Read in array length and allocate array
    int len = s.readInt();
    SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, len);
    Object[] elements = new Object[len];

    // Read in all elements in the proper order.
    for (int i = 0; i < len; i++)
        elements[i] = s.readObject();
    setArray(elements);
}
 
Example 15
Source Project: openjdk-8   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void runFinalization() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f = (Finalizer)queue.poll();
                if (f == null) break;
                f.runFinalizer(jla);
            }
        }
    });
}
 
Example 16
Source Project: jdk8u_jdk   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void runAllFinalizers() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            // in case of recursive call to run()
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f;
                synchronized (lock) {
                    f = unfinalized;
                    if (f == null) break;
                    unfinalized = f.next;
                }
                f.runFinalizer(jla);
            }}});
}
 
Example 17
Source Project: jdk8u60   Source File: SSLSocketImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static String getOriginalHostname(InetAddress inetAddress) {
    /*
     * Get the original hostname via sun.misc.SharedSecrets.
     */
    JavaNetAccess jna = SharedSecrets.getJavaNetAccess();
    String originalHostname = jna.getOriginalHostName(inetAddress);

    /*
     * If no application specified hostname, use the IP address.
     */
    if (originalHostname == null || originalHostname.length() == 0) {
        originalHostname = inetAddress.getHostAddress();
    }

    return originalHostname;
}
 
Example 18
Source Project: JDKSourceCode1.8   Source File: IdentityHashMap.java    License: MIT License 6 votes vote down vote up
/**
 * Reconstitutes the <tt>IdentityHashMap</tt> instance from a stream (i.e.,
 * deserializes it).
 */
private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException  {
    // Read in any hidden stuff
    s.defaultReadObject();

    // Read in size (number of Mappings)
    int size = s.readInt();
    if (size < 0)
        throw new java.io.StreamCorruptedException
            ("Illegal mappings count: " + size);
    int cap = capacity(size);
    SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, cap);
    init(cap);

    // Read the keys and values, and put the mappings in the table
    for (int i=0; i<size; i++) {
        @SuppressWarnings("unchecked")
            K key = (K) s.readObject();
        @SuppressWarnings("unchecked")
            V value = (V) s.readObject();
        putForCreate(key, value);
    }
}
 
Example 19
Source Project: openjdk-jdk8u   Source File: CheckArrayTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test SharedSecrets checkArray with an ObjectInputStream subclassed to
 * handle all input stream functions.
 */
@Test(dataProvider = "Patterns")
public void subclassedOIS(String pattern, int arraySize, Object[] array) throws IOException {
    byte[] bytes = SerialFilterTest.writeObjects(array);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
         ObjectInputStream ois = new MyInputStream(bais)) {
        // Check the arraysize against the filter
        ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern);
        ObjectInputFilter.Config.setObjectInputFilter(ois, filter);
        SharedSecrets.getJavaOISAccess()
                .checkArray(ois, array.getClass(), arraySize);
        Assert.assertTrue(array.length >= arraySize,
                "Should have thrown InvalidClassException due to array size");
    } catch (InvalidClassException ice) {
        Assert.assertFalse(array.length > arraySize,
                "Should NOT have thrown InvalidClassException due to array size");
    }
}
 
Example 20
Source Project: TencentKona-8   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void runFinalization() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            // in case of recursive call to run()
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f = (Finalizer)queue.poll();
                if (f == null) break;
                f.runFinalizer(jla);
            }
        }
    });
}
 
Example 21
Source Project: jdk8u-dev-jdk   Source File: Finalizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void runAllFinalizers() {
    if (!VM.isBooted()) {
        return;
    }

    forkSecondaryFinalizer(new Runnable() {
        private volatile boolean running;
        public void run() {
            if (running)
                return;
            final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
            running = true;
            for (;;) {
                Finalizer f;
                synchronized (lock) {
                    f = unfinalized;
                    if (f == null) break;
                    unfinalized = f.next;
                }
                f.runFinalizer(jla);
            }}});
}
 
Example 22
Source Project: hmftools   Source File: ArrayDeck.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Reconstitutes this deque from a stream (that is, deserializes it).
 */
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
    s.defaultReadObject();

    // Read in size and allocate array
    int size = s.readInt();
    int capacity = calculateSize(size);
    SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, capacity);
    allocateElements(size);
    head = 0;
    tail = size;

    // Read in all elements in the proper order.
    for (int i = 0; i < size; i++) {
        elements[i] = s.readObject();
    }
}
 
Example 23
Source Project: JDKSourceCode1.8   Source File: ArrayList.java    License: MIT License 6 votes vote down vote up
/**
 * 从流中重构ArrayList实例(即反序列化)。
 */
private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
    elementData = EMPTY_ELEMENTDATA;

    // 执行默认的序列化/反序列化过程
    s.defaultReadObject();

    // 读入数组长度
    s.readInt(); // ignored

    if (size > 0) {
        // 像clone()方法 ,但根据大小而不是容量分配数组
        int capacity = calculateCapacity(elementData, size);
        SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, capacity);
        ensureCapacityInternal(size);

        Object[] a = elementData;
        //读入所有元素
        for (int i = 0; i < size; i++) {
            a[i] = s.readObject();
        }
    }
}
 
Example 24
/**
 * Reconstitutes this list from a stream (that is, deserializes it).
 * @param s the stream
 * @throws ClassNotFoundException if the class of a serialized object
 *         could not be found
 * @throws java.io.IOException if an I/O error occurs
 */
private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException {

    s.defaultReadObject();

    // bind to new lock
    resetLock();

    // Read in array length and allocate array
    int len = s.readInt();
    SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, len);
    Object[] elements = new Object[len];

    // Read in all elements in the proper order.
    for (int i = 0; i < len; i++)
        elements[i] = s.readObject();
    setArray(elements);
}
 
Example 25
Source Project: TencentKona-8   Source File: ArrayList.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
 * deserialize it).
 */
private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException {
    elementData = EMPTY_ELEMENTDATA;

    // Read in size, and any hidden stuff
    s.defaultReadObject();

    // Read in capacity
    s.readInt(); // ignored

    if (size > 0) {
        // be like clone(), allocate array based upon size not capacity
        int capacity = calculateCapacity(elementData, size);
        SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, capacity);
        ensureCapacityInternal(size);

        Object[] a = elementData;
        // Read in all elements in the proper order.
        for (int i=0; i<size; i++) {
            a[i] = s.readObject();
        }
    }
}
 
Example 26
FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {
    // on Windows we need to keep a handle to the file
    JavaIOFileDescriptorAccess fdAccess =
        SharedSecrets.getJavaIOFileDescriptorAccess();
    FileDescriptor result = new FileDescriptor();
    long handle = duplicateHandle(fdAccess.getHandle(fd));
    fdAccess.setHandle(result, handle);
    return result;
}
 
Example 27
Source Project: jdk1.8-source-analysis   Source File: LogManager.java    License: Apache License 2.0 5 votes vote down vote up
private LoggerContext getUserContext() {
    LoggerContext context = null;

    SecurityManager sm = System.getSecurityManager();
    JavaAWTAccess javaAwtAccess = SharedSecrets.getJavaAWTAccess();
    if (sm != null && javaAwtAccess != null) {
        // for each applet, it has its own LoggerContext isolated from others
        final Object ecx = javaAwtAccess.getAppletContext();
        if (ecx != null) {
            synchronized (javaAwtAccess) {
                // find the AppContext of the applet code
                // will be null if we are in the main app context.
                if (contextsMap == null) {
                    contextsMap = new WeakHashMap<>();
                }
                context = contextsMap.get(ecx);
                if (context == null) {
                    // Create a new LoggerContext for the applet.
                    context = new LoggerContext();
                    contextsMap.put(ecx, context);
                }
            }
        }
    }
    // for standalone app, return userContext
    return context != null ? context : userContext;
}
 
Example 28
Source Project: jdk1.8-source-analysis   Source File: LogRecord.java    License: Apache License 2.0 5 votes vote down vote up
private void inferCaller() {
    needToInferCaller = false;
    JavaLangAccess access = SharedSecrets.getJavaLangAccess();
    Throwable throwable = new Throwable();
    int depth = access.getStackTraceDepth(throwable);

    boolean lookingForLogger = true;
    for (int ix = 0; ix < depth; ix++) {
        // Calling getStackTraceElement directly prevents the VM
        // from paying the cost of building the entire stack frame.
        StackTraceElement frame =
            access.getStackTraceElement(throwable, ix);
        String cname = frame.getClassName();
        boolean isLoggerImpl = isLoggerImplFrame(cname);
        if (lookingForLogger) {
            // Skip all frames until we have found the first logger frame.
            if (isLoggerImpl) {
                lookingForLogger = false;
            }
        } else {
            if (!isLoggerImpl) {
                // skip reflection call
                if (!cname.startsWith("java.lang.reflect.") && !cname.startsWith("sun.reflect.")) {
                   // We've found the relevant frame.
                   setSourceClassName(cname);
                   setSourceMethodName(frame.getMethodName());
                   return;
                }
            }
        }
    }
    // We haven't found a suitable frame, so just punt.  This is
    // OK as we are only committed to making a "best effort" here.
}
 
Example 29
Source Project: JDKSourceCode1.8   Source File: LogRecord.java    License: MIT License 5 votes vote down vote up
private void inferCaller() {
    needToInferCaller = false;
    JavaLangAccess access = SharedSecrets.getJavaLangAccess();
    Throwable throwable = new Throwable();
    int depth = access.getStackTraceDepth(throwable);

    boolean lookingForLogger = true;
    for (int ix = 0; ix < depth; ix++) {
        // Calling getStackTraceElement directly prevents the VM
        // from paying the cost of building the entire stack frame.
        StackTraceElement frame =
            access.getStackTraceElement(throwable, ix);
        String cname = frame.getClassName();
        boolean isLoggerImpl = isLoggerImplFrame(cname);
        if (lookingForLogger) {
            // Skip all frames until we have found the first logger frame.
            if (isLoggerImpl) {
                lookingForLogger = false;
            }
        } else {
            if (!isLoggerImpl) {
                // skip reflection call
                if (!cname.startsWith("java.lang.reflect.") && !cname.startsWith("sun.reflect.")) {
                   // We've found the relevant frame.
                   setSourceClassName(cname);
                   setSourceMethodName(frame.getMethodName());
                   return;
                }
            }
        }
    }
    // We haven't found a suitable frame, so just punt.  This is
    // OK as we are only committed to making a "best effort" here.
}
 
Example 30
Source Project: openjdk-8   Source File: VersionHelper12.java    License: GNU General Public License v2.0 5 votes vote down vote up
Thread createThread(final Runnable r) {
    final AccessControlContext acc = AccessController.getContext();
    // 4290486: doPrivileged is needed to create a thread in
    // an environment that restricts "modifyThreadGroup".
    return AccessController.doPrivileged(
            new PrivilegedAction<Thread>() {
                public Thread run() {
                    return SharedSecrets.getJavaLangAccess()
                            .newThreadWithAcc(r, acc);
                }
            }
    );
}