1
|
1 |
/*
|
3261
|
2 |
* Copyright 2002-2009 Sun Microsystems, Inc. All Rights Reserved.
|
1
|
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
|
|
25 |
package sun.jvm.hotspot.tools.jcore;
|
|
26 |
|
|
27 |
import sun.jvm.hotspot.oops.*;
|
|
28 |
import sun.jvm.hotspot.interpreter.*;
|
|
29 |
import sun.jvm.hotspot.utilities.*;
|
|
30 |
import sun.jvm.hotspot.debugger.*;
|
|
31 |
import sun.jvm.hotspot.runtime.*;
|
|
32 |
|
|
33 |
public class ByteCodeRewriter
|
|
34 |
{
|
|
35 |
private Method method;
|
|
36 |
private ConstantPool cpool;
|
|
37 |
private ConstantPoolCache cpCache;
|
|
38 |
private byte[] code;
|
|
39 |
private Bytes bytes;
|
|
40 |
|
|
41 |
public static final boolean DEBUG = false;
|
|
42 |
private static final int jintSize = 4;
|
|
43 |
|
|
44 |
protected void debugMessage(String message) {
|
|
45 |
System.out.println(message);
|
|
46 |
}
|
|
47 |
|
|
48 |
public ByteCodeRewriter(Method method, ConstantPool cpool, byte[] code) {
|
|
49 |
this.method = method;
|
|
50 |
this.cpool = cpool;
|
|
51 |
this.cpCache = cpool.getCache();
|
|
52 |
this.code = code;
|
|
53 |
this.bytes = VM.getVM().getBytes();
|
|
54 |
|
|
55 |
}
|
|
56 |
|
|
57 |
protected short getConstantPoolIndex(int bci) {
|
|
58 |
// get ConstantPool index from ConstantPoolCacheIndex at given bci
|
|
59 |
short cpCacheIndex = method.getBytecodeShortArg(bci);
|
|
60 |
if (cpCache == null) {
|
|
61 |
return cpCacheIndex;
|
|
62 |
} else {
|
|
63 |
// change byte-ordering and go via cache
|
|
64 |
return (short) cpCache.getEntryAt((int) (0xFFFF & bytes.swapShort(cpCacheIndex))).getConstantPoolIndex();
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
static private void writeShort(byte[] buf, int index, short value) {
|
|
69 |
buf[index] = (byte) ((value >> 8) & 0x00FF);
|
|
70 |
buf[index + 1] = (byte) (value & 0x00FF);
|
|
71 |
}
|
|
72 |
|
|
73 |
public void rewrite() {
|
|
74 |
int bytecode = Bytecodes._illegal;
|
|
75 |
int hotspotcode = Bytecodes._illegal;
|
|
76 |
int len = 0;
|
|
77 |
|
|
78 |
for (int bci = 0; bci < code.length;) {
|
|
79 |
hotspotcode = Bytecodes.codeAt(method, bci);
|
|
80 |
bytecode = Bytecodes.javaCode(hotspotcode);
|
|
81 |
|
|
82 |
if (Assert.ASSERTS_ENABLED) {
|
|
83 |
int code_from_buffer = 0xFF & code[bci];
|
|
84 |
Assert.that(code_from_buffer == hotspotcode
|
|
85 |
|| code_from_buffer == Bytecodes._breakpoint,
|
|
86 |
"Unexpected bytecode found in method bytecode buffer!");
|
|
87 |
}
|
|
88 |
|
|
89 |
// update the code buffer hotspot specific bytecode with the jvm bytecode
|
|
90 |
code[bci] = (byte) (0xFF & bytecode);
|
|
91 |
|
|
92 |
short cpoolIndex = 0;
|
|
93 |
switch (bytecode) {
|
|
94 |
// bytecodes with ConstantPoolCache index
|
|
95 |
case Bytecodes._getstatic:
|
|
96 |
case Bytecodes._putstatic:
|
|
97 |
case Bytecodes._getfield:
|
|
98 |
case Bytecodes._putfield:
|
|
99 |
case Bytecodes._invokevirtual:
|
|
100 |
case Bytecodes._invokespecial:
|
|
101 |
case Bytecodes._invokestatic:
|
|
102 |
case Bytecodes._invokeinterface: {
|
|
103 |
cpoolIndex = getConstantPoolIndex(bci + 1);
|
|
104 |
writeShort(code, bci + 1, cpoolIndex);
|
|
105 |
break;
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
len = Bytecodes.lengthFor(bytecode);
|
|
110 |
if (len <= 0) len = Bytecodes.lengthAt(method, bci);
|
|
111 |
|
|
112 |
if (DEBUG) {
|
|
113 |
String operand = "";
|
|
114 |
switch (len) {
|
|
115 |
case 2:
|
|
116 |
operand += code[bci + 1];
|
|
117 |
break;
|
|
118 |
case 3:
|
|
119 |
operand += (cpoolIndex != 0)? cpoolIndex :
|
|
120 |
method.getBytecodeShortArg(bci + 1);
|
|
121 |
break;
|
|
122 |
case 5:
|
|
123 |
operand += method.getBytecodeIntArg(bci + 1);
|
|
124 |
break;
|
|
125 |
}
|
|
126 |
|
|
127 |
// the operand following # is not quite like javap output.
|
|
128 |
// in particular, for goto & goto_w, the operand is PC relative
|
|
129 |
// offset for jump. Javap adds relative offset with current PC
|
|
130 |
// to give absolute bci to jump to.
|
|
131 |
|
|
132 |
String message = "\t\t" + bci + " " + Bytecodes.name(bytecode);
|
|
133 |
if (hotspotcode != bytecode)
|
|
134 |
message += " [" + Bytecodes.name(hotspotcode) + "]";
|
|
135 |
if (operand != "")
|
|
136 |
message += " #" + operand;
|
|
137 |
|
|
138 |
if (DEBUG) debugMessage(message);
|
|
139 |
}
|
|
140 |
|
|
141 |
bci += len;
|
|
142 |
}
|
|
143 |
}
|
|
144 |
}
|