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
air: the suite runs on the Adreno, and it truncates u32 to f32
vcvkkc2e49e121043
diff --git a/README.md b/README.mdindex 11d907a..707ee64 100644--- a/README.md+++ b/README.md@@ -242,6 +242,14 @@ XL_SECS=900 XL_CMDLINE="touchscreen=1 cpufreq=1 bootcpu=7 cpus=8" ./xl run #**`./xl stop`** — завершить сеанс сейчас. `XL_FOLLOW=5 ./xl run` каждые 5 секунд сохраняет журнал в`logs/…-run.log.live`: если телефон сбросится без единого слова, останется всё, что ядро успело сказать.+**Журнал загрузчика хранит первые 256 КБ и последний мегабайт, а середину выбрасывает**, сообщая об этом+одной строкой («the log between the first 256 KB and the last 1 MB was dropped»). Это важно для всякого+сеанса, который что-то печатает сам: с `REST=1` iOS насыпает больше мегабайта `os_log` за две минуты, и+если ваш вывод был в начале, он уедет ровно в выброшенную середину — из 225 запущенных программ в журнале+нашлись первые 27. `XL_FOLLOW` от этого не спасает: к моменту, когда он забирает журнал, середина уже+выброшена. Лечится не фильтром (фильтр убирает строки, которых уже нет), а коротким окном: `XL_SECS=30`+вместо 120 — и весь сеанс укладывается в 1,2 МБ целиком.+Управление как на iPhone X: свайп вверх от нижнего края — разблокировка и «домой», погасший экран будитсякасанием. **Закончить сеанс раньше времени — приложить к экрану пять пальцев на полторы секунды** (или`./xl stop` с компьютера): ядро вернётся в загрузчик (кнопки телефона заняты своим делом: питание гасит и будит экран, как на iPhone).@@ -495,11 +503,34 @@ P3 поверхности кадров копировались, и этогопо одной против эталона вне этого дерева, 71 из них совпадает с функциями C на каждом из 45 607 случаев,47 — ещё и когда их **исполняет** softpipe, а не складывает оптимизатор (2 586 случаев), 10 атомиков ибарьер — на написанных для них ядрах, 13 текстурных операций — точно, по нескольким текселям каждая.-Всё это — softpipe и фолдер NIR на x86; на GPU телефона из этого не исполнялось ничего.--Чего нет: ни один из семи шейдеров, прошедших на GPU телефона, не вызывает `air.*` — **на самом GPU не-исполнена ни одна операция**; 64-битное слово состояния семплера (`constexpr sampler`) не разобрано;-`air_run` рисует, но не отправляет вычислительные ядра (из ядер Apple отправлено одно, и то программно).+**И все 78 прогнаны на самом Adreno телефона** (`logs/080952-run.log`, 20 октября): из 71 точно+определённой операции **68 совпадают с C на каждом из 812 случаев, а 3 расходятся**. Заодно ответился+открытый вопрос про узкие типы: 24 объявления с `i1`, `i8`, `i16`, `f16`, которые softpipe не может+исполнить вовсе (у TGSI регистры 32-битные), на Adreno исполнились и оказались среди совпавших — у ir3+настоящие 16-битные регистры, и они правы. Ни один модуль не отказался запускаться.++Эти 3 — единственное за всю работу, что нельзя было найти на хосте никаким движением, и ровно за этим+набор и ехал на телефон: **Adreno усекает `u32 → f32` там, где C, NIR и softpipe округляют.**+`0xFFFFFFFF` даёт 4 294 967 040 вместо 4 294 967 296 — ровно отброшенные младшие 8 бит, то есть+усечение к нулю вместо округления к ближайшему, которого требует OpenCL (а Metal наследует его+математику). Наш фронтенд при этом выдаёт правильный опкод `nir_u2f32`, а ir3 превращает его в+одиночную `cov.u32f32` без доводки — машинный код модуля это пять инструкций, четыре из них `cov`,+и между загрузкой и преобразованием ничего. То есть виноваты не мы и не понижение в ir3, а сама+инструкция a6xx; **кто должен это компенсировать — не установлено**, для этого нужна документация на+ISA a6xx, которой здесь нет. Предсказание, которое из этого следует и которое ещё не проверено:+OpenGL ES порта идёт через тот же ir3 и ту же инструкцию, значит шейдер GLES на этом телефоне должен+усекать так же.++Строка таблицы «запущено на самом GPU телефона | 7» от этого не меняется: она считает модули корпуса из+456, а набор — сгенерированные модули, в корпус не входящие, и ни один из тех семи шейдеров корпуса+`air.*` не вызывает. Два разных измерения, и складывать их нельзя.++Чего нет: 64-битное слово состояния семплера (`constexpr sampler`) не разобрано;+`air_run` рисует, но не отправляет вычислительные ядра (из ядер Apple отправлено одно, и то программно);+`air.fast_pow` на Adreno даёт NaN при отрицательном основании, где softpipe даёт точное значение — по+стандарту это разрешено (`fast::` в Metal, как `native_` в OpenCL, вне своей области определения не+определены), но всякий шейдер корпуса, у которого основание может уйти ниже нуля, поведёт себя на+телефоне иначе, чем в любой проверке на этой машине.Полная сводка — `docs/air.md` и `docs/air-report.md`.**CoreImage при этом уже работает без всякого Metal** (раздел 7 `docs/metal.md`): нужен был один@@ -1603,7 +1634,26 @@ REST=1 userland/build.sh`REST=1` добавляет ~350 штатных служб iOS «по требованию» (без них часть приложений зависает на старте);список исключений — `DENY` в `userland/mkjobs.py`, пропустить службу из него для опыта: `ALLOW="слово"`.-Остальные переменные (`JOBS`, `OSLOG`, `INJECT`, `RC_JOBS`) описаны в начале `userland/build.sh`.+Остальные переменные (`JOBS`, `OSLOG`, `INJECT`, `RC_JOBS`, `EXTRA`, `EXTRA_SHARE`) описаны в начале+`userland/build.sh`.++Список заданий (`RC_JOBS=файл`, он же `/redwood/etc/rc.jobs`) выполняет не оболочка, а наш `init`+(`run_command` в `userland/src/init.c`), и рассчитывать в нём нельзя на четыре вещи:++- **на оболочку.** В корне самой iOS 16.5 её нет вообще: `/bin` — это `df` и `ps`, и больше ничего. Строка,+ начинающаяся с `/bin/sh`, кончается `No such file or directory` и кодом 127. `/var/jb/bin/sh`, который+ запускают `rc.jobs.jb` и `rc.jobs.sileo`, — оболочка префикса пакетного менеджера, и её несёт только образ+ с `JB=1`;+- **на переменные окружения.** Присваивание в начале строки — не присваивание, а первое слово команды.+ Программе, которой нужно передать значение, нужен для этого параметр: именно поэтому у `air_run`+ появился `--buffer` вместо `AIR_RUN_BUFFER`;+- **на кавычки.** Строка делится по пробелам и табуляциям, кавычки ничего не группируют; из перенаправлений+ понятны только `>> ФАЙЛ` и `&` в конце;+- **на длину.** Файл читается целиком в буфер фиксированного размера (64 КБ). Раньше их было три+ разных — и во всех трёх местах хвост просто не читался, молча: `inject.conf` в 512 байт уходил в+ `kern.exec_inject` половиной имени программы, которая ни с чем не совпадёт, а список заданий на 21 КБ+ выполнялся до 94-й строки, что выглядит как тест, ничего не нашедший. Теперь чтение одно+ (`read_whole`), и файл, который не влез, говорит об этом строкой `init: ... does not fit in N bytes`.`GL=1 REST=1 userland/build.sh` — образ, в котором оконный сервер рисует через OpenGL ES, а не программнымрастеризатором CoreAnimation: за `EAGLContext` встаёт Mesa (прослойка — `userland/src/eagl.c`). На телефоне сdiff --git a/docs/air.md b/docs/air.mdindex 3f28756..fae3547 100644--- a/docs/air.md+++ b/docs/air.md@@ -33,6 +33,8 @@ And of the 119 distinct `air.*` operations those modules call (`tools/air/intrin| of those, exactly defined and agreeing on every case (45 607 cases) | 71 || of those, approximate, so measured against libm and nothing concluded | 7 || of those, also *run* by a Gallium driver rather than folded (softpipe, 2 586 cases) | 47 |+| of those, run **on the phone's own Adreno** (812 cases) and agreeing there | 68 |+| of those, **disagreeing on the Adreno** where every host engine agrees | 3 || the atomics and the barrier, against kernels written for them (`atomiccheck.py`) | 10 || texture operations verified exactly, on several texels each (`texopcheck.py`) | 13 || texture operations with a case softpipe cannot run: a 16-bit result | 7 |@@ -43,15 +45,77 @@ functions and not entry points at all.**What those numbers are not**, because they will be quoted without this paragraph otherwise:-- **The arithmetic operations are now checked; nothing checked them against the Adreno.** 71 of- the corpus's declarations agree with C's own functions on every one of 45 607 cases, and 47 of- them agree again when *executed* by softpipe instead of folded — which is a real answer to what- used to be the largest hole here (section 4, item 4), and found three bugs getting there. But- both engines are on an x86 host. An opcode whose Adreno instruction differs from what- `nir_opt_constant_folding` and TGSI compute is invisible to all 48 193 of those cases, and the- 24 narrow-type declarations softpipe cannot run at all are exactly the ones where ir3 differs- most from it. None of the seven shaders that have run on the phone calls an `air.*` function:- **no operation has been run on the GPU.**+- **The arithmetic operations are checked; two of them have now been checked against the+ Adreno, and the other 76 have not.** 71 of the corpus's declarations agree with C's own+ functions on every one of 45 607 cases, and 47 of them agree again when *executed* by softpipe+ instead of folded — which is a real answer to what used to be the largest hole here (section 4,+ item 4), and found three bugs getting there. But those two engines are both on an x86 host. An+ opcode whose Adreno instruction differs from what `nir_opt_constant_folding` and TGSI compute is+ invisible to all 48 193 of those cases, and the 24 narrow-type declarations softpipe cannot run+ at all are exactly the ones where ir3 differs most from it.++ On 20 October that stopped being true. All 78 declarations ran on the phone's own Adreno+ (`logs/080952-run.log`, section 4 item 4): **68 of the 71 exactly-defined ones agree with C on+ every one of 812 cases, and 3 disagree** — and the 3 are the whole point of having gone there,+ because every engine on this machine agrees with C about them.++ Two things that run went better than expected. The **24 narrow-type declarations** that softpipe+ cannot run at all — `i1`, `i8`, `i16`, `f16`, where TGSI's 32-bit registers made a verdict+ impossible — ran on the Adreno and are among the 68 that agree. ir3 has real 16-bit registers,+ that was the open question, and the answer is that they are right. And nothing refused to run:+ no assertion, no fallback, no module missing from the log.++ The row "run on the phone's GPU | 7" in the first table above is unaffected and still reads 7:+ it counts modules of the 456-module corpus, and these are generated modules that are not in the+ corpus. The two measurements are deliberately not added together.+- **The Adreno truncates `u32 → f32` where C, NIR and softpipe round.** This is the first thing+ found here that no engine on this machine could have found, and it is why the suite was carried+ to the phone at all. `air.convert.f.f32.u.i32` and its 2- and 3-component forms, given+ `0xFFFFFFFF` (4 294 967 295 unsigned), return **4 294 967 040** where C returns+ **4 294 967 296**.++ The arithmetic is worth writing out, because the two numbers look like a printing difference and+ are not. In `binary32` the spacing between representable values in [2³¹, 2³²) is 2⁸ = 256, so the+ neighbours of 2³² − 1 are 2³² − 256 and 2³². Their distances from it are 255 and 1, so+ round-to-nearest gives 2³² = `0x4f800000`, which is what `(float)4294967295u` is in C, checked+ by compiling it. The Adreno's answer, 2³² − 256, is exactly `0xFFFFFFFF & 0xFFFFFF00`: the low+ eight bits dropped. That is truncation toward zero, not rounding. Metal's conversions are+ OpenCL's, which require round-to-nearest-even, so this is a real divergence and not a matter of+ taste. Only the cases where rounding decides anything differ — 1 of 4, 2 of 8 and 3 of 12 — and+ every value that is exactly representable agrees.++ **Where it comes from is settled as far as inspection can settle it, and no further.** Two of the+ three possible culprits are eliminated by reading code rather than by guessing:++ - the frontend emits `nir_u2fN`, the correct opcode (`air_intrinsics.c`, the `convert` branch);+ - ir3 turns that into a *single* `cov.u32f32` with no compensation. For a 32-bit source+ `create_cov` has no special case at all — the special cases there are for 8-bit sources and+ for `f32 → u8`. The generated Adreno code for the module is five instructions, four of them+ `cov.u32f32` reading straight out of the constant buffer, with nothing between the load and the+ conversion.++ So the truncation is the behaviour of the a6xx `cov.u32f32` instruction itself. **Whose job it is+ to compensate is not established**, and needs the a6xx ISA documentation, which is not to hand:+ if `cov` is specified to round, this is a hardware erratum ir3 should work around; if it is+ specified to truncate, then every API that requires round-to-nearest — OpenCL, Metal, and Vulkan's+ `ConvertUToF` — has to compensate, and ir3 does not. Not worked around in the frontend, because+ the frontend is emitting the right opcode and a workaround there would hide a fault that belongs+ to everything ir3 compiles.++ **A prediction that follows and has not been tested:** the port's OpenGL ES goes through the same+ ir3 and the same instruction, so a GLES shader converting a large `uint` to `float` on this phone+ should truncate identically. That would place the fault beyond this frontend entirely. It is one+ shader's worth of work and it has not been done.++- **`air.fast_pow` returns NaN on the Adreno for a negative base, where softpipe returns the exact+ value.** `fast_pow(-0.5, 1)` is `-0.5` in C and on softpipe, and NaN on the phone. This is+ recorded rather than reported as a bug: Metal's `fast::` functions, like OpenCL's `native_`+ ones, are explicitly undefined outside a restricted domain, and a negative base is outside it.+ It is worth naming because it sits inside the "approximate, nothing concluded" bucket, which is+ exactly where a real difference can hide behind a reasonable-looking ulp figure — the line says+ "worst inf ulp", and an inf ulp is not a rounding difference. Any corpus shader that calls+ `fast_pow` with a base that can go negative will behave differently on this phone than in any+ test on this machine.- **The atomics compute the right thing and the barrier waits; whether the atomics are *atomic*is still open.** `tools/air/atomiccheck.py` dispatches two kernels written for the purpose(section 4, item 5) and all eight operations the corpus uses agree — five on device memory@@ -555,16 +619,69 @@ Three references, each independent of the translator:(`32767` for a half that rounds to `32768`) to notice, not an assertion, and a check thathad trusted the number would have recorded a frontend bug that does not exist.- **It has not run on the phone.** `--emit DIR` writes the modules, the bytes for their input- buffer, a `manifest.json` of what each should return, and a `run.sh` that uses only `for`,- `echo` and shell parameter expansion — no `sed`, no `paste` — because what runs it is a Darwin- userland and not this machine's shell. `--check DIR LOG` then judges the log wherever it was- produced, so the arithmetic reference never leaves the host and the device needs nothing but- `air_run`. `--cases 4` cuts it to 211 modules and 1.7 MB, which is carryable. What is missing- is only the carrying: getting that directory into the boot image is the flash-root machinery of- subsystem (2), which this work is not supposed to touch, and the phone was held by another- session all night. **So every number above is from an x86 host, and the bottom row of the- table at the top of this document still reads 7.**+ **It has now run on the phone, and two operations of it are answered there.** `--emit DIR`+ writes the modules, the bytes for their input buffer, a `manifest.json` of what each should+ return, a `run.sh` for this machine and an `rc.jobs` for the device. `--check DIR LOG` then+ judges the log wherever it was produced, so the arithmetic reference never leaves the host and+ the device needs nothing but `air_run`. `manifest.json` is deliberately not carried: nothing on+ the device reads it, and leaving it behind keeps the stronger claim literally true — the device+ cannot be made to agree by reading the answer.++ **The reason this took a night longer than it needed to was a sentence in this document, and+ it was wrong.** It said that "getting that directory into the boot image is the flash-root+ machinery of subsystem (2), which this work is not supposed to touch". It is not. `/redwood/share`+ already existed and already carried a data file that a test reads (`pattern.bin`, which `init`+ verifies through `read()` and `mmap()`), and `EXTRA` already carried arbitrary host files into+ `/redwood/bin`. `EXTRA_SHARE` is ten lines of `build.sh`. A boundary the brief drew for good+ reasons had been reached for as the reason not to look into something, and the check that would+ have cost a minute — reading what `/redwood/share` is for — was never made. A constraint is not+ evidence about a thing it does not cover.++ **What the first run on the phone actually found was in the harness, not the GPU**+ (`logs/074504-run.log`, 20 October 07:45). The flash root mounted, the Adreno came up, and+ `air_run` opened it: `clearFragment01` and then two generated modules each printed+ `on Adreno 7c+ Gen 3 (the GPU, through the kernel's msm device)`. `air.fast_sqrt.f32` returned+ `1.44972523e-09 2.15926367e+15` and `air.dot.v4f32` returned `-inf -inf` — bit for bit what+ the host returns for the same module over the same pattern buffer, which is the comparison+ those two were put in the job list to make: a job line cannot set `AIR_RUN_BUFFER`, so they+ read `air_run`'s own pattern bytes, which the host can read too, and their numbers are+ therefore comparable without the manifest and without a shell.++ The other 98 of the 100 modules carried did not run, because the job list ended with+ `/bin/sh /redwood/share/airops/run.sh` and the answer was+ **`/bin/sh: No such file or directory`, status 127. iOS 16.5's own root has no shell at all**+ — `/bin` holds `df` and `ps`, and nothing else. The `/var/jb/bin/sh` that `rc.jobs.jb` and+ `rc.jobs.sileo` run belongs to the package manager's prefix, which only a `JB=1` image carries.+ This was asserted, not checked: a `grep` for `bin/sh` matched six `rc.jobs*` files and the+ matches were read as `/bin/sh` without opening a line of any of them — the same shape of+ mistake as the claim about `air_run`'s texture format a day earlier, a conclusion drawn from a+ pattern match instead of from the code. It is now written beside `RC_JOBS` in `build.sh`,+ because anyone writing a job list with a shell in it will meet the same 127.++ So the shell was removed rather than replaced. `air_run` grew `--buffer FILE` — `AIR_RUN_BUFFER`+ as an argument, which is the one thing `init`'s `run_command` can express — and `--emit` now+ writes an `rc.jobs` with one line per module. On the phone 27 modules ran in 0.6 s, about 22 ms+ each including opening the GPU, so 225 separate processes cost about five seconds and a loop+ inside one process would buy nothing worth the refactor. (An earlier estimate of 50–150 ms per+ module, taken from the spacing of nearby `launchd` lines, was wrong by a factor of five; the+ figure above is from the run that executed them.)++ **Two more things had to be found before the suite would report anything, and neither was in the+ GPU.** The first was in `init` and is written up where it belongs (`userland/src/init.c`,+ `read_whole`): three configuration files were each read with one `read()` into a fixed buffer+ with no check, so a file longer than its buffer silently lost its tail. The job list is 21 KB+ against a 8 KB buffer, so 94 of its 244 lines were read and about 150 modules did not run —+ which reads exactly like a test that found nothing. Caught before a run rather than after,+ by counting how many lines fit before claiming that 225 modules had passed.++ The second is a property of the loader's log and will catch anything verbose:+ **the log keeps the first 256 KB and the last 1 MB and drops the middle**, saying so in one+ line. With `XL_SECS=120` and `REST=1`, iOS wrote more than a megabyte of `os_log` after the+ suite had finished, so the suite's own output was in the dropped middle — 27 modules survived,+ at the front. `XL_FOLLOW`'s intermediate log does not help, because the drop has already+ happened by the time it is fetched. The cure is a short window, not a filter: a filter removes+ lines that are already gone. `XL_SECS=30` leaves the whole run at 1.19 MB with all 225 modules+ in it.5. **The atomics, with a kernel written to make a lost update show.** This document used to saythat none of Apple's kernels could test an atomic and why: "a sum written by threads that neverdiff --git a/tools/air/intrincheck.py b/tools/air/intrincheck.pyindex a15fcb6..5f8c95e 100755--- a/tools/air/intrincheck.py+++ b/tools/air/intrincheck.py@@ -909,6 +909,28 @@ done"""+JOBS_HEAD = """# Commands run by the launchd job com.redwood.demo, generated by+# tools/air/intrincheck.py --emit. Do not edit: regenerate it.+#+# %(n)d modules over %(decls)d air.* declarations. Each one calls a single operation on+# arguments it reads out of a buffer -- never on immediates, because a shader whose+# arguments are constants is answered by the compiler and the GPU is never asked.+#+# Nothing here says what any of them should return. The expected values stay on the host+# in manifest.json, which is not carried, and `intrincheck.py --check DIR LOG` judges the+# log afterwards -- so the device cannot be made to agree by reading the answer, and this+# file needs no updating when the suite is regenerated.+#+# The first line is the shader M1 is stated against: it takes no input buffer, and+# --disasm makes ir3 print the Adreno instructions it generated. The screen's name proves+# fd_screen_create won; only the disassembly proves the shader went through ir3 and not+# some later software path, and the four floats look identical either way. Expect+# 2.10170319e-18 0.178894654 1.48687698e+16 1.21394141e+33+/redwood/bin/air_run --disasm /System/Library/Extensions/AppleMetalGLRenderer.bundle/default.metallib clearFragment01+# Then the suite, carried into %(there)s by build.sh EXTRA_SHARE.+"""++def emit_suite(arith, outdir, want, verbose, few=False, cases=0):"""Write the modules, their inputs, the manifest and the runner."""os.makedirs(outdir, exist_ok=True)@@ -960,8 +982,28 @@ def emit_suite(arith, outdir, want, verbose, few=False, cases=0):with open(run, "w") as f:f.write(RUNNER)os.chmod(run, 0o755)- print("intrincheck: %d modules in %s, %d declarations, with run.sh and manifest.json"- % (len(manifest), outdir, len(set(m["name"] for m in manifest))))+ # rc.jobs: the same suite as a job list, for a device with no shell. run.sh+ # above is for this machine; the phone cannot use it, because iOS 16.5's own+ # root carries no sh at all (/bin holds df and ps, nothing else) and the+ # /var/jb/bin/sh that userland/rc.jobs.jb runs belongs to the jailbreak+ # prefix, which a flash-root image does not carry. So every module gets a+ # line of its own and its bytes come from --buffer, which is the one thing+ # init's run_command can express: full paths, blanks between them, no+ # variable and no quoting.+ #+ # The device path is where userland/build.sh EXTRA_SHARE puts this+ # directory, which is /redwood/share and its own name.+ there = "/redwood/share/" + os.path.basename(os.path.normpath(outdir))+ with open(os.path.join(outdir, "rc.jobs"), "w") as f:+ f.write(JOBS_HEAD % dict(n=len(manifest),+ decls=len(set(m["name"] for m in manifest)),+ there=there))+ for m in manifest:+ f.write("/redwood/bin/air_run --buffer %s/%s.bin %s/%s.bc\n"+ % (there, m["id"], there, m["id"]))+ print("intrincheck: %d modules in %s, %d declarations, with run.sh, rc.jobs "+ "and manifest.json" % (len(manifest), outdir,+ len(set(m["name"] for m in manifest))))return 0@@ -980,6 +1022,15 @@ def is_narrow(name, retty):return False+def is_number(t):+ """Is this line a number whole, and not a line that merely starts like one?"""+ try:+ float(t)+ return True+ except ValueError:+ return False++def parse_runner_log(path):"""({id: result}, screen) from whatever the runner printed.@@ -1027,20 +1078,49 @@ def parse_runner_log(path):breakout[ident] = ("refused", why or (block[-1].strip() if block else "no output"))return+ # The values, which air_run prints one per line and then closes with a+ # line of its own ("air_run: drew ..."). That closing line is what ends+ # the reading, not the first line that does not look like a number: a+ # phone's console is shared, and launchd, backboardd and the DRM shim+ # all write to it between one line of ours and the next. Breaking at+ # the first foreign line would silently take three values where four+ # were printed, and a short row reads exactly like a wrong answer. So+ # anything that is not a number is skipped, and only our own next line+ # stops it.vals = []for line in text[i:].splitlines()[1:]:t = line.strip()- if not t or not re.match(r"^-?[0-9.]|^-?(inf|nan)", t):+ if t.startswith("air_run:") or t.startswith("=== "):break- vals.append(t)+ # A value is a line that is a number and nothing else. Matching a+ # leading digit is not enough: "2026-10-20 07:50:00.641 distnoted+ # [14:221] ..." begins with one, and taking it for a value put a+ # string where an integer was expected and crashed the check --+ # which is the good failure, but on another day it would have been+ # counted as a wrong answer from the GPU.+ if is_number(t):+ vals.append(t)out[ident] = ("ok", vals)for line in lines:if screen is None and " on " in line and line.startswith("air_run: "):screen = "softpipe" if "softpipe" in line else line.split(" on ")[-1].strip()+ # A block starts at run.sh's marker, or -- when there was no run.sh,+ # because the device has no shell and every module was a job line of its+ # own -- at air_run's own first line, which names the module it was+ # given. Either way the id comes from the log and not from the order of+ # the blocks in it, so a line lost or repeated anywhere cannot move one+ # module's numbers onto another's name.+ mark = Noneif line.startswith("=== "):+ mark = line[4:].strip()+ else:+ m = re.match(r"^air_run: (?:\S*/)?(\d+)\.bc[ :]", line)+ if m:+ mark = m.group(1)+ if mark is not None:finish()- ident = line[4:].strip()+ ident = markblock = []else:block.append(line)@@ -1065,6 +1145,7 @@ def check_log(outdir, logpath, verbose):narrow_skipped = set()missing = 0refused = {}+ short = {}total = 0for m in manifest:if narrow_here and is_narrow(m["name"], m["retty"]):@@ -1079,6 +1160,18 @@ def check_log(outdir, logpath, verbose):continuevals = r[1]isf = parse_type(m["retty"])[0] in ("float", "half", "double")+ # A block that answered printed the render target's four components,+ # always four whether the module filled two of them or all four. Any+ # other count means the block was not read whole -- a line of ours lost+ # among the console's other writers, or one seen twice -- and it is+ # reported as that rather than compared. The loop below used to skip a+ # case whose value was simply not there (`idx >= len(flat)`), which is+ # the quiet failure this repository has been bitten by before: cases+ # that vanish instead of failing, and a total that is lower by exactly+ # as much as nobody noticed.+ if len(vals) != 4:+ short.setdefault(m["name"], []).append((m["id"], len(vals)))+ continueflat = []for v in vals:try:@@ -1086,7 +1179,9 @@ def check_log(outdir, logpath, verbose):except ValueError:flat.append(float("nan"))if not isf and m["signed"]:- flat = [wrap(v, 32, True) for v in flat]+ # only what really came back as an integer: a value that would not+ # parse is a NaN here, and wrap() cannot take one+ flat = [wrap(v, 32, True) if isinstance(v, int) else v for v in flat]st = per_decl.setdefault(m["name"], dict(exact=m["exact"], n=0, bad=[],worst=0, worst_case=None))for i, args in enumerate(m["cases"]):@@ -1151,12 +1246,17 @@ def check_log(outdir, logpath, verbose):print("intrincheck: %d declarations were refused by the harness" % len(refused))if missing:print("intrincheck: %d modules are in the manifest and not in the log" % missing)+ if short:+ n = sum(len(v) for v in short.values())+ print("intrincheck: %d modules printed something other than four values and were "+ "not read: %s" % (n, ", ".join("%s (%s)" % (k, " ".join(+ "%s:%d" % (i, c) for i, c in v)) for k, v in sorted(short.items()))))if narrow_skipped:print("intrincheck: %d declarations left out: a narrow type (i1, i8, i16, f16) ""and this ran on softpipe, whose registers are 32-bit" % len(narrow_skipped))print("intrincheck: %d cases in all, as RUN on %s and not as folded"% (total, screen or "an unnamed screen"))- return 1 if (bad or missing) else 0+ return 1 if (bad or missing or short) else 0def main(argv):diff --git a/tools/mesa/air/air_run.c b/tools/mesa/air/air_run.cindex c257d76..cea22fa 100644--- a/tools/mesa/air/air_run.c+++ b/tools/mesa/air/air_run.c@@ -417,14 +417,31 @@ main(int argc, char **argv)* creation, so it has to be set before the screen is made.*/bool want_disasm = false;- if (argc > 1 && !strcmp(argv[1], "--disasm")) {- want_disasm = true;- setenv("IR3_SHADER_DEBUG", "disasm", 1);- argv++;- argc--;+ /* --buffer FILE is AIR_RUN_BUFFER as an argument, and it exists because the+ * device has no shell to set one with. iOS 16.5's own root carries no sh at+ * all -- /bin holds df and ps -- and the job list that runs this is read by+ * our init, which splits a line on blanks and has no notion of a variable+ * (userland/src/init.c, run_command). So a generated suite whose every+ * module reads its own bytes could not be run on the phone until this+ * option existed: with it each module is one ordinary job line.+ */+ while (argc > 1 && argv[1][0] == '-') {+ if (!strcmp(argv[1], "--disasm")) {+ want_disasm = true;+ setenv("IR3_SHADER_DEBUG", "disasm", 1);+ argv++;+ argc--;+ } else if (!strcmp(argv[1], "--buffer") && argc > 2) {+ setenv("AIR_RUN_BUFFER", argv[2], 1);+ argv += 2;+ argc -= 2;+ } else {+ break;+ }}if (argc < 2) {- fprintf(stderr, "usage: air_run [--disasm] FILE[.metallib] [FUNCTION]\n");+ fprintf(stderr, "usage: air_run [--disasm] [--buffer FILE] "+ "FILE[.metallib] [FUNCTION]\n");return 2;}diff --git a/userland/build.sh b/userland/build.shindex 9a5b02d..d90489e 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -27,7 +27,17 @@# for at every boot -- a few megabytes, not more# RC_JOBS=<file> what the job com.redwood.demo runs: rc.jobs (default), or# rc.jobs.unlock, which also swipes the lock screen away (for the phone,-# where nothing else can touch the screen yet)+# where nothing else can touch the screen yet).+# A job list cannot assume a shell. iOS 16.5's own root has none at all+# -- /bin is df and ps, and nothing else -- so a line beginning /bin/sh+# fails with "No such file or directory" and status 127 on a flash-root+# boot. The /var/jb/bin/sh that rc.jobs.jb and rc.jobs.sileo run is the+# package manager's prefix, which only a JB=1 image carries. And init+# runs these lines itself (src/init.c, run_command): words split on+# blanks, no quoting, no variable assignment, no redirection beyond a+# trailing ">> FILE" and "&". So a program that needs a value passed to+# it needs an option for it, not an environment variable -- which is why+# air_run grew --buffer.# GL=1 with an OpenGL ES implementation for the window server: Mesa# (tools/mk-mesa.sh fetches and builds it) behind src/eagl.c, as# lib/libOSMesa.8.dylib (15 MB) and lib/libredwood-gl.dylib. Where thediff --git a/userland/rc.jobs.airops b/userland/rc.jobs.airopsdeleted file mode 100644index 98c78ec..0000000--- a/userland/rc.jobs.airops+++ /dev/null@@ -1,45 +0,0 @@-# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).-#-# The first air.* operation executed on this GPU (RC_JOBS=rc.jobs.airops). Every number in-# docs/air.md section 4 about the arithmetic -- 45607 folded cases, 2586 run ones -- was computed-# on an x86 host by NIR's constant folder and by softpipe. Neither is an Adreno. An opcode whose-# Adreno instruction disagrees with what those two compute is invisible to all of it, and the-# seven shaders that have run on the phone call no air.* function at all. This job is that gap.-#-# The first line is the shader M1 is stated against. It is here for two reasons that the suite-# below cannot cover for itself: it takes no input buffer, so it can be an ordinary job line, and-# --disasm makes ir3 print the Adreno instructions it generated. The screen's name proves-# fd_screen_create won; only the disassembly proves the shader went through ir3 and not some-# later software path, and the four floats look identical either way. Expect-# 2.10170319e-18 0.178894654 1.48687698e+16 1.21394141e+33-/redwood/bin/air_run --disasm /System/Library/Extensions/AppleMetalGLRenderer.bundle/default.metallib clearFragment01-# Then the generated suite: 100 modules over 78 air.* declarations, written by-# tools/air/intrincheck.py --emit and carried in by EXTRA_SHARE. Each module calls one operation-# on arguments it reads out of a buffer -- never on immediates, because a shader whose arguments-# are constants is answered by the compiler and the GPU is never asked.-#-# Nothing here says what any of them should return, and that is deliberate: the expected values-# stay on the host in manifest.json, which is not carried, and `intrincheck.py --check DIR LOG`-# judges this log afterwards. So the device cannot be made to agree by reading the answer, and-# this file needs no updating when the suite is regenerated.-#-# Two of those modules again, run directly, before the shell is asked for anything. They are here-# so that this run cannot come back with nothing: a job line cannot set AIR_RUN_BUFFER, so these-# two read air_run's own pattern buffer instead ((i * 7 + 13) & 0xff) -- which the host can read-# too, so their numbers are comparable without the manifest and without a shell. If the loop below-# fails for any reason at all, these two lines still answer the question this whole job exists for:-# an air.* operation compiled by ir3 and executed on the Adreno.-#-# What softpipe computes for them on an x86 host, which is what the Adreno has to match:-# 0077 air.fast_sqrt.f32 1.44972523e-09 2.15926367e+15 0 0-# 0061 air.dot.v4f32 -inf -inf 0 0-# (the pattern bytes read as floats are extreme, so the dot product overflows; -inf is the right-# answer to the question asked, not a failure)-/redwood/bin/air_run /redwood/share/airops/0077.bc-/redwood/bin/air_run /redwood/share/airops/0061.bc-# A shell, and not 100 job lines: init's run_command splits a line on blanks and cannot set a-# variable (src/init.c), while every module needs AIR_RUN_BUFFER pointing at its own bytes. The-# shell is iOS's own /bin/sh -- with the root on the flash this volume is the iOS one, and-# rc.jobs.jb and rc.jobs.sileo already run it there. run.sh finds air_run in /redwood/bin when-# there is none beside the modules.-/bin/sh /redwood/share/airops/run.shdiff --git a/userland/src/init.c b/userland/src/init.cindex d5edfed..f21bca0 100644--- a/userland/src/init.c+++ b/userland/src/init.c@@ -779,6 +779,47 @@ graft_tree(const char *from, const char *to, int top, int *made, int *linked)return ok && chmod(to, mode) == 0;}+/*+ * A configuration file read whole into a fixed buffer, and loudly when it does not fit.+ *+ * Three places here read a file of ours this way, and all three used to do it with one+ * read() and no check at all, so a file longer than its buffer simply lost its tail. What+ * that looks like is different every time and never looks like a short read: half a program+ * name handed to kern.exec_inject, which then matches nothing and the insertion quietly does+ * not happen; directories of /private/var that are merely absent, which is what cost the+ * package manager's work a long hunt for a missing /var/mobile/tmp; job lines that do not+ * run, which reads as a test that found nothing.+ *+ * The loop matters as much as the size: read() is allowed to return less than it was asked+ * for without being at the end of the file, so a single read() is not enough even when the+ * file does fit. The buffers are still fixed -- this is an init with no allocator worth the+ * name -- but a file that does not fit now says so instead of going quiet.+ *+ * The length, or -1 if there is no such file.+ */+static ssize_t+read_whole(const char *path, char *buf, size_t size)+{+ int fd = open(path, O_RDONLY, 0);+ size_t got = 0;+ ssize_t n;+ char over;++ if (fd < 0) {+ return -1;+ }+ while (got < size - 1 && (n = read(fd, buf + got, size - 1 - got)) > 0) {+ got += (size_t)n;+ }+ /* one byte left to read means the buffer ended, not the file */+ if (got == size - 1 && read(fd, &over, 1) > 0) {+ printf("init: %s does not fit in %zu bytes: the rest of it is not used\n", path, size - 1);+ }+ close(fd);+ buf[got] = 0;+ return (ssize_t)got;+}+static voidmount_writable(void){@@ -788,7 +829,6 @@ mount_writable(void)int ok, fd, made = 0, files = 0;const char *jb;struct stat st;- ssize_t n;printf("\n-- writable filesystem --\n");ok = mount("ramfs", "/private/var", 0, NULL) == 0;@@ -798,10 +838,7 @@ mount_writable(void)check(ok, "mount a memory filesystem on /private/var");/* the directories of the data volume, as listed in the template that ships with iOS (ios165/mkvarskel.py) */- fd = open(in_image("/etc/var.skel"), O_RDONLY, 0);- n = fd >= 0 ? read(fd, skeleton, sizeof(skeleton) - 1) : -1;- close(fd);- skeleton[n > 0 ? n : 0] = 0;+ read_whole(in_image("/etc/var.skel"), skeleton, sizeof(skeleton));for (char *line = skeleton; ok && *line;) {char *end = strchr(line, '\n'), *f[6];int count;@@ -1223,16 +1260,17 @@ run_command(char *line)static voidrun_rc(const char *path){- static char script[8192];- int fd = open(path, O_RDONLY, 0);- ssize_t n;+ /*+ * 64 KB and not 8 KB: a generated job list is one line per case, and+ * tools/air/intrincheck.py --emit writes 226 of them, 21 KB. At 8 KB this read the+ * first 94 lines and the rest was not there -- no error, no short line, simply jobs+ * that did not run, which looks exactly like a test that found nothing.+ */+ static char script[65536];- if (fd < 0) {+ if (read_whole(path, script, sizeof(script)) < 0) {return;}- n = read(fd, script, sizeof(script) - 1);- close(fd);- script[n > 0 ? n : 0] = 0;printf("\n=== %s ===\n", path);for (char *line = script; *line;) {@@ -1309,15 +1347,19 @@ test_time(void)static voidconfigure_inject(void){- char conf[512];- int fd = open(in_image("/etc/inject.conf"), O_RDONLY, 0);- long n;+ /*+ * 4 KB and not 512 bytes: the file was already 267 bytes with three insertions in it,+ * half the buffer, and what overflowing it does is the worst of the three places this+ * pattern was in. The tail is not dropped -- it is handed to kern.exec_inject as half a+ * program name, which then matches nothing, so the insertion simply never happens and+ * nothing says why.+ */+ char conf[4096];+ long n = (long)read_whole(in_image("/etc/inject.conf"), conf, sizeof(conf));- if (fd < 0) {+ if (n < 0) {return;}- n = read(fd, conf, sizeof(conf) - 1);- close(fd);while (n > 0 && (conf[n - 1] == '\n' || conf[n - 1] == ' ')) {n--;}