drd: Add a consistency check
[valgrind.git] / memcheck / tests / test-plo.c
blob5d4da6b9f359cad179ae80c73c0935a2494136e5
1 #include "tests/malloc.h"
2 #include <stdio.h>
3 #include <assert.h>
5 typedef unsigned long long int ULong;
6 typedef unsigned long int UWord;
8 __attribute__((noinline))
9 static int my_ffsll ( ULong x )
11 int i;
12 for (i = 0; i < 64; i++) {
13 if ((x & 1ULL) == 1ULL)
14 break;
15 x >>= 1;
17 return i+1;
20 /* Find length of string, assuming it is aligned and shorter than 8
21 characters. Little-endian only. */
22 __attribute__((noinline))
23 static int aligned_strlen(char *s)
25 /* This is for 64-bit platforms */
26 assert(sizeof(ULong) == 8);
27 /* ..and only works for aligned input */
28 assert(((unsigned long)s & 0x7) == 0);
30 /* read 8 bytes */
31 ULong val = *(ULong*)s;
32 /* Subtract one from each byte */
33 ULong val2 = val - 0x0101010101010101ULL;
34 /* Find lowest byte whose high bit changed */
35 val2 ^= val;
36 val2 &= 0x8080808080808080ULL;
38 return (my_ffsll(val2) / 8) - 1;
41 __attribute__((noinline)) void foo ( int x )
43 __asm__ __volatile__("":::"memory");
46 int
47 main(int argc, char *argv[])
49 char *buf = memalign16(5);
50 buf[0] = 'a';
51 buf[1] = 'b';
52 buf[2] = 'c';
53 buf[3] = 'd';
54 buf[4] = '\0';
56 /* --partial-loads-ok=no: expect addr error (here) */
57 /* --partial-loads-ok=yes: expect no error */
58 if (aligned_strlen(buf) == 4)
59 foo(44);
61 /* --partial-loads-ok=no: expect addr error (here) */
62 /* --partial-loads-ok=yes: expect value error (in my_ffsll) */
63 buf[4] = 'x';
64 if (aligned_strlen(buf) == 0)
65 foo(37);
67 free(buf);
69 /* Also, we need to check that a completely out-of-range,
70 word-sized load gives an addressing error regardless of the
71 start of --partial-loads-ok=. *And* that the resulting
72 value is completely defined. */
73 UWord* words = malloc(3 * sizeof(UWord));
74 free(words);
76 /* Should ALWAYS give an addr error. */
77 UWord w = words[1];
79 /* Should NEVER give an error (you might expect a value one, but no.) */
80 if (w == 0x31415927) {
81 fprintf(stderr,
82 "Elvis is alive and well and living in Milton Keynes.\n");
85 return 0;