Skip to content

Commit 4ad4887

Browse files
authored
fix(Messenger - Remove Meta AI): Improve patch logic (#5153)
1 parent bc79e48 commit 4ad4887

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

extensions/messenger/src/main/java/app/revanced/extension/messenger/metaai/RemoveMetaAIPatch.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package app.revanced.extension.messenger.metaai;
22

3+
import java.util.*;
4+
5+
import app.revanced.extension.shared.Logger;
6+
37
@SuppressWarnings("unused")
48
public class RemoveMetaAIPatch {
9+
private static final Set<Long> loggedIDs = Collections.synchronizedSet(new HashSet<>());
10+
511
public static boolean overrideBooleanFlag(long id, boolean value) {
6-
// This catches all flag IDs related to Meta AI.
7-
// The IDs change slightly with every update,
8-
// so to work around this, IDs from different versions were compared
9-
// to find what they have in common, which turned out to be those first bits.
10-
// TODO: Find the specific flags that we care about and patch the code they control instead.
11-
if ((id & 0x7FFFFFC000000000L) == 0x810A8000000000L) {
12-
return false;
12+
try {
13+
if (Long.toString(id).startsWith("REPLACED_BY_PATCH")) {
14+
if (loggedIDs.add(id))
15+
Logger.printInfo(() -> "Overriding " + id + " from " + value + " to false");
16+
17+
return false;
18+
}
19+
} catch (Exception ex) {
20+
Logger.printException(() -> "overrideBooleanFlag failure", ex);
1321
}
1422

1523
return value;

patches/src/main/kotlin/app/revanced/patches/messenger/metaai/Fingerprints.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ internal val getMobileConfigBoolFingerprint = fingerprint {
1111
classDef.interfaces.contains("Lcom/facebook/mobileconfig/factory/MobileConfigUnsafeContext;")
1212
}
1313
}
14+
15+
internal val metaAIKillSwitchCheckFingerprint = fingerprint {
16+
strings("SearchAiagentImplementationsKillSwitch")
17+
opcodes(Opcode.CONST_WIDE)
18+
}
19+
20+
internal val extensionMethodFingerprint = fingerprint {
21+
strings("REPLACED_BY_PATCH")
22+
custom { method, classDef ->
23+
method.name == EXTENSION_METHOD_NAME && classDef.type == EXTENSION_CLASS_DESCRIPTOR
24+
}
25+
}

patches/src/main/kotlin/app/revanced/patches/messenger/metaai/RemoveMetaAIPatch.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package app.revanced.patches.messenger.metaai
22

33
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
44
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
5+
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
56
import app.revanced.patcher.patch.bytecodePatch
67
import app.revanced.patches.messenger.misc.extension.sharedExtensionPatch
78
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
9+
import com.android.tools.smali.dexlib2.iface.instruction.WideLiteralInstruction
810

9-
private const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/extension/messenger/metaai/RemoveMetaAIPatch;"
11+
internal const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/extension/messenger/metaai/RemoveMetaAIPatch;"
12+
internal const val EXTENSION_METHOD_NAME = "overrideBooleanFlag"
1013

1114
@Suppress("unused")
1215
val removeMetaAIPatch = bytecodePatch(
@@ -25,10 +28,25 @@ val removeMetaAIPatch = bytecodePatch(
2528
addInstructions(
2629
returnIndex,
2730
"""
28-
invoke-static { p1, p2, v$returnRegister }, $EXTENSION_CLASS_DESCRIPTOR->overrideBooleanFlag(JZ)Z
31+
invoke-static { p1, p2, v$returnRegister }, $EXTENSION_CLASS_DESCRIPTOR->$EXTENSION_METHOD_NAME(JZ)Z
2932
move-result v$returnRegister
3033
"""
3134
)
3235
}
36+
37+
// Extract the common starting digits of Meta AI flag IDs from a flag found in code.
38+
val relevantDigits = with(metaAIKillSwitchCheckFingerprint) {
39+
method.getInstruction<WideLiteralInstruction>(patternMatch!!.startIndex).wideLiteral
40+
}.toString().substring(0, 7)
41+
42+
// Replace placeholder in the extension method.
43+
with(extensionMethodFingerprint) {
44+
method.replaceInstruction(
45+
stringMatches!!.first().index,
46+
"""
47+
const-string v1, "$relevantDigits"
48+
"""
49+
)
50+
}
3351
}
3452
}

patches/src/main/kotlin/app/revanced/patches/messenger/misc/extension/ExtensionPatch.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package app.revanced.patches.messenger.misc.extension
22

33
import app.revanced.patches.shared.misc.extension.sharedExtensionPatch
44

5-
val sharedExtensionPatch = sharedExtensionPatch("messenger", mainActivityOnCreateHook)
5+
val sharedExtensionPatch = sharedExtensionPatch("messenger", messengerApplicationOnCreateHook)

patches/src/main/kotlin/app/revanced/patches/messenger/misc/extension/Hooks.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package app.revanced.patches.messenger.misc.extension
22

33
import app.revanced.patches.shared.misc.extension.extensionHook
44

5-
internal val mainActivityOnCreateHook = extensionHook {
6-
strings("MainActivity_onCreate_begin")
5+
internal val messengerApplicationOnCreateHook = extensionHook {
6+
custom { method, classDef ->
7+
method.name == "onCreate" && classDef.endsWith("/MessengerApplication;")
8+
}
79
}

0 commit comments

Comments
 (0)