From a798aab51918b9b1acac53fe2ced75821a59742c Mon Sep 17 00:00:00 2001 From: inotia00 Date: Mon, 25 Sep 2023 11:25:39 +0900 Subject: [PATCH] feat(music/litho-filter): no longer uses `identifier` parameter --- .../utils/litho/patch/LithoFilterPatch.kt | 4 +- .../litho/EmptyComponentBuilderFingerprint.kt | 7 ++ .../patch/litho/ComponentParserPatch.kt | 92 +++++++------------ 3 files changed, 44 insertions(+), 59 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/music/utils/litho/patch/LithoFilterPatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/litho/patch/LithoFilterPatch.kt index f9946c077..4b56e6fc1 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/litho/patch/LithoFilterPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/litho/patch/LithoFilterPatch.kt @@ -9,7 +9,7 @@ import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patches.music.utils.annotations.MusicCompatibility import app.revanced.patches.music.utils.litho.fingerprints.LithoFilterFingerprint import app.revanced.patches.shared.patch.litho.ComponentParserPatch -import app.revanced.patches.shared.patch.litho.ComponentParserPatch.Companion.identifierHook +import app.revanced.patches.shared.patch.litho.ComponentParserPatch.Companion.pathBuilderHook import app.revanced.util.integrations.Constants.MUSIC_ADS_PATH import java.io.Closeable @@ -19,7 +19,7 @@ class LithoFilterPatch : BytecodePatch( listOf(LithoFilterFingerprint) ), Closeable { override fun execute(context: BytecodeContext) { - identifierHook("$MUSIC_ADS_PATH/LithoFilterPatch;->filter") + pathBuilderHook("$MUSIC_ADS_PATH/LithoFilterPatch;->filter") LithoFilterFingerprint.result?.let { it.mutableMethod.apply { diff --git a/src/main/kotlin/app/revanced/patches/shared/fingerprints/litho/EmptyComponentBuilderFingerprint.kt b/src/main/kotlin/app/revanced/patches/shared/fingerprints/litho/EmptyComponentBuilderFingerprint.kt index ed61857f5..366a6e649 100644 --- a/src/main/kotlin/app/revanced/patches/shared/fingerprints/litho/EmptyComponentBuilderFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/shared/fingerprints/litho/EmptyComponentBuilderFingerprint.kt @@ -3,9 +3,16 @@ package app.revanced.patches.shared.fingerprints.litho import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import com.android.tools.smali.dexlib2.AccessFlags +import com.android.tools.smali.dexlib2.Opcode object EmptyComponentBuilderFingerprint : MethodFingerprint( returnType = "L", accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + opcodes = listOf( + Opcode.INVOKE_INTERFACE, + Opcode.INVOKE_STATIC_RANGE, + Opcode.MOVE_RESULT_OBJECT, + Opcode.IGET_OBJECT + ), strings = listOf("Error while converting %s") ) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/shared/patch/litho/ComponentParserPatch.kt b/src/main/kotlin/app/revanced/patches/shared/patch/litho/ComponentParserPatch.kt index 087e4399d..0f2118168 100644 --- a/src/main/kotlin/app/revanced/patches/shared/patch/litho/ComponentParserPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/patch/litho/ComponentParserPatch.kt @@ -5,13 +5,11 @@ import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.BytecodePatch -import app.revanced.patcher.patch.PatchException import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.shared.fingerprints.litho.EmptyComponentBuilderFingerprint import app.revanced.patches.shared.fingerprints.litho.IdentifierFingerprint import app.revanced.util.bytecode.getStringIndex -import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.builder.instruction.BuilderInstruction35c import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction @@ -27,36 +25,26 @@ class ComponentParserPatch : BytecodePatch( ) { override fun execute(context: BytecodeContext) { + /** + * Shared fingerprint + */ EmptyComponentBuilderFingerprint.result?.let { it.mutableMethod.apply { - val targetStringIndex = getStringIndex("Error while converting %s") + insertMethod = this + emptyComponentIndex = it.scanResult.patternScanResult!!.startIndex + 1 - for (index in targetStringIndex until implementation!!.instructions.size - 1) { - if (getInstruction(index).opcode != Opcode.INVOKE_STATIC_RANGE) continue + val builderMethodDescriptor = + getInstruction(emptyComponentIndex).reference + val emptyComponentFieldDescriptor = + getInstruction(emptyComponentIndex + 2).reference - val builderMethodDescriptor = - getInstruction(index).reference - val emptyComponentFieldDescriptor = - getInstruction(index + 2).reference - - emptyComponentLabel = """ + emptyComponentLabel = """ move-object/from16 v0, p1 invoke-static {v0}, $builderMethodDescriptor move-result-object v0 iget-object v0, v0, $emptyComponentFieldDescriptor return-object v0 """ - break - } - - if (emptyComponentLabel.isEmpty()) - throw PatchException("could not find Empty Component Label in method") - } - } ?: throw EmptyComponentBuilderFingerprint.exception - - IdentifierFingerprint.result?.let { - it.mutableMethod.apply { - insertMethod = this val stringBuilderIndex = implementation!!.instructions.indexOfFirst { instruction -> @@ -65,29 +53,26 @@ class ComponentParserPatch : BytecodePatch( fieldReference?.let { reference -> reference.type == "Ljava/lang/StringBuilder;" } == true } - val identifierIndex = it.scanResult.patternScanResult!!.endIndex - val objectIndex = getStringIndex("") + 1 - val freeIndex = implementation!!.instructions.indexOfFirst { instruction -> - instruction.opcode == Opcode.CONST - } - stringBuilderRegister = getInstruction(stringBuilderIndex).registerA - identifierRegister = - getInstruction(identifierIndex).registerA - objectRegister = getInstruction(objectIndex).registerC - - val register = getInstruction(freeIndex).registerA - - freeRegister = - if (register == stringBuilderRegister || register == identifierRegister || register == objectRegister) - 15 - else - register insertIndex = stringBuilderIndex + 1 } - } ?: throw IdentifierFingerprint.exception + } ?: throw EmptyComponentBuilderFingerprint.exception + + /** + * Only used in YouTube + */ + IdentifierFingerprint.result?.let { + it.mutableMethod.apply { + val identifierIndex = it.scanResult.patternScanResult!!.endIndex + val objectIndex = getStringIndex("") + 1 + + identifierRegister = + getInstruction(identifierIndex).registerA + objectRegister = getInstruction(objectIndex).registerC + } + } } @@ -95,42 +80,35 @@ class ComponentParserPatch : BytecodePatch( lateinit var emptyComponentLabel: String lateinit var insertMethod: MutableMethod + var emptyComponentIndex by Delegates.notNull() var insertIndex by Delegates.notNull() - var freeRegister = 15 - var identifierRegister by Delegates.notNull() var objectRegister by Delegates.notNull() var stringBuilderRegister by Delegates.notNull() - fun generalHook( - descriptor: String - ) { + fun generalHook(descriptor: String) { insertMethod.apply { addInstructionsWithLabels( insertIndex, """ invoke-static {v$stringBuilderRegister, v$identifierRegister, v$objectRegister}, $descriptor(Ljava/lang/StringBuilder;Ljava/lang/String;Ljava/lang/Object;)Z - move-result v$freeRegister - if-eqz v$freeRegister, :unfiltered - """ + emptyComponentLabel, - ExternalLabel("unfiltered", getInstruction(insertIndex)) + move-result v$stringBuilderRegister + if-nez v$stringBuilderRegister, :filter + """, ExternalLabel("filter", getInstruction(emptyComponentIndex)) ) } } - fun identifierHook( - descriptor: String - ) { + fun pathBuilderHook(descriptor: String) { insertMethod.apply { addInstructionsWithLabels( insertIndex, """ - invoke-static {v$stringBuilderRegister, v$identifierRegister}, $descriptor(Ljava/lang/StringBuilder;Ljava/lang/String;)Z - move-result v$freeRegister - if-eqz v$freeRegister, :unfiltered - """ + emptyComponentLabel, - ExternalLabel("unfiltered", getInstruction(insertIndex)) + invoke-static {v$stringBuilderRegister}, $descriptor(Ljava/lang/StringBuilder;)Z + move-result v$stringBuilderRegister + if-nez v$stringBuilderRegister, :filter + """, ExternalLabel("filter", getInstruction(emptyComponentIndex)) ) } }