1*673dc3d4SNico Weber // FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=316
2*673dc3d4SNico Weber // XFAIL: android
3*673dc3d4SNico Weber
4*673dc3d4SNico Weber // RUN: rm -rf %t-dir
5*673dc3d4SNico Weber // RUN: mkdir -p %t-dir
6*673dc3d4SNico Weber //
7*673dc3d4SNico Weber // RUN: %clangxx_asan -O0 %s -DTEMP_DIR='"'"%t-dir"'"' -o %t && %run %t 2>&1 | FileCheck %s
8*673dc3d4SNico Weber // RUN: %clangxx_asan -O1 %s -DTEMP_DIR='"'"%t-dir"'"' -o %t && %run %t 2>&1 | FileCheck %s
9*673dc3d4SNico Weber // RUN: %clangxx_asan -O2 %s -DTEMP_DIR='"'"%t-dir"'"' -o %t && %run %t 2>&1 | FileCheck %s
10*673dc3d4SNico Weber // RUN: %clangxx_asan -O3 %s -DTEMP_DIR='"'"%t-dir"'"' -o %t && %run %t 2>&1 | FileCheck %s
11*673dc3d4SNico Weber //
12*673dc3d4SNico Weber // RUN: %clangxx_asan -O0 %s -D_FILE_OFFSET_BITS=64 -DTEMP_DIR='"'"%t-dir"'"' -o %t && %run %t 2>&1 | FileCheck %s
13*673dc3d4SNico Weber // RUN: %clangxx_asan -O1 %s -D_FILE_OFFSET_BITS=64 -DTEMP_DIR='"'"%t-dir"'"' -o %t && %run %t 2>&1 | FileCheck %s
14*673dc3d4SNico Weber // RUN: %clangxx_asan -O2 %s -D_FILE_OFFSET_BITS=64 -DTEMP_DIR='"'"%t-dir"'"' -o %t && %run %t 2>&1 | FileCheck %s
15*673dc3d4SNico Weber // RUN: %clangxx_asan -O3 %s -D_FILE_OFFSET_BITS=64 -DTEMP_DIR='"'"%t-dir"'"' -o %t && %run %t 2>&1 | FileCheck %s
16*673dc3d4SNico Weber
17*673dc3d4SNico Weber #include <dirent.h>
18*673dc3d4SNico Weber #include <memory.h>
19*673dc3d4SNico Weber #include <stdio.h>
20*673dc3d4SNico Weber #include <stdlib.h>
21*673dc3d4SNico Weber #include <unistd.h>
22*673dc3d4SNico Weber
23*673dc3d4SNico Weber
main()24*673dc3d4SNico Weber int main() {
25*673dc3d4SNico Weber // Ensure the readdir_r interceptor doesn't erroneously mark the entire dirent
26*673dc3d4SNico Weber // as written when the end of the directory pointer is reached.
27*673dc3d4SNico Weber fputs("test1: reading the " TEMP_DIR " directory...\n", stderr);
28*673dc3d4SNico Weber DIR *d = opendir(TEMP_DIR);
29*673dc3d4SNico Weber struct dirent *result = (struct dirent *)(0xfeedbeef);
30*673dc3d4SNico Weber // We assume the temp dir for this test doesn't have crazy long file names.
31*673dc3d4SNico Weber char entry_buffer[4096];
32*673dc3d4SNico Weber memset(entry_buffer, 0xab, sizeof(entry_buffer));
33*673dc3d4SNico Weber unsigned count = 0;
34*673dc3d4SNico Weber do {
35*673dc3d4SNico Weber // Stamp the entry struct to try to trick the interceptor.
36*673dc3d4SNico Weber ((struct dirent *)entry_buffer)->d_reclen = 9999;
37*673dc3d4SNico Weber if (readdir_r(d, (struct dirent *)entry_buffer, &result) != 0)
38*673dc3d4SNico Weber abort();
39*673dc3d4SNico Weber ++count;
40*673dc3d4SNico Weber } while (result != NULL);
41*673dc3d4SNico Weber fprintf(stderr, "read %d entries\n", count);
42*673dc3d4SNico Weber closedir(d);
43*673dc3d4SNico Weber // CHECK: test1: reading the {{.*}} directory...
44*673dc3d4SNico Weber // CHECK-NOT: stack-buffer-overflow
45*673dc3d4SNico Weber // CHECK: read {{.*}} entries
46*673dc3d4SNico Weber
47*673dc3d4SNico Weber // Ensure the readdir64_r interceptor doesn't have the bug either.
48*673dc3d4SNico Weber fputs("test2: reading the " TEMP_DIR " directory...\n", stderr);
49*673dc3d4SNico Weber d = opendir(TEMP_DIR);
50*673dc3d4SNico Weber struct dirent64 *result64;
51*673dc3d4SNico Weber memset(entry_buffer, 0xab, sizeof(entry_buffer));
52*673dc3d4SNico Weber count = 0;
53*673dc3d4SNico Weber do {
54*673dc3d4SNico Weber // Stamp the entry struct to try to trick the interceptor.
55*673dc3d4SNico Weber ((struct dirent64 *)entry_buffer)->d_reclen = 9999;
56*673dc3d4SNico Weber if (readdir64_r(d, (struct dirent64 *)entry_buffer, &result64) != 0)
57*673dc3d4SNico Weber abort();
58*673dc3d4SNico Weber ++count;
59*673dc3d4SNico Weber } while (result64 != NULL);
60*673dc3d4SNico Weber fprintf(stderr, "read %d entries\n", count);
61*673dc3d4SNico Weber closedir(d);
62*673dc3d4SNico Weber // CHECK: test2: reading the {{.*}} directory...
63*673dc3d4SNico Weber // CHECK-NOT: stack-buffer-overflow
64*673dc3d4SNico Weber // CHECK: read {{.*}} entries
65*673dc3d4SNico Weber }
66