aboutsummaryrefslogtreecommitdiff
path: root/test/msan/Linux/glob_altdirfunc.cc
blob: 2c02e735e05e499df840e61349aac932be5e4242 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// RUN: %clangxx_msan -m64 -O0 %s -o %t && %run %t %p 2>&1 | FileCheck %s
// RUN: %clangxx_msan -m64 -O0 -D_FILE_OFFSET_BITS=64 %s -o %t && %run %t %p 2>&1 | FileCheck %s
// RUN: %clangxx_msan -m64 -O3 %s -o %t && %run %t %p 2>&1 | FileCheck %s

#include <assert.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>

#include <sanitizer/msan_interface.h>

static void my_gl_closedir(void *dir) {
  if (!dir)
    exit(1);
  closedir((DIR *)dir);
}

static struct dirent *my_gl_readdir(void *dir) {
  if (!dir)
    exit(1);
  struct dirent *d = readdir((DIR *)dir);
  if (d) __msan_poison(d, d->d_reclen); // hehe
  return d;
}

static void *my_gl_opendir(const char *s) {
  assert(__msan_test_shadow(s, strlen(s) + 1) == (size_t)-1);
  return opendir(s);
}

static int my_gl_lstat(const char *s, struct stat *st) {
  assert(__msan_test_shadow(s, strlen(s) + 1) == (size_t)-1);
  if (!st)
    exit(1);
  return lstat(s, st);
}

static int my_gl_stat(const char *s, struct stat *st) {
  assert(__msan_test_shadow(s, strlen(s) + 1) == (size_t)-1);
  if (!st)
    exit(1);
  return lstat(s, st);
}

int main(int argc, char *argv[]) {
  assert(argc == 2);
  char buf[1024];
  snprintf(buf, sizeof(buf), "%s/%s", argv[1], "glob_test_root/*a");

  glob_t globbuf;
  globbuf.gl_closedir = my_gl_closedir;
  globbuf.gl_readdir = my_gl_readdir;
  globbuf.gl_opendir = my_gl_opendir;
  globbuf.gl_lstat = my_gl_lstat;
  globbuf.gl_stat = my_gl_stat;
  for (int i = 0; i < 10000; ++i) {
    int res = glob(buf, GLOB_ALTDIRFUNC | GLOB_MARK, 0, &globbuf);
    assert(res == 0);
    printf("%d %s\n", errno, strerror(errno));
    assert(globbuf.gl_pathc == 2);
    printf("%zu\n", strlen(globbuf.gl_pathv[0]));
    printf("%zu\n", strlen(globbuf.gl_pathv[1]));
    __msan_poison(globbuf.gl_pathv[0], strlen(globbuf.gl_pathv[0]) + 1);
    __msan_poison(globbuf.gl_pathv[1], strlen(globbuf.gl_pathv[1]) + 1);
    globfree(&globbuf);
  }

  printf("PASS\n");
  // CHECK: PASS
  return 0;
}