1 // This program is a thorough test of the LOADVn/STOREVn shadow memory
8 #include "tests/sys_mman.h"
9 #include "memcheck/memcheck.h"
11 // All the sizes here are in *bytes*, not bits.
13 typedef unsigned char U1
;
14 typedef unsigned short U2
;
15 typedef unsigned int U4
;
16 typedef unsigned long long U8
;
21 typedef unsigned long UWord
;
23 #define PAGE_SIZE 4096ULL
26 // XXX: should check the error cases for SET/GET_VBITS also
28 // For the byte 'x', build a value of 'size' bytes from that byte, eg:
32 // size 8 --> xxxxxxxx
33 // where the 0 bits are seen by Memcheck as defined, and the 1 bits are
34 // seen as undefined (ie. the value of each bit matches its V bit, ie. the
35 // resulting value is the same as its metavalue).
37 U8
build(int size
, U1 byte
)
42 U8 res
= 0xffffffffffffffffULL
, res2
;
43 (void)VALGRIND_MAKE_MEM_UNDEFINED(&res
, 8);
44 assert(1 == size
|| 2 == size
|| 4 == size
|| 8 == size
);
46 for (i
= 0; i
< size
; i
++) {
53 // res is now considered partially defined, but we know exactly what its
54 // value is (it happens to be the same as its metavalue).
56 (void)VALGRIND_GET_VBITS(&res
, &shres
, 8);
58 (void)VALGRIND_MAKE_MEM_DEFINED(&res2
, 8); // avoid the 'undefined' warning
59 assert(res2
== shres
);
63 U1
make_defined ( U1 x
)
66 (void)VALGRIND_MAKE_MEM_DEFINED(&xx
, 1);
70 void check(U1
* arr
, int n
, char* who
)
73 U1
* shadow
= malloc(n
);
76 (void)VALGRIND_GET_VBITS(arr
, shadow
, n
);
77 for (i
= 0; i
< n
; i
++) {
78 arr_i
= make_defined(arr
[i
]);
79 if (arr_i
!= shadow
[i
]) {
80 fprintf(stderr
, "\n\nFAILURE: %s, byte %d -- "
81 "is 0x%x, should be 0x%x\n\n",
82 who
, i
, shadow
[i
], arr
[i
]);
88 printf("test passed, sum = %llu (%9.5f per byte)\n",
89 sum
, (F8
)sum
/ (F8
)n
);
92 static inline U4
randomU4 ( void )
95 /* From "Numerical Recipes in C" 2nd Edition */
96 n
= 1664525UL * n
+ 1013904223UL;
100 static inline U1
randomU1 ( void )
102 return 0xFF & (randomU4() >> 13);
105 #define N_BYTES 300000
106 #define N_EVENTS (5 * N_BYTES)
109 void do_test_at ( U1
* arr
)
113 U4 mv1
= 0, mv2
= 0, mv4
= 0, mv8
= 0, mv4f
= 0, mv8f
= 0;
115 /* Fill arr with random bytes whose shadows match them. */
116 if (0) printf("-------- arr = %p\n", arr
);
118 printf("initialising\n");
119 for (i
= 0; i
< N_BYTES
; i
++)
120 arr
[i
] = (U1
)build(1, randomU1());
122 printf("post-initialisation check\n");
123 check(arr
, N_BYTES
, "after initialisation");
125 /* Now do huge numbers of memory copies. */
126 printf("doing copies\n");
127 for (i
= 0; i
< N_EVENTS
; i
++) {
129 ty
= (randomU4() >> 13) % 5;
131 src
= (randomU4() >> 1) % N_BYTES
;
132 dst
= (randomU4() >> 3) % N_BYTES
;
135 *(U1
*)(arr
+dst
) = *(U1
*)(arr
+src
);
140 if (src
+2 >= N_BYTES
|| dst
+2 >= N_BYTES
)
142 *(U2
*)(arr
+dst
) = *(U2
*)(arr
+src
);
147 if (src
+4 >= N_BYTES
|| dst
+4 >= N_BYTES
)
149 *(U4
*)(arr
+dst
) = *(U4
*)(arr
+src
);
154 if (src
+8 >= N_BYTES
|| dst
+8 >= N_BYTES
)
156 *(U8
*)(arr
+dst
) = *(U8
*)(arr
+src
);
160 /* Don't bother with 32-bit floats. These cause
161 horrible complications, as discussed in sh-mem.c. */
164 if (src+4 >= N_BYTES || dst+4 >= N_BYTES)
166 *(F4*)(arr+dst) = *(F4*)(arr+src);
172 if (src
+8 >= N_BYTES
|| dst
+8 >= N_BYTES
)
174 #if defined(__i386__)
175 /* Copying via an x87 register causes the test to fail,
176 because (I think) some obscure values that are FP
177 denormals get changed during the copy due to the FPU
178 normalising, or rounding, or whatever, them. This
179 causes them to no longer bit-for-bit match the
180 accompanying metadata. Yet we still need to do a
181 genuine 8-byte load/store to test the relevant memcheck
182 {LOADV8,STOREV8} routines. Hence use the MMX registers
183 instead, as copying through them should be
185 __asm__
__volatile__(
186 "movq (%1), %%mm2\n\t"
187 "movq %%mm2, (%0)\n\t"
189 : : "r"(arr
+dst
), "r"(arr
+src
) : "memory"
192 /* Straightforward. On amd64, this gives a load/store of
193 the bottom half of an xmm register. On ppc32/64 this
194 is a straighforward load/store of an FP register. */
195 *(F8
*)(arr
+dst
) = *(F8
*)(arr
+src
);
201 fprintf(stderr
, "sh-mem-random: bad size\n");
206 printf("final check\n");
207 check(arr
, N_BYTES
, "final check");
209 printf("counts 1/2/4/8/F4/F8: %d %d %d %d %d %d\n",
210 mv1
, mv2
, mv4
, mv8
, mv4f
, mv8f
);
219 if (0 == RUNNING_ON_VALGRIND
) {
220 fprintf(stderr
, "error: this program only works when run under Valgrind\n");
224 printf("-------- testing non-auxmap range --------\n");
226 arr
= malloc(N_BYTES
);
231 if (sizeof(void*) == 8) {
235 // (U1*)(UWord)constULL funny casting to keep gcc quiet on
237 U1
* huge_addr
= (U1
*)(UWord
)0x6600000000ULL
; // 408GB
238 // Note, kernel 2.6.? on Athlon64 refuses fixed mmap requests
241 printf("-------- testing auxmap range --------\n");
243 nbytes_p
= (N_BYTES
+ PAGE_SIZE
) & ~(PAGE_SIZE
-1);
245 for (tries
= 0; tries
< 10; tries
++) {
246 arr
= mmap(huge_addr
, nbytes_p
, PROT_READ
|PROT_WRITE
,
247 MAP_FIXED
|MAP_PRIVATE
|MAP_ANONYMOUS
, -1,0);
248 if (arr
!= MAP_FAILED
)
250 // hmm. fudge the address and try again.
251 huge_addr
+= (randomU4() & ~(PAGE_SIZE
-1));
255 fprintf(stderr
, "sh-mem-random: can't mmap hi-mem\n");
258 assert(arr
!= MAP_FAILED
);