Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/nvrm/native_ioctl_fixture.h # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / nvrm / native_ioctl_fixture.h` 181 lines · 9861 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Test oracle only: trap ioctls to one deliberately unopened fd in a child. 3 * Ordinary Linux syscall execution and Omega's request construction are real; 4 * the SIGSYS handler stands in for the RM/UVM kernel and checks every byte. */ 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 static unsigned char fixture_expected[9264]; 13 static size_t fixture_size,fixture_status; 14 static unsigned long fixture_request; 15 static volatile sig_atomic_t fixture_seen,fixture_bad; 16 static int fixture_mutant; 17 static unsigned char fixture_params[16]; 18 static void fixture_sigsys(int signal,siginfo_t *info,void *opaque) { 19 (void)signal; 20 ucontext_t *uc=opaque; 21 unsigned long request; unsigned char *bytes; 22 #if defined(__aarch64__) 23 request=uc->uc_mcontext.regs[1];bytes=(void *)(uintptr_t)uc->uc_mcontext.regs[2]; 24 uc->uc_mcontext.regs[0]=0; 25 #elif defined(__x86_64__) 26 request=uc->uc_mcontext.gregs[REG_RSI];bytes=(void *)(uintptr_t)uc->uc_mcontext.gregs[REG_RDX]; 27 uc->uc_mcontext.gregs[REG_RAX]=0; 28 #else 29 #error "NVRM native syscall fixture requires Linux AArch64 or x86-64" 30 #endif 31 fixture_seen++; 32 if(info->si_syscall!=SYS_ioctl || request!=(fixture_request^(unsigned)fixture_mutant))fixture_bad=1; 33 for(size_t i=0;i=0); 111 if(!child){fixture_mutant=mutant;_exit(fixture_child());} 112 int status=0;assert(waitpid(child,&status,0)==child && WIFEXITED(status)); 113 assert(WEXITSTATUS(status)==mutant); 114 } 115 puts("NVRM NATIVE IOCTL PASS (all RM/UVM request numbers and bytes, fake nonzero replies, parameter copyback, syscall request mutant caught; HOST_ONLY)"); 116 } 117 #undef FIXTURE_RM 118 #undef FIXTURE_UVM 119 120 /* One denied cleanup followed by a real retry. The test-only third syscall 121 * argument lets the SIGSYS handler forward munmap without recursively trapping; 122 * Linux munmap ignores it. No production syscall or bridge gate is changed. */ 123 static volatile sig_atomic_t fixture_cleanup_seen; 124 static void fixture_cleanup_sigsys(int signal,siginfo_t *info,void *opaque) { 125 (void)signal; 126 ucontext_t *uc=opaque; 127 uintptr_t address;size_t size;long result; 128 #if defined(__aarch64__) 129 address=uc->uc_mcontext.regs[0];size=uc->uc_mcontext.regs[1]; 130 #elif defined(__x86_64__) 131 address=uc->uc_mcontext.gregs[REG_RDI];size=uc->uc_mcontext.gregs[REG_RSI]; 132 #endif 133 if(info->si_syscall!=SYS_munmap)_exit(80); 134 fixture_cleanup_seen++; 135 int saved_errno=errno; 136 result=fixture_cleanup_seen==1?-EPERM:syscall(SYS_munmap,address,size,0x43554441); 137 errno=saved_errno; 138 #if defined(__aarch64__) 139 uc->uc_mcontext.regs[0]=(uint64_t)result; 140 #elif defined(__x86_64__) 141 uc->uc_mcontext.gregs[REG_RAX]=result; 142 #endif 143 } 144 static void fixture_cleanup_child(void) { 145 Nvrm rm={0}; 146 void *address=(void *)(uintptr_t)UINT64_C(0x7000000000); 147 assert(nvrm_cpu_reserve(&rm,(uintptr_t)address,4096)==address); 148 uint64_t owner=rm.omega_pending_mapping; 149 int fd=memfd_create("cudapp-nvrm-cleanup-fixture",MFD_CLOEXEC); 150 assert(fd>=0 && ftruncate(fd,4096)==0); 151 struct sigaction action={0};action.sa_sigaction=fixture_cleanup_sigsys;action.sa_flags=SA_SIGINFO; 152 sigemptyset(&action.sa_mask);assert(sigaction(SIGSYS,&action,NULL)==0); 153 struct sock_filter filters[]={ 154 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(struct seccomp_data,nr)), 155 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,SYS_munmap,0,3), 156 BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(struct seccomp_data,args[2])), 157 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,0x43554441,1,0), 158 BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_TRAP), 159 BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ALLOW) 160 }; 161 struct sock_fprog program={.len=sizeof filters/sizeof filters[0],.filter=filters}; 162 assert(prctl(PR_SET_NO_NEW_PRIVS,1,0,0,0)==0); 163 assert(prctl(PR_SET_SECCOMP,SECCOMP_MODE_FILTER,&program)==0); 164 assert(nvrm_cpu_map(&rm,-1,4096,address)==MAP_FAILED && errno==EPROTO); 165 assert(fixture_cleanup_seen==1 && rm.omega_pending_mapping==owner); 166 assert(rm.omega_pending_address==(uintptr_t)address && rm.omega_pending_size==4096); 167 uint64_t result=0x12345678; 168 assert(omega2_ctx_nvrm_bind(rm.omega_context,&result,owner,(uint64_t)fd)!=0); 169 assert(result==0x12345678 && fixture_cleanup_seen==1); 170 assert(omega_native_context_destroy(rm.omega_context)==36); 171 assert(nvrm_cpu_drop(&rm,address,4096)==0 && !rm.omega_pending_mapping); 172 assert(fixture_cleanup_seen==2); 173 assert(nvrm_mapping_finish(&rm)==0 && !rm.omega_context); 174 assert(close(fd)==0); 175 } 176 static void fixture_cleanup_run(void) { 177 pid_t child=fork();assert(child>=0); 178 if(!child){fixture_cleanup_child();_exit(0);} 179 int status=0;assert(waitpid(child,&status,0)==child && WIFEXITED(status) && WEXITSTATUS(status)==0); 180 puts("NVRM CLEANUP FAILURE PASS (failed bind plus denied munmap retains quarantined owner, refuses rebind/destroy, retry drops; HOST_ONLY)"); 181 }