Skip to content

Commit cf87647

Browse files
committed
fear(obfuscator): added invoke-dynamic initial support
1 parent d4575c8 commit cf87647

5 files changed

Lines changed: 44 additions & 10 deletions

File tree

maple-ir/org.mapleir.ir/src/main/java/org/mapleir/ir/TypeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static Set<Type> __getIntrinsicErrors() {
3535
public static final Type CLONEABLE_TYPE = Type.getType(Cloneable.class);
3636
public static final Type SERIALIZABLE_TYPE = Type.getType(Serializable.class);
3737
public static final Type THROWABLE = Type.getType(Throwable.class);
38-
public static final Type NULL_TYPE = OBJECT_TYPE;
38+
public static final Type NULL_TYPE = Type.getType(Null.class);
3939
public static final Type UNDEFINED_TYPE = Type.getType(Undefined.class);
4040

4141

maple-ir/org.mapleir.ir/src/main/java/org/mapleir/ir/cfg/builder/GenerationPass.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ protected void process(LabelNode label) {
318318
frameNode.local == null ? new Object[0] : frameNode.local.toArray(),
319319
frameNode.stack == null ? new Object[0] : frameNode.stack.toArray()
320320
);
321-
break;
321+
322+
continue;
322323
}
323324
}
324325

obfuscator/src/main/java/dev/skidfuscator/obfuscator/hierarchy/Hierarchy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.skidfuscator.obfuscator.hierarchy;
22

3+
import dev.skidfuscator.obfuscator.hierarchy.matching.ClassMethodHash;
34
import dev.skidfuscator.obfuscator.skidasm.*;
45
import org.mapleir.asm.ClassNode;
56
import org.mapleir.asm.MethodNode;
@@ -9,6 +10,8 @@
910
public interface Hierarchy {
1011
void cache();
1112

13+
SkidGroup getGroup(final ClassMethodHash methodNode);
14+
1215
SkidGroup getGroup(final SkidMethodNode methodNode);
1316

1417
List<ClassNode> getClasses();

obfuscator/src/main/java/dev/skidfuscator/obfuscator/hierarchy/SkidHierarchy.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ private void setupInvoke() {
192192
if (invocation instanceof DynamicInvocationExpr) {
193193
final DynamicInvocationExpr e = (DynamicInvocationExpr) invocation;
194194

195-
if (true)
196-
return;
197-
198195
if (!e.getOwner().equals("java/lang/invoke/LambdaMetafactory")
199196
|| !e.getName().equals("metafactory")) {
200197
throw new IllegalStateException("Invalid invoke dynamic!");
@@ -246,6 +243,11 @@ private void setupInvoke() {
246243
}
247244
}
248245

246+
public SkidGroup getGroup(final ClassMethodHash methodNode) {
247+
return hashToGroupMap.get(methodNode);
248+
}
249+
250+
249251
private SkidGroup getGroup(final Skidfuscator session, final MethodNode methodNode) {
250252
SkidGroup group = methodToGroupMap.get(methodNode);
251253

obfuscator/src/main/java/dev/skidfuscator/obfuscator/predicate/renderer/impl/IntegerBlockPredicateRenderer.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dev.skidfuscator.obfuscator.event.impl.transform.group.InitGroupTransformEvent;
77
import dev.skidfuscator.obfuscator.event.impl.transform.method.InitMethodTransformEvent;
88
import dev.skidfuscator.obfuscator.event.impl.transform.method.PostMethodTransformEvent;
9+
import dev.skidfuscator.obfuscator.hierarchy.matching.ClassMethodHash;
910
import dev.skidfuscator.obfuscator.number.NumberManager;
1011
import dev.skidfuscator.obfuscator.number.encrypt.impl.XorNumberTransformer;
1112
import dev.skidfuscator.obfuscator.number.hash.HashTransformer;
@@ -41,10 +42,12 @@
4142
import org.mapleir.ir.code.expr.ConstantExpr;
4243
import org.mapleir.ir.code.expr.FieldLoadExpr;
4344
import org.mapleir.ir.code.expr.VarExpr;
45+
import org.mapleir.ir.code.expr.invoke.DynamicInvocationExpr;
4446
import org.mapleir.ir.code.expr.invoke.StaticInvocationExpr;
4547
import org.mapleir.ir.code.stmt.*;
4648
import org.mapleir.ir.code.stmt.copy.CopyVarStmt;
4749
import org.mapleir.ir.locals.Local;
50+
import org.objectweb.asm.Handle;
4851
import org.objectweb.asm.Opcodes;
4952
import org.objectweb.asm.Type;
5053

@@ -473,6 +476,12 @@ void handle(final InitGroupTransformEvent event) {
473476
parameter.addParameter(Type.INT_TYPE);
474477
methodNode.node.desc = desc = parameter.getDesc();
475478

479+
final ClassMethodHash classMethodHash = new ClassMethodHash(skidMethodNode);
480+
481+
if (skidfuscator.getHierarchy().getGroup(classMethodHash) != null) {
482+
skidGroup.setName(skidGroup.getName() + "$" + RandomUtil.nextInt());
483+
}
484+
476485
if (local == null) {
477486
local = skidMethodNode.getCfg().getLocals().get(stackHeight);
478487
}
@@ -481,27 +490,46 @@ void handle(final InitGroupTransformEvent event) {
481490
for (SkidInvocation invoker : skidGroup.getInvokers()) {
482491
assert invoker != null : String.format("Invoker %s is null!", Arrays.toString(skidGroup.getInvokers().toArray()));
483492
assert invoker.getExpr() != null : String.format("Invoker %s is null!", invoker.getOwner().getDisplayName());
493+
final boolean isDynamic = invoker.getExpr() instanceof DynamicInvocationExpr;
484494

485495
int index = 0;
486-
for (Expr argumentExpr : invoker.getExpr().getArgumentExprs()) {
496+
final Expr[] params = /*isDynamic
497+
? ((DynamicInvocationExpr) invoker.getExpr()).getPrintedArgs()
498+
: */invoker.getExpr().getArgumentExprs();
499+
for (Expr argumentExpr : params) {
487500
assert argumentExpr != null : "Argument of index " + index + " is null!";
488501
index++;
489502
}
490503

491-
final Expr[] args = new Expr[invoker.getExpr().getArgumentExprs().length + 1];
504+
final Expr[] args = new Expr[params.length + 1];
492505
System.arraycopy(
493-
invoker.getExpr().getArgumentExprs(),
506+
params,
494507
0,
495508
args,
496509
0,
497-
invoker.getExpr().getArgumentExprs().length
510+
params.length
498511
);
499512

500513
final ConstantExpr constant = new SkidConstantExpr(skidGroup.getPredicate().getPublic());
501514
args[args.length - 1] = constant;
502515

503516
invoker.getExpr().setArgumentExprs(args);
504-
invoker.getExpr().setDesc(desc);
517+
518+
if (isDynamic) {
519+
final Handle boundFunc = (Handle) ((DynamicInvocationExpr) invoker.getExpr()).getBootstrapArgs()[1];
520+
final Parameter handlerDesc = new Parameter(boundFunc.getDesc());
521+
handlerDesc.addParameter(Type.INT_TYPE);
522+
final Handle newBoundFunc = new Handle(boundFunc.getTag(), boundFunc.getOwner(), boundFunc.getName(),
523+
handlerDesc.getDesc(), boundFunc.isInterface());
524+
525+
final Parameter parameter = new Parameter(invoker.getExpr().getDesc());
526+
parameter.addParameter(Type.INT_TYPE);
527+
invoker.getExpr().setDesc(parameter.getDesc());
528+
//System.out.println(((DynamicInvocationExpr) invoker.getExpr()).getDesc() + " new: " + desc);
529+
((DynamicInvocationExpr) invoker.getExpr()).getBootstrapArgs()[1] = newBoundFunc;
530+
} else {
531+
invoker.getExpr().setDesc(desc);
532+
}
505533
}
506534

507535
final int finalStackHeight = stackHeight;

0 commit comments

Comments
 (0)