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
CUDApp / tests / share / native-share-test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* |
| 3 | * Host test of native/share/share_native.om, the OM-2 function-mode zero-copy unit, called |
| 4 | * through a hosted native library that omega-bridge tools/native --host builds from that one |
| 5 | * input. The library is a test artifact under the build directory; the product links the |
| 6 | * combined hosted library that Integration generates. |
| 7 | * |
| 8 | * Fake RM/UVM: seccomp traps ioctl on two deliberately unopened descriptors and the SIGSYS |
| 9 | * handler answers. It decodes every request with the NVIDIA 580.173.02 headers, so a literal in |
| 10 | * the unit that disagrees with them fails here instead of agreeing with itself. Memory objects |
| 11 | * are memfds; RM_MAP_MEMORY dup3()s the object's memfd over the fresh descriptor the unit |
| 12 | * opened, so the unit's own mapbind maps the owner's real bytes. No device file is opened. |
| 13 | * Exit 0 and "ZERO-COPY SHARE NATIVE PASS" on success; "native-share: FAIL line N" otherwise. |
| 14 | */ |
| 15 | #define _GNU_SOURCE |
| 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <linux/filter.h> |
| 19 | #include <linux/seccomp.h> |
| 20 | #include <signal.h> |
| 21 | #include <stddef.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/prctl.h> |
| 29 | #include <sys/syscall.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <ucontext.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include "nv-ioctl-numbers.h" |
| 35 | #include "nv-unix-nvos-params-wrappers.h" |
| 36 | #include "nv_escape.h" |
| 37 | #include "nvos.h" |
| 38 | #include "nvstatuscodes.h" |
| 39 | #include "rs_access.h" |
| 40 | #include "uvm_ioctl.h" |
| 41 | |
| 42 | #include "omega_api.h" |
| 43 | |
| 44 | #define CHECK(c) do { if (!(c)) { fprintf(stderr, "native-share: FAIL line %d: %s\n", __LINE__, #c); exit(1); } } while (0) |
| 45 | #define REQ(nr, type) ((unsigned long)_IOWR(NV_IOCTL_MAGIC, (nr), type)) |
| 46 | |
| 47 | enum { FK_CTL = 12345, FK_UVM = 12346 }; |
| 48 | #define OC 0xc1d00001u /* owner client */ |
| 49 | #define OD 0xd1000001u /* owner device */ |
| 50 | #define OH 0xbeef0010u /* owner memory */ |
| 51 | #define OH2 0xbeef0020u /* owner memory never shared */ |
| 52 | #define IC 0xc1d00002u /* importer client */ |
| 53 | #define ID 0xd1000002u /* importer device */ |
| 54 | #define IV 0x7a000001u /* importer GPU VA space */ |
| 55 | #define NEWH 0xa0010001u |
| 56 | #define SZ 8192u |
| 57 | /* WolframLanguageEvaluator 16^^5a00000000 -> 386547056640; < 2^40 - 2^16 -> True. */ |
| 58 | #define A1 0x5a00000000ull |
| 59 | #define A2 0x5a00100000ull |
| 60 | #define A3 0x5a00200000ull |
| 61 | |
| 62 | enum { K_SHARE, K_DUP, K_FREE, K_MAP, K_DMA, K_UNDMA, K_RANGE, K_EXT, K_UFREE, K_N }; |
| 63 | struct fkobj { uint32_t client, parent, handle; int fd; uint64_t size; int shared, used; }; |
| 64 | struct fkrange { uint64_t base, size; uint32_t mem; int dma, ext, used; }; |
| 65 | static struct fkobj fk_obj[16]; |
| 66 | static struct fkrange fk_rng[8]; |
| 67 | static volatile sig_atomic_t fk_bad; |
| 68 | static unsigned fk_calls[K_N]; |
| 69 | static unsigned fk_fail_kind = K_N; |
| 70 | static NvU32 fk_fail_status; |
| 71 | static int fk_dupzero, fk_dmashift; |
| 72 | static uint32_t fk_mode; /* the access mode the next import must request: 0 RW, 1 RO */ |
| 73 | static const uint8_t fk_uuid[16] = { 0x47, 0x42, 0x31, 0x30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; |
| 74 | |
| 75 | #define BAD() (fk_bad = fk_bad ? fk_bad : __LINE__) |
| 76 | |
| 77 | static int fk_find(uint32_t client, uint32_t handle) |
| 78 | { |
| 79 | for (int i = 0; i < 16; i++) |
| 80 | if (fk_obj[i].used && fk_obj[i].client == client && fk_obj[i].handle == handle) |
| 81 | return i; |
| 82 | return -1; |
| 83 | } |
| 84 | static int fk_new(uint32_t client, uint32_t parent, uint32_t handle, int fd, uint64_t size) |
| 85 | { |
| 86 | for (int i = 0; i < 16; i++) |
| 87 | if (!fk_obj[i].used) { |
| 88 | fk_obj[i] = (struct fkobj){ client, parent, handle, fd, size, 0, 1 }; |
| 89 | return i; |
| 90 | } |
| 91 | return -1; |
| 92 | } |
| 93 | static int fk_range(uint64_t base) |
| 94 | { |
| 95 | for (int i = 0; i < 8; i++) |
| 96 | if (fk_rng[i].used && fk_rng[i].base == base) |
| 97 | return i; |
| 98 | return -1; |
| 99 | } |
| 100 | /* Count the call; answer an injected status once. */ |
| 101 | static int fk_inject(unsigned kind, NvU32 *status) |
| 102 | { |
| 103 | fk_calls[kind]++; |
| 104 | if (fk_fail_kind != kind) |
| 105 | return 0; |
| 106 | fk_fail_kind = K_N; |
| 107 | *status = fk_fail_status; |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | static void fk_rm(unsigned long req, void *arg) |
| 112 | { |
| 113 | if (req == REQ(NV_ESC_RM_SHARE, NVOS57_PARAMETERS)) { |
| 114 | NVOS57_PARAMETERS *p = arg; |
| 115 | if (fk_inject(K_SHARE, &p->status)) |
| 116 | return; |
| 117 | RS_SHARE_POLICY *sp = &p->sharePolicy; |
| 118 | if (sp->target != 0 || sp->accessMask.limbs[0] != (1u << RS_ACCESS_DUP_OBJECT) || |
| 119 | sp->type != RS_SHARE_TYPE_OS_SECURITY_TOKEN || sp->action != 0) |
| 120 | BAD(); |
| 121 | for (unsigned i = 1; i < SDK_RS_ACCESS_MAX_LIMBS; i++) |
| 122 | if (sp->accessMask.limbs[i]) |
| 123 | BAD(); |
| 124 | int i = fk_find(p->hClient, p->hObject); |
| 125 | p->status = i < 0 ? NV_ERR_OBJECT_NOT_FOUND : NV_OK; |
| 126 | if (i >= 0) |
| 127 | fk_obj[i].shared = 1; |
| 128 | } else if (req == REQ(NV_ESC_RM_DUP_OBJECT, NVOS55_PARAMETERS)) { |
| 129 | NVOS55_PARAMETERS *p = arg; |
| 130 | if (fk_inject(K_DUP, &p->status)) |
| 131 | return; |
| 132 | if (p->flags != NV04_DUP_HANDLE_FLAGS_NONE || p->hClient != IC || p->hParent != ID) |
| 133 | BAD(); |
| 134 | int s = fk_find(p->hClientSrc, p->hObjectSrc); |
| 135 | if (s < 0) |
| 136 | p->status = NV_ERR_OBJECT_NOT_FOUND; |
| 137 | else if (!fk_obj[s].shared && p->hClientSrc != p->hClient) |
| 138 | p->status = NV_ERR_INSUFFICIENT_PERMISSIONS; |
| 139 | else if (fk_find(p->hClient, p->hObject) >= 0) |
| 140 | p->status = NV_ERR_INSERT_DUPLICATE_NAME; |
| 141 | else { |
| 142 | int fd = fcntl(fk_obj[s].fd, F_DUPFD_CLOEXEC, 100); |
| 143 | if (fd < 0 || fk_new(p->hClient, p->hParent, p->hObject, fd, fk_obj[s].size) < 0) |
| 144 | BAD(); |
| 145 | p->status = NV_OK; |
| 146 | if (fk_dupzero) |
| 147 | p->hObject = 0; |
| 148 | } |
| 149 | } else if (req == REQ(NV_ESC_RM_FREE, NVOS00_PARAMETERS)) { |
| 150 | NVOS00_PARAMETERS *p = arg; |
| 151 | if (fk_inject(K_FREE, &p->status)) |
| 152 | return; |
| 153 | int i = fk_find(p->hRoot, p->hObjectOld); |
| 154 | if (i < 0 || fk_obj[i].parent != p->hObjectParent) { |
| 155 | p->status = NV_ERR_OBJECT_NOT_FOUND; |
| 156 | return; |
| 157 | } |
| 158 | /* nvmrel order: the GPU mappings of the object go before the object. */ |
| 159 | for (int r = 0; r < 8; r++) |
| 160 | if (fk_rng[r].used && fk_rng[r].mem == p->hObjectOld && (fk_rng[r].dma || fk_rng[r].ext)) |
| 161 | BAD(); |
| 162 | close(fk_obj[i].fd); |
| 163 | fk_obj[i].used = 0; |
| 164 | p->status = NV_OK; |
| 165 | } else if (req == REQ(NV_ESC_RM_MAP_MEMORY, nv_ioctl_nvos33_parameters_with_fd)) { |
| 166 | nv_ioctl_nvos33_parameters_with_fd *w = arg; |
| 167 | NVOS33_PARAMETERS *p = &w->params; |
| 168 | if (fk_inject(K_MAP, &p->status)) |
| 169 | return; |
| 170 | if (p->hClient != IC || p->hDevice != ID || w->fd < 3 || w->fd == FK_CTL || |
| 171 | p->flags != (fk_mode ? NVOS33_FLAGS_ACCESS_READ_ONLY : 0) || p->pLinearAddress) |
| 172 | BAD(); |
| 173 | int i = fk_find(p->hClient, p->hMemory); |
| 174 | if (i < 0) |
| 175 | p->status = NV_ERR_OBJECT_NOT_FOUND; |
| 176 | else if (p->offset >= fk_obj[i].size) |
| 177 | p->status = NV_ERR_INVALID_BASE; |
| 178 | else if (p->length == 0 || p->length > fk_obj[i].size - p->offset) |
| 179 | p->status = NV_ERR_INVALID_LIMIT; |
| 180 | else { |
| 181 | if (dup3(fk_obj[i].fd, w->fd, O_CLOEXEC) != w->fd) |
| 182 | BAD(); |
| 183 | p->status = NV_OK; |
| 184 | } |
| 185 | } else if (req == REQ(NV_ESC_RM_MAP_MEMORY_DMA, NVOS46_PARAMETERS)) { |
| 186 | NVOS46_PARAMETERS *p = arg; |
| 187 | if (fk_inject(K_DMA, &p->status)) |
| 188 | return; |
| 189 | int i = fk_find(p->hClient, p->hMemory), r = fk_range(p->dmaOffset); |
| 190 | /* context.om nvalloc flags; WolframLanguageEvaluator BitOr[2^15,2^8,2^4] -> 33040. */ |
| 191 | if (p->hClient != IC || p->hDevice != ID || p->hDma != IV || p->offset != 0 || |
| 192 | p->flags != (33040u | (fk_mode ? NVOS46_FLAGS_ACCESS_READ_ONLY : 0)) || |
| 193 | p->flags2 != 0 || p->kindOverride != 0) |
| 194 | BAD(); |
| 195 | if (i < 0 || r < 0 || fk_rng[r].dma || p->length != fk_obj[i].size || fk_rng[r].size != p->length) { |
| 196 | p->status = NV_ERR_INVALID_ARGUMENT; |
| 197 | return; |
| 198 | } |
| 199 | fk_rng[r].dma = 1; |
| 200 | fk_rng[r].mem = p->hMemory; |
| 201 | if (fk_dmashift) |
| 202 | p->dmaOffset += 4096; |
| 203 | p->status = NV_OK; |
| 204 | } else if (req == REQ(NV_ESC_RM_UNMAP_MEMORY_DMA, NVOS47_PARAMETERS)) { |
| 205 | NVOS47_PARAMETERS *p = arg; |
| 206 | if (fk_inject(K_UNDMA, &p->status)) |
| 207 | return; |
| 208 | int r = fk_range(p->dmaOffset); |
| 209 | if (p->hClient != IC || p->hDevice != ID || p->hDma != IV || p->flags != 0) |
| 210 | BAD(); |
| 211 | /* UVM_FREE of the range goes first in nvmrel order; the DMA record stays here. */ |
| 212 | if (r >= 0 || p->size != SZ) { |
| 213 | p->status = NV_ERR_INVALID_ARGUMENT; |
| 214 | return; |
| 215 | } |
| 216 | p->status = NV_OK; |
| 217 | } else |
| 218 | BAD(); |
| 219 | } |
| 220 | |
| 221 | static void fk_uvm(unsigned long req, void *arg) |
| 222 | { |
| 223 | if (req == UVM_CREATE_EXTERNAL_RANGE) { |
| 224 | UVM_CREATE_EXTERNAL_RANGE_PARAMS *p = arg; |
| 225 | if (fk_inject(K_RANGE, &p->rmStatus)) |
| 226 | return; |
| 227 | for (int i = 0; i < 8; i++) |
| 228 | if (!fk_rng[i].used) { |
| 229 | fk_rng[i] = (struct fkrange){ p->base, p->length, 0, 0, 0, 1 }; |
| 230 | p->rmStatus = NV_OK; |
| 231 | return; |
| 232 | } |
| 233 | BAD(); |
| 234 | } else if (req == UVM_MAP_EXTERNAL_ALLOCATION) { |
| 235 | UVM_MAP_EXTERNAL_ALLOCATION_PARAMS *p = arg; |
| 236 | if (fk_inject(K_EXT, &p->rmStatus)) |
| 237 | return; |
| 238 | int r = fk_range(p->base); |
| 239 | if (p->offset != 0 || p->gpuAttributesCount != 1 || p->rmCtrlFd != FK_CTL || p->hClient != IC || |
| 240 | memcmp(&p->perGpuAttributes[0].gpuUuid, fk_uuid, 16) != 0 || |
| 241 | p->perGpuAttributes[0].gpuMappingType != |
| 242 | (fk_mode ? UvmGpuMappingTypeReadOnly : UvmGpuMappingTypeReadWriteAtomic)) |
| 243 | BAD(); |
| 244 | if (r < 0 || !fk_rng[r].dma || fk_rng[r].ext || fk_rng[r].mem != p->hMemory || p->length != fk_rng[r].size) { |
| 245 | p->rmStatus = NV_ERR_INVALID_ARGUMENT; |
| 246 | return; |
| 247 | } |
| 248 | fk_rng[r].ext = 1; |
| 249 | p->rmStatus = NV_OK; |
| 250 | } else if (req == UVM_FREE) { |
| 251 | UVM_FREE_PARAMS *p = arg; |
| 252 | if (fk_inject(K_UFREE, &p->rmStatus)) |
| 253 | return; |
| 254 | int r = fk_range(p->base); |
| 255 | if (r < 0 || p->length != fk_rng[r].size) { |
| 256 | p->rmStatus = NV_ERR_INVALID_ARGUMENT; |
| 257 | return; |
| 258 | } |
| 259 | fk_rng[r].used = 0; |
| 260 | p->rmStatus = NV_OK; |
| 261 | } else |
| 262 | BAD(); |
| 263 | } |
| 264 | |
| 265 | static void fk_sigsys(int signal, siginfo_t *info, void *opaque) |
| 266 | { |
| 267 | (void)signal; |
| 268 | ucontext_t *uc = opaque; |
| 269 | int saved = errno; |
| 270 | #if defined(__aarch64__) |
| 271 | long fd = (long)uc->uc_mcontext.regs[0]; |
| 272 | unsigned long req = uc->uc_mcontext.regs[1]; |
| 273 | void *arg = (void *)(uintptr_t)uc->uc_mcontext.regs[2]; |
| 274 | uc->uc_mcontext.regs[0] = 0; |
| 275 | #elif defined(__x86_64__) |
| 276 | long fd = (long)uc->uc_mcontext.gregs[REG_RDI]; |
| 277 | unsigned long req = uc->uc_mcontext.gregs[REG_RSI]; |
| 278 | void *arg = (void *)(uintptr_t)uc->uc_mcontext.gregs[REG_RDX]; |
| 279 | uc->uc_mcontext.gregs[REG_RAX] = 0; |
| 280 | #else |
| 281 | #error "native-share-test requires Linux AArch64 or x86-64" |
| 282 | #endif |
| 283 | if (info->si_syscall != SYS_ioctl) |
| 284 | BAD(); |
| 285 | else if (fd == FK_CTL) |
| 286 | fk_rm(req, arg); |
| 287 | else if (fd == FK_UVM) |
| 288 | fk_uvm(req, arg); |
| 289 | else |
| 290 | BAD(); |
| 291 | errno = saved; |
| 292 | } |
| 293 | |
| 294 | static void fk_install(void) |
| 295 | { |
| 296 | struct sigaction action = { 0 }; |
| 297 | action.sa_sigaction = fk_sigsys; |
| 298 | action.sa_flags = SA_SIGINFO; |
| 299 | sigemptyset(&action.sa_mask); |
| 300 | CHECK(sigaction(SIGSYS, &action, NULL) == 0); |
| 301 | struct sock_filter filters[] = { |
| 302 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)), |
| 303 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_ioctl, 0, 4), |
| 304 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, args[0])), |
| 305 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, FK_CTL, 1, 0), |
| 306 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, FK_UVM, 0, 1), |
| 307 | BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP), |
| 308 | BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), |
| 309 | }; |
| 310 | struct sock_fprog program = { .len = sizeof filters / sizeof filters[0], .filter = filters }; |
| 311 | CHECK(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == 0); |
| 312 | CHECK(prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &program) == 0); |
| 313 | } |
| 314 | |
| 315 | static omega_native_context *ctx; |
| 316 | static uint8_t par[9264]; |
| 317 | static const char devpath[] = "/dev/null"; |
| 318 | |
| 319 | static unsigned calls(void) |
| 320 | { |
| 321 | unsigned n = 0; |
| 322 | for (int k = 0; k < K_N; k++) |
| 323 | n += fk_calls[k]; |
| 324 | return n; |
| 325 | } |
| 326 | static int64_t ex(zcshare *s, zcrec *r) |
| 327 | { |
| 328 | uint64_t res = 0xdead; |
| 329 | CHECK(omega2_ctx_nvshare_export(ctx, &res, (uint8_t *)s, (uint8_t *)r, par, sizeof par) == 0); |
| 330 | CHECK((int64_t)res == s->err); |
| 331 | return (int64_t)res; |
| 332 | } |
| 333 | static int64_t im_ctx(omega_native_context *c, zcshare *s, const zcrec *r) |
| 334 | { |
| 335 | uint64_t res = 0xdead; |
| 336 | CHECK(omega2_ctx_nvshare_import(c, &res, (uint8_t *)s, (const uint8_t *)r, par, sizeof par, |
| 337 | (const uint8_t *)devpath, strlen(devpath)) == 0); |
| 338 | CHECK((int64_t)res == s->err); |
| 339 | return (int64_t)res; |
| 340 | } |
| 341 | static int64_t im(zcshare *s, const zcrec *r) { return im_ctx(ctx, s, r); } |
| 342 | static int64_t rel(zcshare *s) |
| 343 | { |
| 344 | uint64_t res = 0xdead; |
| 345 | CHECK(omega2_ctx_nvshare_release(ctx, &res, (uint8_t *)s, par, sizeof par) == 0); |
| 346 | CHECK((int64_t)res == s->err); |
| 347 | return (int64_t)res; |
| 348 | } |
| 349 | static void block(zcshare *s, uint32_t newh, uint32_t mode, uint64_t size, uint64_t addr) |
| 350 | { |
| 351 | memset(s, 0, sizeof *s); |
| 352 | s->ctlfd = FK_CTL; |
| 353 | s->uvmfd = FK_UVM; |
| 354 | s->client = IC; |
| 355 | s->device = ID; |
| 356 | s->vaspace = IV; |
| 357 | s->newh = newh; |
| 358 | s->mode = mode; |
| 359 | s->size = size; |
| 360 | s->addr = addr; |
| 361 | memcpy(s->uuid, fk_uuid, 16); |
| 362 | fk_mode = mode; |
| 363 | } |
| 364 | static int unmapped(uint64_t addr) |
| 365 | { |
| 366 | unsigned char v; |
| 367 | return mincore((void *)(uintptr_t)addr, 4096, &v) != 0 && errno == ENOMEM; |
| 368 | } |
| 369 | /* Write one byte at addr in a child: 0 if the write landed, else the terminating signal. */ |
| 370 | static int child_write(uint64_t addr) |
| 371 | { |
| 372 | pid_t c = fork(); |
| 373 | CHECK(c >= 0); |
| 374 | if (!c) { |
| 375 | *(volatile uint8_t *)(uintptr_t)addr = 0x99; |
| 376 | _exit(0); |
| 377 | } |
| 378 | int st = 0; |
| 379 | CHECK(waitpid(c, &st, 0) == c); |
| 380 | return WIFSIGNALED(st) ? WTERMSIG(st) : WEXITSTATUS(st) ? -1 : 0; |
| 381 | } |
| 382 | /* A clean refusal: the expected error, no RM or UVM call, nothing live, no fault. */ |
| 383 | static void refused(zcshare *s, const zcrec *r, int64_t want, int line) |
| 384 | { |
| 385 | unsigned before = calls(); |
| 386 | int64_t got = im(s, r); |
| 387 | if (got != want || calls() != before || s->live || s->fault || s->obj) { |
| 388 | fprintf(stderr, "native-share: FAIL line %d: refusal got %lld want %lld calls %u->%u live %u fault %u\n", |
| 389 | line, (long long)got, (long long)want, before, calls(), s->live, s->fault); |
| 390 | exit(1); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | int main(void) |
| 395 | { |
| 396 | /* Owner object: two pages with a known pattern, also mapped here to watch aliasing. */ |
| 397 | int ofd = memfd_create("zero-copy-owner", MFD_CLOEXEC); |
| 398 | CHECK(ofd >= 0 && ftruncate(ofd, SZ) == 0); |
| 399 | uint8_t *ov = mmap(NULL, SZ, PROT_READ | PROT_WRITE, MAP_SHARED, ofd, 0); |
| 400 | CHECK(ov != MAP_FAILED); |
| 401 | for (unsigned i = 0; i < SZ; i++) |
| 402 | ov[i] = (uint8_t)(i * 7 + 1); |
| 403 | int ofd2 = memfd_create("zero-copy-owner2", MFD_CLOEXEC); |
| 404 | CHECK(ofd2 >= 0 && ftruncate(ofd2, SZ) == 0); |
| 405 | CHECK(fk_new(OC, OD, OH, ofd, SZ) >= 0 && fk_new(OC, OD, OH2, ofd2, SZ) >= 0); |
| 406 | CHECK(omega_native_context_create(4, &ctx) == 0); |
| 407 | fk_install(); |
| 408 | |
| 409 | /* Export. */ |
| 410 | zcshare own, s; |
| 411 | zcrec rec, junk; |
| 412 | memset(&own, 0, sizeof own); |
| 413 | own.ctlfd = FK_CTL; |
| 414 | own.client = OC; |
| 415 | own.device = OD; |
| 416 | own.obj = OH; |
| 417 | own.size = SZ; |
| 418 | memset(&rec, 0xaa, sizeof rec); |
| 419 | CHECK(ex(&own, &rec) == 0 && fk_calls[K_SHARE] == 1 && own.stage == 51 && !fk_bad); |
| 420 | CHECK(rec.magic == 1129991503u && rec.version == 1 && rec.client == OC && rec.handle == OH && rec.size == SZ); |
| 421 | CHECK(fk_obj[fk_find(OC, OH)].shared && !fk_obj[fk_find(OC, OH2)].shared); |
| 422 | /* Export refusals: no RM call, record untouched; an RM refusal is its positive status. */ |
| 423 | unsigned before = calls(); |
| 424 | zcshare bad = own; |
| 425 | memset(&junk, 0xaa, sizeof junk); |
| 426 | zcrec junk0 = junk; |
| 427 | bad.obj = 0; |
| 428 | CHECK(ex(&bad, &junk) == -22); |
| 429 | bad = own; |
| 430 | bad.size = 4097; |
| 431 | CHECK(ex(&bad, &junk) == -22); |
| 432 | bad.size = 0; |
| 433 | CHECK(ex(&bad, &junk) == -22); |
| 434 | bad.size = 68719476736ull + 4096; /* WolframLanguageEvaluator 2^36 -> 68719476736 */ |
| 435 | CHECK(ex(&bad, &junk) == -22); |
| 436 | bad = own; |
| 437 | bad.fault = 1; |
| 438 | CHECK(ex(&bad, &junk) == -5); |
| 439 | CHECK(calls() == before && memcmp(&junk, &junk0, sizeof junk) == 0); |
| 440 | bad = own; |
| 441 | bad.obj = OH + 99; |
| 442 | CHECK(ex(&bad, &junk) == NV_ERR_OBJECT_NOT_FOUND && fk_calls[K_SHARE] == 2); |
| 443 | CHECK(memcmp(&junk, &junk0, sizeof junk) == 0); |
| 444 | |
| 445 | /* Read/write import at A1: one RM duplicate, size proof, CPU map, GPU map. */ |
| 446 | block(&s, NEWH, 0, SZ, A1); |
| 447 | uint64_t res = 0xdead; |
| 448 | before = calls(); |
| 449 | CHECK(omega2_ctx_nvshare_import(ctx, &res, (uint8_t *)&s, (const uint8_t *)&rec, par, sizeof par - 1, |
| 450 | (const uint8_t *)devpath, strlen(devpath)) == OMEGA_BAD_ARG); |
| 451 | CHECK(res == 0xdead && calls() == before && s.live == 0); |
| 452 | CHECK(im(&s, &rec) == 0 && !fk_bad); |
| 453 | CHECK(s.live == 31 && s.stage == 57 && s.obj == NEWH && s.map > 0 && s.fault == 0); |
| 454 | CHECK(fk_calls[K_DUP] == 1 && fk_calls[K_MAP] == 2 && fk_calls[K_RANGE] == 1 && fk_calls[K_DMA] == 1 && |
| 455 | fk_calls[K_EXT] == 1); |
| 456 | uint8_t *a = (uint8_t *)(uintptr_t)A1; |
| 457 | CHECK(memcmp(a, ov, SZ) == 0); |
| 458 | a[100] = 0x5c; |
| 459 | ov[SZ - 1] = 0x77; |
| 460 | CHECK(ov[100] == 0x5c && a[SZ - 1] == 0x77); |
| 461 | CHECK(child_write(A1 + 64) == 0); |
| 462 | /* The owner's object goes away; the duplicate keeps its own reference. */ |
| 463 | int oi = fk_find(OC, OH); |
| 464 | int keep = dup(fk_obj[oi].fd); |
| 465 | close(fk_obj[oi].fd); |
| 466 | fk_obj[oi].used = 0; |
| 467 | CHECK(a[100] == 0x5c && fk_find(IC, NEWH) >= 0); |
| 468 | /* An import cannot be re-exported, and the block holds one import at a time. */ |
| 469 | before = calls(); |
| 470 | CHECK(ex(&s, &junk) == -1 && im(&s, &rec) == -16 && calls() == before && s.live == 31); |
| 471 | |
| 472 | /* Release through another context: the RM and UVM steps run, then mapdrop finds the token |
| 473 | * foreign and the call traps (bridge status 3, result unwritten). The block, written in |
| 474 | * place, still records the CPU mapping (live 16|2); the right context finishes the release. */ |
| 475 | omega_native_context *other = NULL; |
| 476 | CHECK(omega_native_context_create(2, &other) == 0); |
| 477 | CHECK(omega_native_context_destroy(ctx) == OMEGA_CONTEXT_BUSY); |
| 478 | res = 0xdead; |
| 479 | uint32_t st = omega2_ctx_nvshare_release(other, &res, (uint8_t *)&s, par, sizeof par); |
| 480 | CHECK(st == 3 /* Omega trap 3: mapdrop of a foreign token */ && res == 0xdead && s.live == 18 && s.fault == 0); |
| 481 | CHECK(s.map > 0 && a[100] == 0x5c); |
| 482 | CHECK(fk_calls[K_UFREE] == 1 && fk_calls[K_UNDMA] == 1 && fk_calls[K_FREE] == 1 && fk_find(IC, NEWH) < 0); |
| 483 | CHECK(omega_native_context_destroy(other) == 0); |
| 484 | CHECK(rel(&s) == 0 && s.live == 0 && s.obj == 0 && s.map == 0 && !fk_bad); |
| 485 | CHECK(fk_calls[K_UFREE] == 1 && fk_calls[K_UNDMA] == 1 && fk_calls[K_FREE] == 1); |
| 486 | CHECK(unmapped(A1)); |
| 487 | CHECK(rel(&s) == 0 && calls() == before + 3); |
| 488 | /* Restore the owner's object for the remaining imports. */ |
| 489 | CHECK(fk_new(OC, OD, OH, keep, SZ) >= 0); |
| 490 | fk_obj[fk_find(OC, OH)].shared = 1; |
| 491 | |
| 492 | /* A complete release order: UVM free, DMA unmap, RM free, CPU unmap; then the context is empty. */ |
| 493 | block(&s, NEWH, 0, SZ, A1); |
| 494 | CHECK(im(&s, &rec) == 0 && s.live == 31); |
| 495 | CHECK(rel(&s) == 0 && s.live == 0 && unmapped(A1) && !fk_bad); |
| 496 | CHECK(fk_calls[K_UFREE] == 2 && fk_calls[K_UNDMA] == 2 && fk_calls[K_FREE] == 2); |
| 497 | |
| 498 | /* Read-only import: every access flag read-only; a CPU write faults. */ |
| 499 | block(&s, NEWH, 1, SZ, A2); |
| 500 | CHECK(im(&s, &rec) == 0 && s.live == 31 && !fk_bad); |
| 501 | CHECK(memcmp((void *)(uintptr_t)A2, ov, SZ) == 0); |
| 502 | CHECK(child_write(A2 + 64) == SIGSEGV); |
| 503 | CHECK(rel(&s) == 0 && unmapped(A2)); |
| 504 | fk_mode = 0; |
| 505 | |
| 506 | /* Clean refusals before any RM call. */ |
| 507 | zcrec r2; |
| 508 | block(&s, NEWH, 0, SZ, A1); |
| 509 | r2 = rec; |
| 510 | r2.magic++; |
| 511 | refused(&s, &r2, -22, __LINE__); |
| 512 | r2 = rec; |
| 513 | r2.version = 2; |
| 514 | refused(&s, &r2, -22, __LINE__); |
| 515 | r2 = rec; |
| 516 | r2.client = 0; |
| 517 | refused(&s, &r2, -22, __LINE__); |
| 518 | r2 = rec; |
| 519 | r2.handle = 0; |
| 520 | refused(&s, &r2, -22, __LINE__); |
| 521 | r2 = rec; |
| 522 | r2.size = SZ + 1; |
| 523 | refused(&s, &r2, -22, __LINE__); |
| 524 | block(&s, NEWH, 0, SZ * 2, A1); |
| 525 | refused(&s, &rec, -34, __LINE__); |
| 526 | block(&s, NEWH, 2, SZ, A1); |
| 527 | refused(&s, &rec, -22, __LINE__); |
| 528 | block(&s, 0, 0, SZ, A1); |
| 529 | refused(&s, &rec, -22, __LINE__); |
| 530 | block(&s, NEWH, 0, SZ, A1 + 16); |
| 531 | refused(&s, &rec, -75, __LINE__); |
| 532 | block(&s, NEWH, 0, SZ, 4096); |
| 533 | refused(&s, &rec, -75, __LINE__); |
| 534 | /* WolframLanguageEvaluator 2^40 - 8192 -> 1099511619584: the first page past the bound. */ |
| 535 | block(&s, NEWH, 0, SZ, 1099511619584ull + 4096); |
| 536 | refused(&s, &rec, -75, __LINE__); |
| 537 | block(&s, NEWH, 0, SZ, A1); |
| 538 | s.fault = 1; |
| 539 | before = calls(); |
| 540 | CHECK(im(&s, &rec) == -5 && calls() == before); |
| 541 | |
| 542 | /* RM refusals: an unshared object, an unknown one; positive status, nothing live. */ |
| 543 | block(&s, NEWH, 0, SZ, A1); |
| 544 | r2 = rec; |
| 545 | r2.handle = OH2; |
| 546 | CHECK(im(&s, &r2) == NV_ERR_INSUFFICIENT_PERMISSIONS && s.live == 0 && s.fault == 0 && s.obj == 0); |
| 547 | r2.handle = OH + 5; |
| 548 | CHECK(im(&s, &r2) == NV_ERR_OBJECT_NOT_FOUND && s.live == 0 && s.fault == 0); |
| 549 | |
| 550 | /* Size lies: the record claims more or less than the object holds. The duplicate is freed. */ |
| 551 | unsigned frees = fk_calls[K_FREE]; |
| 552 | block(&s, NEWH, 0, SZ * 2, A1); |
| 553 | r2 = rec; |
| 554 | r2.size = SZ * 2; |
| 555 | CHECK(im(&s, &r2) == -34 && s.live == 0 && s.fault == 0 && s.obj == 0 && s.stage == 53); |
| 556 | CHECK(fk_calls[K_FREE] == frees + 1 && fk_find(IC, NEWH) < 0 && unmapped(A1)); |
| 557 | block(&s, NEWH, 0, 4096, A1); |
| 558 | r2.size = 4096; |
| 559 | CHECK(im(&s, &r2) == -34 && s.live == 0 && s.fault == 0 && fk_calls[K_FREE] == frees + 2); |
| 560 | /* An RM refusal of the CPU map is that status, also cleanly undone. */ |
| 561 | block(&s, NEWH, 0, SZ, A1); |
| 562 | fk_fail_kind = K_MAP; |
| 563 | fk_fail_status = NV_ERR_NO_MEMORY; |
| 564 | CHECK(im(&s, &rec) == NV_ERR_NO_MEMORY && s.live == 0 && s.fault == 0 && fk_calls[K_FREE] == frees + 3); |
| 565 | |
| 566 | /* Slots: a context with one free slot takes one import; the next is refused cleanly. */ |
| 567 | omega_native_context *small = NULL; |
| 568 | CHECK(omega_native_context_create(1, &small) == 0); |
| 569 | block(&s, NEWH, 0, SZ, A1); |
| 570 | CHECK(im_ctx(small, &s, &rec) == 0 && s.live == 31); |
| 571 | zcshare s2; |
| 572 | block(&s2, NEWH + 1, 0, SZ, A3); |
| 573 | CHECK(im_ctx(small, &s2, &rec) == -24 && s2.live == 0 && s2.fault == 0 && fk_find(IC, NEWH + 1) < 0); |
| 574 | CHECK(omega_native_context_destroy(small) == OMEGA_CONTEXT_BUSY); |
| 575 | res = 0xdead; |
| 576 | CHECK(omega2_ctx_nvshare_release(small, &res, (uint8_t *)&s, par, sizeof par) == 0 && res == 0); |
| 577 | CHECK(s.live == 0 && omega_native_context_destroy(small) == 0); |
| 578 | |
| 579 | /* Faults. A duplicate returned as handle 0 faults the block; later imports are refused. */ |
| 580 | block(&s, NEWH, 0, SZ, A1); |
| 581 | fk_dupzero = 1; |
| 582 | CHECK(im(&s, &rec) == -22 && s.fault == 1 && s.live == 0); |
| 583 | fk_dupzero = 0; |
| 584 | CHECK(im(&s, &rec) == -5); |
| 585 | CHECK(fk_find(IC, NEWH) >= 0); |
| 586 | fk_obj[fk_find(IC, NEWH)].used = 0; /* the fake's leaked duplicate */ |
| 587 | /* A refused DMA map faults with the CPU map and UVM range live; release undoes both. */ |
| 588 | block(&s, NEWH, 0, SZ, A1); |
| 589 | fk_fail_kind = K_DMA; |
| 590 | fk_fail_status = NV_ERR_INVALID_ARGUMENT; |
| 591 | CHECK(im(&s, &rec) == NV_ERR_INVALID_ARGUMENT && s.fault == 1 && s.live == 7 && s.stage == 55); |
| 592 | CHECK(rel(&s) == 0 && s.live == 0 && s.fault == 1 && unmapped(A1) && fk_find(IC, NEWH) < 0 && !fk_bad); |
| 593 | CHECK(im(&s, &rec) == -5); |
| 594 | /* A DMA map at another address than asked faults with everything recorded for release. */ |
| 595 | block(&s, NEWH, 0, SZ, A1); |
| 596 | fk_dmashift = 1; |
| 597 | CHECK(im(&s, &rec) == -22 && s.fault == 1 && s.live == 15); |
| 598 | fk_dmashift = 0; |
| 599 | CHECK(rel(&s) == 0 && s.live == 0 && unmapped(A1) && !fk_bad); |
| 600 | /* A refused UVM external mapping faults; release still undoes the rest. */ |
| 601 | block(&s, NEWH, 0, SZ, A1); |
| 602 | fk_fail_kind = K_EXT; |
| 603 | fk_fail_status = NV_ERR_INVALID_ARGUMENT; |
| 604 | CHECK(im(&s, &rec) == NV_ERR_INVALID_ARGUMENT && s.fault == 1 && s.live == 15 && s.stage == 56); |
| 605 | CHECK(rel(&s) == 0 && s.live == 0 && unmapped(A1) && !fk_bad); |
| 606 | /* A refused RM free keeps the duplicate and the CPU map live for a retry. */ |
| 607 | block(&s, NEWH, 0, SZ, A1); |
| 608 | CHECK(im(&s, &rec) == 0); |
| 609 | fk_fail_kind = K_FREE; |
| 610 | fk_fail_status = NV_ERR_INVALID_STATE; |
| 611 | CHECK(rel(&s) == NV_ERR_INVALID_STATE && s.live == 19 && s.fault == 1 && !unmapped(A1)); |
| 612 | CHECK(rel(&s) == 0 && s.live == 0 && unmapped(A1) && fk_find(IC, NEWH) < 0); |
| 613 | |
| 614 | CHECK(!fk_bad); |
| 615 | CHECK(omega_native_context_destroy(ctx) == 0); |
| 616 | printf("ZERO-COPY SHARE NATIVE PASS (export, read/write and read-only import, aliasing, release order, " |
| 617 | "refusals, faults, slots; fake RM via seccomp, HOST_ONLY)\n"); |
| 618 | return 0; |
| 619 | } |