jdk/test/java/lang/System/SecurityRace.java
author darcy
Fri, 19 Aug 2011 17:42:24 -0700
changeset 10350 6d009f117062
parent 5506 202f599c92aa
permissions -rw-r--r--
4748706: typos in java.util.Observable documentation Reviewed-by: iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6302839
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary SecurityRace System field accesses in two threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Pete Soper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @build SecurityRace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main/othervm/policy=System.policy SecurityRace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * By default the test runs for a very short time.  Use main arg "stress"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * to have a real chance of exposing races. Use main arg "time" to report
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the average nanoseconds per invocation of System.setSecurityManager and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * System.getSecurityManager. No testing for access races is performed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * argument "time."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Requires security permissions "setSecurityManager" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * "createSecurityManager."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class SecurityRace implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // Number of iterations to "warm up" and get methods compiled/inlined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // (this is conservative)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final int WARMUP_LOOPS = 100000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Number of timing trials
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final int TIMING_TRIALS = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Seconds to run this in "stress" mode. This is double the average
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // time to expose the races of bug 6302839 on a Blade 1000. Invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // the program with the "stress" option multiple times for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // confidence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final int STRESS_MILLISECONDS = 300000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final int SET_TIMING_LOOPS    = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Max seconds to run before terminating the test ("declaring victory").
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static int MAX_MILLISECONDS = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Number of iterations to time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final int GET_TIMING_LOOPS = 10000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Set true by main thread when NPE caught or time to terminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Set true by other thread when NPE caught. It makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // no difference where the NPE is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static volatile boolean stopthreads = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Number of getProperty invocations between main loop checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final int       GETPROPERTY_LOOPS = 30000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Used by race and timing tests. Must get set non-null at lease once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static SecurityManager sm = new SecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (argv.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (argv[0].equals("time")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                // Run the timing method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                // First warm up the method to make sure it gets compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                for (int i = 0; i < WARMUP_LOOPS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    timeit(1, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                System.out.println("boo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                // Now do the actual timing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                timeit(TIMING_TRIALS, GET_TIMING_LOOPS, SET_TIMING_LOOPS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else if (argv[0].equals("stress")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                // For stress test the test duration is boosted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                MAX_MILLISECONDS = STRESS_MILLISECONDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    "SecurityRace: " + argv[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    + " argument to main not recognized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }    // if argv
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }        // if length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        long start = System.currentTimeMillis(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
             end   = start + MAX_MILLISECONDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Create and start racing thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        (new Thread(new SecurityRace())).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // main thread alternates batches of getProperty() with time checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (stopthreads) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    // other thread suffered an NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    throw new RuntimeException("SecurityRace failed with NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                for (int i = 0; i < GETPROPERTY_LOOPS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    s = System.getProperty("java.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } while (System.currentTimeMillis() < end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new RuntimeException("SecurityRace failed with NPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // make sure other thread terminates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            stopthreads = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }    // main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // System.security mutator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (stopthreads) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.setSecurityManager(sm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // The goal is to catch another thread testing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // value set above and trying to use it after it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // nulled below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                System.setSecurityManager(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            stopthreads = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // Time method execution. Collects trials number of timings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // for the number of accessor and mutator invocation loops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static void timeit(int timing_trials, int get_timing_loops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                              int set_timing_loops) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            long start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // Time the methods and report average.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // Time multiple trials so noise is apparent and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // T test can be used to establish significance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            for (int j = 0; j < timing_trials; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                start = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                for (int i = 0; i < get_timing_loops; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                // Don't print for "warmup" case. This might mean that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                // the compiler fails to compile the println (setting it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                // up to execute via interpretation using an "uncommon trap")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                // but we don't care if this println runs slowly!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (timing_trials > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    System.out.println((float) (System.nanoTime() - start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                       / (float) get_timing_loops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            for (int j = 0; j < timing_trials; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                start = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                for (int i = 0; i < set_timing_loops; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    System.setSecurityManager(sm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (timing_trials > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    System.out.println((float) (System.nanoTime() - start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                       / (float) set_timing_loops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new RuntimeException("SecurityRace got unexpected: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }    // timeit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}    // SecurityRace