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