summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/bytecode/ConstPoolEditor.java
blob: a00b86b5324e2c9ee061c8bc9cbbff3ca93fb7b2 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.bytecode;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;

import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import cuchaz.enigma.bytecode.accessors.ClassInfoAccessor;
import cuchaz.enigma.bytecode.accessors.ConstInfoAccessor;
import cuchaz.enigma.bytecode.accessors.MemberRefInfoAccessor;

public class ConstPoolEditor {
	
	private static Method m_getItem;
	private static Method m_addItem;
	private static Method m_addItem0;
	private static Field m_items;
	private static Field m_cache;
	private static Field m_numItems;
	private static Field m_objects;
	private static Field m_elements;
	private static Method m_methodWritePool;
	private static Constructor<ConstPool> m_constructorPool;
	
	static {
		try {
			m_getItem = ConstPool.class.getDeclaredMethod("getItem", int.class);
			m_getItem.setAccessible(true);
			
			m_addItem = ConstPool.class.getDeclaredMethod("addItem", Class.forName("javassist.bytecode.ConstInfo"));
			m_addItem.setAccessible(true);
			
			m_addItem0 = ConstPool.class.getDeclaredMethod("addItem0", Class.forName("javassist.bytecode.ConstInfo"));
			m_addItem0.setAccessible(true);
			
			m_items = ConstPool.class.getDeclaredField("items");
			m_items.setAccessible(true);
			
			m_cache = ConstPool.class.getDeclaredField("itemsCache");
			m_cache.setAccessible(true);
			
			m_numItems = ConstPool.class.getDeclaredField("numOfItems");
			m_numItems.setAccessible(true);
			
			m_objects = Class.forName("javassist.bytecode.LongVector").getDeclaredField("objects");
			m_objects.setAccessible(true);
			
			m_elements = Class.forName("javassist.bytecode.LongVector").getDeclaredField("elements");
			m_elements.setAccessible(true);
			
			m_methodWritePool = ConstPool.class.getDeclaredMethod("write", DataOutputStream.class);
			m_methodWritePool.setAccessible(true);
			
			m_constructorPool = ConstPool.class.getDeclaredConstructor(DataInputStream.class);
			m_constructorPool.setAccessible(true);
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	private ConstPool m_pool;
	
	public ConstPoolEditor(ConstPool pool) {
		m_pool = pool;
	}
	
	public void writePool(DataOutputStream out) {
		try {
			m_methodWritePool.invoke(m_pool, out);
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	public static ConstPool readPool(DataInputStream in) {
		try {
			return m_constructorPool.newInstance(in);
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	public String getMemberrefClassname(int memberrefIndex) {
		return Descriptor.toJvmName(m_pool.getClassInfo(m_pool.getMemberClass(memberrefIndex)));
	}
	
	public String getMemberrefName(int memberrefIndex) {
		return m_pool.getUtf8Info(m_pool.getNameAndTypeName(m_pool.getMemberNameAndType(memberrefIndex)));
	}
	
	public String getMemberrefType(int memberrefIndex) {
		return m_pool.getUtf8Info(m_pool.getNameAndTypeDescriptor(m_pool.getMemberNameAndType(memberrefIndex)));
	}
	
	public ConstInfoAccessor getItem(int index) {
		try {
			Object entry = m_getItem.invoke(m_pool, index);
			if (entry == null) {
				return null;
			}
			return new ConstInfoAccessor(entry);
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	public int addItem(Object item) {
		try {
			return (Integer)m_addItem.invoke(m_pool, item);
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	public int addItemForceNew(Object item) {
		try {
			return (Integer)m_addItem0.invoke(m_pool, item);
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	@SuppressWarnings("rawtypes")
	public void removeLastItem() {
		try {
			// remove the item from the cache
			HashMap cache = getCache();
			if (cache != null) {
				Object item = getItem(m_pool.getSize() - 1);
				cache.remove(item);
			}
			
			// remove the actual item
			// based off of LongVector.addElement()
			Object items = m_items.get(m_pool);
			Object[][] objects = (Object[][])m_objects.get(items);
			int numElements = (Integer)m_elements.get(items) - 1;
			int nth = numElements >> 7;
			int offset = numElements & (128 - 1);
			objects[nth][offset] = null;
			
			// decrement the number of items
			m_elements.set(items, numElements);
			m_numItems.set(m_pool, (Integer)m_numItems.get(m_pool) - 1);
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	@SuppressWarnings("rawtypes")
	public HashMap getCache() {
		try {
			return (HashMap)m_cache.get(m_pool);
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void changeMemberrefNameAndType(int memberrefIndex, String newName, String newType) {
		// NOTE: when changing values, we always need to copy-on-write
		try {
			// get the memberref item
			Object item = getItem(memberrefIndex).getItem();
			
			// update the cache
			HashMap cache = getCache();
			if (cache != null) {
				cache.remove(item);
			}
			
			new MemberRefInfoAccessor(item).setNameAndTypeIndex(m_pool.addNameAndTypeInfo(newName, newType));
			
			// update the cache
			if (cache != null) {
				cache.put(item, item);
			}
		} catch (Exception ex) {
			throw new Error(ex);
		}
		
		// make sure the change worked
		assert (newName.equals(getMemberrefName(memberrefIndex)));
		assert (newType.equals(getMemberrefType(memberrefIndex)));
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void changeClassName(int classNameIndex, String newName) {
		// NOTE: when changing values, we always need to copy-on-write
		try {
			// get the class item
			Object item = getItem(classNameIndex).getItem();
			
			// update the cache
			HashMap cache = getCache();
			if (cache != null) {
				cache.remove(item);
			}
			
			// add the new name and repoint the name-and-type to it
			new ClassInfoAccessor(item).setNameIndex(m_pool.addUtf8Info(newName));
			
			// update the cache
			if (cache != null) {
				cache.put(item, item);
			}
		} catch (Exception ex) {
			throw new Error(ex);
		}
	}
	
	public static ConstPool newConstPool() {
		// const pool expects the name of a class to initialize itself
		// but we want an empty pool
		// so give it a bogus name, and then clear the entries afterwards
		ConstPool pool = new ConstPool("a");
		
		ConstPoolEditor editor = new ConstPoolEditor(pool);
		int size = pool.getSize();
		for (int i = 0; i < size - 1; i++) {
			editor.removeLastItem();
		}
		
		// make sure the pool is actually empty
		// although, in this case "empty" means one thing in it
		// the JVM spec says index 0 should be reserved
		assert (pool.getSize() == 1);
		assert (editor.getItem(0) == null);
		assert (editor.getItem(1) == null);
		assert (editor.getItem(2) == null);
		assert (editor.getItem(3) == null);
		
		// also, clear the cache
		editor.getCache().clear();
		
		return pool;
	}
	
	public String dump() {
		StringBuilder buf = new StringBuilder();
		for (int i = 1; i < m_pool.getSize(); i++) {
			buf.append(String.format("%4d", i));
			buf.append("   ");
			buf.append(getItem(i).toString());
			buf.append("\n");
		}
		return buf.toString();
	}
}