From 8a10f677d6b0372517ed3b51e5b0ee8395fc31c1 Mon Sep 17 00:00:00 2001 From: shifujun Date: Tue, 18 Apr 2023 17:05:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(core.transform-kit):=20=E5=8E=BB=E6=8E=89ja?= =?UTF-8?q?vassist=20replaceNew=E4=B8=AD=E7=9A=84DUP=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit javac总是生成new+dup,但这并不是字节码规范。proguard等工具可能会优化掉dup。 fix #1155 --- .../java/javassist/EnhancedCodeConverter.java | 6 ++ .../convert/TransformNewClassFix.java | 70 +++++++++++++++++++ .../transform/specific/WebViewTransform.kt | 4 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 projects/sdk/core/transform-kit/src/main/java/javassist/convert/TransformNewClassFix.java diff --git a/projects/sdk/core/transform-kit/src/main/java/javassist/EnhancedCodeConverter.java b/projects/sdk/core/transform-kit/src/main/java/javassist/EnhancedCodeConverter.java index 44b507904..bd803b3c3 100644 --- a/projects/sdk/core/transform-kit/src/main/java/javassist/EnhancedCodeConverter.java +++ b/projects/sdk/core/transform-kit/src/main/java/javassist/EnhancedCodeConverter.java @@ -1,6 +1,7 @@ package javassist; import javassist.convert.TransformCallExceptSuperCallToStatic; +import javassist.convert.TransformNewClassFix; public class EnhancedCodeConverter extends CodeConverter { @@ -8,4 +9,9 @@ public void redirectMethodCallExceptSuperCallToStatic(CtMethod origMethod, CtMet transformers = new TransformCallExceptSuperCallToStatic(transformers, origMethod, substMethod); } + + public void replaceNew(CtClass oldClass, CtClass newClass) { + transformers = new TransformNewClassFix(transformers, oldClass.getName(), + newClass.getName()); + } } diff --git a/projects/sdk/core/transform-kit/src/main/java/javassist/convert/TransformNewClassFix.java b/projects/sdk/core/transform-kit/src/main/java/javassist/convert/TransformNewClassFix.java new file mode 100644 index 000000000..2348fcd37 --- /dev/null +++ b/projects/sdk/core/transform-kit/src/main/java/javassist/convert/TransformNewClassFix.java @@ -0,0 +1,70 @@ +package javassist.convert; + +import javassist.CannotCompileException; +import javassist.CtClass; +import javassist.bytecode.CodeAttribute; +import javassist.bytecode.CodeIterator; +import javassist.bytecode.ConstPool; + +/** + * 完全复制自TransformNewClass代码 + * 去掉“"NEW followed by no DUP was found"检查 + */ +final public class TransformNewClassFix extends Transformer { + private int nested; + private String classname, newClassName; + private int newClassIndex, newMethodNTIndex, newMethodIndex; + + public TransformNewClassFix(Transformer next, + String classname, String newClassName) { + super(next); + this.classname = classname; + this.newClassName = newClassName; + } + + @Override + public void initialize(ConstPool cp, CodeAttribute attr) { + nested = 0; + newClassIndex = newMethodNTIndex = newMethodIndex = 0; + } + + /** + * Modifies a sequence of + * NEW classname + * DUP + * ... + * INVOKESPECIAL classname:method + */ + @Override + public int transform(CtClass clazz, int pos, CodeIterator iterator, + ConstPool cp) throws CannotCompileException { + int index; + int c = iterator.byteAt(pos); + if (c == NEW) { + index = iterator.u16bitAt(pos + 1); + if (cp.getClassInfo(index).equals(classname)) { + + if (newClassIndex == 0) + newClassIndex = cp.addClassInfo(newClassName); + + iterator.write16bit(newClassIndex, pos + 1); + ++nested; + } + } else if (c == INVOKESPECIAL) { + index = iterator.u16bitAt(pos + 1); + int typedesc = cp.isConstructor(classname, index); + if (typedesc != 0 && nested > 0) { + int nt = cp.getMethodrefNameAndType(index); + if (newMethodNTIndex != nt) { + newMethodNTIndex = nt; + newMethodIndex = cp.addMethodrefInfo(newClassIndex, nt); + } + + iterator.write16bit(newMethodIndex, pos + 1); + --nested; + } + } + + return pos; + } +} diff --git a/projects/sdk/core/transform/src/main/kotlin/com/tencent/shadow/core/transform/specific/WebViewTransform.kt b/projects/sdk/core/transform/src/main/kotlin/com/tencent/shadow/core/transform/specific/WebViewTransform.kt index 4d7ea4fd3..e93a73109 100644 --- a/projects/sdk/core/transform/src/main/kotlin/com/tencent/shadow/core/transform/specific/WebViewTransform.kt +++ b/projects/sdk/core/transform/src/main/kotlin/com/tencent/shadow/core/transform/specific/WebViewTransform.kt @@ -20,8 +20,8 @@ package com.tencent.shadow.core.transform.specific import com.tencent.shadow.core.transform_kit.SpecificTransform import com.tencent.shadow.core.transform_kit.TransformStep -import javassist.CodeConverter import javassist.CtClass +import javassist.EnhancedCodeConverter class WebViewTransform : SpecificTransform() { companion object { @@ -29,7 +29,7 @@ class WebViewTransform : SpecificTransform() { const val ShadowWebViewClassname = "com.tencent.shadow.core.runtime.ShadowWebView" } - val codeConverter = CodeConverter() + val codeConverter = EnhancedCodeConverter() override fun setup(allInputClass: Set) { codeConverter.replaceNew( mClassPool[AndroidWebViewClassname],