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
docs: the four crashing kernels dispatch, and the texture count is not a threshold
vcvkkb9a686b90fca
diff --git a/README.md b/README.mdindex 0f9afdc..bd8c3d7 100644--- a/README.md+++ b/README.md@@ -27,7 +27,7 @@|---|---|| Экран блокировки, разблокировка свайпом, домашний экран с полным набором значков (две страницы), стандартные обои iOS 16 | Сотовая сеть, Bluetooth || Системные жесты: свайп снизу — «домой» и переключатель приложений, свайп от левого края — «назад», Пункт управления | Без `gpu=1` (программный рендер; в QEMU всегда) часы экрана блокировки просто белые, без «вибрации» под цвет обоев, и верх обоев под ними не затемнён |-| Запуск приложений: Настройки (вглубь), Часы, Календарь, Фото, Заметки и др.; расширения приложений (виджеты «Погода» и «Календарь» на домашнем экране рисуют своё содержимое — на телефоне; в QEMU им не хватает скорости) | Metal у приложений (они рисуют через него сами; WebGL, видоискатель «Камеры», эффекты снимка). Но уже не «не предполагается»: наш передний конец читает шейдеры, которые Apple скомпилировала для Metal, и исполняет их на Adreno — 7 из 456 прошли на GPU телефона, шесть из них текстурные и вернули точный тексель. Ни одна из ~50 арифметических операций `air.*` численно не проверена ничем; вычислительных ядер запущено одно и на программном отрисовщике; слово состояния сэмплера не разобрано, то есть фильтрация и адресация самой Apple не проверены. См. `docs/air.md` |+| Запуск приложений: Настройки (вглубь), Часы, Календарь, Фото, Заметки и др.; расширения приложений (виджеты «Погода» и «Календарь» на домашнем экране рисуют своё содержимое — на телефоне; в QEMU им не хватает скорости) | Metal у приложений (они рисуют через него сами; WebGL, видоискатель «Камеры», эффекты снимка). Но уже не «не предполагается»: наш передний конец читает шейдеры, которые Apple скомпилировала для Metal, и исполняет их на Adreno — 51 из 456 прошли на GPU телефона: 7 отрисовкой (шесть текстурных вернули точный тексель) и 44 вычислительных ядра диспатчем. 71 операция `air.*` сходится с LLVM на всех 45 697 случаях, 61 из них проверена на самом Adreno; слово состояния сэмплера не разобрано, то есть фильтрация и адресация самой Apple не проверены. См. `docs/air.md` || Сенсорный экран телефона (Goodix GT9916), мультитач, пробуждение касанием | Без `cpus=N` работает одно ядро процессора || Несколько ядер процессора (`cpus=8`: все семь, что не заняты загрузчиком; малые — после больших) | || Кнопки: на телефоне настоящие — питание (боковая кнопка iPhone) и громкость; в QEMU — клавишами | Зарядка под нашим ядром (телефон только разряжается) |@@ -539,13 +539,13 @@ P3 поверхности кадров копировались, и этого| что | сколько из 456 || --- | --- || читает наш собственный читатель битового кода, побайтово как `llvm-dis` | 456 |-| транслируется в NIR и проходит `nir_validate` | 313 |-| компилируется ir3 в машинный код Adreno | 313 |+| транслируется в NIR и проходит `nir_validate` | 318 |+| компилируется ir3 в машинный код Adreno | 318 || совпадает с LLVM численно и **рисуется** (softpipe) | 255 |-| запущено на самом GPU телефона (Adreno 7c+ Gen 3, через `msm` в ядре): 7 отрисовкой, 40 диспатчем | **47** |+| запущено на самом GPU телефона (Adreno 7c+ Gen 3, через `msm` в ядре): 7 отрисовкой, 44 диспатчем | **51** |Из 321 точки входа (135 модулей — библиотечные функции `air.visible`, не точки входа) транслируется-313, то есть 97,5 %. Смысл каждой операции `air.*` взят из словаря самого компилятора Apple+318, то есть 99,1 %. Смысл каждой операции `air.*` взят из словаря самого компилятора Apple(`AGXCompilerCore`, 137 строк-суффиксов), а не угадан — так нашлась и одна ошибка в наших догадках.Операции проверены численно — на хосте: из 119 разных `air.*`, что встречаются в корпусе, 78 прогнаны@@ -639,13 +639,13 @@ P3 поверхности кадров копировались, и этогоСтрока таблицы «запущено на самом GPU телефона» этих модулей не считает: она про модули корпуса из456, а набор — сгенерированные, в корпус не входящие, и складывать два измерения нельзя. Сама строка-теперь **47** по другой причине: 7 фрагментных шейдеров отрисовкой и 40 вычислительных ядер+теперь **51** по другой причине: 7 фрагментных шейдеров отрисовкой и 44 вычислительных ядердиспатчем, множества не пересекаются — ядро не фрагментный шейдер. До прогона ядер там стояло 7, инекоторое время после него тоже, и это третий раз, когда именно эту цифру оставляет позади работа,которая её и сдвинула.Чего нет: 64-битное слово состояния семплера (`constexpr sampler`) не разобрано;-`air_run` рисует, но не отправляет вычислительные ядра (из ядер Apple отправлено одно, и то программно);+из 47 вычислительных ядер Apple три отказаны передним концом (маска шаффла не константа);`air.fast_pow` на Adreno даёт NaN при отрицательном основании, где softpipe даёт точное значение — постандарту это разрешено (`fast::` в Metal, как `native_` в OpenCL, вне своей области определения неопределены), но всякий шейдер корпуса, у которого основание может уйти ниже нуля, поведёт себя наdiff --git a/docs/air-report.md b/docs/air-report.mdindex 3be1ef9..e3c2100 100644--- a/docs/air-report.md+++ b/docs/air-report.md@@ -11,15 +11,15 @@ correct**: correctness needs the CPU reference and a run on the GPU.| --- | --- | --- || container | 456 | 100% || reader | 456 | 100% |-| translate | 313 | 69% |-| ir3 | 313 | 69% |+| translate | 318 | 70% |+| ir3 | 318 | 70% |## By stage of the shader| what the module is | modules | translate | ir3 || --- | --- | --- | --- |-| fragment | 271 | 270 | 270 |-| kernel | 47 | 40 | 40 |+| fragment | 271 | 271 | 271 |+| kernel | 47 | 44 | 44 || vertex | 3 | 3 | 3 || visible | 135 | 0 | 0 |@@ -29,10 +29,10 @@ The container's TYPE tag agrees with the module's own metadata on 456 of 456; 0| library | modules | translate | ir3 || --- | --- | --- | --- |-| AppleMetalGLRenderer.bundle/default.metallib | 274 | 273 | 273 |-| PortraitFilters.cifilter/default.metallib | 36 | 34 | 34 |+| AppleMetalGLRenderer.bundle/default.metallib | 274 | 274 | 274 |+| PortraitFilters.cifilter/default.metallib | 36 | 35 | 35 || PortraitFilters.cifilter/portrait_filters.metallib | 135 | 0 | 0 |-| VideoDecoders/AV1SW.metallib | 11 | 6 | 6 |+| VideoDecoders/AV1SW.metallib | 11 | 9 | 9 |## Modules that call no `air.*` at all@@ -43,11 +43,7 @@ The container's TYPE tag agrees with the module's own metadata on 456 of 456; 0| modules | where it stops || --- | --- || 135 | stage: air.visible, a library function (not yet) |-| 2 | instruction: a shuffle mask that is not constant |-| 2 | intrinsic: a call of _ZL13FilmGrainLumaN5metal9texture2dItLNS_6accessE1EvEES2_NS0_IsLS1_1EvEERU11MTLconstantK15FilmGrainParamsPU11MTLconstantKhDv2_j, a function in the module |-| 2 | intrinsic: a call of _ZL15FilmGrainChromaN5metal9texture2dItLNS_6accessE1EvEES2_NS0_IsLS1_1EvEES2_S2_RU11MTLconstantK15FilmGrainParamsPU11MTLconstantKhS8_Dv2_j, a function in the module |-| 1 | intrinsic: a call of ___metal_fract_v2float, a function in the module |-| 1 | intrinsic: a call of ___metal_fract_v4float, a function in the module |+| 3 | instruction: a shuffle mask that is not constant |## The `air.*` functions the corpus callsdiff --git a/docs/air.md b/docs/air.mdindex a9fcd6c..d4ec465 100644--- a/docs/air.md+++ b/docs/air.md@@ -23,7 +23,7 @@ Of the 456 modules of the device's system `.metallib` files (`tools/air/check.sh| translated to NIR, passing `nir_validate` | 318 || compiled by ir3 into Adreno machine code | 318 || numerically identical to LLVM's own execution, and drawn | 255 |-| **run on the phone's GPU** — 7 drawn, 40 dispatched | **47** |+| **run on the phone's GPU** — 7 drawn, 44 dispatched | **51** |And of the 119 distinct `air.*` operations those modules call (`tools/air/intrincheck.py`):@@ -85,8 +85,8 @@ functions and not entry points at all.The row "run on the phone's GPU" in the first table above does not count these: it countsmodules of the 456-module corpus, and these are generated modules that are not in it. The two- measurements are deliberately not added together. That row now reads **47** for a different- reason — 7 fragment shaders drawn and 40 compute kernels dispatched, which are disjoint sets+ measurements are deliberately not added together. That row now reads **51** for a different+ reason — 7 fragment shaders drawn and 44 compute kernels dispatched, which are disjoint setsbecause a kernel is not a fragment shader. It read 7 until the kernels ran, and it stayed 7 fora while after they did, which is the third time this one figure has been left behind by the workthat moved it.@@ -343,84 +343,77 @@ functions and not entry points at all.`air_get` already materialises one in whichever builder asks, which was checked rather thanassumed.-- **Of Apple's own 47 kernels, 6 are dispatched, all on software.** `tools/air/kernelcheck.py`- prints where each one stops, as a table whose rows are checked against the total before it is- printed:+- **Of Apple's own 47 kernels, 44 are dispatched on the Adreno and none crash.**+ `tools/air/kernelcheck.py` prints where each one stops, as a table whose rows are checked+ against the total before it is printed:| | on softpipe | **on the Adreno** || --- | --- | --- |- | dispatched and read back | 6 | **40** |+ | dispatched and read back | 6 | **44** || softpipe's `nir_to_tgsi`: scratch and intrinsics it lacks | 38 | — || a shuffle mask that is not constant (a frontend limit) | 3 | 3 |- | **crashed `air_run`** (a defect here, not a limit) | 0 | **4** |+ | **crashed `air_run`** (a defect here, not a limit) | 0 | **0** |**The deferred question is answered: ir3 runs them.** 38 kernels stopped at a software- rasteriser that has no scratch memory and lacks compute intrinsics, and on the hardware 40 of- the 47 dispatch and read their results back — 22 of those 40 binding at least one image. The- phone replaced softpipe and the column that was never ours emptied.+ rasteriser that has no scratch memory and lacks compute intrinsics; on the hardware 44 of the 47+ dispatch and read their results back, and every one of the 23 that bind an image is among them.+ The phone replaced softpipe and the column that was never ours emptied. The corpus report's+ kernel row reads 44 of 47 through ir3 as well, stopping on the same three, so the compile and+ the dispatch agree about which kernels are out of reach.What the rows do **not** say is right. There is no executed reference for these kernelsanywhere: softpipe refuses 38 of them, which is why they came here, so a number out of the- Adreno has nothing on this machine to disagree with. 40 have been shown to *run*.+ Adreno has nothing on this machine to disagree with. 44 have been shown to *run*.- Four crash, and the crash is on this side of the line. All four fault at the same place, which- `llvm-nm` against the binary that ran resolves to-- pc fd6_texture_state + 0xd4- lr fd6_emit_cs_state<chip6> + 0x100- fd6_launch_grid<chip6> + 0xfc- fd_launch_grid + 0x3b4- dispatch_kernel + 0x194c-- — freedreno's own descriptor emission for the compute stage, reached from the image binding this- harness does. Two explanations are already dead: the driver's texture state is not uninitialised- (`CALLOC_STRUCT(fd6_context)` zeroes it, so the loops over `num_textures` do not run), and it is- not "an image with no buffer", because seven kernels with exactly that shape dispatch fine.- Whether the fault is a state this harness builds wrongly or a freedreno bug is **not- established**, and 22 kernels binding images successfully is the reason it cannot be read as- "images do not work".-- **The cause is found and it is this harness's, not freedreno's. Whether it accounts for all four- is a prediction, not yet a result.**+ The four that used to crash now dispatch, and the crash was this harness's:void *csampler = NULL;pipe->bind_sampler_states(pipe, PIPE_SHADER_COMPUTE, 0, num_tex, &csampler);`bind_sampler_states` reads `num_tex` pointers from the address it is given, and `&csampler` is- the address of *one*. At `num_tex == 1` that is right by accident, which is why 22 kernels bound- images without trouble; at two or more the driver takes whatever followed that variable on the- stack and stores it as `samplers[1..]`, and `fd6_texture_state`'s loop over `num_samplers` then- dereferences those for `->seqno`. A wild pointer plus a small field offset is exactly- `0x100000000034`. The draw path beside it has always built a proper `void *samplers[]` array, so- the new code failed to learn what the old code already knew.-- A peer session's reading of the same function proposed a different mechanism — `view_seqno[16]`- overflowing because `num_textures` is unclamped against `textures[32]`, which is a real- fragility in freedreno — and it is not reachable from here: this harness refuses a slot index of- 16 or more by name. Checking whether someone else's bug was reachable, instead of fixing it, is- what left the adjacent line to be read.-- **And the feature that looked discriminating was probably a confound.** Exactly 4 of the 47- kernels carry an inlined module-local function; three crashed and the fourth was refused earlier- on the shuffle mask, so none dispatched and every one that reached dispatch died. That is 3 of 3- and not a correlation — but those same kernels are the only ones reading *two* textures, and two- is precisely the threshold of the bug above. One mechanism may account for all four, including- `spillCorrectionPreProcess`, which carries no call at all. `air_run` now prints the texture- count beside the image count so the next run decides it rather than an argument.-- What distinguishing them achieved, before the cause was known: Three of them — `FilmGrainLumaKernel_Uint`, `FilmGrainLumaKernel_Unorm`,- `FilmGrainChromaKernel_Unorm` — are exactly the kernels that contain an inlined module-local- function, and nothing that dispatched contains one. The fourth,- `spillCorrectionPreProcess`, contains no call at all and is instead the only kernel in the- corpus that binds **four** images, where the next most is three. Seven kernels with the- identical shape to the first three — one image, written, no buffers — dispatch without- trouble, so the counts alone are not the cause and neither is the image binding as such.-- That is a lead and not a diagnosis: it says where to look (the inlining path, and separately- whatever four images do that three do not) and it does not say what is wrong. The temptation- here is to fix the likelier of the two and report the number that follows; what this file has- learned is that a fix without a mechanism is how two of the three earlier attempts on this very- function were spent.+ the address of *one*, so it reads `num_tex - 1` words past that variable and stores whatever is+ there as `samplers[1..]`. The draw path beside it has always built a proper `void *samplers[]`+ array, so the new code failed to learn what the old code already knew. The fault was at+ `fd6_texture_state + 0xd4`, reached through `fd6_emit_cs_state` from `fd_launch_grid`: a wild+ pointer plus a small field offset, which is exactly the `0x100000000034` that was being+ dereferenced.++ **What that mechanism does and does not predict.** Both of freedreno's loops skip an empty slot+ (`if (!tex->samplers[i]) continue;` in `fd6_texture_state`, a zeroed dummy in the emit path), and+ `fd_sampler_states_bind` sets a slot's valid bit only for a non-NULL pointer — so a stack word+ that happens to be zero is read, stored and harmlessly ignored. The read goes out of bounds at+ `num_tex >= 2`; the *fault* needs one of those words to be non-zero. So the texture count is not+ a threshold, and the pre-fix run says so plainly (`logs/155708-run.log`): eight kernels with two+ textures (`BlitKernel_Unorm`, three `BlitLuma*`, `MetalFaceMask_GenerateMask`, `confidence`,+ `fbs_bistochastize_final`, `fbs_slice_trilinear`) and two with three (`BlitChromaKernel_Uint`,+ `BlitChromaKernel_Unorm`) all dispatched while the read was still out of bounds for every one of+ them. The four that died read 4, 4, 6 and 6. Said as a threshold of two, this would predict a+ crash for ten kernels that did not crash.++ **The feature that looked discriminating was a confound, and the run settled it.** Exactly 4 of+ the 47 kernels carry an inlined module-local function; three crashed and the fourth was refused+ earlier on the shuffle mask, so every one that reached dispatch died — 3 of 3, which is not a+ correlation so much as an invitation. `spillCorrectionPreProcess` carries no call at all and+ died with them, and all four now dispatch with the inlining path untouched. Had the inlining+ been "fixed" instead, three might have gone through and the fourth would have been left needing+ a second cause.++ Two other explanations were already dead before this one, and killing them was what left the+ adjacent line to be read: the driver's texture state is not uninitialised+ (`CALLOC_STRUCT(fd6_context)` zeroes it), and it was not "an image with no buffer", because+ nine kernels of exactly that shape — one image, written — dispatched throughout+ (`logs/155708-run.log`). A peer session's reading of the same+ function proposed `view_seqno[16]` overflowing against `textures[32]` — a real fragility in+ freedreno — and it is not reachable from here, because this harness refuses a slot index of 16+ or more by name. Checking whether someone else's bug was reachable, instead of looking one line+ further for our own, is how two of the three earlier attempts on this function were spent.++ `air_run` now prints the texture count beside the image count, which is what made the paragraph+ above a measurement instead of an argument — and the first thing written from that run was still+ the wrong generalisation, "two is the threshold", corrected with a guess ("there are surely+ kernels reading two among the forty") when the counts were already in the log. A peer session+ counted them. That is where accuracy usually goes: not in the first claim but in its repair,+ which arrives feeling like diligence and is rarely re-checked against the same evidence.The row for a call of a module-local function was 5 until those calls were translated; it is nowgone, its five having moved to softpipe (four) and the shuffle mask (one). Twice in a row the@@ -441,7 +434,7 @@ functions and not entry points at all.as well, which `texopcheck.py` had listed as having no case for exactly this reason.So the atomics being checked (above) is still a statement about two kernels written here for- that purpose, not about the 17 of Apple's 40 that use one.+ that purpose, not about the 17 of Apple's 44 that use one.## 1. Where the frontend lives: inside Mesa, as `src/compiler/air`