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 / nvrm / policy_test.c

114 lines · 6331 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* HOST_ONLY. Compares the C oracle and the Omega policy on the same inputs.
3 * No device node is opened. NVRM_POLICY_MUTANT=1 flips one input so the
4 * comparison must fail: that is the red check. */
5#include "nvrm_policy.h"
6#include <stdio.h>
7#include <stdint.h>
8
9static int fails;
10
11static void __attribute__((unused)) eq(const char *name, uint64_t c, uint64_t o) {
12 if (c != o) {
13 printf("DIFF %s c=%llu o=%llu\n", name, (unsigned long long)c, (unsigned long long)o);
14 fails++;
15 }
16}
17
18static void __attribute__((unused)) eq4(const char *name, const uint64_t *c, const uint64_t *o) {
19 for (int i = 0; i < 4; i++) {
20 if (c[i] != o[i]) {
21 printf("DIFF %s[%d] c=%llu o=%llu\n", name, i,
22 (unsigned long long)c[i], (unsigned long long)o[i]);
23 fails++;
24 }
25 }
26}
27
28int main(void) {
29#ifdef NVRM_POLICY_MUTANT
30 /* Red check: one input differs, so the two sides must disagree. */
31 if (nvrm_c_nrstat(0) == nvrm_om_nrstat(0x51)) {
32 printf("RED CHECK FAILED: a flipped input still compared equal\n");
33 return 0;
34 }
35 printf("NVRM policy red check caught the flipped input\n");
36 return 1;
37#else
38 eq("nrstat0", nvrm_c_nrstat(0), nvrm_om_nrstat(0));
39 eq("nrstat_nomem", nvrm_c_nrstat(0x51), nvrm_om_nrstat(0x51));
40 eq("nrstat_other", nvrm_c_nrstat(1), nvrm_om_nrstat(1));
41
42 eq("hnext_root", nvrm_c_hnext(0x41, 5, 0), nvrm_om_hnext(0x41, 5, 0));
43 eq("hnext_adv", nvrm_c_hnext(0x42, 7, 3), nvrm_om_hnext(0x42, 7, 3));
44 eq("hnext_handle_full", nvrm_c_hnext(0x42, 4294967295ull, 0), nvrm_om_hnext(0x42, 4294967295ull, 0));
45 eq("hnext_acct_full", nvrm_c_hnext(0x41, 0, 18446744073709551615ull), nvrm_om_hnext(0x41, 0, 18446744073709551615ull));
46
47 eq("vunits0", nvrm_c_vunits(0), nvrm_om_vunits(0));
48 eq("vunits1", nvrm_c_vunits(1), nvrm_om_vunits(1));
49 eq("vunits_4g", nvrm_c_vunits(4294967296ull), nvrm_om_vunits(4294967296ull));
50 eq("vunits_over", nvrm_c_vunits(18446744073709551615ull), nvrm_om_vunits(18446744073709551615ull));
51
52 uint64_t co[4] = {0}, oo[4] = {0};
53 uint64_t cd = nvrm_c_vcover(0x100000000ull, 0x100000000ull, 0x100000100ull, co);
54 uint64_t od = nvrm_om_vcover(0x100000000ull, 0x100000000ull, 0x100000100ull, oo);
55 eq("vcover_covered", cd, od);
56 eq4("vcover_covered_out", co, oo);
57 cd = nvrm_c_vcover(0x100000000ull, 0x100000000ull, 0x200000000ull, co);
58 od = nvrm_om_vcover(0x100000000ull, 0x100000000ull, 0x200000000ull, oo);
59 eq("vcover_grow", cd, od);
60 eq4("vcover_grow_out", co, oo);
61 eq("vcover_noslot", nvrm_c_vcover(0, 1, 2, co), nvrm_om_vcover(0, 1, 2, oo));
62 eq("vcover_bad", nvrm_c_vcover(0x100ull, 0x50ull, 0x200ull, co), nvrm_om_vcover(0x100ull, 0x50ull, 0x200ull, oo));
63 cd = nvrm_c_vcover(0x100000000ull, 0x100000000ull, 0x200000001ull, co);
64 od = nvrm_om_vcover(0x100000000ull, 0x100000000ull, 0x200000001ull, oo);
65 eq("vcover_cas", cd, od);
66 eq4("vcover_cas_out", co, oo);
67 eq("vcover_units0", nvrm_c_vcover(1, 18446744069414584322ull, 2, co), nvrm_om_vcover(1, 18446744069414584322ull, 2, oo));
68 eq("vcover_endv", nvrm_c_vcover(0x100ull, 0x200ull, 0x50ull, co), nvrm_om_vcover(0x100ull, 0x200ull, 0x50ull, oo));
69 eq("vcover_want0", nvrm_c_vcover(1, 2, 18446744069414584322ull, co), nvrm_om_vcover(1, 2, 18446744069414584322ull, oo));
70
71 eq("palign_ok", nvrm_c_palign(0x1000, 0x100), nvrm_om_palign(0x1000, 0x100));
72 eq("palign_bad", nvrm_c_palign(0x1000, 3), nvrm_om_palign(0x1000, 3));
73 eq("palign_zero", nvrm_c_palign(0x1000, 0), nvrm_om_palign(0x1000, 0));
74 eq("palign_over", nvrm_c_palign(18446744073709551605ull, 256), nvrm_om_palign(18446744073709551605ull, 256));
75
76 eq("pspan_ok", nvrm_c_pspan(0, 4, 64, 0x1000), nvrm_om_pspan(0, 4, 64, 0x1000));
77 eq("pspan_misalign", nvrm_c_pspan(1, 4, 64, 0x1000), nvrm_om_pspan(1, 4, 64, 0x1000));
78 eq("pspan_zero", nvrm_c_pspan(0, 0, 64, 0x1000), nvrm_om_pspan(0, 0, 64, 0x1000));
79 eq("pspan_big", nvrm_c_pspan(0, 2097152ull, 64, 0x1000), nvrm_om_pspan(0, 2097152ull, 64, 0x1000));
80 eq("pspan_vaover", nvrm_c_pspan(0, 4, 64, 18446744073709551615ull), nvrm_om_pspan(0, 4, 64, 18446744073709551615ull));
81 eq("pspan_offover", nvrm_c_pspan(18446744073709551600ull, 4, 18446744073709551615ull, 0), nvrm_om_pspan(18446744073709551600ull, 4, 18446744073709551615ull, 0));
82 eq("pspan_spanbig", nvrm_c_pspan(0, 4, 8, 0), nvrm_om_pspan(0, 4, 8, 0));
83 eq("pspan_vaoverflow", nvrm_c_pspan(0x1000, 4, 18446744073709551615ull, 18446744073709547520ull), nvrm_om_pspan(0x1000, 4, 18446744073709551615ull, 18446744073709547520ull));
84
85 eq("pfull_room", nvrm_c_pfull(3, 1, 8), nvrm_om_pfull(3, 1, 8));
86 eq("pfull_full", nvrm_c_pfull(8, 1, 8), nvrm_om_pfull(8, 1, 8));
87 eq("pspan_limit", nvrm_c_pspan(0, 2097151ull, 8388604ull, 0x1000), nvrm_om_pspan(0, 2097151ull, 8388604ull, 0x1000));
88 eq("pfull_wrap", nvrm_c_pfull(1, 4294967295ull, 4), nvrm_om_pfull(1, 4294967295ull, 4));
89
90 eq("chadm_clean", nvrm_c_chadm(0, 0, 0, 0, 1, 3), nvrm_om_chadm(0, 0, 0, 0, 1, 3));
91 eq("chadm_dirty", nvrm_c_chadm(1, 0, 0, 0, 0, 3), nvrm_om_chadm(1, 0, 0, 0, 0, 3));
92 eq("chadm_extra", nvrm_c_chadm(0, 0, 0, 0, 4, 3), nvrm_om_chadm(0, 0, 0, 0, 4, 3));
93 eq("chadm_chgroup", nvrm_c_chadm(0, 1, 0, 0, 1, 3), nvrm_om_chadm(0, 1, 0, 0, 1, 3));
94 eq("chadm_regd", nvrm_c_chadm(0, 0, 1, 0, 1, 3), nvrm_om_chadm(0, 0, 1, 0, 1, 3));
95 eq("chadm_nextra", nvrm_c_chadm(0, 0, 0, 1, 1, 3), nvrm_om_chadm(0, 0, 0, 1, 1, 3));
96
97 eq("retry_ok", nvrm_c_retry(1, 0, 0, 3), nvrm_om_retry(1, 0, 0, 3));
98 eq("retry_other", nvrm_c_retry(0, 0, 0, 3), nvrm_om_retry(0, 0, 0, 3));
99 eq("retry_budget", nvrm_c_retry(0, 1, 3, 3), nvrm_om_retry(0, 1, 3, 3));
100 eq("retry_again", nvrm_c_retry(0, 1, 1, 3), nvrm_om_retry(0, 1, 1, 3));
101 eq("retry_loop", nvrm_c_retry(1, 1, 5, 1), nvrm_om_retry(1, 1, 5, 1));
102 eq("retry_nonmem", nvrm_c_retry(1, 0, 5, 0), nvrm_om_retry(1, 0, 5, 0));
103
104 eq("mthd", nvrm_c_mthd(1, 0x005c, 5), nvrm_om_mthd(1, 0x005c, 5));
105 eq("gpentry", nvrm_c_gpentry(0x1000, 16), nvrm_om_gpentry(0x1000, 16));
106
107 if (fails) {
108 printf("NVRM POLICY DIFFERENTIAL FAIL %d\n", fails);
109 return 1;
110 }
111 printf("NVRM POLICY DIFFERENTIAL PASS\n");
112 return 0;
113#endif
114}