1*673dc3d4SNico Weber // Emulate the behavior of the NVIDIA CUDA driver
2*673dc3d4SNico Weber // that mmaps memory inside the asan's shadow gap.
3*673dc3d4SNico Weber //
4*673dc3d4SNico Weber // REQUIRES: x86_64-target-arch, shadow-scale-3
5*673dc3d4SNico Weber //
6*673dc3d4SNico Weber // RUN: %clangxx_asan %s -o %t
7*673dc3d4SNico Weber // RUN: not %env_asan_opts=protect_shadow_gap=1 %t 2>&1 | FileCheck %s  --check-prefix=CHECK-PROTECT1
8*673dc3d4SNico Weber // RUN: not                                     %t 2>&1 | FileCheck %s  --check-prefix=CHECK-PROTECT1
9*673dc3d4SNico Weber // RUN: not %env_asan_opts=protect_shadow_gap=0 %t 2>&1 | FileCheck %s  --check-prefix=CHECK-PROTECT0
10*673dc3d4SNico Weber #include <assert.h>
11*673dc3d4SNico Weber #include <unistd.h>
12*673dc3d4SNico Weber #include <sys/mman.h>
13*673dc3d4SNico Weber #include <stdint.h>
14*673dc3d4SNico Weber 
15*673dc3d4SNico Weber #include "sanitizer/asan_interface.h"
16*673dc3d4SNico Weber 
main(void)17*673dc3d4SNico Weber int main(void) {
18*673dc3d4SNico Weber   uintptr_t Base = 0x200000000;
19*673dc3d4SNico Weber   uintptr_t Size = 0x1100000000;
20*673dc3d4SNico Weber   void *addr =
21*673dc3d4SNico Weber       mmap((void *)Base, Size, PROT_READ | PROT_WRITE,
22*673dc3d4SNico Weber            MAP_NORESERVE | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
23*673dc3d4SNico Weber   assert(addr == (void*)Base);
24*673dc3d4SNico Weber   // Make sure we can access memory in shadow gap.
25*673dc3d4SNico Weber   // W/o protect_shadow_gap=0 we should fail here.
26*673dc3d4SNico Weber   for (uintptr_t Addr = Base; Addr < Base + Size; Addr += Size / 100)
27*673dc3d4SNico Weber     *(char*)Addr = 1;
28*673dc3d4SNico Weber   // CHECK-PROTECT1: AddressSanitizer: SEGV on unknown address 0x0000bfff8000
29*673dc3d4SNico Weber 
30*673dc3d4SNico Weber   // Poison a part of gap's shadow:
31*673dc3d4SNico Weber   __asan_poison_memory_region((void*)Base, 4096);
32*673dc3d4SNico Weber   // Now we should fail with use-after-poison.
33*673dc3d4SNico Weber   *(char*)(Base + 1234) = 1;
34*673dc3d4SNico Weber   // CHECK-PROTECT0: AddressSanitizer: use-after-poison on address 0x0002000004d2
35*673dc3d4SNico Weber }
36