sewardj | dc6734b | 2006-04-02 01:53:01 +0000 | [diff] [blame^] | 1 | // This program is a thorough test of the LOADVn/STOREVn shadow memory |
| 2 | // operations. |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <sys/mman.h> |
| 9 | #include "memcheck/memcheck.h" |
| 10 | |
| 11 | // All the sizes here are in *bytes*, not bits. |
| 12 | |
| 13 | typedef unsigned char U1; |
| 14 | typedef unsigned short U2; |
| 15 | typedef unsigned int U4; |
| 16 | typedef unsigned long long U8; |
| 17 | |
| 18 | typedef float F4; |
| 19 | typedef double F8; |
| 20 | |
| 21 | #define PAGE_SIZE 4096ULL |
| 22 | |
| 23 | |
| 24 | // XXX: should check the error cases for SET/GET_VBITS also |
| 25 | |
| 26 | // For the byte 'x', build a value of 'size' bytes from that byte, eg: |
| 27 | // size 1 --> x |
| 28 | // size 2 --> xx |
| 29 | // size 4 --> xxxx |
| 30 | // size 8 --> xxxxxxxx |
| 31 | // where the 0 bits are seen by Memcheck as defined, and the 1 bits are |
| 32 | // seen as undefined (ie. the value of each bit matches its V bit, ie. the |
| 33 | // resulting value is the same as its metavalue). |
| 34 | // |
| 35 | U8 build(int size, U1 byte) |
| 36 | { |
| 37 | int i; |
| 38 | U8 mask = 0; |
| 39 | U8 shres; |
| 40 | U8 res = 0xffffffffffffffffULL, res2; |
| 41 | VALGRIND_MAKE_WRITABLE(&res, 8); |
| 42 | assert(1 == size || 2 == size || 4 == size || 8 == size); |
| 43 | |
| 44 | for (i = 0; i < size; i++) { |
| 45 | mask <<= 8; |
| 46 | mask |= (U8)byte; |
| 47 | } |
| 48 | |
| 49 | res &= mask; |
| 50 | |
| 51 | // res is now considered partially defined, but we know exactly what its |
| 52 | // value is (it happens to be the same as its metavalue). |
| 53 | |
| 54 | VALGRIND_GET_VBITS(&res, &shres, 8); |
| 55 | res2 = res; |
| 56 | VALGRIND_MAKE_READABLE(&res2, 8); // avoid the 'undefined' warning |
| 57 | assert(res2 == shres); |
| 58 | return res; |
| 59 | } |
| 60 | |
| 61 | U1 make_defined ( U1 x ) |
| 62 | { |
| 63 | volatile U1 xx = x; |
| 64 | VALGRIND_MAKE_READABLE(&xx, 1); |
| 65 | return xx; |
| 66 | } |
| 67 | |
| 68 | void check(U1* arr, int n, char* who) |
| 69 | { |
| 70 | int i; |
| 71 | U1* shadow = malloc(n); |
| 72 | U1 arr_i; |
| 73 | U8 sum = 0; |
| 74 | VALGRIND_GET_VBITS(arr, shadow, n); |
| 75 | for (i = 0; i < n; i++) { |
| 76 | arr_i = make_defined(arr[i]); |
| 77 | if (arr_i != shadow[i]) { |
| 78 | fprintf(stderr, "\n\nFAILURE: %s, byte %d -- " |
| 79 | "is 0x%x, should be 0x%x\n\n", |
| 80 | who, i, shadow[i], arr[i]); |
| 81 | exit(1); |
| 82 | } |
| 83 | sum += (U8)arr_i; |
| 84 | } |
| 85 | free(shadow); |
| 86 | printf("test passed, sum = %llu (%9.5f per byte)\n", |
| 87 | sum, (F8)sum / (F8)n); |
| 88 | } |
| 89 | |
| 90 | static inline U4 randomU4 ( void ) |
| 91 | { |
| 92 | static U4 n = 0; |
| 93 | /* From "Numerical Recipes in C" 2nd Edition */ |
| 94 | n = 1664525UL * n + 1013904223UL; |
| 95 | return n; |
| 96 | } |
| 97 | |
| 98 | static inline U1 randomU1 ( void ) |
| 99 | { |
| 100 | return 0xFF & (randomU4() >> 13); |
| 101 | } |
| 102 | |
| 103 | #define N_BYTES 300000 |
| 104 | #define N_EVENTS (5 * N_BYTES) |
| 105 | |
| 106 | |
| 107 | void do_test_at ( U1* arr ) |
| 108 | { |
| 109 | int i; |
| 110 | |
| 111 | U4 mv1 = 0, mv2 = 0, mv4 = 0, mv8 = 0, mv4f = 0, mv8f = 0; |
| 112 | |
| 113 | /* Fill arr with random bytes whose shadows match them. */ |
| 114 | printf("-------- arr = %p\n", arr); |
| 115 | |
| 116 | printf("initialising\n"); |
| 117 | for (i = 0; i < N_BYTES; i++) |
| 118 | arr[i] = (U1)build(1, randomU1()); |
| 119 | |
| 120 | printf("post-initialisation check\n"); |
| 121 | check(arr, N_BYTES, "after initialisation"); |
| 122 | |
| 123 | /* Now do huge numbers of memory copies. */ |
| 124 | printf("doing copies\n"); |
| 125 | for (i = 0; i < N_EVENTS; i++) { |
| 126 | U4 ty, src, dst; |
| 127 | ty = (randomU4() >> 13) % 5; |
| 128 | tryagain: |
| 129 | src = (randomU4() >> 1) % N_BYTES; |
| 130 | dst = (randomU4() >> 3) % N_BYTES; |
| 131 | switch (ty) { |
| 132 | case 0: { // U1 |
| 133 | *(U1*)(arr+dst) = *(U1*)(arr+src); |
| 134 | mv1++; |
| 135 | break; |
| 136 | } |
| 137 | case 1: { // U2 |
| 138 | if (src+2 >= N_BYTES || dst+2 >= N_BYTES) |
| 139 | goto tryagain; |
| 140 | *(U2*)(arr+dst) = *(U2*)(arr+src); |
| 141 | mv2++; |
| 142 | break; |
| 143 | } |
| 144 | case 2: { // U4 |
| 145 | if (src+4 >= N_BYTES || dst+4 >= N_BYTES) |
| 146 | goto tryagain; |
| 147 | *(U4*)(arr+dst) = *(U4*)(arr+src); |
| 148 | mv4++; |
| 149 | break; |
| 150 | } |
| 151 | case 3: { // U8 |
| 152 | if (src+8 >= N_BYTES || dst+8 >= N_BYTES) |
| 153 | goto tryagain; |
| 154 | *(U8*)(arr+dst) = *(U8*)(arr+src); |
| 155 | mv8++; |
| 156 | break; |
| 157 | } |
| 158 | /* Don't bother with 32-bit floats. These cause |
| 159 | horrible complications, as discussed in sh-mem.c. */ |
| 160 | /* |
| 161 | case 4: { // F4 |
| 162 | if (src+4 >= N_BYTES || dst+4 >= N_BYTES) |
| 163 | goto tryagain; |
| 164 | *(F4*)(arr+dst) = *(F4*)(arr+src); |
| 165 | mv4f++; |
| 166 | break; |
| 167 | } |
| 168 | */ |
| 169 | case 4: { // F8 |
| 170 | if (src+8 >= N_BYTES || dst+8 >= N_BYTES) |
| 171 | goto tryagain; |
| 172 | *(F8*)(arr+dst) = *(F8*)(arr+src); |
| 173 | mv8f++; |
| 174 | break; |
| 175 | } |
| 176 | default: |
| 177 | fprintf(stderr, "sh-mem-random: bad size\n"); |
| 178 | exit(0); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | printf("final check\n"); |
| 183 | check(arr, N_BYTES, "final check"); |
| 184 | |
| 185 | printf("counts 1/2/4/8/F4/F8: %d %d %d %d %d %d\n", |
| 186 | mv1, mv2, mv4, mv8, mv4f, mv8f); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | int main(void) |
| 192 | { |
| 193 | U1* arr; |
| 194 | |
| 195 | if (0 == RUNNING_ON_VALGRIND) { |
| 196 | fprintf(stderr, "error: this program only works when run under Valgrind\n"); |
| 197 | exit(1); |
| 198 | } |
| 199 | |
| 200 | printf("-------- testing non-auxmap range --------\n"); |
| 201 | |
| 202 | arr = malloc(N_BYTES); |
| 203 | assert(arr); |
| 204 | do_test_at(arr); |
| 205 | free(arr); |
| 206 | |
| 207 | if (sizeof(void*) == 8) { |
| 208 | // 64-bit platform. |
| 209 | int tries; |
| 210 | int nbytes_p; |
| 211 | U1* huge_addr = (U1*)0x6600000000; // 408GB |
| 212 | // Note, kernel 2.6.? on Athlon64 refuses fixed mmap requests |
| 213 | // at above 512GB. |
| 214 | |
| 215 | printf("-------- testing auxmap range --------\n"); |
| 216 | |
| 217 | nbytes_p = (N_BYTES + PAGE_SIZE) & ~(PAGE_SIZE-1); |
| 218 | |
| 219 | for (tries = 0; tries < 10; tries++) { |
| 220 | arr = mmap(huge_addr, nbytes_p, PROT_READ|PROT_WRITE, |
| 221 | MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0,0); |
| 222 | if (arr != MAP_FAILED) |
| 223 | break; |
| 224 | // hmm. fudge the address and try again. |
| 225 | huge_addr += (randomU4() & ~(PAGE_SIZE-1)); |
| 226 | } |
| 227 | |
| 228 | if (tries >= 10) { |
| 229 | fprintf(stderr, "sh-mem-random: can't mmap hi-mem\n"); |
| 230 | exit(0); |
| 231 | } |
| 232 | assert(arr != MAP_FAILED); |
| 233 | |
| 234 | do_test_at(arr); |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | |
| 239 | } |