Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

fix: recover cold accessibility hierarchy reads

Yanni Pang4fdf452689f3
diff --git a/Research/0_binary_patch_comparison.md b/Research/0_binary_patch_comparison.md
index 2c12cffc..7fee0469 100644
--- a/Research/0_binary_patch_comparison.md
+++ b/Research/0_binary_patch_comparison.md
@@ -2144,3 +2144,23 @@ Foreground PID stayed unchanged and accessibility switches were restored for
each helper run. Temporary services were unloaded and their directories removed;
pcc-research-02 was untouched. Helper evidence is not an integrated daemon
release or cold-start readiness claim.
+
+Installed-daemon cold-read testing subsequently exposed an initial Settings
+snapshot containing only its unavailable root. The native walker now retries
+only the first root label copy with error -25215 when this invocation actually
+enabled accessibility, after one 400 ms readiness wait. It shares the original
+deadline and query budget, preserves the initial error in readiness metadata,
+and retains unresolved errors. No child error or action is retried.
+
+After a supported bundle installation and phone01 restart, the first Settings
+CLI read recovered and returned 220 nodes with 217 matching parent checks and
+3 disagreements; subsequent reads returned 176. Fresh Calculator returned
+44 partial nodes and Clock 66 complete nodes. Flat queries and the nested
+alias also worked. These are diagnostic inspection results on that VM,
+not full action-cycle performance acceptance. Phone02 remained stopped.
+
+The Release build also reproduced SIGPIPE failures in bundle validation:
+early-exiting grep and awk closed their file/vtool producers under pipefail.
+Those probes now drain producer output while keeping matching, first-platform
+selection, failure propagation, and all signature checks. The full workspace
+build and strict signatures then passed.
diff --git a/VPhoneDaemon/Native/vphoned_ax_hierarchy.h b/VPhoneDaemon/Native/vphoned_ax_hierarchy.h
index 3927b2f3..fa88c43f 100644
--- a/VPhoneDaemon/Native/vphoned_ax_hierarchy.h
+++ b/VPhoneDaemon/Native/vphoned_ax_hierarchy.h
@@ -9,4 +9,6 @@ typedef struct {
Boolean (*getValue)(CFTypeRef,int,void *);
} VPAxFunctions;
NSDictionary *vp_ax_walk(VPAxFunctions functions, CFTypeRef root, pid_t pid, int maxElements, int maxDepth, int timeoutMS);
+// switchesEnabled is true only when this invocation enabled a previously disabled AX switch.
+NSDictionary *vp_ax_walk_with_readiness(VPAxFunctions functions, CFTypeRef root, pid_t pid, int maxElements, int maxDepth, int timeoutMS, BOOL switchesEnabled);
NSDictionary *vp_ax_hierarchy(int pid, int maxElements, int maxDepth, int timeoutMS);
diff --git a/VPhoneDaemon/Native/vphoned_ax_hierarchy.m b/VPhoneDaemon/Native/vphoned_ax_hierarchy.m
index cee479a9..ab035982 100644
--- a/VPhoneDaemon/Native/vphoned_ax_hierarchy.m
+++ b/VPhoneDaemon/Native/vphoned_ax_hierarchy.m
@@ -18,7 +18,9 @@ static double milliseconds(void) {
double start;
NSMutableArray *seen;
NSMutableSet *active,*reasons;
- BOOL incomplete;
+ BOOL incomplete,switchesEnabled,retryAttempted,retryResolved;
+ double readinessWait;
+ NSArray *initialReadinessErrors;
}
- (NSDictionary *)node:(CFTypeRef)element parent:(CFTypeRef)parent depth:(int)depth;
@end
@@ -45,6 +47,27 @@ static double milliseconds(void) {
}
return CFBridgingRelease(value);
}
+// Only a newly enabled AX invocation may wait for the first root query to become ready.
+// Historical failures remain separate after resolution; child-query failures never retry.
+- (id)rootLabel:(CFTypeRef)element errors:(NSMutableArray *)errors {
+ id value=[self attribute:2001 element:element errors:errors];
+ NSDictionary *initial=errors.lastObject;
+ if(value || !switchesEnabled || queries!=1 || [initial[@"attribute"] intValue]!=2001 ||
+ [initial[@"error_code"] intValue]!=-25215 || [initial[@"value_present"] boolValue] ||
+ [initial[@"timeout_configuration_failed"] boolValue]) return value;
+ initialReadinessErrors=@[initial];
+ // Reserve time for the retry. Waiting must not reset the walk's global deadline.
+ if(![self budget] || timeoutMS-(milliseconds()-start)<=400.0) return nil;
+ double waitStart=milliseconds();
+ struct timespec delay={.tv_sec=0,.tv_nsec=400000000};nanosleep(&delay,NULL);
+ readinessWait=milliseconds()-waitStart;
+ if(![self budget]) return nil;
+ int before=queries;
+ value=[self attribute:2001 element:element errors:errors];
+ retryAttempted=queries>before;retryResolved=value!=nil;
+ if(retryResolved) [errors removeObject:initial];
+ return value;
+}
- (NSString *)text:(id)value {
if(!value || CFGetTypeID((__bridge CFTypeRef)value)!=CFStringGetTypeID())return @"";
NSString *string=value;
@@ -81,7 +104,7 @@ static double milliseconds(void) {
[seen addObject:(__bridge id)element];[active addObject:identity];
NSMutableArray *errors=[NSMutableArray array];
id proof=[self parentProof:element parent:parent errors:errors];
- NSString *label=[self text:[self attribute:2001 element:element errors:errors]];
+ NSString *label=[self text:depth==0?[self rootLabel:element errors:errors]:[self attribute:2001 element:element errors:errors]];
NSString *identifier=[self text:[self attribute:5019 element:element errors:errors]];
id type=[self attribute:5003 element:element errors:errors];
NSString *role=[self text:type];
@@ -113,7 +136,10 @@ static double milliseconds(void) {
@end
NSDictionary *vp_ax_walk(VPAxFunctions functions, CFTypeRef root, pid_t pid, int elements, int depth, int duration) {
- VPAxWalker *walker=[VPAxWalker new];walker->f=functions;
+ return vp_ax_walk_with_readiness(functions,root,pid,elements,depth,duration,NO);
+}
+NSDictionary *vp_ax_walk_with_readiness(VPAxFunctions functions, CFTypeRef root, pid_t pid, int elements, int depth, int duration, BOOL switchesEnabled) {
+ VPAxWalker *walker=[VPAxWalker new];walker->f=functions;walker->switchesEnabled=switchesEnabled;
walker->maxElements=MAX(1,MIN(elements,2000));walker->maxDepth=MAX(0,MIN(depth,32));
walker->timeoutMS=MAX(100,MIN(duration,10000));walker->maxQueries=walker->maxElements*8;
walker->seen=[NSMutableArray array];walker->active=[NSMutableSet set];walker->reasons=[NSMutableSet set];walker->start=milliseconds();
@@ -125,6 +151,9 @@ NSDictionary *vp_ax_walk(VPAxFunctions functions, CFTypeRef root, pid_t pid, int
@"truncated":@(truncated),@"truncation_reasons":[walker->reasons.allObjects sortedArrayUsingSelector:@selector(compare:)],
@"limits":@{@"max_elements":@(walker->maxElements),@"max_depth":@(walker->maxDepth),@"max_queries":@(walker->maxQueries),@"timeout_ms":@(walker->timeoutMS)},
@"queries":@(walker->queries),@"elapsed_ms":@(milliseconds()-walker->start),
+ @"readiness":@{@"switches_enabled_for_invocation":@(switchesEnabled),@"retry_attempted":@(walker->retryAttempted),
+ @"retry_resolved":@(walker->retryResolved),@"wait_ms":@(walker->readinessWait),@"max_wait_ms":@400,
+ @"initial_query_errors":walker->initialReadinessErrors?:@[]},
@"parent_checks":@{@"verified":@(walker->verified),@"mismatch":@(walker->mismatch),@"unavailable":@(walker->unavailable)}};
}
NSDictionary *vp_ax_hierarchy(int pid,int maxElements,int maxDepth,int timeoutMS) {
@@ -149,7 +178,8 @@ NSDictionary *vp_ax_hierarchy(int pid,int maxElements,int maxDepth,int timeoutMS
NSDictionary *result=nil;
@try {
if(changedApp) setApp(true); if(changedAuto) setAuto(true);
- result=vp_ax_walk(f,root,pid,maxElements,maxDepth,timeoutMS);
+ BOOL enabledForInvocation=(changedApp && appEnabled()) || (changedAuto && autoEnabled());
+ result=vp_ax_walk_with_readiness(f,root,pid,maxElements,maxDepth,timeoutMS,enabledForInvocation);
} @catch(NSException *exception) {
NSMutableDictionary *failure=[vp_ax_walk(f,NULL,pid,maxElements,maxDepth,timeoutMS) mutableCopy];
failure[@"native_exception"]=exception.name; result=failure.copy;
diff --git a/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh b/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
index db94bc84..c8666f26 100755
--- a/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
+++ b/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
@@ -26,7 +26,9 @@ file_copy_spawns="$(/usr/bin/find "$root/VPhoneExecutable" "$root/VPhoneKit" \
require_signed_macho() {
local file="$1"
[[ -f "$file" ]] || { print -u2 "Missing binary: ${file#$bundle/}"; exit 1; }
- /usr/bin/file "$file" | /usr/bin/grep -q 'Mach-O' || {
+ # Drain every architecture line so pipefail does not mistake file's SIGPIPE
+ # after grep's early exit for a malformed universal binary.
+ /usr/bin/file "$file" | /usr/bin/grep 'Mach-O' >/dev/null || {
print -u2 "Not a Mach-O: ${file#$bundle/}"
exit 1
}
@@ -103,8 +105,8 @@ fi
# Host programs live in Contents/MacOS and guest payloads in guest-resources.
# The build platform keeps an iOS binary from landing among the host programs.
while IFS= read -r file; do
- /usr/bin/file "$file" | /usr/bin/grep -q 'Mach-O' || continue
- platform="$(/usr/bin/vtool -show-build "$file" 2>/dev/null | /usr/bin/awk '$1 == "platform" {print $2; exit}')"
+ /usr/bin/file "$file" | /usr/bin/grep 'Mach-O' >/dev/null || continue
+ platform="$(/usr/bin/vtool -show-build "$file" 2>/dev/null | /usr/bin/awk '$1 == "platform" && !found {print $2; found=1}')"
case "$file" in
"$macos/"*)
[[ "$platform" != IOS ]] || { print -u2 "iOS binary in Contents/MacOS: ${file#$bundle/}"; exit 1; }