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 / seed / host / irq-oracle.c
| 1 | /* Read-only Linux UAPI/layout and syscall verification oracle, not product code. |
| 2 | * No preload, device access or ioctl emulation beyond one GET_DEVICE_FD return. |
| 3 | * The child owns a memfd. We substitute that existing fd on GET_DEVICE_FD exit; |
| 4 | * subsequent IRQ ioctls execute unchanged and must return ENOTTY. */ |
| 5 | #define _GNU_SOURCE |
| 6 | #include <asm/ptrace.h> |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <linux/vfio.h> |
| 10 | #include <stddef.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <sys/eventfd.h> |
| 16 | #include <sys/ptrace.h> |
| 17 | #include <sys/uio.h> |
| 18 | #include <sys/wait.h> |
| 19 | #include <sys/syscall.h> |
| 20 | #include <signal.h> |
| 21 | #include <unistd.h> |
| 22 | #include <elf.h> |
| 23 | |
| 24 | static void die(const char *s) { perror(s); exit(90); } |
| 25 | static uint32_t word(pid_t p, uint64_t address) { |
| 26 | errno = 0; |
| 27 | long v = ptrace(PTRACE_PEEKDATA, p, (void *)(uintptr_t)address, 0); |
| 28 | if (errno) die("PTRACE_PEEKDATA"); |
| 29 | return (uint32_t)v; |
| 30 | } |
| 31 | static void layout(void) { |
| 32 | printf("irq_info %zu %zu %zu %zu %zu\n", sizeof(struct vfio_irq_info), |
| 33 | offsetof(struct vfio_irq_info,argsz), offsetof(struct vfio_irq_info,flags), |
| 34 | offsetof(struct vfio_irq_info,index), offsetof(struct vfio_irq_info,count)); |
| 35 | printf("irq_set %zu %zu %zu %zu %zu %zu %zu\n", sizeof(struct vfio_irq_set), |
| 36 | offsetof(struct vfio_irq_set,argsz), offsetof(struct vfio_irq_set,flags), |
| 37 | offsetof(struct vfio_irq_set,index), offsetof(struct vfio_irq_set,start), |
| 38 | offsetof(struct vfio_irq_set,count), offsetof(struct vfio_irq_set,data)); |
| 39 | printf("requests %lu %lu\n", (unsigned long)VFIO_DEVICE_GET_IRQ_INFO, |
| 40 | (unsigned long)VFIO_DEVICE_SET_IRQS); |
| 41 | printf("flags %u %u %u %u\n", VFIO_IRQ_SET_DATA_EVENTFD|VFIO_IRQ_SET_ACTION_TRIGGER, |
| 42 | VFIO_IRQ_SET_DATA_NONE|VFIO_IRQ_SET_ACTION_TRIGGER, |
| 43 | VFIO_IRQ_SET_DATA_NONE|VFIO_IRQ_SET_ACTION_MASK, |
| 44 | VFIO_IRQ_SET_DATA_NONE|VFIO_IRQ_SET_ACTION_UNMASK); |
| 45 | printf("eventfd %d %ld\n", EFD_CLOEXEC|EFD_NONBLOCK, (long)SYS_eventfd2); |
| 46 | } |
| 47 | int main(int argc, char **argv) { |
| 48 | if (argc == 2 && !strcmp(argv[1], "--layout")) { layout(); return 0; } |
| 49 | if (argc == 2 && !strcmp(argv[1], "--bytes")) { |
| 50 | struct vfio_irq_info info = { |
| 51 | .argsz=sizeof info, .flags=VFIO_IRQ_INFO_EVENTFD|VFIO_IRQ_INFO_MASKABLE, |
| 52 | .index=VFIO_PCI_MSIX_IRQ_INDEX, .count=64 |
| 53 | }; |
| 54 | struct vfio_irq_set set = { |
| 55 | .argsz=sizeof set+sizeof(int32_t), |
| 56 | .flags=VFIO_IRQ_SET_DATA_EVENTFD|VFIO_IRQ_SET_ACTION_TRIGGER, |
| 57 | .index=VFIO_PCI_MSIX_IRQ_INDEX, .start=3, .count=1 |
| 58 | }; |
| 59 | int32_t fd=-1; |
| 60 | if (fwrite(&info,sizeof info,1,stdout)!=1 || |
| 61 | fwrite(&set,sizeof set,1,stdout)!=1 || |
| 62 | fwrite(&fd,sizeof fd,1,stdout)!=1) return 1; |
| 63 | return 0; |
| 64 | } |
| 65 | if (argc != 5) return 90; |
| 66 | int expected = atoi(argv[3]), high = atoi(argv[4]); |
| 67 | pid_t p = fork(); |
| 68 | if (p < 0) die("fork"); |
| 69 | if (!p) { |
| 70 | if (high) { |
| 71 | int fd = 0; |
| 72 | while (fd < 1100) { |
| 73 | fd = open("/dev/null", O_RDONLY); |
| 74 | if (fd < 0) _exit(91); |
| 75 | } |
| 76 | } |
| 77 | if (ptrace(PTRACE_TRACEME, 0, 0, 0)) _exit(92); |
| 78 | raise(SIGSTOP); |
| 79 | execl(argv[1], argv[1], argv[2], (char *)0); |
| 80 | _exit(93); |
| 81 | } |
| 82 | int status; |
| 83 | if (waitpid(p, &status, 0) != p || !WIFSTOPPED(status)) die("initial wait"); |
| 84 | if (ptrace(PTRACE_SETOPTIONS, p, 0, PTRACE_O_TRACESYSGOOD|PTRACE_O_EXITKILL)) die("options"); |
| 85 | int entering = 1, fakefd = -1, eventfd = -1, irq = 0, injected = 0; |
| 86 | uint64_t call = 0, request = 0, fd = 0; |
| 87 | for (;;) { |
| 88 | if (ptrace(PTRACE_SYSCALL, p, 0, 0)) die("continue"); |
| 89 | if (waitpid(p, &status, 0) != p) die("waitpid"); |
| 90 | if (WIFEXITED(status)) { |
| 91 | int rc = WEXITSTATUS(status); |
| 92 | int wanted_irq = !strcmp(argv[2],"0") ? 7 : (!strcmp(argv[2],"@") ? 1 : 0); |
| 93 | if (rc != expected || injected != 1 || irq != wanted_irq) { |
| 94 | fprintf(stderr,"FAIL fixture exit=%d expected=%d injected=%d irq=%d\n",rc,expected,injected,irq); |
| 95 | return 1; |
| 96 | } |
| 97 | printf("PASS ptrace fixture mode=%s exit=%d IRQ_ENOTTY=%d highfd=%d\n",argv[2],rc,irq,high); |
| 98 | return 0; |
| 99 | } |
| 100 | if (!WIFSTOPPED(status) || WSTOPSIG(status) != (SIGTRAP|0x80)) { |
| 101 | if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) continue; |
| 102 | fprintf(stderr,"FAIL unexpected child status=%d\n",status); return 1; |
| 103 | } |
| 104 | struct user_pt_regs regs; |
| 105 | struct iovec io = { ®s, sizeof regs }; |
| 106 | if (ptrace(PTRACE_GETREGSET,p,(void *)NT_PRSTATUS,&io)) die("getregset"); |
| 107 | if (entering) { |
| 108 | call=regs.regs[8]; request=regs.regs[1]; fd=regs.regs[0]; |
| 109 | if (call == SYS_eventfd2 && |
| 110 | regs.regs[1] != (EFD_CLOEXEC|EFD_NONBLOCK)) return 1; |
| 111 | if (call == SYS_ioctl && request == VFIO_GROUP_GET_DEVICE_FD) { |
| 112 | fakefd=(int)fd; |
| 113 | if (high && fakefd < 1024) return 1; |
| 114 | } |
| 115 | if (call == SYS_ioctl && (request == VFIO_DEVICE_GET_IRQ_INFO || request == VFIO_DEVICE_SET_IRQS)) { |
| 116 | if ((int)fd != fakefd) return 1; |
| 117 | uint64_t b=regs.regs[2]; |
| 118 | uint32_t sz=word(p,b); |
| 119 | if (request == VFIO_DEVICE_GET_IRQ_INFO) { |
| 120 | if (sz != sizeof(struct vfio_irq_info)) return 1; |
| 121 | } else { |
| 122 | uint32_t flags=word(p,b+offsetof(struct vfio_irq_set,flags)); |
| 123 | uint32_t count=word(p,b+offsetof(struct vfio_irq_set,count)); |
| 124 | if (count > 64) return 1; |
| 125 | if (flags == (VFIO_IRQ_SET_DATA_EVENTFD|VFIO_IRQ_SET_ACTION_TRIGGER)) { |
| 126 | if (!count || sz != sizeof(struct vfio_irq_set)+sizeof(int32_t)*count) return 1; |
| 127 | for (uint32_t i=0; i<count; i++) { |
| 128 | int32_t e=(int32_t)word(p,b+offsetof(struct vfio_irq_set,data)+sizeof(int32_t)*i); |
| 129 | if (e != eventfd && e != -1) return 1; |
| 130 | } |
| 131 | } else if (sz != sizeof(struct vfio_irq_set)) return 1; |
| 132 | } |
| 133 | } |
| 134 | } else { |
| 135 | if (call == SYS_eventfd2 && (int64_t)regs.regs[0] >= 0) eventfd=(int)regs.regs[0]; |
| 136 | if (call == SYS_ioctl && request == VFIO_GROUP_GET_DEVICE_FD) { |
| 137 | if ((int64_t)regs.regs[0] != -ENOTTY) return 1; |
| 138 | regs.regs[0]=(uint64_t)fakefd; |
| 139 | if (ptrace(PTRACE_SETREGSET,p,(void *)NT_PRSTATUS,&io)) die("setregset"); |
| 140 | injected++; |
| 141 | } |
| 142 | if (call == SYS_ioctl && (request == VFIO_DEVICE_GET_IRQ_INFO || request == VFIO_DEVICE_SET_IRQS)) { |
| 143 | if ((int64_t)regs.regs[0] != -ENOTTY) return 1; |
| 144 | irq++; |
| 145 | } |
| 146 | } |
| 147 | entering=!entering; |
| 148 | } |
| 149 | } |