⬢ DragonFlyBSD Kernel Audit
DF-2752 / pipetest2.c
← back to finding ↓ download raw
#include <stdio.h>
#include <unistd.h>
#include <string.h>
/* Fill 20480, read 4096 (window wraps), write 4096 more, then drain 20480 */
static unsigned char out[65536], in[65536];
static void fill(size_t base, size_t n){ for (size_t i=0;i<n;i++) out[base+i]=(unsigned char)((base+i)*7+3); }
int main(void) {
    int p[2];
    if (pipe(p)) { perror("pipe"); return 1; }
    size_t sz = 20480;
    fill(0, sz);
    ssize_t w = write(p[1], out, sz);        /* absolute 0..20479 */
    ssize_t r1 = read(p[0], in, 4096);       /* absolute 0..4095 consumed */
    fill(sz, 4096);
    ssize_t w2 = write(p[1], out + sz, 4096);/* absolute 20480..24575 */
    printf("w=%zd r1=%zd w2=%zd\n", w, r1, w2);
    if (w != (ssize_t)sz || r1 != 4096 || w2 != 4096) { printf("unexpected IO counts\n"); return 1; }
    ssize_t r = read(p[0], in, 65536);       /* should get 20480: abs 4096..24575 */
    printf("drained %zd (expect 20480)\n", r);
    int bad = 0; long firstbad = -1;
    for (ssize_t i = 0; i < r; i++) {
        size_t abs = 4096 + (size_t)i;
        if (in[i] != (unsigned char)((abs*7+3) & 0xff)) { bad++; if (firstbad<0) firstbad = abs; }
    }
    printf("mismatched bytes: %d / %zd, first mismatch at absolute offset %ld\n", bad, r, firstbad);
    if (bad) { printf("FAIL: non-pow2 pipe size CORRUPTS in-flight data (slot aliasing)\n"); return 2; }
    printf("PASS: no corruption\n");
    return 0;
}