test/jdk/java/security/AccessController/DoPriv.java
author dlong
Tue, 18 Dec 2018 16:36:26 -0800
changeset 53068 7496df94b3b7
permissions -rw-r--r--
8214583: AccessController.getContext may return wrong value after JDK-8212605 Reviewed-by: mchung, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53068
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     1
/*
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     4
 *
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     8
 *
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    13
 * accompanied this code).
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    14
 *
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    18
 *
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    21
 * questions.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    22
 */
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    23
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    24
/*
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    25
 * @test
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    26
 * @bug 8214583
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    27
 * @summary Check that getContext works after JIT compiler escape analysis.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    28
 */
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    29
import java.security.AccessControlContext;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    30
import java.security.AccessController;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    31
import java.security.DomainCombiner;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    32
import java.security.ProtectionDomain;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    33
import java.security.PrivilegedActionException;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    34
import java.security.PrivilegedExceptionAction;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    35
import java.net.URL;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    36
import java.util.Collections;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    37
import java.util.HashSet;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    38
import java.util.Set;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    39
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    40
public class DoPriv {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    41
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    42
    static void go(final DomainCombiner dc0, final AccessControlContext co, final int index) throws Exception {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    43
        final AccessControlContext ci = new AccessControlContext(co, dc0);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    44
        AccessController.doPrivileged((PrivilegedExceptionAction<Integer>)() -> {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    45
            AccessControlContext c1 = AccessController.getContext();
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    46
            DomainCombiner dc = c1.getDomainCombiner();
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    47
            if (dc != dc0 || dc == null) {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    48
                throw new AssertionError("iteration " + index + " " + dc + " != " + dc0);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    49
            }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    50
            return 0;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    51
        }, ci);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    52
    }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    53
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    54
    public static void main(String[] args) throws Exception {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    55
        final DomainCombiner dc0 = new DomainCombiner() {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    56
            public ProtectionDomain[] combine(ProtectionDomain[] currentDomains,
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    57
                                            ProtectionDomain[] assignedDomains) {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    58
                return null;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    59
            }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    60
        };
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    61
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    62
        final AccessControlContext co = AccessController.getContext();
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    63
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    64
        for (int i = 0; i < 500_000; ++i) {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    65
            go(dc0, co, i);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    66
        }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    67
    }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    68
}