Fixup SpecialSource now allowing it to be a replacement for RetroGuard in MCP. The Default RemappingMethodAdaptor 'sorts' the LocalVariables. Which causes things to receive random-ish variable indexes causing FernFlower to decompile them differently.

We now use a modified version of RemappingMethodAdaptor that bpasses the superclass LocalVariablesSorter

In addition I added a few more command line options, Most notibly:
kill-source: Removes the 'SourceFile' attribute
kill-lvt: Removes any LocalVariableTable attributes
kill-generics: Remvoes any LocalVariableTypeTable and Signature attributes
identifier: Tags each class with the specified UTF8 string in the constant pool. This is useful for loaders such as FML to know if the class has been obfusicated to SRG names or not.
This commit is contained in:
LexManos 2013-04-24 18:34:00 -07:00 committed by md_5
parent 66b7fb9269
commit 04069fb8b9
3 changed files with 264 additions and 3 deletions

View File

@ -36,10 +36,19 @@ import java.io.InputStream;
import java.util.*; import java.util.*;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream; import java.util.jar.JarOutputStream;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.Remapper; import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.commons.RemappingClassAdapter; import org.objectweb.asm.commons.RemappingClassAdapter;
import org.objectweb.asm.commons.RemappingFieldAdapter;
import org.objectweb.asm.tree.ClassNode;
import static org.objectweb.asm.ClassWriter.*;
public class JarRemapper extends Remapper { public class JarRemapper extends Remapper {
@ -163,6 +172,7 @@ public class JarRemapper extends Remapper {
return remapClassFile(new ClassReader(in)); return remapClassFile(new ClassReader(in));
} }
@SuppressWarnings("unchecked")
private byte[] remapClassFile(ClassReader reader) { private byte[] remapClassFile(ClassReader reader) {
if (remapperPreprocessor != null) { if (remapperPreprocessor != null) {
byte[] pre = remapperPreprocessor.preprocess(reader); byte[] pre = remapperPreprocessor.preprocess(reader);
@ -171,10 +181,65 @@ public class JarRemapper extends Remapper {
} }
} }
ClassWriter wr = new ClassWriter(0); ClassNode node = new ClassNode();
RemappingClassAdapter mapper = new RemappingClassAdapter(wr, this); RemappingClassAdapter mapper = new RemappingClassAdapter(node, this)
reader.accept(mapper, ClassReader.EXPAND_FRAMES); // TODO: EXPAND_FRAMES necessary? {
@Override
protected MethodVisitor createRemappingMethodAdapter(int access, String newDesc, MethodVisitor sup)
{
MethodVisitor remap = new UnsortedRemappingMethodAdapter(access, newDesc, sup, remapper);
return new MethodVisitor(Opcodes.ASM4, remap)
{
@Override
public void visitAttribute(Attribute attr)
{
if (SpecialSource.kill_lvt && attr.type.equals("LocalVariableTable")) return;
if (SpecialSource.kill_generics && attr.type.equals("LocalVariableTypeTable")) return;
if (mv != null) mv.visitAttribute(attr);
}
};
}
@Override
protected FieldVisitor createRemappingFieldAdapter(FieldVisitor sup)
{
FieldVisitor remap = new RemappingFieldAdapter(sup, remapper);
return new FieldVisitor(Opcodes.ASM4, sup)
{
@Override
public void visitAttribute(Attribute attr)
{
if (SpecialSource.kill_lvt && attr.type.equals("LocalVariableTable")) return;
if (SpecialSource.kill_generics && attr.type.equals("LocalVariableTypeTable")) return;
if (fv != null) fv.visitAttribute(attr);
}
};
}
@Override
public void visitSource(String source, String debug)
{
if (!SpecialSource.kill_source && cv != null)
{
cv.visitSource(source, debug);
}
}
@Override
public void visitAttribute(Attribute attr)
{
if (SpecialSource.kill_generics && attr.type.equals("Signature")) return;
if (cv != null) cv.visitAttribute(attr);
}
};
reader.accept(mapper, 0);
ClassWriter wr = new ClassWriter(COMPUTE_MAXS);
node.accept(wr);
if (SpecialSource.identifier != null)
{
wr.newUTF8(SpecialSource.identifier);
}
return wr.toByteArray(); return wr.toByteArray();
} }
} }

View File

@ -49,6 +49,10 @@ public class SpecialSource {
private static OptionSet options; private static OptionSet options;
private static boolean verbose; private static boolean verbose;
public static boolean kill_source = false;
public static boolean kill_lvt = false;
public static boolean kill_generics = false;
public static String identifier = null;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
OptionParser parser = new OptionParser() { OptionParser parser = new OptionParser() {
@ -108,6 +112,15 @@ public class SpecialSource {
//acceptsAll(asList("G", "remap-reflect-field"), "Remap reflection calls to getDeclaredField()"); // TODO //acceptsAll(asList("G", "remap-reflect-field"), "Remap reflection calls to getDeclaredField()"); // TODO
acceptsAll(asList("q", "quiet"), "Quiet mode"); acceptsAll(asList("q", "quiet"), "Quiet mode");
acceptsAll(asList("v", "version"), "Displays version information");
acceptsAll(asList("kill-source"), "Removes the \"SourceFile\" attribute");
acceptsAll(asList("kill-lvt"), "Removes the \"LocalVariableTable\" attribute");
acceptsAll(asList("kill-generics"), "Removes the \"LocalVariableTypeTable\" and \"Signature\" attributes");
acceptsAll(asList("d", "identifier"), "Identifier to place on each class that is transformed, by default, none")
.withRequiredArg()
.ofType(String.class);
} }
}; };
@ -129,8 +142,23 @@ public class SpecialSource {
return; return;
} }
if (options.has("version"))
{
System.out.println("SpecialSource v{something}");
return;
}
JarMapping jarMapping; JarMapping jarMapping;
verbose = !options.has("quiet"); verbose = !options.has("quiet");
kill_source = options.has("kill-source");
kill_lvt = options.has("kill-lvt");
kill_generics = options.has("kill-generics");
if (options.has("identifier"))
{
identifier = (String)options.valueOf("identifier");
}
FileLocator.useCache = !options.has("force-redownload"); FileLocator.useCache = !options.has("force-redownload");
if (options.has("first-jar") && options.has("second-jar")) { if (options.has("first-jar") && options.has("second-jar")) {

View File

@ -0,0 +1,168 @@
/***
* ASM: a very small and fast Java bytecode manipulation framework
* Copyright (c) 2000-2011 INRIA, France Telecom
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.md_5.specialsource;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.commons.RemappingAnnotationAdapter;
/**
* A {@link LocalVariablesSorter} for type mapping.
*
* @author Eugene Kuleshov
*
* Edited 04-24-2013 LexManos:
* Changed super class to MethodVisitor, using LocalVariablesSorter caused
* the LV indexes to be reassigned improperly. Causing decompiled code to
* not follow a predictable pattern and not coincide with RetroGuard's output.
*/
public class UnsortedRemappingMethodAdapter extends MethodVisitor { //Lex: Changed LocalVariablesSorter to MethodVisitor
protected final Remapper remapper;
public UnsortedRemappingMethodAdapter(final int access, final String desc,
final MethodVisitor mv, final Remapper remapper) {
this(Opcodes.ASM4, access, desc, mv, remapper);
}
protected UnsortedRemappingMethodAdapter(final int api, final int access,
final String desc, final MethodVisitor mv, final Remapper remapper) {
super(api, mv); //Lex: Removed access, desc
this.remapper = remapper;
}
@Override
public AnnotationVisitor visitAnnotationDefault() {
AnnotationVisitor av = mv.visitAnnotationDefault();
return av == null ? av : new RemappingAnnotationAdapter(av, remapper);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
AnnotationVisitor av = mv.visitAnnotation(remapper.mapDesc(desc),
visible);
return av == null ? av : new RemappingAnnotationAdapter(av, remapper);
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter,
String desc, boolean visible) {
AnnotationVisitor av = mv.visitParameterAnnotation(parameter,
remapper.mapDesc(desc), visible);
return av == null ? av : new RemappingAnnotationAdapter(av, remapper);
}
@Override
public void visitFrame(int type, int nLocal, Object[] local, int nStack,
Object[] stack) {
super.visitFrame(type, nLocal, remapEntries(nLocal, local), nStack,
remapEntries(nStack, stack));
}
private Object[] remapEntries(int n, Object[] entries) {
for (int i = 0; i < n; i++) {
if (entries[i] instanceof String) {
Object[] newEntries = new Object[n];
if (i > 0) {
System.arraycopy(entries, 0, newEntries, 0, i);
}
do {
Object t = entries[i];
newEntries[i++] = t instanceof String ? remapper
.mapType((String) t) : t;
} while (i < n);
return newEntries;
}
}
return entries;
}
@Override
public void visitFieldInsn(int opcode, String owner, String name,
String desc) {
super.visitFieldInsn(opcode, remapper.mapType(owner),
remapper.mapFieldName(owner, name, desc),
remapper.mapDesc(desc));
}
@Override
public void visitMethodInsn(int opcode, String owner, String name,
String desc) {
super.visitMethodInsn(opcode, remapper.mapType(owner),
remapper.mapMethodName(owner, name, desc),
remapper.mapMethodDesc(desc));
}
@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm,
Object... bsmArgs) {
for (int i = 0; i < bsmArgs.length; i++) {
bsmArgs[i] = remapper.mapValue(bsmArgs[i]);
}
super.visitInvokeDynamicInsn(
remapper.mapInvokeDynamicMethodName(name, desc),
remapper.mapMethodDesc(desc), (Handle) remapper.mapValue(bsm),
bsmArgs);
}
@Override
public void visitTypeInsn(int opcode, String type) {
super.visitTypeInsn(opcode, remapper.mapType(type));
}
@Override
public void visitLdcInsn(Object cst) {
super.visitLdcInsn(remapper.mapValue(cst));
}
@Override
public void visitMultiANewArrayInsn(String desc, int dims) {
super.visitMultiANewArrayInsn(remapper.mapDesc(desc), dims);
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler,
String type) {
super.visitTryCatchBlock(start, end, handler, type == null ? null
: remapper.mapType(type));
}
@Override
public void visitLocalVariable(String name, String desc, String signature,
Label start, Label end, int index) {
super.visitLocalVariable(name, remapper.mapDesc(desc),
remapper.mapSignature(signature, true), start, end, index);
}
}