interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

[Codex Security] Elementwise tail chunks overwrite past caller buffers

open 1a4e92a7ae460d2f #2 · opened 2026-11-15 17:10 UTC

Imported from GitHub drakestapleton/CUDApp#2, by drakestapleton

Base ce03c88fb13b7eec798efa9a2953f6e3ed5c72fd · Head 12076a1f8b56a6833d93a502c7dd9455e710ec38

drakestapleton wrote on 2026-11-15 17:10 UTC

Each elementwise launch now stages only its validated current chunk.

  • run_1d derives bytes from rec[0] after rejecting zero, oversized, or non-representable counts.
  • The validated extent controls both input staging and output copy-back, closing the stale full-chunk read and write path.
  • Full chunks, one-element tails, 128-element tails, and existing public error semantics remain supported.
  • Source assertions cover malformed records and canary-protected EX2 and shared-exchange boundary cases.

Static Verification Checks

  • Tail extent derivation (Passed) — omega_gpu_elementwise_api.c ew_chunk_1d derives bytes from each validated record count, closing stale tail copies.
  • Caller boundary coverage (Passed) — omega_gpu_elementwise_api.c run_1d routes swiglu, ex2, xchg, and reconvergence buffers through the guarded extent.
  • Boundary regression assertions (Passed) — tests/api_kgen_ew_diff_test.c chunk_partition accepts valid tails while rejecting oversized records without exposing partial extents.
  • Guarded caller buffers (Passed) — tests/gpu_elementwise_test.c asserts EX2 and shared-exchange guards remain intact across short-tail launches.
  • Finding: acf_075083989e42bc7dde7979531c42a267

  • Patch: arp_a7f6d87cfe6632a1aaa742b57b1b25a5
  • Base commit: ce03c88fb13b7eec798efa9a2953f6e3ed5c72fd
  • Patch SHA-256: 8bed58559dcc62e3300e291b541c9f671e7794b4839197222e4cc3a8d11d6eaf

Apply it

git checkout -b patch-1a4e92a7ae460d2f ce03c88fb13b7eec798efa9a2953f6e3ed5c72fd
curl -fsSL https://interplane.io/drakestapleton/CUDApp/patches/1a4e92a7ae460d2f.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
drakestapleton/CUDApp
2
12076a1f8b56a6833d93a502c7dd9455e710ec38
{"kind":"github-import","owner":"drakestapleton","repo":"CUDApp","number":2,"url":"https://github.com/drakestapleton/CUDApp/pull/2","export_sha256":"47b9e2c453935cee8da188bd59c976ababe9290e5e88e31eaf77f752aade7d4c","imported_utc":"2026-11-18T16:39:25Z"}

Changes

diff --git a/src/omega_gpu_elementwise_api.c b/src/omega_gpu_elementwise_api.c
index d825a8d0..0e59a5c0 100644
--- a/src/omega_gpu_elementwise_api.c
+++ b/src/omega_gpu_elementwise_api.c
@@ -1061,6 +1061,18 @@ static int begin(OmegaGpuEwOp op, Slot **ks, OmegaGpuEwInfo *info) {
     return OMEGA_GPU_EW_OK;
 }
 
+/* Turn the current record count into the only extent run_1d may stage.  Keep
+ * the outputs unchanged on refusal so no caller can reuse a partial extent. */
+static int ew_chunk_1d(uint32_t remaining, uint64_t record_count,
+                       uint32_t *count, size_t *bytes) {
+    if (record_count == 0 || record_count > remaining ||
+        record_count > SIZE_MAX / sizeof(uint32_t))
+        return OMEGA_GPU_EW_BAD_ARGS;
+    *count = (uint32_t)record_count;
+    *bytes = (size_t)record_count * sizeof(uint32_t);
+    return OMEGA_GPU_EW_OK;
+}
+
 int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight,
                           float eps, float *out, OmegaGpuEwInfo *info) {
     uint64_t t0 = now_ns();
@@ -1126,11 +1138,16 @@ static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, voi
     if (rc != OMEGA_GPU_EW_OK) return rc;
     for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; ) {
         uint64_t rec[8];
-        if (av_ew_rec(0, n - i0, 0, 0, rec)) { rc = OMEGA_GPU_EW_BAD_ARGS; break; }
-        uint32_t ni = (uint32_t)rec[0];
+        uint32_t remaining = n - i0;
+        if (av_ew_rec(0, remaining, 0, 0, rec)) { rc = OMEGA_GPU_EW_BAD_ARGS; break; }
+        uint32_t ni;
+        size_t bytes;
+        if (ew_chunk_1d(remaining, rec[0], &ni, &bytes) != OMEGA_GPU_EW_OK) {
+            rc = OMEGA_GPU_EW_BAD_ARGS;
+            break;
+        }
         uint32_t grid = (uint32_t)rec[1];
         uint32_t params[4] = { ni, 0, 0, 0 };
-        size_t bytes=p[10];
         rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes,
                      (uint8_t *)c + (size_t)i0 * 4, bytes, (uint32_t)rec[6], grid, params, info);
         i0 += ni;
diff --git a/tests/api_kgen_ew_diff_test.c b/tests/api_kgen_ew_diff_test.c
index 46311c76..f0bfc717 100644
--- a/tests/api_kgen_ew_diff_test.c
+++ b/tests/api_kgen_ew_diff_test.c
@@ -17,6 +17,39 @@ static int cubin_same(const uint8_t *a, size_t na, const uint8_t *b, size_t nb)
     int same=ra==0 && rb==0 && nca==ncb && memcmp(ca,cb,nca)==0;
     omega_cubin_free(ca);omega_cubin_free(cb);return same;
 }
+static int chunk_partition(void) {
+    struct {
+        uint32_t remaining;
+        uint64_t record_count;
+        int rc;
+        uint32_t count;
+        size_t bytes;
+    } cases[] = {
+        { 131073, 131072, OMEGA_GPU_EW_OK, 131072, 524288 },
+        { 1, 1, OMEGA_GPU_EW_OK, 1, 4 },
+        { 128, 128, OMEGA_GPU_EW_OK, 128, 512 },
+        { 1, 0, OMEGA_GPU_EW_BAD_ARGS, 0, 0 },
+        { 1, 2, OMEGA_GPU_EW_BAD_ARGS, 0, 0 },
+        { UINT32_MAX, UINT64_MAX, OMEGA_GPU_EW_BAD_ARGS, 0, 0 },
+    };
+    unsigned accepted=0,refused=0;
+    for(size_t i=0;i<sizeof cases/sizeof *cases;i++) {
+        uint32_t count=UINT32_C(0xa5a5a5a5);
+        size_t bytes=SIZE_MAX;
+        int rc=ew_chunk_1d(cases[i].remaining,cases[i].record_count,&count,&bytes);
+        if(rc!=cases[i].rc || (rc==OMEGA_GPU_EW_OK &&
+           (count!=cases[i].count || bytes!=cases[i].bytes)) ||
+           (rc!=OMEGA_GPU_EW_OK &&
+           (count!=UINT32_C(0xa5a5a5a5) || bytes!=SIZE_MAX))) {
+            fprintf(stderr,"FAIL chunk remaining=%u record=%llu rc=%d count=%u bytes=%zu\n",
+              cases[i].remaining,(unsigned long long)cases[i].record_count,rc,count,bytes);
+            return 1;
+        }
+        if(rc==OMEGA_GPU_EW_OK)accepted++;else refused++;
+    }
+    printf("PASS API EW chunk extents: accepted=%u refused=%u\n",accepted,refused);
+    return 0;
+}
 static int shape_partition(void) {
     /* om/components/api_ew_plan.om: zero admission; even/128 divisibility;
      * 1024-row and 131072-element clamps; 2048/16384 dimension caps;
@@ -46,6 +79,7 @@ int main(void) {
     uint8_t *text=malloc(BW_MAX_IR_INSNS*16u);
     unsigned accepted=0,refused=0;
     if(!c||!om||!text)return 2;
+    if(chunk_partition()!=0)return 1;
     if(shape_partition()!=0)return 1;
     /* Shape is deliberately absent: EW IR depends only on op, mutant and
      * test_path. n/dim/epsilon/sieve limits reside in launch constant memory.
diff --git a/tests/gpu_elementwise_test.c b/tests/gpu_elementwise_test.c
index 59b3c5fd..f3408abe 100644
--- a/tests/gpu_elementwise_test.c
+++ b/tests/gpu_elementwise_test.c
@@ -83,6 +83,18 @@ static size_t compare(const float *got, const float *want, size_t n, double rel,
     return bad;
 }
 
+#define EW_GUARD_WORD UINT32_C(0xa55ac33c)
+static void guard_fill(void *p, size_t words) {
+    uint32_t *u = p;
+    for (size_t i = 0; i < words; i++) u[i] = EW_GUARD_WORD;
+}
+static size_t guard_bad(const void *p, size_t words) {
+    const uint32_t *u = p;
+    size_t bad = 0;
+    for (size_t i = 0; i < words; i++) if (u[i] != EW_GUARD_WORD) bad++;
+    return bad;
+}
+
 /* ------------------------------------------------------------- host only */
 static int sm121dis_check(OmegaGpuEwOp op, const char *name, const char *const *must, int n_must) {
     OmegaBlackwellKernel k; memset(&k, 0, sizeof k);
@@ -112,6 +124,11 @@ static int sm121dis_check(OmegaGpuEwOp op, const char *name, const char *const *
 }
 
 static void host_only(void) {
+    uint32_t guard_control[4];
+    guard_fill(guard_control, 4);
+    CHECK(guard_bad(guard_control, 4) == 0, "guard control accepts intact words");
+    guard_control[2] ^= 1;
+    CHECK(guard_bad(guard_control, 4) == 1, "guard negative control detects one overwritten word");
     CHECK(omega_blackwell_verify_codegen_fixtures_fb1cut4() == 0, "word fixtures MUFU_EX2 / BAR_SYNC / LDS32 / STS32 (rc %d)", omega_blackwell_verify_codegen_fixtures_fb1cut4());
     float a[256] = {0}, b[256] = {0}, c[256] = {0};
     CHECK(omega_gpu_rmsnorm_f32(0, 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "rows=0 refused");
@@ -172,32 +189,46 @@ static int chip(const char *out_path) {
 
     /* 1. EX2 probe, with a negative control on the comparison itself */
     {
-        size_t n = 4096; float *x = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *wrong = malloc(n * 4);
+        size_t n = 131073, guard_words = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS - 1;
+        size_t capacity = n + guard_words;
+        float *x = malloc(capacity * 4), *got = malloc(capacity * 4), *want = malloc(n * 4), *wrong = malloc(n * 4);
         for (size_t i = 0; i < n; i++) { x[i] = frand(&seed, -20.0f, 20.0f); want[i] = (float)exp2((double)x[i]); wrong[i] = (float)exp2((double)x[i] + 1.0); }
+        guard_fill(x + n, guard_words); guard_fill(got + n, guard_words);
         Case c = { .name = "ex2_probe", .n = n };
         c.rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info);
         c.bad = compare(got, want, n, 1e-6, 0.0, &c.worst);
+        CHECK(guard_bad(x + n, guard_words) == 0, "ex2 n=131073 input guard remains intact");
+        CHECK(guard_bad(got + n, guard_words) == 0, "ex2 n=131073 output guard remains intact");
         double w2; size_t neg = compare(got, wrong, n, 1e-6, 0.0, &w2);
         CHECK(neg > n / 2, "ex2 negative control: the check rejects a wrong expectation (%zu of %zu flagged)", neg, n);
         c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words;
         omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_EX2);
+        guard_fill(got + n, guard_words);
         c.mutant_rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info);
         c.mutant_bad = compare(got, want, n, 1e-6, 0.0, &w2);
+        CHECK(guard_bad(got + n, guard_words) == 0, "ex2 n=131073 mutant output guard remains intact");
         c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0;
         omega_gpu_elementwise_test_set_mutant(0);
         record(c, out); free(x); free(got); free(want); free(wrong);
     }
     /* 2. shared-memory exchange probe (STS, BAR.SYNC, LDS) */
     {
-        size_t n = 4096; uint32_t *x = malloc(n * 4), *got = malloc(n * 4);
+        size_t n = 131200, guard_words = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS - 128;
+        size_t capacity = n + guard_words;
+        uint32_t *x = malloc(capacity * 4), *got = malloc(capacity * 4);
         for (size_t i = 0; i < n; i++) x[i] = lcg(&seed);
+        guard_fill(x + n, guard_words); guard_fill(got + n, guard_words);
         Case c = { .name = "shared_xchg_probe", .n = n };
         c.rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info);
         for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.bad++;
+        CHECK(guard_bad(x + n, guard_words) == 0, "xchg n=131200 input guard remains intact");
+        CHECK(guard_bad(got + n, guard_words) == 0, "xchg n=131200 output guard remains intact");
         c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words;
         omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_XCHG);
+        guard_fill(got + n, guard_words);
         c.mutant_rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info);
         for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.mutant_bad++;
+        CHECK(guard_bad(got + n, guard_words) == 0, "xchg n=131200 mutant output guard remains intact");
         c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0;
         omega_gpu_elementwise_test_set_mutant(0);
         record(c, out); free(x); free(got);