Debian Codegraph @

gcc/testsuite/gcc.target/i386/strncmp-1.c

raw
1 /* { dg-do run { target { sysconf && mmap } } } */
2 /* { dg-options "-O2" } */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/mman.h>
9
10 #ifndef MAP_ANONYMOUS
11 #define MAP_ANONYMOUS MAP_ANON
12 #endif
13
14 int
15 __attribute__ ((noclone, noinline))
16 compare (char *d, char *s, unsigned int l)
17 {
18 return __builtin_strncmp (d, s, l);
19 }
20
21 int
22 main ()
23 {
24 size_t page_size = sysconf(_SC_PAGESIZE);
25 char *buf = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
26 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
27 if (buf == MAP_FAILED)
28 {
29 perror ("mmap");
30 abort ();
31 }
32
33 if (mprotect (buf + page_size, page_size, PROT_NONE))
34 {
35 perror ("mprotect");
36 abort ();
37 }
38
39 char *src1 = buf + page_size - sizeof ("foo");
40 char *src2 = buf;
41 memcpy (src1, "foo", sizeof ("foo"));
42 memcpy (src2, "foo", sizeof ("foo"));
43 int result = compare (src1, src2, sizeof ("foo") + 16);
44 if (result != 0)
45 abort ();
46 return 0;
47 }