interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / kfd / trace.c

116 lines · 5013 bytes Raw Blame History
1/* Independent AArch64 ptrace instrument. No device access. ioctls use fd -1.
2 * Observe actual generated arguments; poison only private output pointers after
3 * the real EBADF, proving copy-out never discloses compiler-bound addresses. */
4#define _GNU_SOURCE
5#include <asm/ptrace.h>
6#include <elf.h>
7#include <errno.h>
8#include <linux/kfd_ioctl.h>
9#include <stdint.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <sys/ptrace.h>
13#include <sys/syscall.h>
14#include <sys/uio.h>
15#include <sys/wait.h>
16#include <signal.h>
17#include <unistd.h>
18
19static void die(const char *s) { perror(s); exit(90); }
20static uint64_t peek(pid_t p, uint64_t a) {
21 errno=0;
22 long v=ptrace(PTRACE_PEEKDATA,p,(void *)(uintptr_t)a,0);
23 if (errno) die("peek");
24 return (uint64_t)v;
25}
26static void poke(pid_t p, uint64_t a, uint64_t v) {
27 if (ptrace(PTRACE_POKEDATA,p,(void *)(uintptr_t)a,(void *)(uintptr_t)v)) die("poke");
28}
29int main(int argc, char **argv) {
30 if (argc!=2 && argc!=3 && argc!=4) return 90;
31 uint64_t wp=0,rp=0;
32 if (argc==4) { wp=strtoull(argv[2],NULL,10); rp=strtoull(argv[3],NULL,10); }
33 uint64_t aperture_offset=argc==3 ? strtoull(argv[2],NULL,10) : 0;
34 pid_t p=fork();
35 if (p<0) die("fork");
36 if (!p) {
37 if (ptrace(PTRACE_TRACEME,0,0,0)) _exit(91);
38 raise(SIGSTOP);
39 execl(argv[1],argv[1],(char *)0);
40 _exit(92);
41 }
42 int status;
43 if (waitpid(p,&status,0)!=p || !WIFSTOPPED(status)) die("initial wait");
44 if (ptrace(PTRACE_SETOPTIONS,p,0,PTRACE_O_TRACESYSGOOD|PTRACE_O_EXITKILL)) die("options");
45 int entering=1, seen=0;
46 uint64_t call=0,q=0,h=0,base=0,payload=0,arg0=0,arg1=0;
47 unsigned offsets[5],count=0;
48 for (;;) {
49 if (ptrace(PTRACE_SYSCALL,p,0,0)) die("continue");
50 if (waitpid(p,&status,0)!=p) die("wait");
51 if (WIFEXITED(status)) {
52 if (WEXITSTATUS(status) || seen!=1) {
53 fprintf(stderr,"FAIL trace child=%d seen=%d\n",WEXITSTATUS(status),seen);
54 return 1;
55 }
56 fprintf(stderr,"PASS checked binding and private copy-out request=%#llx\n",(unsigned long long)q);
57 return 0;
58 }
59 if (!WIFSTOPPED(status)) return 1;
60 if (WSTOPSIG(status)==SIGTRAP) continue;
61 if (WSTOPSIG(status)!=(SIGTRAP|0x80)) return 1;
62 struct user_pt_regs regs;
63 struct iovec io={&regs,sizeof regs};
64 if (ptrace(PTRACE_GETREGSET,p,(void *)NT_PRSTATUS,&io)) die("regs");
65 if (entering) {
66 call=regs.regs[8]; arg0=regs.regs[0]; arg1=regs.regs[1];
67 if (call==SYS_write && arg0==1) payload=arg1;
68 if (call==SYS_munmap && arg0==base) base+=arg1;
69 if (call==SYS_ioctl) {
70 if ((int64_t)arg0!=-1) return 1;
71 q=arg1; h=regs.regs[2]; count=0;
72 uint64_t expected=base;
73 switch (q) {
74 case AMDKFD_IOC_GET_PROCESS_APERTURES_NEW:
75 offsets[count++]=0; expected=argc==3 ? base+aperture_offset : payload; break;
76 case AMDKFD_IOC_WAIT_EVENTS:
77 offsets[count++]=0; expected=payload; break;
78 case AMDKFD_IOC_MAP_MEMORY_TO_GPU:
79 case AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU:
80 offsets[count++]=8; expected=payload; break;
81 case AMDKFD_IOC_ALLOC_MEMORY_OF_GPU:
82 case AMDKFD_IOC_SET_SCRATCH_BACKING_VA:
83 offsets[count++]=0; break;
84 case AMDKFD_IOC_CREATE_QUEUE:
85 offsets[count++]=0; offsets[count++]=8; offsets[count++]=16;
86 offsets[count++]=56; offsets[count++]=72; break;
87 case AMDKFD_IOC_GET_VERSION: break;
88 default: return 1;
89 }
90 for (unsigned i=0;i<count;i++) {
91 uint64_t actual=peek(p,h+offsets[i]);
92 uint64_t target=expected;
93 if (q==AMDKFD_IOC_CREATE_QUEUE) {
94 if (offsets[i]==8) target+=wp;
95 if (offsets[i]==16) target+=rp;
96 }
97 if (!expected || actual!=target) {
98 fprintf(stderr,"FAIL pointer offset=%u actual=%#llx expected=%#llx\n",offsets[i],(unsigned long long)actual,(unsigned long long)target);
99 return 1;
100 }
101 }
102 seen++;
103 }
104 } else {
105 if (call==SYS_mmap && (int64_t)regs.regs[0]>=0) base=regs.regs[0];
106 if (call==SYS_ioctl) {
107 if ((int64_t)regs.regs[0]!=-EBADF) return 1;
108 for (unsigned i=0;i<count;i++) poke(p,h+offsets[i],0xdeadbeef);
109 if (q==AMDKFD_IOC_GET_VERSION) poke(p,h,0x44332211);
110 if (q==AMDKFD_IOC_ALLOC_MEMORY_OF_GPU) poke(p,h+16,0x44332211);
111 if (q==AMDKFD_IOC_GET_PROCESS_APERTURES_NEW && argc==3) poke(p,h+8,7);
112 }
113 }
114 entering=!entering;
115 }
116}