Skip to content

Commit e6a468c

Browse files
author
LisoUseInAIKyrios
authored
fix(TikTok): Hook application context earlier to prevent crash (#2893)
1 parent 38392de commit e6a468c

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

api/revanced-patches.api

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,19 @@ public abstract class app/revanced/patches/shared/misc/integrations/BaseIntegrat
628628
public fun <init> ()V
629629
public fun <init> (Ljava/lang/String;Ljava/lang/Integer;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)V
630630
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Integer;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
631+
public fun <init> (Ljava/lang/String;Ljava/lang/Integer;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)V
632+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Integer;Ljava/lang/Iterable;Ljava/lang/Iterable;Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
631633
public final fun invoke (Ljava/lang/String;)V
632634
}
633635

636+
public abstract interface class app/revanced/patches/shared/misc/integrations/BaseIntegrationsPatch$IntegrationsFingerprint$IHookInsertIndexResolver : kotlin/jvm/functions/Function1 {
637+
public abstract fun invoke (Lcom/android/tools/smali/dexlib2/iface/Method;)Ljava/lang/Integer;
638+
}
639+
640+
public final class app/revanced/patches/shared/misc/integrations/BaseIntegrationsPatch$IntegrationsFingerprint$IHookInsertIndexResolver$DefaultImpls {
641+
public static fun invoke (Lapp/revanced/patches/shared/misc/integrations/BaseIntegrationsPatch$IntegrationsFingerprint$IHookInsertIndexResolver;Lcom/android/tools/smali/dexlib2/iface/Method;)Ljava/lang/Integer;
642+
}
643+
634644
public abstract interface class app/revanced/patches/shared/misc/integrations/BaseIntegrationsPatch$IntegrationsFingerprint$IRegisterResolver : kotlin/jvm/functions/Function1 {
635645
public abstract fun invoke (Lcom/android/tools/smali/dexlib2/iface/Method;)Ljava/lang/Integer;
636646
}

src/main/kotlin/app/revanced/patches/shared/misc/integrations/BaseIntegrationsPatch.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ abstract class BaseIntegrationsPatch(
4949
opcodes: Iterable<Opcode?>? = null,
5050
strings: Iterable<String>? = null,
5151
customFingerprint: ((methodDef: Method, classDef: ClassDef) -> Boolean)? = null,
52-
private val contextRegisterResolver: (Method) -> Int = object : IRegisterResolver {},
52+
private val insertIndexResolver: ((Method) -> Int) = object : IHookInsertIndexResolver {},
53+
private val contextRegisterResolver: (Method) -> Int = object : IRegisterResolver {}
5354
) : MethodFingerprint(
5455
returnType,
5556
accessFlags,
@@ -58,18 +59,45 @@ abstract class BaseIntegrationsPatch(
5859
strings,
5960
customFingerprint,
6061
) {
62+
@Deprecated("Previous constructor that is missing the insert index." +
63+
"Here only for binary compatibility, " +
64+
"and this can be removed after the next major version update.")
65+
constructor(
66+
returnType: String? = null,
67+
accessFlags: Int? = null,
68+
parameters: Iterable<String>? = null,
69+
opcodes: Iterable<Opcode?>? = null,
70+
strings: Iterable<String>? = null,
71+
customFingerprint: ((methodDef: Method, classDef: ClassDef) -> Boolean)? = null,
72+
contextRegisterResolver: (Method) -> Int = object : IRegisterResolver {}
73+
) : this(
74+
returnType,
75+
accessFlags,
76+
parameters,
77+
opcodes,
78+
strings,
79+
customFingerprint,
80+
object : IHookInsertIndexResolver {},
81+
contextRegisterResolver
82+
)
83+
6184
fun invoke(integrationsDescriptor: String) {
6285
result?.mutableMethod?.let { method ->
86+
val insertIndex = insertIndexResolver(method)
6387
val contextRegister = contextRegisterResolver(method)
6488

6589
method.addInstruction(
66-
0,
90+
insertIndex,
6791
"invoke-static/range { v$contextRegister .. v$contextRegister }, " +
6892
"$integrationsDescriptor->setContext(Landroid/content/Context;)V",
6993
)
7094
} ?: throw PatchException("Could not find hook target fingerprint.")
7195
}
7296

97+
interface IHookInsertIndexResolver : (Method) -> Int {
98+
override operator fun invoke(method: Method) = 0
99+
}
100+
73101
interface IRegisterResolver : (Method) -> Int {
74102
override operator fun invoke(method: Method) = method.implementation!!.registerCount - 1
75103
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package app.revanced.patches.tiktok.misc.integrations.fingerprints
22

3+
import app.revanced.patcher.extensions.or
34
import app.revanced.patches.shared.misc.integrations.BaseIntegrationsPatch.IntegrationsFingerprint
5+
import com.android.tools.smali.dexlib2.AccessFlags
46

57
internal object InitFingerprint : IntegrationsFingerprint(
8+
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
69
customFingerprint = { methodDef, _ ->
710
methodDef.definingClass.endsWith("/AwemeHostApplication;") &&
8-
methodDef.name == "onCreate"
9-
}
11+
methodDef.name == "<init>"
12+
},
13+
insertIndexResolver = { 1 } // Insert after call to super class.
1014
)

0 commit comments

Comments
 (0)