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
omega-mv / tests / kfd / fdtrace.c
| 1 | /* Independent AArch64 syscall-boundary instrument; no AMD device is opened. |
| 2 | * A real memfd supplies mmap backing. Only stat metadata is synthetic. */ |
| 3 | #define _GNU_SOURCE |
| 4 | #include <asm/ptrace.h> |
| 5 | #include <elf.h> |
| 6 | #include <errno.h> |
| 7 | #include <stdint.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <sys/ptrace.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sys/syscall.h> |
| 14 | #include <sys/sysmacros.h> |
| 15 | #include <sys/uio.h> |
| 16 | #include <sys/wait.h> |
| 17 | #include <signal.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | static void die(const char *s) { perror(s); exit(90); } |
| 21 | static void poke(pid_t p, uint64_t a, const void *src, size_t n) { |
| 22 | const unsigned char *s=src; |
| 23 | for (size_t i=0;i<n;i+=sizeof(long)) { |
| 24 | long v=0; |
| 25 | memcpy(&v,s+i,n-i<sizeof v?n-i:sizeof v); |
| 26 | if (ptrace(PTRACE_POKEDATA,p,(void *)(uintptr_t)(a+i),(void *)v)) die("poke"); |
| 27 | } |
| 28 | } |
| 29 | int main(int argc, char **argv) { |
| 30 | if (argc!=4) return 90; |
| 31 | unsigned mode=(unsigned)strtoul(argv[2],0,10); |
| 32 | uint64_t bytes=strtoull(argv[3],0,10); |
| 33 | int success=mode==0 || mode==2; |
| 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("wait"); |
| 44 | if (ptrace(PTRACE_SETOPTIONS,p,0,PTRACE_O_TRACESYSGOOD|PTRACE_O_EXITKILL)) die("options"); |
| 45 | int entering=1, maps=0, stats=0; |
| 46 | uint64_t call=0,buf=0; |
| 47 | for (;;) { |
| 48 | if (ptrace(PTRACE_SYSCALL,p,0,0)) die("continue"); |
| 49 | if (waitpid(p,&status,0)!=p) die("wait"); |
| 50 | if (WIFEXITED(status)) { |
| 51 | int expected=success?0:103; |
| 52 | if (WEXITSTATUS(status)!=expected || maps!=success || stats<1) return 1; |
| 53 | fprintf(stderr,"PASS synthetic fd mode=%u maps=%d child=%d span=%llu\n",mode,maps,expected,(unsigned long long)bytes); |
| 54 | return 0; |
| 55 | } |
| 56 | if (!WIFSTOPPED(status)) return 1; |
| 57 | if (WSTOPSIG(status)==SIGTRAP) continue; |
| 58 | if (WSTOPSIG(status)!=(SIGTRAP|0x80)) return 1; |
| 59 | struct user_pt_regs regs; |
| 60 | struct iovec io={®s,sizeof regs}; |
| 61 | if (ptrace(PTRACE_GETREGSET,p,(void *)NT_PRSTATUS,&io)) die("regs"); |
| 62 | if (entering) { |
| 63 | call=regs.regs[8]; |
| 64 | if (call==SYS_fstat) buf=regs.regs[1]; |
| 65 | if (call==SYS_newfstatat) buf=regs.regs[2]; |
| 66 | if (call==SYS_mmap) { |
| 67 | if (regs.regs[1]!=bytes || regs.regs[2]!=3 || regs.regs[3]!=1) return 1; |
| 68 | maps++; |
| 69 | } |
| 70 | } else if (call==SYS_fstat || call==SYS_newfstatat) { |
| 71 | struct stat st={0}; |
| 72 | st.st_mode=S_IFCHR|0600; |
| 73 | st.st_rdev=makedev(226,128); |
| 74 | if (mode==1) st.st_rdev=makedev(226,127); |
| 75 | if (mode>=2) st.st_rdev=makedev(234,0); |
| 76 | if (call==SYS_newfstatat) st.st_rdev=makedev(234,0); |
| 77 | if (call==SYS_newfstatat && mode==3) st.st_rdev=makedev(234,1); |
| 78 | if (call==SYS_newfstatat && mode==4) st.st_mode=S_IFREG|0600; |
| 79 | if (call==SYS_fstat && mode==5) st.st_mode=S_IFREG|0600; |
| 80 | poke(p,buf,&st,sizeof st); |
| 81 | regs.regs[0]=0; |
| 82 | if (mode==6) regs.regs[0]=(uint64_t)-EBADF; |
| 83 | if (ptrace(PTRACE_SETREGSET,p,(void *)NT_PRSTATUS,&io)) die("setregs"); |
| 84 | stats++; |
| 85 | } |
| 86 | entering=!entering; |
| 87 | } |
| 88 | } |