/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6302839
* @summary SecurityRace System field accesses in two threads
* @author Pete Soper
* @build SecurityRace
*/
/*
* By default the test runs for a very short time. Use main arg "stress"
* to have a real chance of exposing races. Use main arg "time" to report
* the average nanoseconds per invocation of System.setSecurityManager and
* System.getSecurityManager. No testing for access races is performed with
* argument "time."
*
* Requires security permissions "setSecurityManager" and
* "createSecurityManager."
*/
// (this is conservative)
// Number of timing trials
// Seconds to run this in "stress" mode. This is double the average
// time to expose the races of bug 6302839 on a Blade 1000. Invoke
// the program with the "stress" option multiple times for more
// confidence.
// Max seconds to run before terminating the test ("declaring victory").
// Number of iterations to time
// Set true by main thread when NPE caught or time to terminate.
// Set true by other thread when NPE caught. It makes
// no difference where the NPE is thrown.
static volatile boolean stopthreads = false;
// Number of getProperty invocations between main loop checks
// Used by race and timing tests. Must get set non-null at lease once.
String s;
// Run the timing method
// First warm up the method to make sure it gets compiled
for (int i = 0; i < WARMUP_LOOPS; i++) {
}
// Now do the actual timing
// For stress test the test duration is boosted
} else {
throw new RuntimeException(
+ " argument to main not recognized");
} // if argv
} // if length
// Create and start racing thread
// main thread alternates batches of getProperty() with time checks
try {
do {
if (stopthreads) {
// other thread suffered an NPE
throw new RuntimeException("SecurityRace failed with NPE");
}
for (int i = 0; i < GETPROPERTY_LOOPS; i++) {
}
} catch (NullPointerException e) {
throw new RuntimeException("SecurityRace failed with NPE");
} finally {
// make sure other thread terminates
stopthreads = true;
}
} // main
// System.security mutator.
public void run() {
try {
while (true) {
if (stopthreads) {
return;
}
// The goal is to catch another thread testing the
// value set above and trying to use it after it's
// nulled below.
}
} catch (NullPointerException e) {
stopthreads = true;
return;
}
}
// Time method execution. Collects trials number of timings
// for the number of accessor and mutator invocation loops
// specified.
int set_timing_loops) {
try {
long start;
// Time the methods and report average.
// Time multiple trials so noise is apparent and a
// T test can be used to establish significance.
for (int j = 0; j < timing_trials; j++) {
for (int i = 0; i < get_timing_loops; i++) {
}
// Don't print for "warmup" case. This might mean that
// the compiler fails to compile the println (setting it
// up to execute via interpretation using an "uncommon trap")
// but we don't care if this println runs slowly!
if (timing_trials > 1) {
/ (float) get_timing_loops);
}
}
for (int j = 0; j < timing_trials; j++) {
for (int i = 0; i < set_timing_loops; i++) {
}
if (timing_trials > 1) {
/ (float) set_timing_loops);
}
}
return;
} catch (Exception e) {
throw new RuntimeException("SecurityRace got unexpected: " + e);
}
} // timeit
} // SecurityRace