Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
Inside appstored: the storefront was the gate, and the request still never becomes a transfer
vcvkk584be64c0910
diff --git a/README.md b/README.mdindex 48837c4..c7cd183 100644--- a/README.md+++ b/README.md@@ -1705,6 +1705,42 @@ store_test: after: ams_localiTunesAccount: STOREFRONT (null)`src/inject_sb.c` и соседние). Она и положила бы витрину на ту самую учётную запись, которойпользуется AMS, и заодно показала бы, строится ли запрос мешка вообще.+**Внутри `appstored`: витрина отвечена, запрос мешка строится, переносом не становится.** Раз снаружи+витрину не отдать, её отвечают изнутри: `userland/src/inject_store.c` — библиотека, которую сборка с+`STOREFRONT=1` подсаживает в `appstored` (тот же механизм `INJECT`, что и для SpringBoard). Она ничего не+записывает: она подменяет *геттеры*, через которые AMS спрашивает витрину (`-[ACAccount ams_storefront]`+и соседей), и заодно считает входы в шаги запроса. За 150 с после старта демона, уже после начального+потока на консоли:++```+entered 3 -[ACAccount ams_storefront] (было (nil), стало 143469-2,8)+entered 3 -[AMSBagActiveAccountProvider _bagAccountForAccountMediaType:mustContainStorefront:]+entered 1 -[AMSBagNetworkTask _performFetchWithAttemptedCount:account:storefront:]+entered 1 -[AMSBagNetworkTask _createRequestWithQueryItems:] -> <AMSMutablePromise: ...>+entered 0 -[AMSBagNetworkTask URLSessionPromise]+entered 0 -[AMSURLSession dataTaskPromiseWithRequest:]+entered 0 -[AMSURLSession dataTaskWithRequest:]+entered 0 -[NSURLSessionTask resume]+```++То есть **витрина и была воротами: с ней AMS запрос мешка строит** («BAG FETCH attempt 0, storefront+143469-2,8, account local (…)»). И переносом этот запрос не становится: он собирается в+`AMSMutablePromise`, который никто не разрешает, поэтому сессия не запрашивается, задача не создаётся и+`resume` не зовётся ни разу — ни для мешка, ни вообще, за весь шестиминутный сеанс. Отсюда и пустота в+журнале соединений ядра. Магазин по-прежнему показывает «LOADING»: мешок не загрузился.++Следующий шаг — на шаг глубже: чего ждёт этот промис запроса. Это кодировщик запроса и заголовки, которые+AMS добавляет перед отправкой (`ams_addCookiesForAccount:clientInfo:bag:`,+`ams_addStoreFrontHeaderForAccount:client:withBag:`, `ams_addAnisetteHeadersForAccount:type:bag:`,+`-[AMSURLSession _prepareRequest:properties:error:]`). Что-то из этого не возвращается.++Две вещи, которые стоили по прогону и которые стоит знать тому, кто будет писать следующую такую+библиотеку. `class_getInstanceMethod` идёт вверх по цепочке, и подмена метода, который класс только+**наследует**, подменяет его всем остальным наследникам: подмена категории `NSObject` перезапустила+`appstored` 31 раз за прогон. И исключение Objective-C кончается `abort()`, а не `SIGSEGV`, так что+сторож аварий, который слушает только сигналы памяти, о такой смерти не скажет ничего (теперь слушает+`SIGABRT`, `SIGILL` и `SIGTRAP` тоже).+**`mobileactivationd` — ещё одна служба из списка «никогда не пробовали».** Её спрашивал как раз`appstored` (четыре ненайденных обращения за сеанс и двадцать строк «[HRNSupport] Failed to copy activationrecord ... MobileActivation.ErrorDomain Code=-1 "IPC Error"» из каждого потока, который трогает мешок).diff --git a/userland/build.sh b/userland/build.shindex 7837f69..34644a8 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -17,6 +17,8 @@# (default: src/inject.c in backboardd, src/inject_sb.c in SpringBoard,# src/inject_media.c in mediaserverd);# init hands the list to the kernel (etc/inject.conf -> sysctl kern.exec_inject)+# STOREFRONT=1 ... and src/inject_store.c inside appstored: the storefront answered,+# and a bag request watched for (an experiment; see that file)# EXTRA="..." more programs for /redwood/bin (files on this machine): one-off# diagnostics, which a file given as RC_JOBS can then run# RC_JOBS=<file> what the job com.redwood.demo runs: rc.jobs (default), or@@ -100,6 +102,13 @@ INJECT=${INJECT-"/redwood/lib/libredwood.dylib backboardd containermanagerd cont/redwood/lib/libredwood-media.dylib mediaserverd/redwood/lib/libredwood-app.dylib Sileo/redwood/lib/libredwood-tweaks.dylib Preferences"}+# STOREFRONT=1: and a library of ours inside appstored as well (src/inject_store.c). It is not in the+# list above because it changes what the daemon believes: AMS's account is made to answer with the+# storefront Apple's bag server returned for this device, which is how the App Store is asked whether+# a storefront is the only thing it is missing. It also writes down whether a bag request is ever+# built at all. For an experiment, not for an image anybody else boots.+[ -n "$STOREFRONT" ] && INJECT="$INJECT+/redwood/lib/libredwood-store.dylib appstored"mkdir -p objif [ -z "$PREBUILT" ] && ! command -v ld64 >/dev/null; then PREBUILT=1; fi@@ -190,6 +199,10 @@ if [ -d $IOS/rootfs/usr/lib ]; then$DYN -O2 -dynamiclib -install_name /redwood/lib/libredwood-media.dylib -framework CoreFoundation -framework CoreMedia \-framework CoreVideo -framework IOSurface -lobjc \-o obj/libredwood-media.dylib src/inject_media.c src/camera_device.c src/camera_isp.c+ # ... and inside appstored (STOREFRONT=1), which answers with a storefront and watches for a bag+ # request being built (src/inject_store.c)+ $DYN -O2 -dynamiclib -install_name /redwood/lib/libredwood-store.dylib -framework CoreFoundation -lobjc \+ -o obj/libredwood-store.dylib src/inject_store.c# the output device of the audio server: a plug-in for CoreAudio's HAL$DYN -bundle -framework CoreFoundation -o obj/RedwoodAudio src/audio_plugin.cif [ -n "$GL" ]; then@@ -232,6 +245,7 @@ if [ -d $IOS/rootfs/usr/lib ]; thenmkdir -p root/libcp obj/libredwood.dylib obj/libredwood-sb.dylib root/lib/[ -f obj/libredwood-media.dylib ] && cp obj/libredwood-media.dylib root/lib/ # (as with cpu_test)+ [ -f obj/libredwood-store.dylib ] && cp obj/libredwood-store.dylib root/lib/[ -f obj/libredwood-app.dylib ] && cp obj/libredwood-app.dylib root/lib/[ -f obj/libredwood-tweaks.dylib ] && cp obj/libredwood-tweaks.dylib root/lib/if [ -f obj/RedwoodAudio ]; thendiff --git a/userland/rc.jobs.store b/userland/rc.jobs.storeindex 935813d..fb0fa78 100644--- a/userland/rc.jobs.store+++ b/userland/rc.jobs.store@@ -43,10 +43,11 @@/redwood/bin/rwtouch wait 55 swipe 0.5 0.985 0.5 0.3 250 wait 20/redwood/bin/net_test waitaddr en0 60/redwood/bin/net_test fetch https://bag.itunes.apple.com/bag.xml-# the account as it stands, then the number the bag server gave THIS device, by AMS's own setter.-# (Answered, and the answer is no: the value sets in memory and does not keep, because AMS's local-# account is made fresh every boot and accountsd has no iTunes account at all. See src/store_test.m.)-/redwood/bin/store_test user 501 set 143469-2,8+# the account as it stands (setting it from here was tried and cannot work: the value does not keep,+# AMS's local account being made fresh every boot and accountsd holding no iTunes account at all --+# see src/store_test.m). The storefront is answered inside appstored instead, by the library+# src/inject_store.c, which a build with STOREFRONT=1 puts there.+/redwood/bin/store_test user 501 show# and only then the store, open long enough to time out (30 s) and try again/redwood/bin/rwtouch tap 0.615 0.59 wait 95/bin/ps -Adiff --git a/userland/src/inject_store.c b/userland/src/inject_store.cnew file mode 100644index 0000000..241cb16--- /dev/null+++ b/userland/src/inject_store.c@@ -0,0 +1,593 @@+/*+ * libredwood-store.dylib: code of ours inside appstored (see inject.c for how it gets there).+ *+ * What it is for. The App Store opens and shows LOADING for ever. The front page is a "bag" --+ * AppleMediaServices' configuration -- and AMS will not load one until it knows the storefront, the+ * number that says which country's store this device belongs to. Everything outside the process has+ * been measured and is well:+ * - the bag itself is fetched from this phone by hand, 56 KB in half a second, and the server+ * hands over a storefront in the bargain: "x-set-apple-store-front: 143469-2,8" from both+ * bag.itunes.apple.com and init.itunes.apple.com (rc.jobs.store, src/net_test.m);+ * - appstored opens no socket at all while the store spins, so nothing is failing on the network;+ * - the storefront cannot be put on the device from another process: AMS's "local" account is not+ * a row in accountsd at all -- a different identifier every boot -- so there is nothing to write+ * to and nothing that would keep (src/store_test.m).+ *+ * So the storefront is not written anywhere here either. The account is made to ANSWER with one:+ * -[ACAccount ams_storefront] is the door every consumer in AMS goes through, and its neighbours are+ * taken in the same pass because different callers knock at different ones. No persistence, no+ * accountsd, nothing that outlives the process.+ *+ * And the question that is worth more than the storefront: is a bag request ever built? From outside+ * only the socket log could answer, and only negatively, over a console that drops lines. From in+ * here it can be answered positively. -[AMSBagNetworkTask _performFetchWithAttemptedCount:account:+ * storefront:] and its _queryItemsForClientInfo:... are where a request is assembled: they are not+ * replaced, only watched, and what they were called with is written down. If neither is entered,+ * nothing about the network matters and the gate is above them.+ *+ * The storefront is 143469-2,8 because that is what Apple's own bag server returned for THIS device+ * when asked with none -- a region, picked from the address the request came from. No Apple ID is+ * signed in, none can be, and the store must still refuse to install: there is nothing to buy with.+ *+ * AppleMediaServices and Accounts are not loaded yet when this library is: the methods are taken+ * when their images arrive (_dyld_register_func_for_add_image).+ *+ * WHAT IT ANSWERED. The storefront was the gate, and it is not the only one.+ * entered 3 -[ACAccount ams_storefront] (answered 143469-2,8; was (nil))+ * entered 3 -[AMSBagActiveAccountProvider _bagAccountForAccountMediaType:mustContainStorefront:]+ * mustContainStorefront:0 -> the local account, mustContainStorefront:1 -> (nil)+ * entered 1 -[AMSBagNetworkTask _performFetchWithAttemptedCount:account:storefront:]+ * "BAG FETCH attempt 0, storefront 143469-2,8, account local (...)" -> a promise+ * entered 1 -[AMSBagNetworkTask _createRequestWithQueryItems:] -> <AMSMutablePromise: ...>+ * entered 0 -[AMSBagNetworkTask URLSessionPromise]+ * entered 0 -[AMSURLSession dataTaskPromiseWithRequest:]+ * entered 0 -[AMSURLSession dataTaskWithRequest:]+ * entered 0 -[NSURLSessionTask resume]+ * So with a storefront to answer with, AMS DOES build a bag request -- that question is settled, and+ * positively, from inside. And the request never becomes a transfer: it is assembled into an+ * AMSMutablePromise that nothing ever resolves, so no session is asked for, no task is made and+ * nothing is ever resumed. Not one task is resumed in the whole daemon in a six-minute session,+ * which is why the socket log never had appstored in it. The store still shows LOADING, as it must:+ * the bag never loads.+ * Where to look next is one step further in: what that request promise waits on -- the request+ * encoder and the headers AMS adds before sending (ams_addCookiesForAccount:clientInfo:bag:,+ * ams_addStoreFrontHeaderForAccount:client:withBag:, ams_addAnisetteHeadersForAccount:type:bag:,+ * -[AMSURLSession _prepareRequest:properties:error:]). One of those never comes back.+ *+ * Two things this library learned about itself, for whoever writes the next one:+ * - class_getInstanceMethod walks up the chain, so replacing what a class only INHERITS replaces it+ * for everything else that inherits it too. Doing that to an NSObject category restarted+ * appstored 31 times in one run. image_added now refuses anything the superclass answers to.+ * - an ObjC exception ends in abort(), not in SIGSEGV, so a fault reporter that watches only the+ * memory signals says nothing at all about the usual way a replacement of ours kills a daemon.+ */+#include <CoreFoundation/CoreFoundation.h>+#include <mach-o/dyld.h>+#include <objc/message.h>+#include <objc/runtime.h>+#include <pthread.h>+#include <signal.h>+#include <stdarg.h>+#include <stdbool.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/ucontext.h>+#include <unistd.h>++extern const void *_dyld_get_shared_cache_range(size_t *length);++#define CACHE_BASE 0x180000000UL /* where the cache was built to be, before the slide */+#define STOREFRONT "143469-2,8" /* what bag.itunes.apple.com returned for this device */++static uintptr_t slide;+static CFStringRef storefront;++/* the table itself is at the end, where its functions are; the fetch hook reports what is missing */+struct replacement {+ const char *class_name;+ const char *selector;+ bool class_method;+ IMP replacement;+ IMP *original;+ bool done;+};+static struct replacement replacements[];+static size_t replacement_count(void);++/*+ * How many times each hook was entered: a count that survives a console that drops lines. The names+ * below are in the order the hooks count themselves in, which is NOT the order of the table at the+ * end -- the first version of this printed one against the other and every count came out against+ * the wrong name, which had to be undone by hand afterwards. Keep the two lists apart on purpose.+ */+enum {+ COUNT_ams_storefront, COUNT_cachedStorefront, COUNT_bagStorefront_fixed, COUNT_bagStorefront_active,+ COUNT_copyStorefront, COUNT_bagAccount, COUNT_performFetch, COUNT_queryItems, COUNT_performRequest,+ COUNT_performRequestBlock, COUNT_resume, COUNT_resumeLocal, COUNT_createRequest, COUNT_sessionPromise,+ COUNT_dataTaskPromise, COUNT_dataTask, COUNT_last+};+static const char *const counted[COUNT_last] = {+ "-[ACAccount ams_storefront]", "-[AMSBagNetworkDataSource cachedStorefront]",+ "-[AMSBagFixedAccountProvider bagStorefrontForAccountMediaType:]",+ "-[AMSBagActiveAccountProvider bagStorefrontForAccountMediaType:]",+ "-[ACAccount ams_copyStorefrontFromAccount:]",+ "-[AMSBagActiveAccountProvider _bagAccountForAccountMediaType:mustContainStorefront:]",+ "-[AMSBagNetworkTask _performFetchWithAttemptedCount:account:storefront:]",+ "-[AMSBagNetworkTask _queryItemsForClientInfo:...]", "-[AMSURLSession performRequest:completion:]",+ "-[AMSURLSession performRequest:completionBlock:]", "-[NSURLSessionTask resume]",+ "-[__NSCFLocalSessionTask resume]", "-[AMSBagNetworkTask _createRequestWithQueryItems:]",+ "-[AMSBagNetworkTask URLSessionPromise]", "-[AMSURLSession dataTaskPromiseWithRequest:]",+ "-[AMSURLSession dataTaskWithRequest:]",+};+static unsigned long entered[COUNT_last];++/* Each line of ours alone on the console: it drops them under the start's flood (see rc.jobs.store). */+static void+say(const char *format, ...)+{+ char line[512];+ va_list ap;+ int n;++ va_start(ap, format);+ n = vsnprintf(line, sizeof(line), format, ap);+ va_end(ap);+ if (n > (int)sizeof(line) - 1) {+ n = sizeof(line) - 1;+ }+ write(2, line, (size_t)n);+}++/* an object as a short string, for the log: a description, cut to fit */+static const char *+text_of(id object, char *buffer, size_t size)+{+ CFStringRef description;++ buffer[0] = 0;+ if (object == NULL) {+ return "(nil)";+ }+ description = (CFStringRef)((id (*)(id, SEL))objc_msgSend)(object, sel_registerName("description"));+ if (description == NULL || !CFStringGetCString(description, buffer, (CFIndex)size, kCFStringEncodingUTF8)) {+ return "(?)";+ }+ return buffer;+}++/*+ * The fault reporter, as in inject.c: iOS answers a crash in a stock daemon with+ * "Failed to send exception EXC_CORPSE_NOTIFY" and nothing else, so a fault in here would be a+ * silent exit. This turns one into an address, unslid, which "ipsw dyld a2s" turns into a name.+ */+static void+say_the_fault(int signal_number, siginfo_t *info, void *context_in)+{+ ucontext_t *context = context_in;+ uintptr_t pc = 0, lr = 0, fp = 0;+ char frames[8 * 20 + 1] = "";+ size_t used = 0;++ if (context != NULL && context->uc_mcontext != NULL) {+ pc = (uintptr_t)context->uc_mcontext->__ss.__pc;+ lr = (uintptr_t)context->uc_mcontext->__ss.__lr;+ fp = (uintptr_t)context->uc_mcontext->__ss.__fp;+ }+ for (unsigned i = 0; i < 8 && fp != 0 && (fp & 7) == 0; i++) {+ uintptr_t next = ((uintptr_t *)fp)[0], ret = ((uintptr_t *)fp)[1];++ if (ret == 0) {+ break;+ }+ used += (size_t)snprintf(frames + used, sizeof(frames) - used, " %#lx", ret - slide);+ fp = next > fp ? next : 0; /* the chain only ever goes up the stack */+ }+ say("redwood: appstored: signal %d at %p, pc %#lx, lr %#lx, called from%s (unslid)\n", signal_number,+ info != NULL ? info->si_addr : NULL, pc - slide, lr - slide, frames);+ signal(signal_number, SIG_DFL);+ raise(signal_number);+}++static void+watch_for_faults(void)+{+ struct sigaction action;++ memset(&action, 0, sizeof(action));+ action.sa_sigaction = say_the_fault;+ action.sa_flags = SA_SIGINFO | SA_RESETHAND;+ sigaction(SIGSEGV, &action, NULL);+ sigaction(SIGBUS, &action, NULL);+ /* an ObjC exception ends in abort(), and a bad replacement of ours is likelier to do that than+ to touch bad memory: without these a daemon that dies of us leaves nothing at all */+ sigaction(SIGABRT, &action, NULL);+ sigaction(SIGILL, &action, NULL);+ sigaction(SIGTRAP, &action, NULL);+}++/* ---- the storefront, answered ----------------------------------------------------------------- */++static id (*old_ams_storefront)(id, SEL);+static id (*old_cached_storefront)(id, SEL);+static id (*old_bag_storefront)(id, SEL, id);+static id (*old_bag_storefront_active)(id, SEL, id);+static id (*old_copy_storefront)(id, SEL, id);+static id (*old_bag_account)(id, SEL, id, bool);+static id (*old_perform_fetch)(id, SEL, long, id, id);+static id (*old_query_items)(id, SEL, id, id, id, id, id, id);+static id (*old_perform_request)(id, SEL, id, id);+static id (*old_perform_request_block)(id, SEL, id, id);+static void (*old_resume_base)(id, SEL);+static void (*old_resume_local)(id, SEL);+static id (*old_create_request)(id, SEL, id);+static id (*old_session_promise)(id, SEL);+static id (*old_data_task_promise)(id, SEL, id);+static id (*old_data_task)(id, SEL, id);++static id+new_ams_storefront(id self, SEL _cmd)+{+ entered[COUNT_ams_storefront]++;+ id was = old_ams_storefront != NULL ? old_ams_storefront(self, _cmd) : NULL;+ static bool said;++ if (!said) {+ char buffer[128];++ said = true;+ say("redwood: appstored: ams_storefront was %s, now " STOREFRONT "\n", text_of(was, buffer, sizeof(buffer)));+ }+ return was != NULL ? was : (id)storefront;+}++static id+new_cached_storefront(id self, SEL _cmd)+{+ entered[COUNT_cachedStorefront]++;+ id was = old_cached_storefront != NULL ? old_cached_storefront(self, _cmd) : NULL;++ say("redwood: appstored: cachedStorefront was %s\n", was != NULL ? "set" : "(nil), now " STOREFRONT);+ return was != NULL ? was : (id)storefront;+}++static id+new_bag_storefront(id self, SEL _cmd, id media_type)+{+ entered[COUNT_bagStorefront_fixed]++;+ id was = old_bag_storefront != NULL ? old_bag_storefront(self, _cmd, media_type) : NULL;+ char buffer[128];++ say("redwood: appstored: bagStorefrontForAccountMediaType:%s was %s\n", text_of(media_type, buffer, sizeof(buffer)),+ was != NULL ? "set" : "(nil), now " STOREFRONT);+ return was != NULL ? was : (id)storefront;+}++static id+new_bag_storefront_active(id self, SEL _cmd, id media_type)+{+ entered[COUNT_bagStorefront_active]++;+ id was = old_bag_storefront_active != NULL ? old_bag_storefront_active(self, _cmd, media_type) : NULL;+ char buffer[128];++ say("redwood: appstored: active bagStorefrontForAccountMediaType:%s was %s\n",+ text_of(media_type, buffer, sizeof(buffer)), was != NULL ? "set" : "(nil), now " STOREFRONT);+ return was != NULL ? was : (id)storefront;+}++static id+new_copy_storefront(id self, SEL _cmd, id account)+{+ entered[COUNT_copyStorefront]++;+ id was = old_copy_storefront != NULL ? old_copy_storefront(self, _cmd, account) : NULL;++ say("redwood: appstored: ams_copyStorefrontFromAccount: was %s\n",+ was != NULL ? "set" : "(nil), now " STOREFRONT);+ return was != NULL ? was : (id)storefront;+}++/* not replaced, watched: this is the fallback line, and which way it goes is the question */+static id+new_bag_account(id self, SEL _cmd, id media_type, bool must_contain_storefront)+{+ entered[COUNT_bagAccount]++;+ id account = old_bag_account(self, _cmd, media_type, must_contain_storefront);+ char one[128], two[192];++ say("redwood: appstored: _bagAccountForAccountMediaType:%s mustContainStorefront:%d -> %s\n",+ text_of(media_type, one, sizeof(one)), must_contain_storefront ? 1 : 0, text_of(account, two, sizeof(two)));+ return account;+}++/* ---- is a request ever built? ------------------------------------------------------------------ */++static id+new_perform_fetch(id self, SEL _cmd, long attempt, id account, id front)+{+ entered[COUNT_performFetch]++;+ char one[160], two[64];++ id promise;++ say("redwood: appstored: BAG FETCH attempt %ld, storefront %s, account %s\n", attempt,+ text_of(front, two, sizeof(two)), text_of(account, one, sizeof(one)));+ for (size_t i = 0; i < replacement_count(); i++) {+ if (!replacements[i].done) {+ say("redwood: appstored: NOT TAKEN %c[%s %s]\n", replacements[i].class_method ? '+' : '-',+ replacements[i].class_name, replacements[i].selector);+ }+ }+ promise = old_perform_fetch(self, _cmd, attempt, account, front);+ say("redwood: appstored: BAG FETCH returned %s\n", promise != NULL ? "a promise" : "(nil)");+ return promise;+}++static id+new_query_items(id self, SEL _cmd, id client, id profile, id version, id cookies, id account, id front)+{+ entered[COUNT_queryItems]++;+ id items = old_query_items(self, _cmd, client, profile, version, cookies, account, front);+ char one[64], two[160];++ say("redwood: appstored: BAG QUERY profile %s, storefront %s -> %s\n", text_of(profile, one, sizeof(one)),+ text_of(front, two, sizeof(two)), items != NULL ? "items" : "(nil)");+ return items;+}++/* the session layer: a request that is built but never sent stops between these and the wire */+static id+new_perform_request(id self, SEL _cmd, id request, id completion)+{+ entered[COUNT_performRequest]++;+ char buffer[320];++ say("redwood: appstored: AMSURLSession performRequest:completion: %s\n", text_of(request, buffer, sizeof(buffer)));+ return old_perform_request(self, _cmd, request, completion);+}++static id+new_perform_request_block(id self, SEL _cmd, id request, id completion)+{+ entered[COUNT_performRequestBlock]++;+ char buffer[320];++ say("redwood: appstored: AMSURLSession performRequest:completionBlock: %s\n", text_of(request, buffer, sizeof(buffer)));+ return old_perform_request_block(self, _cmd, request, completion);+}++/*+ * And the narrowest gate of all: every transfer, foreground or background, in process or handed to+ * nsurlsessiond, becomes a task that somebody resumes. If the bag request is built and no task is+ * ever resumed, it never became a transfer at all; if one is, the loss is below CFNetwork.+ */+static void+say_resume(id task, const char *where)+{+ char buffer[320];+ id request = ((id (*)(id, SEL))objc_msgSend)(task, sel_registerName("originalRequest"));++ say("redwood: appstored: TASK RESUME (%s) %s\n", where, text_of(request, buffer, sizeof(buffer)));+}++static void+new_resume_base(id self, SEL _cmd)+{+ entered[COUNT_resume]++;+ say_resume(self, "NSURLSessionTask");+ old_resume_base(self, _cmd);+}++static void+new_resume_local(id self, SEL _cmd)+{+ entered[COUNT_resumeLocal]++;+ say_resume(self, "__NSCFLocalSessionTask");+ old_resume_local(self, _cmd);+}++/*+ * What a class really answers to. The bag request is built and no task is ever resumed, so the stop+ * is between the two, in AMS's own session layer -- whose selectors are not the ones guessed from+ * the cache's strings (AMSURLSession has no performRequest:completion:). Rather than guess again,+ * ask the runtime, late enough that the console is no longer dropping lines.+ */+static void+dump_selectors(const char *class_name, const char *needle)+{+ Class cls = objc_getClass(class_name);+ unsigned count = 0;+ Method *methods = cls != NULL ? class_copyMethodList(cls, &count) : NULL;++ if (methods == NULL) {+ say("redwood: appstored: %s: no such class\n", class_name);+ return;+ }+ for (unsigned i = 0; i < count; i++) {+ const char *name = sel_getName(method_getName(methods[i]));++ if (needle == NULL || strstr(name, needle) != NULL) {+ say("redwood: appstored: %s has -%s\n", class_name, name);+ }+ }+ free(methods);+}++static void *+late_report(__unused void *ignored)+{+ sleep(150); /* past the start's flood, where the console keeps what it is given */+ for (size_t i = 0; i < replacement_count(); i++) {+ if (!replacements[i].done) {+ say("redwood: appstored: NOT TAKEN %c[%s %s]\n", replacements[i].class_method ? '+' : '-',+ replacements[i].class_name, replacements[i].selector);+ }+ }+ for (int i = 0; i < COUNT_last; i++) {+ say("redwood: appstored: entered %4lu %s\n", entered[i], counted[i]);+ }+ dump_selectors("AMSURLSession", "equest");+ dump_selectors("AMSBagNetworkTask", NULL);+ return NULL;+}++/*+ * The four steps between a bag request and a transfer, as the runtime says AMS really spells them:+ * AMSBagNetworkTask builds the request (_createRequestWithQueryItems:), asks for its session+ * (URLSessionPromise -- made lazily, and AMS has a line about being deallocated "while lazily+ * instantiating its AMSURLSession"), and the session makes a task (dataTaskPromiseWithRequest: or+ * dataTaskWithRequest:) which somebody resumes. resume is never reached, so the stop is one of+ * these; each says what it was given and what it gave back.+ */+static id+new_create_request(id self, SEL _cmd, id items)+{+ id request;+ char buffer[320];++ entered[COUNT_createRequest]++;+ request = old_create_request(self, _cmd, items);+ say("redwood: appstored: STEP _createRequestWithQueryItems: -> %s\n", text_of(request, buffer, sizeof(buffer)));+ return request;+}++static id+new_session_promise(id self, SEL _cmd)+{+ id promise;+ char buffer[200];++ entered[COUNT_sessionPromise]++;+ promise = old_session_promise(self, _cmd);+ say("redwood: appstored: STEP URLSessionPromise -> %s\n", text_of(promise, buffer, sizeof(buffer)));+ return promise;+}++static id+new_data_task_promise(id self, SEL _cmd, id request)+{+ id promise;+ char buffer[200];++ entered[COUNT_dataTaskPromise]++;+ promise = old_data_task_promise(self, _cmd, request);+ say("redwood: appstored: STEP dataTaskPromiseWithRequest: -> %s\n", text_of(promise, buffer, sizeof(buffer)));+ return promise;+}++static id+new_data_task(id self, SEL _cmd, id request)+{+ id task;+ char buffer[200];++ entered[COUNT_dataTask]++;+ task = old_data_task(self, _cmd, request);+ say("redwood: appstored: STEP dataTaskWithRequest: -> %s\n", text_of(task, buffer, sizeof(buffer)));+ return task;+}++/* ---- installing them --------------------------------------------------------------------------- */++static struct replacement replacements[] = {+ /* the door every consumer of a storefront goes through */+ { "ACAccount", "ams_storefront", false, (IMP)new_ams_storefront, (IMP *)&old_ams_storefront, false },+ { "ACAccount", "ams_copyStorefrontFromAccount:", false, (IMP)new_copy_storefront, (IMP *)&old_copy_storefront, false },+ /* and the ones particular callers knock at instead */+ { "AMSBagNetworkDataSource", "cachedStorefront", false, (IMP)new_cached_storefront, (IMP *)&old_cached_storefront, false },+ { "AMSBagFixedAccountProvider", "bagStorefrontForAccountMediaType:", false, (IMP)new_bag_storefront,+ (IMP *)&old_bag_storefront, false },+ { "AMSBagActiveAccountProvider", "bagStorefrontForAccountMediaType:", false, (IMP)new_bag_storefront_active,+ (IMP *)&old_bag_storefront_active, false },+ /* watched, not replaced */+ { "AMSBagActiveAccountProvider", "_bagAccountForAccountMediaType:mustContainStorefront:", false,+ (IMP)new_bag_account, (IMP *)&old_bag_account, false },+ { "AMSBagNetworkTask", "_performFetchWithAttemptedCount:account:storefront:", false, (IMP)new_perform_fetch,+ (IMP *)&old_perform_fetch, false },+ { "AMSBagNetworkTask", "_queryItemsForClientInfo:profile:profileVersion:cookieNames:account:storefront:", false,+ (IMP)new_query_items, (IMP *)&old_query_items, false },+ { "AMSURLSession", "performRequest:completion:", false, (IMP)new_perform_request, (IMP *)&old_perform_request, false },+ { "AMSURLSession", "performRequest:completionBlock:", false, (IMP)new_perform_request_block,+ (IMP *)&old_perform_request_block, false },+ { "AMSBagNetworkTask", "_createRequestWithQueryItems:", false, (IMP)new_create_request, (IMP *)&old_create_request,+ false },+ { "AMSBagNetworkTask", "URLSessionPromise", false, (IMP)new_session_promise, (IMP *)&old_session_promise, false },+ { "AMSURLSession", "dataTaskPromiseWithRequest:", false, (IMP)new_data_task_promise, (IMP *)&old_data_task_promise,+ false },+ { "AMSURLSession", "dataTaskWithRequest:", false, (IMP)new_data_task, (IMP *)&old_data_task, false },+ { "NSURLSessionTask", "resume", false, (IMP)new_resume_base, (IMP *)&old_resume_base, false },+ { "__NSCFLocalSessionTask", "resume", false, (IMP)new_resume_local, (IMP *)&old_resume_local, false },+};++static size_t+replacement_count(void)+{+ return sizeof(replacements) / sizeof(replacements[0]);+}++static void+image_added(__unused const struct mach_header *header, __unused intptr_t image_slide)+{+ for (size_t i = 0; i < sizeof(replacements) / sizeof(replacements[0]); i++) {+ struct replacement *r = &replacements[i];+ Class cls;+ Method method;+ IMP original;++ if (r->done || (cls = objc_getClass(r->class_name)) == NULL) {+ continue;+ }+ SEL selector = sel_registerName(r->selector);+ Class super = class_getSuperclass(cls);++ method = r->class_method ? class_getClassMethod(cls, selector) : class_getInstanceMethod(cls, selector);+ if (method == NULL) {+ continue; /* the class is there, the selector is not */+ }+ /*+ * class_getInstanceMethod walks up: a method this class only INHERITS is the superclass's,+ * and method_setImplementation on it would replace it for every class that inherits it as+ * well -- for NSObject, that is every object in the daemon. appstored restarted 31 times in+ * a run before this check was here, and said nothing on its way out (an ObjC exception ends+ * in abort(), which the fault reporter did not watch for either; now it does).+ */+ if (super != NULL && method == (r->class_method ? class_getClassMethod(super, selector) :+ class_getInstanceMethod(super, selector))) {+ r->done = true;+ say("redwood: appstored: %c[%s %s] is inherited, not this class's: left alone\n",+ r->class_method ? '+' : '-', r->class_name, r->selector);+ continue;+ }+ r->done = true;+ original = method_getImplementation(method);+ if (r->original != NULL && *r->original == NULL) {+ *r->original = original;+ }+ method_setImplementation(method, r->replacement);+ say("redwood: appstored: %c[%s %s] taken (was %#lx unslid)\n", r->class_method ? '+' : '-', r->class_name,+ r->selector, (uintptr_t)original - slide);+ }+}++__attribute__((constructor))+static void+redwood_store_init(void)+{+ const char *name = getprogname();+ size_t length = 0;++ if (name == NULL || strcmp(name, "appstored") != 0) {+ return;+ }+ slide = (uintptr_t)_dyld_get_shared_cache_range(&length) - CACHE_BASE;+ watch_for_faults();+ storefront = CFStringCreateWithCString(NULL, STOREFRONT, kCFStringEncodingUTF8);+ say("redwood: appstored: ours is in, storefront to answer with is " STOREFRONT "\n");+ {+ pthread_t thread;++ if (pthread_create(&thread, NULL, late_report, NULL) == 0) {+ pthread_detach(thread);+ }+ }+ _dyld_register_func_for_add_image(image_added); /* also called for every image already there */+}