interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / test_core_residue.c

172 lines · 5840 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Admission messages against the C snprintf format, Ampere packing against the
3 * inline sign-extend formula, and a cubin image written only under /tmp.
4 * WolframLanguageEvaluator: {Mod[56, 64], 128/64, 192/64, 256 + 32,
5 * BitAnd[16^^FFFFFFFF, 2147483648]} -> {56, 2, 3, 288, 2147483648}. */
6#include "omega_cubin.h"
7#include "cudapp_native.h"
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <stdint.h>
13#include <unistd.h>
14
15static int fails;
16
17static void fail(const char *what) {
18 fprintf(stderr, "RESIDUE FAIL %s\n", what);
19 fails++;
20}
21
22static uint64_t sext32(uint64_t v) {
23 v &= 0xffffffffull;
24 if (v & 0x80000000ull) v |= 0xffffffff00000000ull;
25 return v;
26}
27
28static void oracle0(const uint64_t src[16], uint64_t dst[8]) {
29 int i;
30 for (i = 0; i < 6; i++) dst[i] = sext32(src[i]);
31 dst[6] = (src[6] & 0xffffffffull) | ((src[7] & 0xffffffffull) << 32);
32 dst[7] = src[8] | (src[9] << 1) | (src[10] << 2) | (src[15] << 8)
33 | (src[11] << 16) | (src[12] << 24) | (src[13] << 32) | (src[14] << 40);
34}
35
36/* 0 match, 2 mismatch, 1 status error. */
37static int ampack_one(uint64_t mode, const uint64_t src[16], int flip) {
38 uint64_t got[16], want[16];
39 size_t n = mode == 0 ? 8 : 16;
40 size_t i;
41 memset(got, 0x5a, sizeof got);
42 if (omega_ampack(&(uint64_t){0}, mode, src, 16, got, 16) != 0) {
43 fail("ampack status");
44 return 1;
45 }
46 if (mode == 0) oracle0(src, want);
47 else for (i = 0; i < 16; i++) want[i] = sext32(src[i]);
48 if (flip) got[0] ^= 1;
49 if (memcmp(got, want, n * sizeof(uint64_t)) != 0) return 2;
50 return 0;
51}
52
53static int ampack_cases(void) {
54 static const uint64_t edge[] = {0, 1, 0x7fffffffull, 0x80000000ull, 0xffffffffull};
55 size_t a;
56 for (a = 0; a < sizeof edge / sizeof edge[0]; a++) {
57 uint64_t src[16];
58 int i;
59 for (i = 0; i < 16; i++) src[i] = edge[(a + (size_t)i) % 5] ^ (uint64_t)(i * 17);
60 src[1] = 0xffffffff80000000ull;
61 src[6] = 0x1122334455667788ull;
62 src[7] = 0xaabbccddeeff0011ull;
63 src[15] = 7;
64 if (ampack_one(0, src, 0) != 0 || ampack_one(1, src, 0) != 0) {
65 fail("ampack bytes");
66 return 1;
67 }
68 }
69 return 0;
70}
71
72static int admit_one(uint32_t sm, size_t nparams, size_t cap) {
73 char err[128], want[128];
74 uint8_t text[16];
75 uint8_t *out = NULL;
76 size_t len = 0;
77 int rc, want_rc = 0;
78 memset(text, 0x11, sizeof text);
79 memset(err, 0xa5, sizeof err);
80 want[0] = 0;
81 rc = omega_cubin_pack_target(sm, "k", text, sizeof text, NULL, nparams, 1024, 8, 0,
82 &out, &len, cap ? err : NULL, cap);
83 if (sm != 86 && sm != 121) {
84 if (cap) snprintf(want, cap, "unsupported cubin target sm_%u", sm);
85 want_rc = OMEGA_CUBIN_E_TARGET;
86 } else if (nparams) {
87 if (cap) snprintf(want, cap, "null parameter array");
88 want_rc = OMEGA_CUBIN_E_ARG;
89 }
90 if (rc != want_rc) {
91 fprintf(stderr, "RESIDUE FAIL admit rc sm=%u np=%zu got=%d want=%d\n", sm, nparams, rc, want_rc);
92 fails++;
93 omega_cubin_free(out);
94 return 1;
95 }
96 if (cap && strcmp(err, want) != 0) {
97 fprintf(stderr, "RESIDUE FAIL admit msg sm=%u got='%s' want='%s'\n", sm, err, want);
98 fails++;
99 omega_cubin_free(out);
100 return 1;
101 }
102 if (want_rc && (out || len)) fail("refusal left an image");
103 omega_cubin_free(out);
104 return fails ? 1 : 0;
105}
106
107static int image_tmp(void) {
108 uint8_t text[16];
109 uint8_t *a = NULL, *b = NULL;
110 size_t na = 0, nb = 0;
111 char err[128];
112 char path[] = "/tmp/core-residue-XXXXXX";
113 int fd;
114 FILE *f;
115 uint8_t magic[4];
116 memset(text, 0x3c, sizeof text);
117 if (omega_cubin_pack_target(86, "k", text, sizeof text, NULL, 0, 1024, 8, 0, &a, &na, err, sizeof err) != 0 ||
118 omega_cubin_pack_target(121, "k", text, sizeof text, NULL, 0, 1024, 8, 0, &b, &nb, err, sizeof err) != 0) {
119 fail("accepted target refused");
120 omega_cubin_free(a);
121 omega_cubin_free(b);
122 return 1;
123 }
124 if (na < 64 || nb < 64 || memcmp(a, "\177ELF", 4) != 0 || memcmp(b, "\177ELF", 4) != 0) {
125 fail("image missing ELF magic");
126 omega_cubin_free(a);
127 omega_cubin_free(b);
128 return 1;
129 }
130 fd = mkstemp(path);
131 if (fd < 0 || write(fd, a, na) != (ssize_t)na) {
132 fail("tmp cubin write");
133 if (fd >= 0) close(fd);
134 omega_cubin_free(a);
135 omega_cubin_free(b);
136 return 1;
137 }
138 close(fd);
139 f = fopen(path, "rb");
140 if (!f || fread(magic, 1, 4, f) != 4 || memcmp(magic, "\177ELF", 4) != 0) fail("tmp cubin reread");
141 if (f) fclose(f);
142 if (unlink(path) != 0) fail("tmp cubin unlink");
143 omega_cubin_free(a);
144 omega_cubin_free(b);
145 return fails ? 1 : 0;
146}
147
148int main(int argc, char **argv) {
149 static const uint32_t sms[] = {0, 85, 86, 87, 120, 121, 122, 0xffffffffu};
150 uint64_t src[16];
151 size_t i;
152 int flip = argc > 1 && strcmp(argv[1], "--flip") == 0;
153 for (i = 0; i < sizeof sms / sizeof sms[0]; i++) {
154 if (admit_one(sms[i], 0, sizeof(char[128]))) return 1;
155 if (admit_one(sms[i], 1, 8)) return 1;
156 }
157 if (admit_one(0, 0, 0)) return 1;
158 if (image_tmp()) return 1;
159 if (flip) {
160 memset(src, 0, sizeof src);
161 src[1] = 0xffffffff80000000ull;
162 /* Exit 0 only if the corrupted word still matches. The makefile treats that as a broken control. */
163 if (ampack_one(0, src, 1) == 2) {
164 fputs("CORE-RESIDUE ampack flip caught\n", stderr);
165 return 1;
166 }
167 return 0;
168 }
169 if (ampack_cases()) return 1;
170 puts("CORE-RESIDUE PASS: admission, /tmp cubin, ampack");
171 return fails ? 1 : 0;
172}