Path: blob/master/test/jdk/java/lang/System/SecurityRace.java
41149 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 630283926* @summary SecurityRace System field accesses in two threads27* @author Pete Soper28* @build SecurityRace29* @run main/othervm/policy=System.policy SecurityRace30*/3132/*33* By default the test runs for a very short time. Use main arg "stress"34* to have a real chance of exposing races. Use main arg "time" to report35* the average nanoseconds per invocation of System.setSecurityManager and36* System.getSecurityManager. No testing for access races is performed with37* argument "time."38*39* Requires security permissions "setSecurityManager" and40* "createSecurityManager."41*/42434445public class SecurityRace implements Runnable {4647// Number of iterations to "warm up" and get methods compiled/inlined.48// (this is conservative)49static final int WARMUP_LOOPS = 100000;5051// Number of timing trials52static final int TIMING_TRIALS = 10;5354// Seconds to run this in "stress" mode. This is double the average55// time to expose the races of bug 6302839 on a Blade 1000. Invoke56// the program with the "stress" option multiple times for more57// confidence.58static final int STRESS_MILLISECONDS = 300000;59static final int SET_TIMING_LOOPS = 10000;6061// Max seconds to run before terminating the test ("declaring victory").62static int MAX_MILLISECONDS = 100;6364// Number of iterations to time65static final int GET_TIMING_LOOPS = 10000000;6667// Set true by main thread when NPE caught or time to terminate.68// Set true by other thread when NPE caught. It makes69// no difference where the NPE is thrown.70static volatile boolean stopthreads = false;7172// Number of getProperty invocations between main loop checks73static final int GETPROPERTY_LOOPS = 30000;7475// Used by race and timing tests. Must get set non-null at lease once.76static SecurityManager sm = new SecurityManager();7778public static void main(String[] argv) throws Exception {79String s;8081if (argv.length > 0) {82if (argv[0].equals("time")) {8384// Run the timing method85// First warm up the method to make sure it gets compiled86for (int i = 0; i < WARMUP_LOOPS; i++) {87timeit(1, 1, 1);88}8990System.out.println("boo");9192// Now do the actual timing93timeit(TIMING_TRIALS, GET_TIMING_LOOPS, SET_TIMING_LOOPS);94} else if (argv[0].equals("stress")) {9596// For stress test the test duration is boosted97MAX_MILLISECONDS = STRESS_MILLISECONDS;98} else {99throw new RuntimeException(100"SecurityRace: " + argv[0]101+ " argument to main not recognized");102} // if argv103} // if length104105long start = System.currentTimeMillis(),106end = start + MAX_MILLISECONDS;107108// Create and start racing thread109(new Thread(new SecurityRace())).start();110111// main thread alternates batches of getProperty() with time checks112try {113do {114if (stopthreads) {115116// other thread suffered an NPE117throw new RuntimeException("SecurityRace failed with NPE");118}119120for (int i = 0; i < GETPROPERTY_LOOPS; i++) {121s = System.getProperty("java.version");122}123} while (System.currentTimeMillis() < end);124} catch (NullPointerException e) {125throw new RuntimeException("SecurityRace failed with NPE");126} finally {127128// make sure other thread terminates129stopthreads = true;130}131} // main132133// System.security mutator.134public void run() {135try {136while (true) {137if (stopthreads) {138return;139}140141System.setSecurityManager(sm);142143// The goal is to catch another thread testing the144// value set above and trying to use it after it's145// nulled below.146System.setSecurityManager(null);147}148} catch (NullPointerException e) {149stopthreads = true;150151return;152}153}154155// Time method execution. Collects trials number of timings156// for the number of accessor and mutator invocation loops157// specified.158public static void timeit(int timing_trials, int get_timing_loops,159int set_timing_loops) {160try {161long start;162163// Time the methods and report average.164// Time multiple trials so noise is apparent and a165// T test can be used to establish significance.166for (int j = 0; j < timing_trials; j++) {167start = System.nanoTime();168169for (int i = 0; i < get_timing_loops; i++) {170sm = System.getSecurityManager();171}172173// Don't print for "warmup" case. This might mean that174// the compiler fails to compile the println (setting it175// up to execute via interpretation using an "uncommon trap")176// but we don't care if this println runs slowly!177if (timing_trials > 1) {178System.out.println((float) (System.nanoTime() - start)179/ (float) get_timing_loops);180}181}182183for (int j = 0; j < timing_trials; j++) {184start = System.nanoTime();185186for (int i = 0; i < set_timing_loops; i++) {187System.setSecurityManager(sm);188}189190if (timing_trials > 1) {191System.out.println((float) (System.nanoTime() - start)192/ (float) set_timing_loops);193}194}195196return;197} catch (Exception e) {198throw new RuntimeException("SecurityRace got unexpected: " + e);199}200} // timeit201} // SecurityRace202203204