1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/*
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* 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.
*/
package nsk.jvmti.RedefineClasses;
import java.io.*;
import java.util.*;
import nsk.share.*;
import nsk.share.jvmti.*;
/**
* The test exercises that the JVMTI function RedefineClasses()
* enable to redefine a local inner class properly. Redefiniton
* is performed in asynchronous manner from a separate thread when
* the VM is provoked to switch into compiled mode.<br>
* The test works as follows. Two threads are started. One is a java
* thread creating and executing a local class in an instance
* invoker method of the outer class. Second is a native thread
* executing an agent code. Then the local class method
* <code>redefclass030HotMethod()</code> is provoked to be compiled
* (optimized), and thus the JVMTI event <code>CompiledMethodLoad</code>
* should be sent. After that, the agent redefines the local class.
* Different kinds of outer fields and a local variable of the outer
* invoker method are accessed from executing methods in both versions
* of the local class. Upon the redefinition, the main test thread verifies
* via the outer fields/local variable values that the inner methods
* have been redefined. It also verifies that the outer class
* is still can access the local class fields after the redefinition.
*/
public class redefclass030 extends DebugeeClass {
final static String REDEF_CLS_SIGNATURE =
"Lnsk/jvmti/RedefineClasses/redefclass030$RedefClassWrapper$1RedefClass;";
static int status = Consts.TEST_PASSED;
static Log log = null;
// path to the directory with redefining class files
static String clfBasePath = null;
// dummy "outer-outer" fields to be changed by the local class
private int prOuterOuterFl[] = {0,0};
int packOuterOuterFl[] = {0,0};
public int pubOuterOuterFl[] = {0,0};
private static int prStOuterOuterFl[] = {0,0};
static int packStOuterOuterFl[] = {0,0};
public static int pubStOuterOuterFl[] = {0,0};
native static void storeClassBytes(byte[] classBytes);
native static void notifyNativeAgent(); /** notify native agent that "hot" method is entered */
native static boolean isRedefinitionOccurred(); /** check whether class redefinition was already occurred */
public static void main(String args[]) {
args = nsk.share.jvmti.JVMTITest.commonInit(args);
// produce JCK-like exit status.
System.exit(run(args, System.out) + Consts.JCK_STATUS_BASE);
}
public static int run(String args[], PrintStream out) {
return new redefclass030().runIt(args, out);
}
private int runIt(String args[], PrintStream out) {
int iter;
ArgumentHandler argHandler = new ArgumentHandler(args);
log = new Log(out, argHandler);
try {
// number of iterations for a method to become 'hotspot' one
iter = Integer.parseInt(args[0]);
// directory to search a redefining class
clfBasePath = args[1];
} catch(Exception e) {
throw new Failure("TEST BUG: Wrong test parameters, caught: "
+ e);
}
storeClassBytes(loadFromClassFile(REDEF_CLS_SIGNATURE));
// testing sync
log.display("waiting for the agent start ...\n");
status = checkStatus(status);
log.display("starting an auxiliary thread ...\n");
RedefClassWrapper redefClsWrapper = new RedefClassWrapper(iter);
redefClsWrapper.setDaemon(true);
synchronized(redefClsWrapper) {
redefClsWrapper.start();
log.display("waiting for auxiliary thread readiness...\n");
try {
redefClsWrapper.wait(); // wait for the thread's readiness
} catch (InterruptedException e) {
redefClsWrapper.interrupt();
throw new Failure("TEST FAILURE: waiting for auxiliary thread start, caught: "
+ e);
}
}
// testing sync
log.display("auxiliary thread started\n"
+ "waiting for the agent finish ...\n");
status = checkStatus(status);
boolean isRedefinitionStarted = waitForRedefinitionStarted();
boolean isRedefinitionCompleted = false;
if (isRedefinitionStarted) {
isRedefinitionCompleted = waitForRedefinitionCompleted(redefClsWrapper);
}
log.display("waiting for auxiliary thread ...\n");
redefClsWrapper.stopMe = true;
try {
redefClsWrapper.join();
} catch (InterruptedException e) {
redefClsWrapper.interrupt();
throw new Failure("TEST FAILURE: waiting for auxiliary thread death, caught: "
+ e);
}
// CR 6604375: check whether class redefinition occurred
if (isRedefinitionCompleted) {
// verify results
checkOuterOuterFields(0, 2);
checkOuterOuterFields(1, 2);
checkOuterFields(redefClsWrapper, 0, 2);
checkOuterFields(redefClsWrapper, 1, 2);
}
return status;
}
private boolean waitForRedefinitionStarted() {
final int SLEEP_MS = 20;
int iterationsLeft = 2000 / SLEEP_MS;
while (iterationsLeft >= 0) {
if (isRedefinitionOccurred()) {
log.display("Redefinition started.");
return true;
}
--iterationsLeft;
safeSleep(SLEEP_MS);
}
log.complain("Redefinition not started. May need more time for -Xcomp.");
status = Consts.TEST_FAILED;
return false;
}
private boolean waitForRedefinitionCompleted(RedefClassWrapper redefClsWrapper) {
final int SLEEP_MS = 20;
int iterationsLeft = 10000 / SLEEP_MS;
while (iterationsLeft >= 0) {
// Check if new code has changed fields.
if (prStOuterOuterFl[0] == 2 && prStOuterOuterFl[1] == 2 && redefClsWrapper.prOuterFl[1] == 2) {
log.display("Redefinition completed.");
return true;
}
--iterationsLeft;
safeSleep(SLEEP_MS);
}
log.complain("Redefinition not completed. May need more time for -Xcomp.");
status = Consts.TEST_FAILED;
return false;
}
private void checkOuterOuterFields(int index, int expValue) {
if (prOuterOuterFl[index] != expValue
|| packOuterOuterFl[index] != expValue
|| pubOuterOuterFl[index] != expValue
|| prStOuterOuterFl[index] != expValue
|| packStOuterOuterFl[index] != expValue
|| pubStOuterOuterFl[index] != expValue) {
status = Consts.TEST_FAILED;
log.complain("TEST FAILED: unexpected values of outer fields of the class"
+ "\n\t\"" + this.toString() + "\":"
+ "\n\t\tprOuterOuterFl["+ index +"]: got: " + prOuterOuterFl[index]
+ ", expected: " + expValue
+ "\n\t\tpackOuterOuterFl["+ index +"]: got: " + packOuterOuterFl[index]
+ ", expected: " + expValue
+ "\n\t\tpubOuterOuterFl["+ index +"]: got: " + pubOuterOuterFl[index]
+ ", expected: " + expValue
+ "\n\t\tprStOuterOuterFl["+ index +"]: got: " + prStOuterOuterFl[index]
+ ", expected: " + expValue
+ "\n\t\tpackStOuterOuterFl["+ index +"]: got: " + packStOuterOuterFl[index]
+ ", expected: " + expValue
+ "\n\t\tpubStOuterOuterFl["+ index +"]: got: " + pubStOuterOuterFl[index]
+ ", expected: " + expValue);
}
}
private void checkOuterFields(RedefClassWrapper redefClsWrapper, int index, int expValue) {
if (redefClsWrapper.prOuterFl[index] != expValue
|| redefClsWrapper.packOuterFl[index] != expValue
|| redefClsWrapper.pubOuterFl[index] != expValue) {
status = Consts.TEST_FAILED;
log.complain("TEST FAILED: unexpected values of outer fields of the class"
+ "\n\t\"" + redefClsWrapper.toString() + "\":"
+ "\n\t\tprOuterFl["+ index +"]: got: " + redefClsWrapper.prOuterFl[index]
+ ", expected: " + expValue
+ "\n\t\tpackOuterFl["+ index +"]: got: " + redefClsWrapper.packOuterFl[index]
+ ", expected: " + expValue
+ "\n\t\tpubOuterFl["+ index +"]: got: " + redefClsWrapper.pubOuterFl[index]
+ ", expected: " + expValue);
}
}
/**
* Load bytes of a redefining class.
*/
private static byte[] loadFromClassFile(String signature) {
String testPath = clfBasePath + File.separator
+ "newclass" + File.separator
+ signature.substring(1, signature.length()-1).replace('/', File.separatorChar)
+ ".class";
File classFile = null;
log.display("looking for class file at\n\t"
+ testPath + " ...\n");
try {
classFile = new File(testPath);
} catch (NullPointerException e) {
throw new Failure("FAILURE: failed to open class file, caught: "
+ e);
}
log.display("loading " + classFile.length()
+ " bytes from class file "+ testPath + " ...\n");
byte[] buf = new byte[(int) classFile.length()];
try {
InputStream in = new FileInputStream(classFile);
in.read(buf);
in.close();
} catch (Exception e) {
throw new Failure("FAILURE: failed to load bytes from class file, caught: "
+ e);
}
log.display(classFile.length()
+ " bytes of a redefining class loaded\n");
return buf;
}
/**
* Class executing the local inner class to be redefined.
*/
class RedefClassWrapper extends Thread {
boolean stopMe = false;
int iter;
// dummy outer fields to be changed by the local class
private int prOuterFl[] = {0,0};
int packOuterFl[] = {0,0};
public int pubOuterFl[] = {0,0};
RedefClassWrapper(int iter) {
super("RedefClassWrapper");
this.iter = iter;
}
public void run() {
// dummy local vars to be changed by the local class
final int outerLocalVar[] = {0,0};
/**
* Local inner class to be redefined in an agent.
*/
class RedefClass {
int iter;
// dummy inner fields to be accessed by the outer class
private int prInnerFl = 1;
int packInnerFl = 1;
public int pubInnerFl = 1;
RedefClass(int iter) {
this.iter = iter;
}
void warmUpMethod() {
prOuterOuterFl[0] = 1;
packOuterOuterFl[0] = 1;
pubOuterOuterFl[0] = 1;
prStOuterOuterFl[0] = 1;
packStOuterOuterFl[0] = 1;
pubStOuterOuterFl[0] = 1;
prOuterFl[0] = 1;
packOuterFl[0] = 1;
pubOuterFl[0] = 1;
outerLocalVar[0] = 1;
for (int i=0; i<iter; i++)
redefclass030HotMethod(i);
redefclass030HotMethod(10);
}
/**
* Hotspot method to be compiled.
*/
void redefclass030HotMethod(int i) {
prOuterOuterFl[1] = 1;
packOuterOuterFl[1] = 1;
pubOuterOuterFl[1] = 1;
prStOuterOuterFl[1] = 1;
packStOuterOuterFl[1] = 1;
pubStOuterOuterFl[1] = 1;
prOuterFl[1] = 1;
packOuterFl[1] = 1;
pubOuterFl[1] = 1;
outerLocalVar[1] = 1;
int j=0;
j +=i;
j--;
if (j >10)
j = 0;
notifyNativeAgent();
}
}
/*************************************************************/
log.display(this.getName() + ": started");
RedefClass redefCls = new RedefClass(iter);
synchronized(this) {
this.notify(); // notify the main thread
while(!stopMe) {
redefCls.warmUpMethod();
// get the main thread chance to obtain CPU
try {
this.wait(100);
} catch(Exception e) {}
}
if (redefCls.prInnerFl != 1
|| redefCls.packInnerFl != 1
|| redefCls.pubInnerFl != 1) {
status = Consts.TEST_FAILED;
log.complain("TEST FAILED: unexpected values of inner fields of the local class"
+ "\n\t\"" + this.toString() + "\":"
+ "\n\t\tprInnerFl: got: " + redefCls.prInnerFl
+ ", expected: 1"
+ "\n\t\tpackInnerFl: got: " + redefCls.packInnerFl
+ ", expected: 1"
+ "\n\t\tpubInnerFl: got: " + redefCls.pubInnerFl
+ ", expected: 1");
}
log.display(this.getName() + ": exiting");
}
}
}
}
|