//
//  krw.m
//  darksword-kexploit-fun
//
//  Created by seo on 3/24/26.
//

#ifndef krw_m
#define krw_m

#include <stdint.h>
#include <stdbool.h>

// BUG.1 step 3: the block writer's bound check + block loop. Dependency-free
// (stdint/stdbool only) so tests/krw_zone_write_host_test.c can compile it on
// the host - see kexploit/krw_zone_write.c.
#include "krw_zone_write.h"

#ifdef __cplusplus
extern "C" {
#endif

#import "offsets.h"

// 1.3.0 (KexProof merge): the Dopamine shim (Sources/shim/primitives.c)
// exports the same typed-accessor names with different signatures, so this
// engine's public r/w API is prefixed ds_. The remap lives HERE so every
// engine TU that includes this header (krw.m, kutils.m, offsets.m,
// kexploit_opa334.m) renames its declarations and call sites automatically.
// A TU that also includes the shim headers (the ClearSword.m adapter) must
// include the shim FIRST, then this header, then #undef these macros before
// touching gPrimitives.
#define kreadbuf        ds_kreadbuf
#define kwritebuf       ds_kwritebuf
#define kread16         ds_kread16
#define kread32         ds_kread32
#define kread64         ds_kread64
#define kread8          ds_kread8
#define kwrite8         ds_kwrite8
#define kwrite16        ds_kwrite16
#define kwrite32        ds_kwrite32
#define kwrite64        ds_kwrite64
#define kread_ptr       ds_kread_ptr
#define kread_smrptr    ds_kread_smrptr

#define BIT(b)           (1ULL << (b))
#define ONES(x)          (BIT((x))-1)
#define PTR_MASK         ONES(64-t1sz_boot)
#define PAC_MASK         (~PTR_MASK)
#define SIGN(p)          ((p) & BIT(55))
#define UNSIGN_PTR(p)    (SIGN(p) ? ((p) | PAC_MASK) : ((p) & ~PAC_MASK))

bool is_kaddr_valid(uint64_t addr);
void kreadbuf(uint64_t addr, void *buf, uint64_t len);
void kwritebuf(uint64_t addr, const void *buf, uint64_t len);
uint16_t kread16(uint64_t kaddr);
uint32_t kread32(uint64_t kaddr);
uint64_t kread64(uint64_t kaddr);
void kwrite8(uint64_t kaddr, uint8_t val);
void kwrite16(uint64_t kaddr, uint16_t val);
void kwrite32(uint64_t kaddr, uint32_t val);
void kwrite64(uint64_t kaddr, uint64_t val);
#ifdef DEBUG
void khexdump(uint64_t addr, size_t size);
#endif
uint64_t kread_ptr(uint64_t va);
uint64_t kread_smrptr(uint64_t va);

// The 32-byte block writer. BUG.1 step 3b (2026-09-11 SE zone-bound panic):
// NOTHING is written unless the caller declares the allocation that contains
// the target - a block is provably safe only inside that allocation, and the
// SE's 32-byte block at +0x50 of a 96-byte kalloc.96 object ran past its end
// and panicked the phone (zalloc.c:1322). A call with no declaration (or with
// one that does not contain the target) is refused with a log line and returns
// a KRW_ZONE_REFUSE_* verdict; callers should check it.
//
// Declare the window with kwrite_zone_element_declared() (by value - preferred)
// or, for the process-wide window, kwrite_zone_element_set_object() before the
// call and kwrite_zone_element_clear_object() after it.
krw_zone_verdict kwrite_zone_element(uint64_t dst, const void *src, uint64_t len);

// BUG.1 step 3: a caller that knows the enclosing allocation of a target can
// declare it here, and the writer then refuses any 32-byte block that is not
// provably inside [base, base+size) instead of running past the object end. The
// declaration only applies to destinations it actually contains, so a stale one
// can never authorise a foreign write. Call kwrite_zone_element_clear_object()
// when the object is no longer the target.
void kwrite_zone_element_set_object(uint64_t base, uint64_t size);
void kwrite_zone_element_clear_object(void);

// Same writer, object passed by value: prefer this one. It cannot leave a
// declaration behind for a later, unrelated write (the globals above are
// process-wide, and the RemoteCall shmem path shares this primitive across
// kernel client threads). A window that does not contain dst is ignored, so the
// call is refused like any other undeclared write.
krw_zone_verdict kwrite_zone_element_declared(uint64_t dst, const void *src, uint64_t len,
                                              uint64_t objBase, uint64_t objSize);

// BUG.1 step 3b: the qword write (what early_kwrite64() does, with the bound
// check early_kwrite64() does not have) - read-patch-write of the 32-byte
// ALIGNED block containing dst, refused unless that block is provably inside
// the declared window. Used by the staged write probe, which had no clamped
// path at all: its restore wrote the saved icmp6filt values back through
// early_kwrite64, i.e. an unclamped 32-byte RMW at an address nothing had
// verified (0.13's BUG.7).
krw_zone_verdict kwrite_zone_element_qword(uint64_t dst, uint64_t value,
                                           uint64_t objBase, uint64_t objSize);

#ifdef __cplusplus
}
#endif

#endif /* krw_m */
