• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Test strchr for strict_string_checks=false does not look beyond necessary
2 // char.
3 // RUN: %clang_asan %s -o %t
4 // RUN: %env_asan_opts=strict_string_checks=false %run %t 2>&1
5 // RUN: %env_asan_opts=strict_string_checks=true not %run %t 2>&1 | FileCheck %s
6 
7 #include <assert.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/mman.h>
12 #include <unistd.h>
13 
main(int argc,char ** argv)14 int main(int argc, char **argv) {
15   size_t page_size = sysconf(_SC_PAGE_SIZE);
16   size_t size = 2 * page_size;
17   char *s = (char *)mmap(0, size, PROT_READ | PROT_WRITE,
18                          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
19   assert(s);
20   assert(((uintptr_t)s & (page_size - 1)) == 0);
21   memset(s, 'o', size);
22   s[size - 1] = 0;
23 
24   char *p = s + page_size - 1;
25   *p = 'x';
26 
27   if (mprotect(p + 1, 1, PROT_NONE))
28     return 1;
29   char *r = strchr(s, 'x');
30   // CHECK: AddressSanitizer: {{SEGV|BUS}} on unknown address
31   assert(r == p);
32 
33   return 0;
34 }
35