Verify allocated code once and cache Android apps' native images
On a phone the bundle compile is the largest startup cost: Jungle spends 8.7 s in HostedAppStart on the Moto. Two parts of it were waste. - Allocation verifies its program with MachineVerifyAllocated, and every encoder verified the same program again. The pipeline now encodes through MachineV7EncodeAllocated, MachineA64EncodeAllocated and MachineX64EncodeAllocated, which skip the repeat; the public encoders still verify what they are given. - The native disk cache never worked in an Android app. Its key needs the compiler's GNU build ID, which Shovey's reproducible libmain.so does not have (-Wl,--build-id=none), and its directory came from HOME, which an app process lacks. Without a build ID the code is now identified by a 64-bit FNV-1a hash of the object's readable executable segments, and on Android an app's cache directory is /data/data/PACKAGE/cache. So only the first launch after an install compiles a bundle. native_disk_cache_cases also runs a zi2zx stripped of its build ID: a cold run stores the image and a warm run maps it.
This commit is contained in:
1 parent
5f95fae22f
commit
10f603625a
6 files changed
+119
-14
No files matched your search
+12
-3
@@ -2648,9 +2648,11 @@ Failure :: (problem: MachineProblem) -> MachineA64Result {
|
||||
// Verifies and measures PROGRAM; with an OUTPUT, writes and publishes the
|
||||
// image there. Every diagnostic from verification, form checks or measuring
|
||||
// returns before OUTPUT is touched.
|
||||
Process :: (program: MachineProgram, limits: MachineLimits, output: *MachineA64Owner) -> MachineA64Result {
|
||||
verified := MachineVerifyAllocated(program, limits)
|
||||
if verified.problem != .None { return .{diagnostic = verified} }
|
||||
Process :: (program: MachineProgram, limits: MachineLimits, output: *MachineA64Owner, verify: bool = true) -> MachineA64Result {
|
||||
if verify {
|
||||
verified := MachineVerifyAllocated(program, limits)
|
||||
if verified.problem != .None { return .{diagnostic = verified} }
|
||||
}
|
||||
if (program.target.arch != .AArch64 || program.target.abi != .Aapcs64) ||
|
||||
(program.target.os != .Linux && program.target.os != .Android) {
|
||||
return Failure(.UnsupportedTarget)
|
||||
@@ -2772,3 +2774,10 @@ MachineA64Encode :: (program: MachineProgram, limits: MachineLimits, output: *Ma
|
||||
if output == null { return Failure(.Owner).diagnostic }
|
||||
return Process(program, limits, output).diagnostic
|
||||
}
|
||||
|
||||
// MachineA64Encode for the program RegAllocAllocate just published, which it
|
||||
// verified, so it is not verified again.
|
||||
MachineA64EncodeAllocated :: (program: MachineProgram, limits: MachineLimits, output: *MachineA64Owner) -> MachineDiagnostic {
|
||||
if output == null { return Failure(.Owner).diagnostic }
|
||||
return Process(program, limits, output, false).diagnostic
|
||||
}
|
||||
+12
-3
@@ -2565,9 +2565,11 @@ Failure :: (problem: MachineProblem) -> MachineV7Result {
|
||||
// Verifies and measures PROGRAM; with an OUTPUT, writes and publishes the
|
||||
// image there. Every diagnostic from verification, form checks or measuring
|
||||
// returns before OUTPUT is touched.
|
||||
Process :: (program: MachineProgram, limits: MachineLimits, output: *MachineV7Owner) -> MachineV7Result {
|
||||
verified := MachineVerifyAllocated(program, limits)
|
||||
if verified.problem != .None { return .{diagnostic = verified} }
|
||||
Process :: (program: MachineProgram, limits: MachineLimits, output: *MachineV7Owner, verify: bool = true) -> MachineV7Result {
|
||||
if verify {
|
||||
verified := MachineVerifyAllocated(program, limits)
|
||||
if verified.problem != .None { return .{diagnostic = verified} }
|
||||
}
|
||||
if (program.target.arch != .ArmV7 || program.target.abi != .Aapcs32) ||
|
||||
(program.target.os != .Linux && program.target.os != .Android) {
|
||||
return Failure(.UnsupportedTarget)
|
||||
@@ -2690,6 +2692,13 @@ MachineV7Encode :: (program: MachineProgram, limits: MachineLimits, output: *Mac
|
||||
return Process(program, limits, output).diagnostic
|
||||
}
|
||||
|
||||
// MachineV7Encode for the program RegAllocAllocate just published, which it
|
||||
// verified, so it is not verified again.
|
||||
MachineV7EncodeAllocated :: (program: MachineProgram, limits: MachineLimits, output: *MachineV7Owner) -> MachineDiagnostic {
|
||||
if output == null { return Failure(.Owner).diagnostic }
|
||||
return Process(program, limits, output, false).diagnostic
|
||||
}
|
||||
|
||||
// Exchanges two complete owners; their borrowed views move with the vectors
|
||||
// they describe (compiler_machine_pipeline.zi NativeExchange).
|
||||
MachineV7Exchange :: (owner: *MachineV7Owner, built: *MachineV7Owner) {
|
||||
|
||||
@@ -271,15 +271,16 @@ Build :: (lir: LirProgram, target: TargetDescription, limits: MachineLimits,
|
||||
MachineNativeFree(*built)
|
||||
return Problem(.UnsupportedTarget)
|
||||
}
|
||||
if target.arch == .ArmV7 { diagnostic = MachineV7Encode(program, limits, *built.encoded_v7) }
|
||||
else { diagnostic = MachineA64Encode(program, limits, *built.encoded_a64) }
|
||||
if target.arch == .ArmV7 { diagnostic = MachineV7EncodeAllocated(program, limits, *built.encoded_v7) }
|
||||
else { diagnostic = MachineA64EncodeAllocated(program, limits, *built.encoded_a64) }
|
||||
if diagnostic.problem != .None { MachineNativeFree(*built); return diagnostic }
|
||||
built.valid = true
|
||||
NativeExchange(output, *built)
|
||||
MachineNativeFree(*built)
|
||||
return NoDiagnostic()
|
||||
}
|
||||
diagnostic = MachineX64Encode(program, limits, *built.encoded)
|
||||
// Allocation verified PROGRAM; the encoders do not verify it again.
|
||||
diagnostic = MachineX64EncodeAllocated(program, limits, *built.encoded)
|
||||
if diagnostic.problem != .None { MachineNativeFree(*built); return diagnostic }
|
||||
if emit_executable {
|
||||
image := MachineX64View(*built.encoded)
|
||||
|
||||
+12
-3
@@ -3155,9 +3155,11 @@ BuildMachineImage :: (program: MachineProgram, output: *MachineX64Owner,
|
||||
// Verifies and measures PROGRAM; with an OUTPUT, writes and publishes the
|
||||
// image there. Every diagnostic from verification, form checks or measuring
|
||||
// returns before OUTPUT is touched.
|
||||
Process :: (program: MachineProgram, limits: MachineLimits, output: *MachineX64Owner) -> MachineX64Result {
|
||||
verified := MachineVerifyAllocated(program, limits)
|
||||
if verified.problem != .None { return .{diagnostic = verified} }
|
||||
Process :: (program: MachineProgram, limits: MachineLimits, output: *MachineX64Owner, verify: bool = true) -> MachineX64Result {
|
||||
if verify {
|
||||
verified := MachineVerifyAllocated(program, limits)
|
||||
if verified.problem != .None { return .{diagnostic = verified} }
|
||||
}
|
||||
if program.target.arch != .X86_64 || !TargetMachineLoweringSupported(program.target) {
|
||||
return Failure(.UnsupportedTarget)
|
||||
}
|
||||
@@ -3324,3 +3326,10 @@ MachineX64Encode :: (program: MachineProgram, limits: MachineLimits, output: *Ma
|
||||
if output == null { return Failure(.Owner).diagnostic }
|
||||
return Process(program, limits, output).diagnostic
|
||||
}
|
||||
|
||||
// MachineX64Encode for the program RegAllocAllocate just published, which it
|
||||
// verified, so it is not verified again.
|
||||
MachineX64EncodeAllocated :: (program: MachineProgram, limits: MachineLimits, output: *MachineX64Owner) -> MachineDiagnostic {
|
||||
if output == null { return Failure(.Owner).diagnostic }
|
||||
return Process(program, limits, output, false).diagnostic
|
||||
}
|
||||
@@ -470,6 +470,38 @@ typedef struct NativeBuildSearch {
|
||||
size_t length;
|
||||
} NativeBuildSearch;
|
||||
|
||||
/* An object linked without a build ID (a reproducible APK's library) is
|
||||
* identified by its loaded code: a 64-bit FNV-1a hash of every executable
|
||||
* segment's bytes, a few milliseconds even for a 12 MB library. */
|
||||
static void
|
||||
native_code_identity(struct dl_phdr_info *info, NativeBuildSearch *search)
|
||||
{
|
||||
uint64_t hash = 14695981039346656037ull;
|
||||
uint64_t bytes = 0;
|
||||
for(int at = 0; at < info->dlpi_phnum; at++) {
|
||||
const ElfW(Phdr) *header = &info->dlpi_phdr[at];
|
||||
// Execute-only memory cannot be read, so only readable code is hashed.
|
||||
if(header->p_type != PT_LOAD || (header->p_flags & PF_X) == 0 || (header->p_flags & PF_R) == 0 ||
|
||||
header->p_filesz == 0) continue;
|
||||
const unsigned char *code = (const unsigned char *)(info->dlpi_addr + header->p_vaddr);
|
||||
size_t length = (size_t)header->p_filesz;
|
||||
size_t words = length / 4;
|
||||
for(size_t word = 0; word < words; word++) {
|
||||
uint32_t value;
|
||||
memcpy(&value, code + word * 4, 4);
|
||||
hash = (hash ^ value) * 1099511628211ull;
|
||||
}
|
||||
for(size_t tail = words * 4; tail < length; tail++) hash = (hash ^ code[tail]) * 1099511628211ull;
|
||||
hash = (hash ^ (uint64_t)length) * 1099511628211ull;
|
||||
bytes += length;
|
||||
}
|
||||
if(bytes == 0) return;
|
||||
static const char tag[] = "code";
|
||||
memcpy(search->id, tag, 4);
|
||||
for(int at = 0; at < 8; at++) search->id[4 + at] = (unsigned char)(hash >> (8 * at));
|
||||
search->length = 12;
|
||||
}
|
||||
|
||||
static int
|
||||
native_build_note(struct dl_phdr_info *info, size_t size, void *data)
|
||||
{
|
||||
@@ -504,11 +536,41 @@ native_build_note(struct dl_phdr_info *info, size_t size, void *data)
|
||||
left -= sizeof(*entry) + name + description;
|
||||
}
|
||||
}
|
||||
native_code_identity(info, search);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
/* An Android app process has no HOME; its own cache directory is
|
||||
* /data/data/PACKAGE/cache, and its command line is PACKAGE, or
|
||||
* PACKAGE:PROCESS for a secondary process. A command such as zi2zx run from
|
||||
* a shell is no package and gets 0. */
|
||||
static int
|
||||
native_android_cache(char *path, size_t capacity)
|
||||
{
|
||||
char name[256];
|
||||
int file = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC);
|
||||
if(file < 0) return 0;
|
||||
ssize_t got = read(file, name, sizeof(name) - 1);
|
||||
close(file);
|
||||
if(got <= 0) return 0;
|
||||
name[got] = 0;
|
||||
char *process = strchr(name, ':');
|
||||
if(process != NULL) *process = 0;
|
||||
if(name[0] == 0 || strchr(name, '.') == NULL || strchr(name, '/') != NULL) return 0;
|
||||
char cache[320];
|
||||
struct stat info;
|
||||
int written = snprintf(cache, sizeof(cache), "/data/data/%s/cache", name);
|
||||
if(written <= 0 || (size_t)written >= sizeof(cache) || stat(cache, &info) != 0 || !S_ISDIR(info.st_mode))
|
||||
return 0;
|
||||
written = snprintf(path, capacity, "%s/ziran/native", cache);
|
||||
return written > 0 && (size_t)written < capacity;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* The cache directory: ZIRAN_NATIVE_CACHE names it, or "off" disables the
|
||||
* cache; otherwise $XDG_CACHE_HOME/ziran/native or ~/.cache/ziran/native. */
|
||||
* cache; otherwise $XDG_CACHE_HOME/ziran/native, an Android app's own cache
|
||||
* directory, or ~/.cache/ziran/native. */
|
||||
static int
|
||||
native_disk_directory(char *path, size_t capacity)
|
||||
{
|
||||
@@ -520,6 +582,9 @@ native_disk_directory(char *path, size_t capacity)
|
||||
const char *cache = getenv("XDG_CACHE_HOME");
|
||||
const char *home = getenv("HOME");
|
||||
if(cache != NULL && cache[0] == '/') written = snprintf(path, capacity, "%s/ziran/native", cache);
|
||||
#if defined(__ANDROID__)
|
||||
else if(native_android_cache(path, capacity)) written = (int)strlen(path);
|
||||
#endif
|
||||
else if(home != NULL && home[0] == '/') written = snprintf(path, capacity, "%s/.cache/ziran/native", home);
|
||||
else return 0;
|
||||
}
|
||||
|
||||
@@ -145,6 +145,18 @@ main :: (argc: s32, argv: **u8) -> s32 {
|
||||
if !NativeAssert(*test, off.output == engine.output && !Counted(off, "native_cache_disk_store") &&
|
||||
!Counted(off, "native_cache_disk_hit"), "ZIRAN_NATIVE_CACHE=off still used the disk") { return 1 }
|
||||
|
||||
// A tool linked without a GNU build ID, as reproducible Android
|
||||
// libraries are, is keyed by its code and caches too.
|
||||
plain := CheckPath(test.work, "zi2zx-plain")
|
||||
strip := string.["objcopy", "--remove-section", ".note.gnu.build-id", tool, plain]
|
||||
unused NativeRun(*test, strip[:])
|
||||
plain_cache := CheckPath(test.work, "plain-cache")
|
||||
plain_cold := DiskRunWith(*test, plain, bundle, plain_cache, "plain-cold")
|
||||
plain_warm := DiskRunWith(*test, plain, bundle, plain_cache, "plain-warm")
|
||||
if test.failed || !NativeAssert(*test, plain_cold.output == engine.output && Counted(plain_cold, "native_cache_disk_store") &&
|
||||
plain_warm.output == engine.output && Counted(plain_warm, "native_cache_disk_hit") && OnlyEntry(plain_cache) != "",
|
||||
"a tool without a build ID did not store and map its entry") { return 1 }
|
||||
|
||||
// A trap in code read from disk still names its function.
|
||||
trap := CheckPath(test.work, "trap.zx")
|
||||
trap_build := string.[tool, "--checks", "--root", spec, "--module-path", std, "--entry", "std_string_bad:main",
|
||||
@@ -176,6 +188,6 @@ main :: (argc: s32, argv: **u8) -> s32 {
|
||||
}
|
||||
NativeUnset(*test, "ZIRAN_NATIVE_CACHE")
|
||||
if !NativeAssert(*test, OnlyEntry(shared) != "", "racing runs left other than one entry") { return 1 }
|
||||
print("native disk cache: cold store, warm hit, abandoned writer, edited, truncated, oversized, wrong-format, wrong-key, wrong-length, wrong-digest and wrong-structure entries named, refused and replaced; off switch, trap sites and racing writers match the engine\n")
|
||||
print("native disk cache: cold store, warm hit, a tool without a build ID, abandoned writer, edited, truncated, oversized, wrong-format, wrong-key, wrong-length, wrong-digest and wrong-structure entries named, refused and replaced; off switch, trap sites and racing writers match the engine\n")
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user