Skip to content

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

input, GPU, display: the touch RPMh write takes the lock, surfaces are checked against their memory, the vsync send is under the lock (docs/driver-review/input.md, gpu-display.md)

shimoku7822f30f5016
diff --git a/docs/driver-review/gpu-display.md b/docs/driver-review/gpu-display.md
index c719db0..1238807 100644
--- a/docs/driver-review/gpu-display.md
+++ b/docs/driver-review/gpu-display.md
@@ -196,3 +196,25 @@ Before each submission buffers are cleaned and invalidated (`dc civac`, adreno.c
- `hakios.image.c` (`hakios_image_file`) and the callers outside this group (`HakiosGPUStart/Stop` call sites, `loader_return.c`).
- Userland (`userland/src/eagl.c`, `tools/mesa/hakios-drm`) and the display_probe code (`adreno.c:3568-3703`; it only reads and writes two clock-enable bits).
- Cellular modem code (out of scope).
+
+## Fix status
+
+Edited: drivers/gpu/hakios.gpu.cpp, drivers/gpu/hakios.adreno.c, xnu/files/iokit/Kernel/MiniIOSurface.cpp, MiniIOMobileFramebuffer.cpp (MiniIOSurface.h unchanged). Nothing was compiled or run; every edit was re-read for syntax and types.
+
+- G1 SKIPPED. Command-stream validation, CP_PROTECT and kernel-only mappings are a design and hardware matter (register semantics, privilege bit meaning); not invented here.
+- G2 FIXED. MiniIOMobileFramebuffer.cpp: new `surfaceHolds()` (64-bit arithmetic) is checked in `drawLayer()` for the rows and columns it reads (offset + (rows-1)*stride + columns*4 must be within allocSize) and in `record()` for the whole display; a surface that does not hold them is not read. This covers a small stride, element height above 1 and a wrapped client size, whatever they were created with. MiniIOSurface.cpp `surfaceCreate()`: wrapped surfaces' owner-given IOSurfaceAllocSize must be non-zero and at most 512 MB (same limit as other surfaces); plane width/height are capped at 16384 and a computed plane size is done in 64 bits and refused above 512 MB. NOT changed: creation still accepts a stride below width*bytes-per-element and the element-height row count, because a client with an unusual element definition would be refused; the read-side check makes both memory-safe.
+- G3 FIXED. gpu.cpp `kMsmWaitFence`: a fence ahead of `hakios_gpu_last()` returns kIOReturnBadArgument before `gpuWait()`, so a bad argument can no longer latch `gGPUReported`.
+- G4 FIXED. gpu.cpp `freeBuffer()`: the buffer is taken out of its handle (memory/surface NULL, counters decremented) and the rest works on a local copy, before `gpuWait()` can drop the lock; a second close of the handle now gets BadArgument.
+- G5 FIXED. MiniIOMobileFramebuffer.cpp `drawLayer()`: destination rectangle computed in 64 bits; one beyond +-16M panel pixels, or wholly off the panel, is not drawn (a layer wholly left of the panel previously computed columns far outside the source row); the row loop and the helper threads' split use the on-panel rows (`ry0/ry1` in `RowJob`), while the scaling still uses the original py0/py1 so partly off-panel layers draw exactly as before.
+- G6 FIXED in part. gpu.cpp `show()`: `rowBytes * height` above 256 MB (the buffer maximum) is refused. SKIPPED: per-client and global byte budgets for buffers and surfaces (a policy choice of limits), and the surface table.
+- G7 FIXED. MiniIOSurface.cpp `methodCreate()`: `allocSize` copied to a local before the creator's hold is dropped.
+- G8 FIXED. MiniIOMobileFramebuffer.cpp `vsync()`: the async send is done with `fLock` held (as the report suggests); `registerNotificationPort()` swaps under the same lock, so the old port is only released after the send. The send uses kIOUserNotifyOptionCanDrop; owner to confirm it cannot block.
+- G9 SKIPPED. Ownership and privilege checks on surface IDs are policy, not to be changed here.
+- G10 FIXED. MiniIOMobileFramebuffer.cpp `drawLayer()`: on any failed blend-row allocation every array that was made is freed with the size it was made with and set to NULL before the size is reset to 0.
+- G11 FIXED in part. gpu.cpp `submit()`: the log no longer prints `addresses[0]`/`words[0]` when `count == 0`. NOT done: rejecting a submission with no command buffer; what the ring does with an empty submission (fence only) was not established, so behaviour is unchanged.
+- G12 FIXED. adreno.c `step_gmu_firmware()`: block address + size is computed in 64 bits in both checks (TCM write is refused if the block wraps past 4 GB; memory copy only if the block lies inside a block that was made).
+- G13 FIXED in part. adreno.c `hakios_gpu_address_free()`: a range overlapping a free one is refused with a message, so a double free cannot corrupt the list. SKIPPED: the TLB sync result (timing and a recovery action need hardware knowledge).
+- G14 FIXED. gpu.cpp `millisecondsUntil()`: seconds*1e9 and the nanosecond add use overflow-checked builtins; an overflowing positive time is the 5 s cap, a negative one is "now".
+- G15 SKIPPED. Releasing the global GPU lock while waiting for ring space is a locking-design change.
+- G16 SKIPPED. Cache invalidation after GPU writes depends on the SMMU/bus coherency model; NEEDS-HARDWARE.
+- G17 SKIPPED. Needs to know whether IOKit serialises `clientClose()` against calls in flight; not established.
diff --git a/docs/driver-review/input.md b/docs/driver-review/input.md
index 75475ef..bcf5913 100644
--- a/docs/driver-review/input.md
+++ b/docs/driver-review/input.md
@@ -189,3 +189,18 @@ virtio_input.c:
- Whether the PRAM download can touch persistent flash, and whether the vendor flow's own integrity step exists in the boot ROM.
- The iOS sandbox profile for `/dev/hakios-touch` (F3 impact).
- Build correctness (flags of `kmem_alloc`, `__builtin_arm_dmb` use) and the other drivers in the tree, other than reading the lines named above for `hakios_rsc_lock`.
+
+## Fix status
+
+Edited: drivers/input/hakios.goodix.c, hakios.input.c, hakios.virtio_input.c. Nothing was compiled or run; every edit was re-read for syntax and types.
+
+- F1 FIXED. goodix.c: `rpmh_write()` now takes `hakios_rsc_lock` (extern declared next to the other externs, `<kern/locks.h>` included) before the first TCS 0 access and releases it on the busy return and at the end. Same lock the audio DSP and modem drivers take; the lock is a leaf here (nothing else is taken while it is held), so no ordering problem. The mutex is held across `sleep_us`, exactly as adsp.c does.
+- F2 FIXED (the ignored error). goodix.c: `power_on()` returns bool and `bring_up()` stops with a message before AVDD or any bus work if `io_supply_on()` fails. NOT done: driving AVDD low and clearing the GCC clock votes when a later bring-up step fails. Power-down order and clock-vote semantics are hardware knowledge; SKIPPED for that part.
+- F3 SKIPPED. Mode 0666 of /dev/hakios-touch is policy and was explicitly not to be changed.
+- F4 FIXED. input.c `touch_read()`: if `uiomove` fails, the dequeued record is put back at the head (only if the ring still has room, so it can never overwrite a live slot).
+- F5 FIXED. input.c `touch_read()`: a zero-length read returns 0, a read of 1..23 bytes returns EINVAL instead of blocking and then returning EOF. Reads of 24 bytes or more behave as before.
+- F6 SKIPPED. A checksum of the FocalTech firmware needs a value generated at build time for hakios.focaltech_fw.h (not a file I may edit) and, for an on-chip check, knowledge of the boot ROM protocol that cannot be established from the repository.
+- F7 FIXED. goodix.c `focaltech_contact_update()`: x and y are clamped to `max_report - 1` before the flip, so the mirroring cannot underflow. In range values and the unflipped case are unchanged (an out-of-range value was already ending at 65535).
+- F8 FIXED. virtio_input.c `input_poll()`: `__builtin_arm_dmb(0x9)` (ISHLD) after the `used->idx` check, before the ring entry and the event are read.
+- F9 FIXED. virtio_input.c `input_attach()`: both failure returns now write STATUS_FAILED (0x80, new define) to the device and `kmem_free` the page. The device was never given the queue on those paths.
+- F10 SKIPPED. Which of gpio52/gpio53 is the chip's output is hardware knowledge, and dropping or changing the probe order is a behaviour decision for the owner.
diff --git a/drivers/gpu/hakios.adreno.c b/drivers/gpu/hakios.adreno.c
index c70a36b..3f1f97a 100644
--- a/drivers/gpu/hakios.adreno.c
+++ b/drivers/gpu/hakios.adreno.c
@@ -1807,7 +1807,9 @@ step_gmu_firmware(void)
if (block.size == 0) {
continue;
}
- if (in_tcm(block.address) && in_tcm(block.address + block.size - 1)) {
+ /* the end is worked out in 64 bits: a block whose address and size wrap past 4 GB is not "inside the TCM" and is not written */
+ if (in_tcm(block.address) && (uint64_t)block.address + block.size <= 0x100000000ull &&
+ in_tcm(block.address + block.size - 1)) {
uint32_t registers = block.address < GMU_DTCM_ADDRESS ? GMU_ITCM + block.address - GMU_ITCM_ADDRESS :
GMU_DTCM + block.address - GMU_DTCM_ADDRESS;
@@ -1833,8 +1835,9 @@ step_gmu_firmware(void)
block.size, block.address);
return false;
}
+ /* (64-bit sum: with 32 bits a block that wraps would pass and the copy would run past the block's memory) */
if (block.address >= gmu_blocks[i].address &&
- block.address + block.size <= gmu_blocks[i].address + gmu_blocks[i].size) {
+ (uint64_t)block.address + block.size <= gmu_blocks[i].address + gmu_blocks[i].size) {
memcpy((void *)(gmu_blocks[i].memory + (block.address - gmu_blocks[i].address)), data, block.size);
to_memory(gmu_blocks[i].memory, gmu_blocks[i].size);
printf("adreno: %u: %#x bytes at the GMU's %#x, in memory\n", step, block.size, block.address);
@@ -3319,6 +3322,12 @@ hakios_gpu_address_free(uint64_t address, uint64_t size)
size = (size + 0xffff) & ~0xffffull;
for (at = 0; at < gpu_free_count && gpu_free[at].start < address; at++) {
}
+ /* a range that overlaps one that is free already was given back twice: leave the list as it is, instead of merging it into nonsense */
+ if ((at > 0 && gpu_free[at - 1].start + gpu_free[at - 1].size > address) ||
+ (at < gpu_free_count && address + size > gpu_free[at].start)) {
+ printf("adreno: the addresses %#llx (%#llx bytes) are free already: not given back again\n", address, size);
+ return;
+ }
if (at > 0 && gpu_free[at - 1].start + gpu_free[at - 1].size == address) {
gpu_free[at - 1].size += size;
if (at < gpu_free_count && address + size == gpu_free[at].start) {
diff --git a/drivers/gpu/hakios.gpu.cpp b/drivers/gpu/hakios.gpu.cpp
index 50e902b..8ed7772 100644
--- a/drivers/gpu/hakios.gpu.cpp
+++ b/drivers/gpu/hakios.gpu.cpp
@@ -254,10 +254,17 @@ static uint64_t
millisecondsUntil(const struct msm_timespec * when)
{
uint64_t now;
- int64_t left;
+ int64_t left, end;
absolutetime_to_nanoseconds(mach_continuous_time(), &now);
- left = when->seconds * (int64_t) NSEC_PER_SEC + when->nanoseconds - (int64_t) now;
+ /* the seconds are the caller's: a number too big to be nanoseconds is a time far ahead (or far behind), not a wrapped one */
+ if (__builtin_mul_overflow(when->seconds, (int64_t) NSEC_PER_SEC, &end) || __builtin_add_overflow(end, when->nanoseconds, &end)) {
+ return when->seconds > 0 && when->nanoseconds >= 0 ? GPU_PATIENCE_MS : 0;
+ }
+ if (end <= 0) {
+ return 0; /* (the clock's reading is never negative: this is past) */
+ }
+ left = end - (int64_t) now;
if (left <= 0) {
return 0;
}
@@ -460,8 +467,20 @@ HakiosGPUUserClient::start(IOService * provider)
* written, and must not be somebody else's by then.
*/
void
-HakiosGPUUserClient::freeBuffer(struct gpu_buffer * b)
+HakiosGPUUserClient::freeBuffer(struct gpu_buffer * slot)
{
+ /*
+ * gpuWait() below lets go of the lock: take the buffer out of its handle first and work on a copy, so that
+ * a second close of the same handle (or the client's closing) cannot free the same memory and address twice.
+ */
+ struct gpu_buffer saved = *slot;
+ struct gpu_buffer * b = &saved;
+
+ slot->memory = NULL;
+ slot->surface = NULL;
+ fBufferCount--;
+ fBufferBytes -= b->size;
+
bool busy = hakios_gpu_ready() && b->fence != 0 && !hakios_gpu_done(b->fence);
if (busy && fLeftCount == GPU_LEFT) {
@@ -501,10 +520,6 @@ HakiosGPUUserClient::freeBuffer(struct gpu_buffer * b)
MiniIOSurfaceDrop(b->surface);
}
}
- b->memory = NULL;
- b->surface = NULL;
- fBufferCount--;
- fBufferBytes -= b->size;
}
/* The buffers that were given back early and that the GPU is done with now: freed. With the lock held. */
@@ -983,8 +998,9 @@ HakiosGPUUserClient::submit(struct msm_gem_submit * request)
hakios_gpu_report("the ring is full");
}
if (fSubmits++ < 4) {
+ /* (addresses and words are only set up to `count`: with no command buffer among the commands, the first would be stack garbage) */
IOLog("HakiosGPU: submission %u: %u command buffers, the first at %#llx, %u words\n", request->fence, count,
- addresses[0], words[0]);
+ count != 0 ? addresses[0] : 0ull, count != 0 ? words[0] : 0u);
}
/* every five seconds in which something was submitted: how much, and what it cost here */
gGPUStat.submissions++;
@@ -1042,6 +1058,10 @@ HakiosGPUUserClient::show(const struct hakios_show * request)
request->rowBytes < 4 * request->width) {
return kIOReturnBadArgument;
}
+ /* rowBytes is the caller's own number: without a limit it asks for terabytes of wired memory (no more than a buffer may be) */
+ if (size > GPU_BUFFER_MAX) {
+ return kIOReturnBadArgument;
+ }
pixels = (uint8_t *) IOMallocData(size);
if (pixels == NULL) {
return kIOReturnNoMemory;
@@ -1139,6 +1159,12 @@ HakiosGPUUserClient::externalMethod(uint32_t selector, IOExternalMethodArguments
case kMsmWaitFence:
waitBegan = mach_absolute_time();
IOLockLock(gGPULock);
+ /* a fence that was never issued is never done: refuse it, so that waiting for it cannot make gpuWait() declare the GPU dead for every client */
+ if ((int32_t)(r.waitFence.fence - hakios_gpu_last()) > 0) {
+ IOLockUnlock(gGPULock);
+ ret = kIOReturnBadArgument;
+ break;
+ }
patience = millisecondsUntil(&r.waitFence.timeout);
if (!gpuWait(r.waitFence.fence, patience)) {
ret = kIOReturnTimeout;
diff --git a/drivers/input/hakios.goodix.c b/drivers/input/hakios.goodix.c
index 867b463..69a6f1b 100644
--- a/drivers/input/hakios.goodix.c
+++ b/drivers/input/hakios.goodix.c
@@ -29,6 +29,7 @@
* own device tree.
*/
#include <kern/clock.h>
+#include <kern/locks.h>
#include <kern/thread.h>
#include <libkern/libkern.h>
#include <mach/mach_types.h>
@@ -42,6 +43,9 @@ extern vm_offset_t ml_io_map(vm_offset_t phys_addr, vm_size_t size);
extern vm_offset_t ml_io_map_wcomb(vm_offset_t phys_addr, vm_size_t size);
extern kern_return_t thread_terminate(thread_t thread);
+/* RPMh's sequencer (TCS 0 of the RSC): the audio DSP's and the modem's drivers write the same slots, so every request goes through this lock */
+extern lck_mtx_t hakios_rsc_lock;
+
static vm_offset_t tlmm, gcc, se, rsc, cmd_db;
static inline uint32_t
@@ -203,13 +207,17 @@ rpmh_wait(uint32_t reg, uint32_t mask, uint32_t value, const char *what)
static bool
rpmh_write(const uint32_t *address, const uint32_t *data, unsigned count)
{
- uint32_t irq_enable = rd(TCS(TCS_IRQ_ENABLE));
+ uint32_t irq_enable;
bool done;
+ /* the other drivers that write this slot set take the same lock, so that two requests never mix their address and data words */
+ lck_mtx_lock(&hakios_rsc_lock);
+ irq_enable = rd(TCS(TCS_IRQ_ENABLE));
/* the slot set must be idle, and switched off the way the hardware wants it: trigger, then enable */
for (int i = 0; rd(TCS(TCS_STATUS)) == 0; i++) {
if (i > 2000) {
printf("goodix: RPMh: the command slots stay busy\n");
+ lck_mtx_unlock(&hakios_rsc_lock);
return false;
}
sleep_us(100);
@@ -235,6 +243,7 @@ rpmh_write(const uint32_t *address, const uint32_t *data, unsigned count)
wr(TCS(TCS_WAIT_FOR_COMPLETION), 0);
wr(TCS(TCS_IRQ_CLEAR), 1);
wr(TCS(TCS_IRQ_ENABLE), irq_enable);
+ lck_mtx_unlock(&hakios_rsc_lock);
return done;
}
@@ -1218,6 +1227,13 @@ focaltech_contact_update(uint32_t id, uint32_t event, uint32_t x, uint32_t y,
return;
}
seen.touches++;
+ /* the chip's 15-bit values can exceed the panel: keep them inside it, so the mirroring below cannot underflow */
+ if (x > max_report_x - 1) {
+ x = max_report_x - 1;
+ }
+ if (y > max_report_y - 1) {
+ y = max_report_y - 1;
+ }
switch (flip) {
case 1: x = max_report_x - 1 - x; break;
case 2: y = max_report_y - 1 - y; break;
@@ -1344,16 +1360,20 @@ show_state(const char *when)
gpio_get(68), gpio_get(96));
}
-/* Power the chip up from nothing, in the vendor driver's order. */
-static void
+/* Power the chip up from nothing, in the vendor driver's order. False if the I/O supply could not be switched on. */
+static bool
power_on(void)
{
gpio_output(PIN_RESET, false, PULL_UP, 8);
gpio_configure(PIN_INT, 0, PULL_UP, 8, false);
- io_supply_on();
+ /* without its I/O rail the chip must not get the analog supply or be driven: stop before AVDD */
+ if (!io_supply_on()) {
+ return false;
+ }
sleep_ms(3);
gpio_output(PIN_AVDD, true, PULL_NONE, 2);
sleep_ms(15);
+ return true;
}
static void
@@ -1380,7 +1400,10 @@ bring_up(void)
touch_chip = touch_chip_detect();
printf("goodix: ID gpio96=%u gpio68=%u: selected %s\n", gpio_get(PIN_GOODIX_ID),
gpio_get(PIN_FOCALTECH_ID), chip_name());
- power_on();
+ if (!power_on()) {
+ printf("goodix: the I/O supply is not on; the chip is left alone\n");
+ return false;
+ }
if (clocks_on()) {
printf("goodix: serial engine: firmware %#x, interface %#x, parameters %#x, source clock %u Hz (select %u)\n",
diff --git a/drivers/input/hakios.input.c b/drivers/input/hakios.input.c
index 5f8aeb8..8e5e556 100644
--- a/drivers/input/hakios.input.c
+++ b/drivers/input/hakios.input.c
@@ -158,6 +158,13 @@ touch_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
struct hakios_touch t;
int error = 0, moved = 0;
+ /* a buffer smaller than one record can never be served: say so, instead of blocking for data and then returning 0 (end of file) */
+ if (uio_resid(uio) == 0) {
+ return 0;
+ }
+ if (uio_resid(uio) < (user_ssize_t)sizeof(t)) {
+ return EINVAL;
+ }
lck_mtx_lock(&touch_lock);
while (touch_head == touch_tail) {
error = msleep(&touch_queue, &touch_lock, PZERO | PCATCH, "hakios-touch", NULL);
@@ -172,6 +179,10 @@ touch_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
error = uiomove((caddr_t)&t, sizeof(t), uio);
lck_mtx_lock(&touch_lock);
if (error) {
+ /* the copy-out failed: put the record back at the head so a lost DOWN or UP does not leave a finger stuck (only if the ring still has room for it) */
+ if (touch_tail - touch_head < TOUCH_QUEUE) {
+ touch_queue[--touch_head % TOUCH_QUEUE] = t;
+ }
break;
}
moved++;
diff --git a/drivers/input/hakios.virtio_input.c b/drivers/input/hakios.virtio_input.c
index 66abf66..5cb0bb7 100644
--- a/drivers/input/hakios.virtio_input.c
+++ b/drivers/input/hakios.virtio_input.c
@@ -51,6 +51,7 @@ extern uint64_t kvtophys(vm_offset_t va); /* pmap_paddr_t */
#define STATUS_DRIVER 2
#define STATUS_DRIVER_OK 4
#define STATUS_FEATURES_OK 8
+#define STATUS_FAILED 0x80
#define QUEUE_SIZE 64
#define DESC_F_WRITE 2
@@ -258,6 +259,8 @@ input_poll(__unused thread_call_param_t p0, __unused thread_call_param_t p1)
bool returned = false;
while (d->seen != *(volatile uint16_t *)&d->used->idx) {
+ /* the ring entry and the event must be read after the index that announced them (arm64 does not order those loads by itself) */
+ __builtin_arm_dmb(0x9); /* ISHLD */
uint32_t id = d->used->ring[d->seen++ % QUEUE_SIZE].id % QUEUE_SIZE;
input_event(d, &d->events[id]);
@@ -302,6 +305,9 @@ input_attach(struct input_device *d, int slot)
reg_write(d, VIRTIO_STATUS, STATUS_ACKNOWLEDGE | STATUS_DRIVER | STATUS_FEATURES_OK);
if (!(reg_read(d, VIRTIO_STATUS) & STATUS_FEATURES_OK)) {
printf("virtio-input: slot %d: features refused\n", slot);
+ /* the device never got this page: give it back and tell the device the driver gave up, so the failing slot costs nothing */
+ reg_write(d, VIRTIO_STATUS, STATUS_FAILED);
+ kmem_free(kernel_map, page, PAGE_SIZE);
return false;
}
@@ -309,6 +315,9 @@ input_attach(struct input_device *d, int slot)
reg_write(d, VIRTIO_QUEUE_SEL, 0);
if (reg_read(d, VIRTIO_QUEUE_NUM_MAX) < QUEUE_SIZE) {
printf("virtio-input: slot %d: event queue of %u\n", slot, reg_read(d, VIRTIO_QUEUE_NUM_MAX));
+ /* same as above: the queue was never given to the device */
+ reg_write(d, VIRTIO_STATUS, STATUS_FAILED);
+ kmem_free(kernel_map, page, PAGE_SIZE);
return false;
}
for (int i = 0; i < QUEUE_SIZE; i++) {
diff --git a/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp b/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp
index 5f6bf22..46f4c59 100644
--- a/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp
+++ b/xnu/files/iokit/Kernel/MiniIOMobileFramebuffer.cpp
@@ -481,6 +481,20 @@ IOMobileFramebuffer::start(IOService * provider)
return true;
}
+/*
+ * Are the first `rows` rows of a surface, `columns` 32-bit pixels of each, inside the memory it
+ * has? Its stride, element height and size come from whoever made it, so they are checked here,
+ * before anything reads the rows: in 64 bits, so that nothing wraps.
+ */
+static bool
+surfaceHolds(const MiniIOSurface * s, uint64_t rows, uint64_t columns)
+{
+ if (rows == 0 || columns == 0) {
+ return true;
+ }
+ return (uint64_t) s->info.offset + (rows - 1) * s->info.bytesPerRow + columns * 4 <= s->info.allocSize;
+}
+
/* Keep a small copy of the frame that is about to be shown (the surface is the whole screen). */
void
IOMobileFramebuffer::record(MiniIOSurface * s)
@@ -496,6 +510,10 @@ IOMobileFramebuffer::record(MiniIOSurface * s)
if (fRecBase == NULL || fRecDone || s->info.width < fWidth || s->info.height < fHeight) {
return;
}
+ /* a surface whose rows do not fit in its memory is not read */
+ if (!surfaceHolds(s, fHeight, fWidth)) {
+ return;
+ }
absolutetime_to_nanoseconds(now - fFirstFrame, &since_first);
if (since_first < (uint64_t) gRecStart * NSEC_PER_SEC) {
return;
@@ -589,10 +607,14 @@ sample_at(int64_t i, int64_t in, int64_t out)
* scaling a whole frame takes one processor some 9 ms, so with boot-arg fb_threads=N it is shared
* out between N of them, each with rows of its own and a row of blending of its own.
*/
+/* A layer's rectangle on the panel, in pixels, further out than this is not drawn (the panel is a few thousand wide). */
+#define DST_LIMIT (1 << 24)
+
struct RowJob {
const uint8_t * pixels;
uint32_t stride;
int32_t sx, sy, sw, sh, px0, py0, px1, py1, cx0, cx1;
+ int32_t ry0, ry1; /* the rows of the rectangle that are on the panel: all that is looked at, whatever its size */
bool filter, unscaled;
};
@@ -697,9 +719,9 @@ IOMobileFramebuffer::helperMain(void * self, wait_result_t result __unused)
job = fb->fHelperJob;
IOLockUnlock(fb->fHelperLock);
/* share index + 1 of fThreads (the presenter takes share 0) */
- rows = job->py1 - job->py0;
- y0 = job->py0 + (int32_t) ((int64_t) rows * (index + 1) / fb->fThreads);
- y1 = job->py0 + (int32_t) ((int64_t) rows * (index + 2) / fb->fThreads);
+ rows = job->ry1 - job->ry0;
+ y0 = job->ry0 + (int32_t) ((int64_t) rows * (index + 1) / fb->fThreads);
+ y1 = job->ry0 + (int32_t) ((int64_t) rows * (index + 2) / fb->fThreads);
blend = fb->fHelperBlend[index];
fb->drawRows(job, y0, y1, blend);
IOLockLock(fb->fHelperLock);
@@ -738,14 +760,33 @@ IOMobileFramebuffer::drawLayer(MiniIOSurface * s, const int32_t * src, const int
if (sx < 0 || sy < 0 || (uint32_t) (sx + sw) > s->info.width || (uint32_t) (sy + sh) > s->info.height) {
return;
}
- /* the rectangle on the panel */
- px0 = (int32_t) ((int64_t) dx * fPanelWidth / fWidth);
- py0 = (int32_t) ((int64_t) dy * fPanelHeight / fHeight);
- px1 = (int32_t) ((int64_t) (dx + dw) * fPanelWidth / fWidth);
- py1 = (int32_t) ((int64_t) (dy + dh) * fPanelHeight / fHeight);
+ /* the rows and columns named must be in the surface's memory, whatever stride and size it was made with */
+ if (!surfaceHolds(s, (uint32_t) (sy + sh), (uint32_t) (sx + sw))) {
+ return;
+ }
+ /*
+ * the rectangle on the panel, worked out in 64 bits: the request's numbers are the caller's, and a
+ * rectangle of billions of rows is not drawn (nor looked at row by row)
+ */
+ int64_t qx0 = (int64_t) dx * fPanelWidth / fWidth;
+ int64_t qy0 = (int64_t) dy * fPanelHeight / fHeight;
+ int64_t qx1 = ((int64_t) dx + dw) * fPanelWidth / fWidth;
+ int64_t qy1 = ((int64_t) dy + dh) * fPanelHeight / fHeight;
+
+ if (qx0 < -DST_LIMIT || qy0 < -DST_LIMIT || qx1 > DST_LIMIT || qy1 > DST_LIMIT) {
+ return;
+ }
+ px0 = (int32_t) qx0;
+ py0 = (int32_t) qy0;
+ px1 = (int32_t) qx1;
+ py1 = (int32_t) qy1;
if (px1 <= px0 || py1 <= py0) {
return;
}
+ /* nothing of it on the panel: nothing to draw (and the columns below are only worked out for ones that are) */
+ if (px1 <= 0 || px0 >= (int32_t) fPanelWidth || py1 <= 0 || py0 >= (int32_t) fPanelHeight) {
+ return;
+ }
/* (blending, a row of the picture mixed from two: room for it, for every one who draws rows) */
if (filter && fBlendRowSize < (uint32_t) sw) {
if (fBlendRow != NULL) {
@@ -758,14 +799,24 @@ IOMobileFramebuffer::drawLayer(MiniIOSurface * s, const int32_t * src, const int
}
fBlendRowSize = (uint32_t) sw;
fBlendRow = IONew(uint32_t, fBlendRowSize);
+ bool allocated = fBlendRow != NULL;
+
for (unsigned i = 0; i + 1 < fThreads; i++) {
fHelperBlend[i] = IONew(uint32_t, fBlendRowSize);
- if (fHelperBlend[i] == NULL && fBlendRow != NULL) {
+ allocated = allocated && fHelperBlend[i] != NULL;
+ }
+ if (!allocated) {
+ /* every array that did get made is given back at the size it was made with, so that no later growth frees one with the wrong size */
+ if (fBlendRow != NULL) {
IODelete(fBlendRow, uint32_t, fBlendRowSize);
fBlendRow = NULL;
}
- }
- if (fBlendRow == NULL) {
+ for (unsigned i = 0; i + 1 < fThreads; i++) {
+ if (fHelperBlend[i] != NULL) {
+ IODelete(fHelperBlend[i], uint32_t, fBlendRowSize);
+ fHelperBlend[i] = NULL;
+ }
+ }
fBlendRowSize = 0;
}
}
@@ -795,10 +846,12 @@ IOMobileFramebuffer::drawLayer(MiniIOSurface * s, const int32_t * src, const int
job.py1 = py1;
job.cx0 = cx0;
job.cx1 = cx1;
+ job.ry0 = py0 < 0 ? 0 : py0;
+ job.ry1 = py1 > (int32_t) fPanelHeight ? (int32_t) fPanelHeight : py1;
job.filter = filter;
job.unscaled = px1 - px0 == sw && py1 - py0 == sh && px0 >= 0 && (uint32_t) px1 <= fPanelWidth;
if (fThreads <= 1 || fDiff || fHelpersStarted + 1 < fThreads) {
- drawRows(&job, py0, py1, fBlendRow);
+ drawRows(&job, job.ry0, job.ry1, fBlendRow);
return;
}
/* shared out: the helpers take their rows, this thread the first share, then waits for them */
@@ -808,7 +861,7 @@ IOMobileFramebuffer::drawLayer(MiniIOSurface * s, const int32_t * src, const int
fHelperGeneration++;
IOLockWakeup(fHelperLock, &fHelperGeneration, false);
IOLockUnlock(fHelperLock);
- drawRows(&job, py0, py0 + (int32_t) ((int64_t) (py1 - py0) / fThreads), fBlendRow);
+ drawRows(&job, job.ry0, job.ry0 + (int32_t) ((int64_t) (job.ry1 - job.ry0) / fThreads), fBlendRow);
IOLockLock(fHelperLock);
while (fHelpersBusy != 0) {
IOLockSleep(fHelperLock, &fHelpersBusy, THREAD_UNINT);
@@ -1251,7 +1304,6 @@ IOMobileFramebufferUserClient::vsync(void)
OSAsyncReference64 ref;
io_user_reference_t args[6];
uint64_t now;
- bool send = false;
IOLockLock(fLock);
now = fVSyncTime;
@@ -1266,13 +1318,15 @@ IOMobileFramebufferUserClient::vsync(void)
args[3] = now >> 32;
args[4] = (uint32_t) fVSyncCount;
args[5] = fVSyncCount >> 32;
- send = true;
armVSync();
- }
- IOLockUnlock(fLock);
- if (send) {
+ /*
+ * sent with the lock held: registerNotificationPort() swaps the port under this lock and
+ * only then lets go of the old one, so the port in `ref` cannot be released while it is sent to
+ * (the send does not wait: a full queue drops the message)
+ */
sendAsyncResult64WithOptions(ref, kIOReturnSuccess, args, 6, kIOUserNotifyOptionCanDrop);
}
+ IOLockUnlock(fLock);
}
IOReturn
diff --git a/xnu/files/iokit/Kernel/MiniIOSurface.cpp b/xnu/files/iokit/Kernel/MiniIOSurface.cpp
index 5523e85..63967e1 100644
--- a/xnu/files/iokit/Kernel/MiniIOSurface.cpp
+++ b/xnu/files/iokit/Kernel/MiniIOSurface.cpp
@@ -527,6 +527,11 @@ surfaceCreate(OSDictionary * props, IOReturn * error)
}
p->width = dictNumber(pd, "IOSurfacePlaneWidth", width);
p->height = dictNumber(pd, "IOSurfacePlaneHeight", height);
+ /* a plane is no bigger than a surface may be: the sizes below are not allowed to wrap */
+ if (p->width > MINISURF_MAX_DIMENSION || p->height > MINISURF_MAX_DIMENSION) {
+ s->release();
+ return NULL;
+ }
p->bytesPerElement = (uint16_t) dictNumber(pd, "IOSurfacePlaneBytesPerElement", bpe ? bpe : 1);
p->elementWidth = (uint8_t) pew;
p->elementHeight = (uint8_t) peh;
@@ -536,7 +541,14 @@ surfaceCreate(OSDictionary * props, IOReturn * error)
}
p->size = dictNumber(pd, "IOSurfacePlaneSize", 0);
if (p->size == 0) {
- p->size = p->bytesPerRow * ((p->height + peh - 1) / peh);
+ /* (in 64 bits: a stride of the caller's times the rows can pass 32) */
+ uint64_t planeBytes = (uint64_t) p->bytesPerRow * ((p->height + peh - 1) / peh);
+
+ if (planeBytes > MINISURF_MAX_ALLOC) {
+ s->release();
+ return NULL;
+ }
+ p->size = (uint32_t) planeBytes;
}
p->offset = dictNumber(pd, "IOSurfacePlaneOffset", dictNumber(pd, "IOSurfacePlaneBase", (uint32_t) needed));
p->extendedPixels[0] = dictNumber(pd, "IOSurfacePlaneExtendedPixelsOnLeft", 0);
@@ -610,6 +622,11 @@ surfaceCreate(OSDictionary * props, IOReturn * error)
} else {
/* (the memory is as big as its owner says, and not a byte of ours is added to it) */
alloc = dictNumber(props, "IOSurfaceAllocSize", (uint32_t) needed);
+ /* the owner's number replaced the checked one above: it gets the same limits, so that a size of 0 or of gigabytes is not taken */
+ if (alloc == 0 || alloc > MINISURF_MAX_ALLOC) {
+ s->release();
+ return NULL;
+ }
}
s->memory = surfaceMemory((vm_size_t) PAGE_SIZE + (wrapped ? 0 : alloc));
@@ -1197,6 +1214,9 @@ IOSurfaceRootUserClient::methodCreate(IOExternalMethodArguments * args)
}
uint64_t mapped_at = mach_absolute_time();
+ /* the surface can be gone once the creator's hold is dropped (when the reply did not fit): its size is read now, not after */
+ uint32_t createdBytes = s->info.allocSize;
+
IOLockLock(gSurfaceLock);
ref = addRef(s);
ret = ref ? returnSurface(ref, false, args) : kIOReturnNoMemory;
@@ -1208,7 +1228,7 @@ IOSurfaceRootUserClient::methodCreate(IOExternalMethodArguments * args)
mapped_at = mach_absolute_time() - mapped_at;
OSAddAtomic64((SInt64) (mach_absolute_time() - began), (SInt64 *) &gSurfaceMaking);
OSIncrementAtomic(&gSurfacesMade);
- if (s->info.allocSize >= SURFACE_KEEP_FROM) {
+ if (createdBytes >= SURFACE_KEEP_FROM) {
/* the big ones (the window server's of the whole display): where the time to make one goes, every 5 s */
static uint64_t since, all, making, mapping;
static uint32_t count;