huangcm
2025-02-28 b45e871a67cd1272e3da9ba5bd383f832b0f1824
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <paths.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
 
// crashes if built with -fsanitize=address
void test_crash_malloc() {
  volatile char* heap = malloc(32);
  heap[32] = heap[32];
  printf("ASAN: Heap Test Failed\n");
}
 
// crashes if built with -fsanitize=address
void test_crash_stack() {
  volatile char stack[32];
  volatile char* p_stack = stack;
  p_stack[32] = p_stack[32];
  printf("ASAN: Stack Test Failed\n");
}
 
int data_asan_exists() {
  int fd = open("/data/asan", O_DIRECTORY | O_PATH | O_CLOEXEC, 0);
  if(fd < 0) {
    printf("ASAN: Missing /data/asan\n");
    return 1;
  }
  close(fd);
  return 0;
}
 
// crashes if built with -fsanitize=memory
void test_msan_crash_stack() {
  volatile int stack[10];
  stack[5] = 0;
  if (stack[0]) {
    stack[0] = 1;
  }
  printf("MSAN: Stack Test Failed\n");
}
 
// crashes if built with -fsanitize=integer
void test_integer_overflow() {
  size_t max = (size_t)-1;
  max++;
  printf("UBSAN: Integer Overflow Test Failed\n");
}
 
// returns 0 if kcov is enabled
int test_kcov() {
  const char* kcov_file = "/sys/kernel/debug/kcov";
  int fd = open(kcov_file, O_RDWR);
  if (fd == -1) {
    printf("KCOV: Could not open %s\n", kcov_file);
    return 1;
  }
  close(fd);
  return 0;
}
 
// returns 0 if kasan was compiled in
int test_kasan() {
  // rely on the exit status of grep to propagate
  if (system("gzip -d < /proc/config.gz | grep CONFIG_KASAN=y >/dev/null")) {
    printf("KASAN: CONFIG_KASAN not in /proc/config.gz\n");
    return 1;
  }
  return 0;
}
 
// executes a test that is expected to crash
// returns 0 if the test crashes
int test(void (*function)()) {
  fflush(stdout);
 
  pid_t child = fork();
  int status = 0;
 
  if (child == -1) {
    perror("fork");
    exit(1);
  }
 
  if (child == 0) {
    // Silence the ASAN report that is generated
    close(2);
 
    // Invoke the target function.  If it does not crash, terminate the process.
    function();
    exit(EXIT_SUCCESS);
  }
 
  // Wait for the child to either crash, or exit cleanly
  while (child == waitpid(child, &status, 0)) {
    if (!WIFEXITED(status))
      continue;
    if (WEXITSTATUS(status) == EXIT_SUCCESS)
      return 1;
    break;
  }
  return 0;
}
 
int have_option(const char* option, const char** argv, const int argc) {
  for (int i = 1; i < argc; i++)
    if (!strcmp(option, argv[i]))
      return 1;
  return 0;
}
 
int sanitizer_status(int argc, const char** argv) {
  int test_everything = 0;
  int failures = 0;
 
  if (argc <= 1)
    test_everything = 1;
 
  if (test_everything || have_option("asan", argv, argc)) {
    int asan_failures = 0;
 
#if !defined(ANDROID_SANITIZE_ADDRESS) && !defined(ANDROID_SANITIZE_HWADDRESS)
    asan_failures += 1;
    printf("ASAN: Compiler flags failed!\n");
#endif
 
    asan_failures += test(test_crash_malloc);
    asan_failures += test(test_crash_stack);
    asan_failures += data_asan_exists();
 
    if (!asan_failures)
      printf("ASAN: OK\n");
 
    failures += asan_failures;
  }
 
  if(test_everything || have_option("cov", argv, argc)) {
    int cov_failures = 0;
 
#ifndef ANDROID_SANITIZE_COVERAGE
    printf("COV: Compiler flags failed!\n");
    cov_failures += 1;
#endif
 
    if (!cov_failures)
      printf("COV: OK\n");
 
    failures += cov_failures;
  }
 
  if (test_everything || have_option("msan", argv, argc)) {
    int msan_failures = 0;
 
    msan_failures += test(test_msan_crash_stack);
 
    if (!msan_failures)
      printf("MSAN: OK\n");
 
    failures += msan_failures;
  }
 
  if (test_everything || have_option("kasan", argv, argc)) {
    int kasan_failures = 0;
 
    kasan_failures += test_kasan();
 
    if(!kasan_failures)
      printf("KASAN: OK\n");
 
    failures += kasan_failures;
  }
 
  if (test_everything || have_option("kcov", argv, argc)) {
    int kcov_failures = 0;
 
    kcov_failures += test_kcov();
 
    if (!kcov_failures)
      printf("KCOV: OK\n");
 
    failures += kcov_failures;
  }
 
  if (test_everything || have_option("ubsan", argv, argc)) {
    int ubsan_failures = 0;
 
    ubsan_failures += test(test_integer_overflow);
 
    if (!ubsan_failures)
      printf("UBSAN: OK\n");
 
    failures += ubsan_failures;
  }
 
  return failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}