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