Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(youtube): improve patching speed #2514

Merged
merged 27 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f20fb23
improve resolving speed
LisoUseInAIKyrios May 31, 2023
acab809
Merge remote-tracking branch 'upstream/dev' into resolving_performanc…
LisoUseInAIKyrios Jun 1, 2023
b01c62a
fixing clunky resolving
LisoUseInAIKyrios Jun 1, 2023
dad2265
Speeding up the slowest resolving fingerprints. Now only 5 fingerpri…
LisoUseInAIKyrios Jun 1, 2023
8e9d552
replace copy pasted fingerprints with existing utility fingerprint
LisoUseInAIKyrios Jun 1, 2023
00e6384
Merge remote-tracking branch 'upstream/dev' into resolving_performanc…
LisoUseInAIKyrios Jun 2, 2023
96a4a76
comments
LisoUseInAIKyrios Jun 3, 2023
fa5ff86
tiny performance tweak
LisoUseInAIKyrios Jun 3, 2023
2328e07
more adjustments for patching on old devices
LisoUseInAIKyrios Jun 4, 2023
61ecd31
adding missing dependency
LisoUseInAIKyrios Jun 4, 2023
ec53897
much faster on old devices
LisoUseInAIKyrios Jun 4, 2023
3237d6c
adding missing code
LisoUseInAIKyrios Jun 4, 2023
e2c4baa
a few last adjustments
LisoUseInAIKyrios Jun 4, 2023
459c88d
removing twitch change that crept in
LisoUseInAIKyrios Jun 4, 2023
eda8897
improve fingerprint that nearly matches 4 other locations ("No match"…
LisoUseInAIKyrios Jun 4, 2023
08bf352
Merge remote-tracking branch 'upstream/dev' into resolving_performanc…
LisoUseInAIKyrios Jun 7, 2023
2cb554b
Merge remote-tracking branch 'upstream/dev' into resolving_performanc…
LisoUseInAIKyrios Jun 10, 2023
84ebcce
Merge remote-tracking branch 'upstream/dev' into resolving_performanc…
LisoUseInAIKyrios Jun 11, 2023
60f09f7
Merge remote-tracking branch 'upstream/dev' into resolving_performanc…
LisoUseInAIKyrios Jun 15, 2023
c044b54
performance tweak: check access flags instead of method name
LisoUseInAIKyrios Jun 15, 2023
f4de070
Merge remote-tracking branch 'upstream/dev' into resolving_performanc…
LisoUseInAIKyrios Jun 18, 2023
22d0414
Merge branch 'revanced:main' into resolving_performance_tweaks
LisoUseInAIKyrios Jun 24, 2023
07a9285
Merge remote-tracking branch 'upstream/dev' into resolving_performanc…
LisoUseInAIKyrios Jun 24, 2023
7f4d223
Merge branch 'dev' into resolving_performance_tweaks
LisoUseInAIKyrios Jun 27, 2023
fc239f5
use a different and more unique parent method fingerprint.
LisoUseInAIKyrios Jun 28, 2023
f801f10
use a more unique section of opcodes
LisoUseInAIKyrios Jun 28, 2023
f1e8975
Merge branch 'dev' into resolving_performance_tweaks
LisoUseInAIKyrios Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/main/kotlin/app/revanced/extensions/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,28 @@ internal fun MutableMethod.injectHideViewCall(
"invoke-static { v$viewRegister }, $classDescriptor->$targetMethod(Landroid/view/View;)V"
)

internal fun MutableMethod.findIndexForIdResource(resourceName: String): Int {
internal fun Method.findIndexForIdResource(resourceName: String): Int {
fun getIdResourceId(resourceName: String) = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == resourceName
}.id

val resourceId = getIdResourceId(resourceName)
return implementation!!.instructions.indexOfFirst {
if (it.opcode != Opcode.CONST) return@indexOfFirst false
return indexOfFirstConstantInstructionValue(getIdResourceId(resourceName))
}

val literal = (it as WideLiteralInstruction).wideLiteral
/**
* @return the first constant instruction with the value, or -1 if not found.
*/
fun Method.indexOfFirstConstantInstructionValue(constantValue: Long): Int {
LisoUseInAIKyrios marked this conversation as resolved.
Show resolved Hide resolved
return implementation?.let {
it.instructions.indexOfFirst { instruction ->
instruction.opcode == Opcode.CONST && (instruction as WideLiteralInstruction).wideLiteral == constantValue
}
} ?: -1
}

return@indexOfFirst resourceId == literal
}
}
/**
* @return if the method contains a constant with the given value.
*/
fun Method.containsConstantInstructionValue(constantValue: Long): Boolean {
return indexOfFirstConstantInstructionValue(constantValue) >= 0
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package app.revanced.patches.music.misc.androidauto.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.music.misc.androidauto.patch.BypassCertificateChecksPatch


object CheckCertificateFingerprint : MethodFingerprint(
"Z",
strings = listOf("No match") // Unique in combination with boolean return type
returnType = "Z",
parameters = listOf("Ljava/lang/String;"),
strings = listOf("X509", "Failed to get public key.", "Failed to get certificate.")
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package app.revanced.patches.shared.fingerprints


import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags

object WatchWhileActivityFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
parameters = listOf(),
customFingerprint = { methodDef, _ ->
methodDef.definingClass.endsWith("WatchWhileActivity;") && methodDef.name == "<init>"
methodDef.definingClass.endsWith("WatchWhileActivity;")
}
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package app.revanced.patches.shared.misc.fix.verticalscroll.fingerprints


import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode

object CanScrollVerticallyFingerprint : MethodFingerprint(
"Z",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "Z",
parameters = emptyList(),
opcodes = listOf(
Opcode.MOVE_RESULT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class SettingsPatch : BytecodePatch(
} - 6


// fixme: instead use Method.indexOfFirstConstantInstructionValue()
val copyrightPolicyIndex = instructions.indexOfFirst {
(it as? WideLiteralInstruction)?.wideLiteral == copyrightPolicyLabelId
} + 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package app.revanced.patches.youtube.ad.getpremium.bytecode.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode

object GetPremiumViewFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PROTECTED or AccessFlags.FINAL,
returnType = "V",
parameters = listOf("I", "I"),
opcodes = listOf(
Opcode.ADD_INT_2ADDR,
Opcode.ADD_INT_2ADDR,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package app.revanced.patches.youtube.interaction.seekbar.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.youtube.interaction.seekbar.patch.EnableSeekbarTappingResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction


object AccessibilityPlayerProgressTimeFingerprint : MethodFingerprint(
object AccessibilityPlayerProgressTimeFingerprint : LiteralValueFingerprint(
returnType = "L",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
if (instruction.opcode != Opcode.CONST) return@any false

val wideLiteral = (instruction as WideLiteralInstruction).wideLiteral

EnableSeekbarTappingResourcePatch.accessibilityPlayerProgressTime == wideLiteral
} ?: false
}
literal = EnableSeekbarTappingResourcePatch.accessibilityPlayerProgressTime
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package app.revanced.patches.youtube.interaction.swipecontrols.fingerprints


import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags

object SwipeControlsHostActivityFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
parameters = listOf(),
customFingerprint = { methodDef, _ ->
methodDef.definingClass == "Lapp/revanced/integrations/swipecontrols/SwipeControlsHostActivity;" && methodDef.name == "<init>"
methodDef.definingClass == "Lapp/revanced/integrations/swipecontrols/SwipeControlsHostActivity;"
}
)
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package app.revanced.patches.youtube.layout.autocaptions.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode


@FuzzyPatternScanMethod(3)
object StartVideoInformerFingerprint : MethodFingerprint(
"V", AccessFlags.PUBLIC or AccessFlags.FINAL, listOf("L", "L", "L", "L"), listOf(
Opcode.INVOKE_STATIC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode

object SubtitleButtonControllerFingerprint : MethodFingerprint(
"V", AccessFlags.PUBLIC or AccessFlags.FINAL, listOf("L"), listOf(
returnType = "V",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("Lcom/google/android/libraries/youtube/player/subtitles/model/SubtitleTrack;"),
opcodes = listOf(
Opcode.IGET_OBJECT,
Opcode.IF_NEZ,
Opcode.RETURN_VOID,
Expand All @@ -16,5 +19,8 @@ object SubtitleButtonControllerFingerprint : MethodFingerprint(
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.IGET_OBJECT,
)
),
customFingerprint = { methodDef, _ ->
methodDef.definingClass.endsWith("SubtitleButtonController;")
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ object SubtitleTrackFingerprint : MethodFingerprint(
Opcode.MOVE_RESULT,
Opcode.RETURN,
),
strings = listOf("DISABLE_CAPTIONS_OPTION")
strings = listOf("DISABLE_CAPTIONS_OPTION"),
customFingerprint = { methodDef, _ ->
methodDef.definingClass.endsWith("SubtitleTrack;")
}
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.revanced.patches.youtube.layout.buttons.autoplay.patch

import app.revanced.extensions.findIndexForIdResource
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
Expand All @@ -23,7 +24,6 @@ import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import org.jf.dexlib2.iface.instruction.Instruction
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction
import org.jf.dexlib2.iface.reference.MethodReference

@Patch
Expand All @@ -48,15 +48,8 @@ class HideAutoplayButtonPatch : BytecodePatch(
LayoutConstructorFingerprint.result?.mutableMethod?.apply {
val layoutGenMethodInstructions = implementation!!.instructions

// resolve the offsets such as ...
val autoNavPreviewStubId = ResourceMappingPatch.resourceMappings.single {
it.name == "autonav_preview_stub"
}.id

// where to insert the branch instructions and ...
val insertIndex = layoutGenMethodInstructions.indexOfFirst {
(it as? WideLiteralInstruction)?.wideLiteral == autoNavPreviewStubId
}
// resolve the offsets of where to insert the branch instructions and ...
val insertIndex = findIndexForIdResource("autonav_preview_stub")

// where to branch away
val branchIndex =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package app.revanced.patches.youtube.layout.buttons.navigation.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.layout.buttons.navigation.patch.ResolvePivotBarFingerprintsPatch
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags

object InitializeButtonsFingerprint : MethodFingerprint(
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any {
it.opcode == Opcode.CONST && (it as WideLiteralInstruction).wideLiteral ==
ResolvePivotBarFingerprintsPatch.imageOnlyTabResourceId
} == true
}
object InitializeButtonsFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "V",
parameters = listOf(),
literal = ResolvePivotBarFingerprintsPatch.imageOnlyTabResourceId
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import org.jf.dexlib2.Opcode

object PlayerControlsVisibilityModelFingerprint : MethodFingerprint(
opcodes = listOf(Opcode.INVOKE_DIRECT_RANGE),
strings = listOf("hasNext", "hasPrevious", "Missing required properties:")
strings = listOf("Missing required properties:", "hasNext", "hasPrevious")
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package app.revanced.patches.youtube.layout.hide.albumcards.bytecode.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.layout.hide.albumcards.resource.patch.AlbumCardsResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction

object AlbumCardsFingerprint : MethodFingerprint(
object AlbumCardsFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
opcodes = listOf(
Opcode.MOVE_RESULT_OBJECT,
Opcode.CONST,
Expand All @@ -14,10 +16,5 @@ object AlbumCardsFingerprint : MethodFingerprint(
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
),
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
instruction.opcode.ordinal == Opcode.CONST.ordinal &&
(instruction as? WideLiteralInstruction)?.wideLiteral == AlbumCardsResourcePatch.albumCardId
} == true
}
literal = AlbumCardsResourcePatch.albumCardId
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package app.revanced.patches.youtube.layout.hide.breakingnews.bytecode.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.layout.hide.breakingnews.resource.patch.BreakingNewsResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction

object BreakingNewsFingerprint : MethodFingerprint(
object BreakingNewsFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
opcodes = listOf(
Opcode.CONST,
Opcode.CONST_4,
Expand All @@ -14,10 +16,5 @@ object BreakingNewsFingerprint : MethodFingerprint(
Opcode.CHECK_CAST,
Opcode.IPUT_OBJECT,
),
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
instruction.opcode.ordinal == Opcode.CONST.ordinal &&
(instruction as? WideLiteralInstruction)?.wideLiteral == BreakingNewsResourcePatch.horizontalCardListId
} == true
}
literal = BreakingNewsResourcePatch.horizontalCardListId
)
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package app.revanced.patches.youtube.layout.hide.crowdfundingbox.bytecode.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.layout.hide.crowdfundingbox.resource.patch.CrowdfundingBoxResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction

object CrowdfundingBoxFingerprint : MethodFingerprint(
object CrowdfundingBoxFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
opcodes = listOf(
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.IPUT_OBJECT,
),
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
instruction.opcode.ordinal == Opcode.CONST.ordinal &&
(instruction as? WideLiteralInstruction)?.wideLiteral == CrowdfundingBoxResourcePatch.crowdfundingBoxId
} == true
}
literal = CrowdfundingBoxResourcePatch.crowdfundingBoxId
)
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package app.revanced.patches.youtube.layout.hide.endscreencards.bytecode.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.youtube.layout.hide.endscreencards.resource.patch.HideEndscreenCardsResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction

object LayoutCircleFingerprint : MethodFingerprint(
object LayoutCircleFingerprint : LiteralValueFingerprint(
returnType = "Landroid/view/View;",
opcodes = listOf(
Opcode.CONST,
Opcode.CONST_4,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
),
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
instruction.opcode.ordinal == Opcode.CONST.ordinal &&
(instruction as? WideLiteralInstruction)?.wideLiteral == HideEndscreenCardsResourcePatch.layoutCircle
} == true
}
literal = HideEndscreenCardsResourcePatch.layoutCircle
)
Loading