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
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Host-only vendor-struct oracle. Never opens a device. */ |
| 3 | #include <stdint.h> |
| 4 | #include <stddef.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include "cudapp_native.h" |
| 8 | #include "nvos.h" |
| 9 | #include "nv-ioctl.h" |
| 10 | #include "nv-unix-nvos-params-wrappers.h" |
| 11 | |
| 12 | static int check(uint64_t op, const uint64_t a[16], const void *oracle, size_t size, |
| 13 | size_t status_at, size_t value_at, size_t value_size) { |
| 14 | unsigned char wire[64], expected[64] = {0}; |
| 15 | uint64_t n = 0, result = 0, reply[2] = {0}; |
| 16 | memcpy(expected, oracle, size); |
| 17 | if (omega_nvrm_wire_pack(&n, op, a, 16, wire, 64) || n != size) return 1; |
| 18 | #ifdef NVRM_WIRE_MUTANT |
| 19 | wire[0] ^= 1; /* Negative control: an incorrect serializer must fail the oracle. */ |
| 20 | #endif |
| 21 | if (memcmp(wire, expected, 64)) return 1; |
| 22 | /* Every byte participates: the deliberately broken request must differ. */ |
| 23 | for (size_t i = 0; i < size; i++) { |
| 24 | wire[i] ^= 1; |
| 25 | if (!memcmp(wire, expected, size)) return 1; |
| 26 | wire[i] ^= 1; |
| 27 | } |
| 28 | if (op == 7) return 0; |
| 29 | /* Fake reply includes nonzero RM status; parser reports it, never hides it. */ |
| 30 | uint32_t status = 0x12345678; |
| 31 | uint64_t value = value_size == 4 ? UINT64_C(0x89abcdef) : UINT64_C(0x1234567889abcdef); |
| 32 | memcpy(wire + status_at, &status, sizeof status); |
| 33 | if (value_size) memcpy(wire + value_at, &value, value_size); |
| 34 | if (omega_nvrm_wire_reply(&result, op, wire, 64, reply, 2) || result || reply[0] != status || |
| 35 | reply[1] != (value_size ? value : 0)) return 1; |
| 36 | return 0; |
| 37 | } |
| 38 | int main(void) { |
| 39 | uint64_t a[16]; |
| 40 | for (unsigned k = 0; k < 256; k++) { |
| 41 | for (unsigned i = 0; i < 16; i++) a[i] = (UINT64_C(0xfedcba9876543210) ^ ((uint64_t)k << 32) ^ i); |
| 42 | NVOS21_PARAMETERS alloc = {0}; |
| 43 | alloc.hRoot=a[0]; alloc.hObjectParent=a[1]; alloc.hObjectNew=a[2]; alloc.hClass=a[3]; |
| 44 | alloc.pAllocParms=(void *)(uintptr_t)a[4]; alloc.paramsSize=a[5]; |
| 45 | if (check(1,a,&alloc,sizeof alloc,offsetof(NVOS21_PARAMETERS,status),offsetof(NVOS21_PARAMETERS,hObjectNew),4)) return 1; |
| 46 | NVOS54_PARAMETERS ctrl = {0}; |
| 47 | ctrl.hClient=a[0]; ctrl.hObject=a[1]; ctrl.cmd=a[2]; ctrl.flags=a[3]; ctrl.params=(void *)(uintptr_t)a[4]; ctrl.paramsSize=a[5]; |
| 48 | if (check(2,a,&ctrl,sizeof ctrl,offsetof(NVOS54_PARAMETERS,status),0,0)) return 2; |
| 49 | NVOS00_PARAMETERS release = {0}; |
| 50 | release.hRoot=a[0]; release.hObjectParent=a[1]; release.hObjectOld=a[2]; |
| 51 | if (check(3,a,&release,sizeof release,offsetof(NVOS00_PARAMETERS,status),0,0)) return 3; |
| 52 | nv_ioctl_nvos33_parameters_with_fd map = {0}; |
| 53 | map.params.hClient=a[0]; map.params.hDevice=a[1]; map.params.hMemory=a[2]; |
| 54 | map.params.offset=a[3]; map.params.length=a[4]; map.params.pLinearAddress=(void *)(uintptr_t)a[5]; map.params.flags=a[6]; map.fd=(int)(uint32_t)a[7]; |
| 55 | if (check(4,a,&map,sizeof map,offsetof(NVOS33_PARAMETERS,status),offsetof(NVOS33_PARAMETERS,pLinearAddress),8)) return 4; |
| 56 | NVOS46_PARAMETERS dma = {0}; |
| 57 | dma.hClient=a[0]; dma.hDevice=a[1]; dma.hDma=a[2]; dma.hMemory=a[3]; dma.offset=a[4]; dma.length=a[5]; |
| 58 | dma.flags=a[6]; dma.flags2=a[7]; dma.kindOverride=a[8]; dma.dmaOffset=a[9]; |
| 59 | if (check(5,a,&dma,sizeof dma,offsetof(NVOS46_PARAMETERS,status),offsetof(NVOS46_PARAMETERS,dmaOffset),8)) return 5; |
| 60 | NVOS47_PARAMETERS unmap = {0}; |
| 61 | unmap.hClient=a[0]; unmap.hDevice=a[1]; unmap.hDma=a[2]; unmap.hMemory=a[3]; unmap.flags=a[4]; unmap.dmaOffset=a[5]; unmap.size=a[6]; |
| 62 | if (check(6,a,&unmap,sizeof unmap,offsetof(NVOS47_PARAMETERS,status),0,0)) return 6; |
| 63 | nv_ioctl_register_fd_t reg = { .ctl_fd=(int)(uint32_t)a[0] }; |
| 64 | if (check(7,a,®,sizeof reg,0,0,0)) return 7; |
| 65 | NVOS32_PARAMETERS heap = {0}; |
| 66 | unsigned char heap_wire[184]; |
| 67 | uint64_t heap_size = 0; |
| 68 | heap.hRoot=a[0]; heap.hObjectParent=a[1]; heap.function=a[2]; heap.data.Info.attr=a[3]; |
| 69 | _Static_assert(sizeof heap == sizeof heap_wire, "vendored NVOS32 layout changed"); |
| 70 | if (omega_nvrm_heap_wire(&heap_size,a,16,heap_wire,sizeof heap_wire) || |
| 71 | heap_size != sizeof heap || memcmp(heap_wire,&heap,sizeof heap)) return 9; |
| 72 | } |
| 73 | unsigned char b[64]; uint64_t n=99, a0[16]={0}, r[2]={0}; |
| 74 | if (omega_nvrm_wire_pack(&n,0,a0,16,b,64) || n || omega_nvrm_wire_reply(&n,0,b,64,r,2) || n != 1) return 8; |
| 75 | puts("NVRM WIRE PASS (vendor byte differential, fake replies, every-byte mutants, invalid op)"); |
| 76 | return 0; |
| 77 | } |