Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
101 changes: 101 additions & 0 deletions tests/recordtag/fixture/build.gradle.kts.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import com.android.build.api.artifact.SingleArtifact
import com.android.tools.smali.dexlib2.DexFileFactory
import com.android.tools.smali.dexlib2.Opcodes
import java.nio.file.AtomicMoveNotSupportedException
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.util.zip.ZipFile
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import org.gradle.process.ExecOperations
buildscript {
repositories { mavenCentral() }
dependencies { classpath("com.android.tools.smali:smali-dexlib2:3.0.10") }
}
plugins { id("com.android.application") version "@AGP@" }
android {
namespace = "org.matrix.vector.recordtest"
lint { checkReleaseBuilds = false }
compileSdk = @COMPILE_SDK@
buildToolsVersion = "@BUILD_TOOLS@"
defaultConfig { minSdk = @MIN_SDK@; multiDexEnabled = !providers.gradleProperty("monoDex").isPresent && @MULTIDEX@ }
compileOptions {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
buildTypes { release { isMinifyEnabled = true; proguardFiles("fixture-rules.pro") } }
}
@PREPARE_FRAMEWORK_DEX@

androidComponents { onVariants(selector().all()) { variant ->
val variantCapped = variant.name.replaceFirstChar { it.uppercase() }
val variantLowered = variant.name.lowercase()
@REGISTER_FRAMEWORK_DEX@
val failure = providers.gradleProperty("failureApk")
if (failure.isPresent) {
val sourceApk = variant.artifacts.get(SingleArtifact.APK)
val failureDirectory = layout.buildDirectory.dir("failure-apk/${variant.name}")
val damage = tasks.register("makeFailureApk$variantCapped") {
inputs.dir(sourceApk)
inputs.property("failure", failure)
outputs.dir(failureDirectory)
doLast {
val original = sourceApk.get().asFile.listFiles { f -> f.extension == "apk" }!!.single()
val destination = failureDirectory.get().asFile.apply { mkdirs() }.resolve("test.apk")
ZipFile(original).use { archive ->
val dex = archive.getInputStream(archive.getEntry("classes.dex")).use { it.readBytes() }
ZipOutputStream(destination.outputStream()).use { zip ->
zip.putNextEntry(ZipEntry("classes.dex"))
zip.write(dex)
zip.closeEntry()
zip.putNextEntry(ZipEntry("classes2.dex"))
zip.write(if (failure.get() == "duplicate") dex else byteArrayOf(0, 1, 2))
zip.closeEntry()
}
}
}
}
prepareFrameworkDex.configure {
dependsOn(damage)
apkDirectory.set(failureDirectory)
}
}

tasks.register("verifyFrameworkClasses$variantCapped") {
dependsOn(prepareFrameworkDex)
doLast {
val apk = variant.artifacts.get(SingleArtifact.APK).get().asFile
.listFiles { f -> f.extension == "apk" }!!.single()
val expected = mutableSetOf<String>()
ZipFile(apk).use { archive ->
archive.entries().asSequence()
.filter { Regex("classes(?:[2-9]|[1-9][0-9]+)?\\.dex").matches(it.name) }
.forEach { entry ->
val dex = temporaryDir.resolve(entry.name)
archive.getInputStream(entry).use { input -> dex.outputStream().use { input.copyTo(it) } }
DexFileFactory.loadDexFile(dex, Opcodes.getDefault()).classes.forEach {
check(expected.add(it.type)) { "Duplicate input class: ${it.type}" }
}
}
}
val actual = DexFileFactory.loadDexFile(
prepareFrameworkDex.get().outputFile.get().asFile, Opcodes.getDefault()
).classes.map { it.type }.toSet()
check(expected == actual) {
"Merged class mismatch: missing=${expected - actual}, extra=${actual - expected}"
}
}
}
tasks.register<Zip>("testFrameworkZip$variantCapped") {
dependsOn(prepareFrameworkDex)
destinationDirectory.set(layout.buildDirectory.dir("test-zips"))
archiveFileName.set("framework-${variant.name}.zip")
into("framework") {
from(dexOutPath) {
include("classes.dex")
rename("classes.dex", "vector.dex")
}
}
}

} }
1 change: 1 addition & 0 deletions tests/recordtag/fixture/fixture-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-keep class org.matrix.vector.recordtest.** { *; }
5 changes: 5 additions & 0 deletions tests/recordtag/fixture/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pluginManagement {
repositories { google(); mavenCentral(); gradlePluginPortal() }
}
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "RecordTagFixture"
1 change: 1 addition & 0 deletions tests/recordtag/fixture/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application /></manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.matrix.vector.recordtest;
public final class RecordProbe {
public record Value(int value) {}
}
162 changes: 162 additions & 0 deletions tests/recordtag/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/usr/bin/env python3
"""Exercise the production packaging task with a small Java-record APK, no NDK build."""
import argparse
from pathlib import Path
import re
import shutil
import subprocess
import zipfile

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", type=Path, required=True, help="New directory for logs and resulting DEX files")
parser.add_argument("--offline", action="store_true", help="Use cached Gradle dependencies only")
args = parser.parse_args()
output = args.output.resolve()
output.mkdir(parents=True, exist_ok=False)
root = Path(__file__).resolve().parents[2]
script = (root / "zygisk/build.gradle.kts").read_text()
root_build = (root / "build.gradle.kts").read_text()
versions = (root / "gradle/libs.versions.toml").read_text()
# Exercise the exact production task and registration, rather than a test reimplementation.
def section(source, beginning, end):
try:
start = source.index(beginning)
return source[start:source.index(end, start)]
except ValueError as error:
raise SystemExit(f"Production Gradle extraction failed: expected {beginning!r} through {end!r}") from error


def match(pattern, source, label):
result = re.search(pattern, source)
if result is None:
raise SystemExit(f"Cannot read {label} from production build configuration")
return result.group(1)


implementation = section(script, "/** Package AGP", "\ndependencies {")
registration = section(script, " // Keep prepared output separate", " // Using Sync")


def setting(name):
return match(r"val " + name + r' = ("[^"\n]+"|[0-9]+)', root_build, name).strip('"')

text = (root / "tests/recordtag/fixture/build.gradle.kts.in").read_text()
for key, value in {
"PREPARE_FRAMEWORK_DEX": implementation,
"REGISTER_FRAMEWORK_DEX": registration,
"AGP": match(r'agp = "([^"]+)"', versions, "AGP version"),
"COMPILE_SDK": setting("androidCompileSdkVersion"),
"BUILD_TOOLS": setting("androidBuildToolsVersion"),
"MIN_SDK": setting("androidMinSdkVersion"),
"MULTIDEX": match(r"multiDexEnabled = (true|false)", script, "multidex setting"),
}.items():
token = "@" + key + "@"
if token not in text:
raise SystemExit(f"Fixture template is missing {token}")
text = text.replace(token, value)
remaining = re.findall(r"@[A-Z][A-Z0-9_]*@", text)
if remaining:
raise SystemExit(f"Unresolved fixture placeholders: {remaining}")

# Keep the generated build and its inputs alongside logs for failure diagnosis.
fixture = output / "fixture"
fixture.mkdir()
for name in ("settings.gradle.kts", "fixture-rules.pro"):
shutil.copyfile(root / "tests/recordtag/fixture" / name, fixture / name)
shutil.copytree(root / "tests/recordtag/fixture/src", fixture / "src")
(fixture / "build.gradle.kts").write_text(text)

def run(name, tasks, negative=False, expected="global synthetics are missing"):
with (output / (name + ".log")).open("w") as log:
result = subprocess.run(["bash", str(root / "gradlew"), "-p", str(fixture),
*(["--offline"] if args.offline else []),
"--console=plain", *tasks], stdout=log, stderr=subprocess.STDOUT)
if negative:
diagnostic = (output / (name + ".log")).read_text()
if result.returncode == 0 or expected not in diagnostic:
raise SystemExit("Negative regression failed; see " + str(output))
if (fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex").exists():
raise SystemExit("Failed packaging left a usable-looking output")
elif result.returncode:
raise SystemExit("Build failed; see " + str(output / (name + ".log")))

run("missing-support-rejected", ["prepareFrameworkDexDebug", "-PmonoDex"], negative=True)
run("debug", ["testFrameworkZipDebug", "verifyFrameworkClassesDebug"])
run("debug-repeat", ["testFrameworkZipDebug", "verifyFrameworkClassesDebug"])
if ":prepareFrameworkDexDebug UP-TO-DATE" not in (output / "debug-repeat.log").read_text():
raise SystemExit("Repeated debug build did not reuse prepared DEX; see debug-repeat.log")
previous = (fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex").read_bytes()
probe = fixture / "src/main/java/org/matrix/vector/recordtest/AddedProbe.java"
probe.write_text("package org.matrix.vector.recordtest; public class AddedProbe { public static int value() { return 123; } }")
run("debug-changed", ["testFrameworkZipDebug", "verifyFrameworkClassesDebug"])
if previous == (fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex").read_bytes():
raise SystemExit("Source change did not regenerate prepared DEX")
shutil.copyfile(fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex", output / "debug-vector.dex")
run("release", ["testFrameworkZipRelease", "verifyFrameworkClassesRelease"])
run("release-repeat", ["testFrameworkZipRelease", "verifyFrameworkClassesRelease"])
if ":prepareFrameworkDexRelease UP-TO-DATE" not in (output / "release-repeat.log").read_text():
raise SystemExit("Repeated release build did not reuse prepared DEX")
previous = (fixture / "build/intermediates/framework_dex/release/minifyReleaseWithR8/classes.dex").read_bytes()
probe.write_text(probe.read_text().replace("return 123", "return 124"))
run("release-changed", ["testFrameworkZipRelease", "verifyFrameworkClassesRelease"])
if previous == (fixture / "build/intermediates/framework_dex/release/minifyReleaseWithR8/classes.dex").read_bytes():
raise SystemExit("Release source change did not regenerate prepared DEX")
shutil.copyfile(fixture / "build/intermediates/framework_dex/release/minifyReleaseWithR8/classes.dex", output / "release-vector.dex")
for variant in ("debug", "release"):
with zipfile.ZipFile(fixture / f"build/test-zips/framework-{variant}.zip") as archive:
dex_entries = [name for name in archive.namelist() if name.endswith(".dex")]
if dex_entries != ["framework/vector.dex"]:
raise SystemExit(f"Unexpected or duplicate framework DEX entries: {dex_entries}")
if archive.read(dex_entries[0]) != (output / f"{variant}-vector.dex").read_bytes():
raise SystemExit("Packaged DEX differs from prepared single-DEX output")

# Simulate the documented NIO exception without adding a filesystem dependency.
# Only the first move is replaced; the production fallback still performs the move.
def force_non_atomic_move(source):
# Match the operation's arguments, allowing Kotlin's normal whitespace/trailing comma styles.
operation = r"""Files\s*\.\s*move\s*\(
\s*staged\s*\.\s*toPath\s*\(\s*\)\s*,
\s*output\s*\.\s*toPath\s*\(\s*\)\s*,
\s*StandardCopyOption\s*\.\s*ATOMIC_MOVE\s*,
\s*StandardCopyOption\s*\.\s*REPLACE_EXISTING\s*,?\s*\)"""
pattern = re.compile(operation, re.VERBOSE)
if len(list(pattern.finditer(source))) != 1:
raise SystemExit("Cannot inject atomic-move failure: expected exactly one staged atomic move in production task")
return pattern.sub('throw AtomicMoveNotSupportedException(staged.path, output.path, "fixture")', source)

forced_fallback = force_non_atomic_move(text)
(fixture / "build.gradle.kts").write_text(forced_fallback)
run("atomic-move-fallback", ["testFrameworkZipDebug", "verifyFrameworkClassesDebug", "--rerun-tasks"])
(fixture / "build.gradle.kts").write_text(text)
# Restore the Debug fixture snapshot after the Release source mutation above.
shutil.copyfile(fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex", output / "debug-vector.dex")
with zipfile.ZipFile(fixture / "build/test-zips/framework-debug.zip") as archive:
if archive.read("framework/vector.dex") != (output / "debug-vector.dex").read_bytes():
raise SystemExit("Atomic-move fallback packaged incorrect bytes")

archive_path = fixture / "build/test-zips/framework-debug.zip"
previous_archive = archive_path.read_bytes()
run("stale-output-rejected", ["testFrameworkZipDebug", "-PmonoDex"], negative=True)
if archive_path.read_bytes() != previous_archive:
raise SystemExit("Failed DEX preparation unexpectedly replaced the module ZIP")
if list((fixture / "build/intermediates/framework_dex").rglob("*.pending")):
raise SystemExit("DEX preparation left an unfinished staged output")

for failure, diagnostic in (("duplicate", "defined multiple times"), ("corrupt", "Compilation failed")):
run(f"d8-{failure}-rejected", ["testFrameworkZipDebug", f"-PfailureApk={failure}"],
negative=True, expected=diagnostic)
if archive_path.read_bytes() != previous_archive:
raise SystemExit(f"D8 {failure} failure replaced the previous ZIP")

# Two individually valid classes whose combined method references exceed one DEX.
for index in range(2):
(probe.parent / f"Overflow{index}.java").write_text(
f"package org.matrix.vector.recordtest; public class Overflow{index} {{\n" +
"".join(f"public static int m{method}() {{ return {method}; }}\n" for method in range(33000)) + "}\n")
run("single-dex-overflow-rejected", ["testFrameworkZipDebug"], negative=True,
expected="single-DEX loader's capacity")
if archive_path.read_bytes() != previous_archive:
raise SystemExit("Single-DEX overflow replaced the previous ZIP")

print("PASS: class preservation, incremental builds, single-DEX ZIPs, missing support, corrupt DEX, duplicate classes and single-DEX overflow rejection")
print("Artifacts:", output)
97 changes: 97 additions & 0 deletions tests/recordtag/runtime/NativeHookProbe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;

/** Run only in a disposable app_process, never inside system_server. */
public final class NativeHookProbe {
private static native boolean init(ClassLoader loader);

private static ClassLoader loader;
private static Class<?> invokerClass;
private static Class<?> typeClass;
private static Method invoke;
private static Object full;
private static int hits;

public static int target(int value) {
return value * 3;
}

private static Object invoker(Method target, Object type) throws Exception {
Constructor<?> constructor = invokerClass.getDeclaredConstructor(Method.class);
constructor.setAccessible(true);
Object instance = constructor.newInstance(target);
loader.loadClass("io.github.libxposed.api.XposedInterface$Invoker")
.getMethod("setType", typeClass).invoke(instance, type);
return instance;
}

/** LSPlant's trampoline delegates to the production invoker and interceptor chain. */
public static final class Hooker {
public Hooker(Executable target) {}

public Object callback(Object[] args) throws Exception {
return invoke.invoke(full, null, args);
}
}

private static void check(Object actual, int expected) {
if (!Integer.valueOf(expected).equals(actual)) {
throw new AssertionError(actual + " != " + expected);
}
}

public static void main(String[] args) throws Exception {
if (args.length != 4) {
throw new IllegalArgumentException("Expected DEX, JNI library, invoker and hook-record class names");
}
loader = (ClassLoader) Class.forName("dalvik.system.InMemoryDexClassLoader")
.getConstructor(ByteBuffer.class, ClassLoader.class)
.newInstance(ByteBuffer.wrap(Files.readAllBytes(Paths.get(args[0]))),
NativeHookProbe.class.getClassLoader());
System.load(args[1]);
if (!init(loader)) throw new AssertionError("Native initialization failed");
Class<?> bridge = loader.loadClass("org.matrix.vector.nativebridge.HookBridge");
invokerClass = loader.loadClass(args[2]);
typeClass = loader.loadClass("io.github.libxposed.api.XposedInterface$Invoker$Type");
Class<?> chain = loader.loadClass(typeClass.getName() + "$Chain");
Object all = chain.getField("FULL").get(null);
Object origin = loader.loadClass(typeClass.getName() + "$Origin").getConstructor().newInstance();
Method target = NativeHookProbe.class.getMethod("target", int.class);
invoke = loader.loadClass("io.github.libxposed.api.XposedInterface$Invoker")
.getMethod("invoke", Object.class, Object[].class);
full = invoker(target, all);
Object original = invoker(target, origin);
Class<?> hookInterface = loader.loadClass("io.github.libxposed.api.XposedInterface$Hooker");
Class<?> chainInterface = loader.loadClass("io.github.libxposed.api.XposedInterface$Chain");
Object callback = Proxy.newProxyInstance(loader, new Class<?>[]{hookInterface}, (proxy, method, arguments) -> {
if (!method.getName().equals("intercept")) throw new AssertionError(method);
hits++;
return ((Integer) chainInterface.getMethod("proceed").invoke(arguments[0])) + 100;
});
Class<?> mode = loader.loadClass("io.github.libxposed.api.XposedInterface$ExceptionMode");
Object record = loader.loadClass(args[3]).getConstructors()[0]
.newInstance(callback, 50, mode.getEnumConstants()[0], "recordtag-probe");
Method hook = bridge.getMethod("hookMethod", boolean.class, Executable.class, Class.class, int.class, Object.class);
check(target.invoke(null, 7), 21);
if (!Boolean.TRUE.equals(hook.invoke(null, true, target, Hooker.class, 50, record))) {
throw new AssertionError("Hook failed");
}
check(target.invoke(null, 7), 121);
check(invoke.invoke(original, null, new Object[]{7}), 21);
Object filtered = invoker(target, chain.getConstructor(int.class).newInstance(49));
check(invoke.invoke(filtered, null, new Object[]{7}), 21);
check(invoke.invoke(full, null, new Object[]{7}), 121);
if (hits != 2) throw new AssertionError("Hook hits=" + hits);
if (!Boolean.TRUE.equals(bridge.getMethod("unhookMethod", boolean.class, Executable.class, Object.class)
.invoke(null, true, target, record))) {
throw new AssertionError("Unhook failed");
}
check(target.invoke(null, 7), 21);
System.out.println("PASS: real native hook dispatch, Chain.FULL, priority filtering, Origin bypass, original return values and unhook");
}
}
Loading