[ASan] Update sanitizer_common and asan test_util headers to support building on...
[blocksruntime.git] / lib / sanitizer_common / tests / sanitizer_format_interceptor_test.cc
blob4cfb95ddb8699e51ba8eed85aa09617d70223d25
1 //===-- sanitizer_format_interceptor_test.cc ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Tests for *scanf interceptors implementation in sanitizer_common.
12 //===----------------------------------------------------------------------===//
13 #include <vector>
15 #include "interception/interception.h"
16 #include "sanitizer_test_utils.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "gtest/gtest.h"
21 using namespace __sanitizer;
23 #define COMMON_INTERCEPTOR_READ_WRITE_RANGE(ctx, ptr, size) \
24 do { \
25 ((std::vector<unsigned> *)ctx)->push_back(size); \
26 ptr = ptr; \
27 } while (0)
29 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
30 COMMON_INTERCEPTOR_READ_WRITE_RANGE(ctx, ptr, size)
32 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
33 COMMON_INTERCEPTOR_READ_WRITE_RANGE(ctx, ptr, size)
35 #define SANITIZER_INTERCEPT_PRINTF 1
36 #include "sanitizer_common/sanitizer_common_interceptors_format.inc"
38 static const unsigned I = sizeof(int);
39 static const unsigned L = sizeof(long);
40 static const unsigned LL = sizeof(long long);
41 static const unsigned S = sizeof(short);
42 static const unsigned C = sizeof(char);
43 static const unsigned LC = sizeof(wchar_t);
44 static const unsigned D = sizeof(double);
45 static const unsigned LD = sizeof(long double);
46 static const unsigned F = sizeof(float);
47 static const unsigned P = sizeof(char *);
49 static void verifyFormatResults(const char *format, unsigned n,
50 const std::vector<unsigned> &computed_sizes,
51 va_list expected_sizes) {
52 // "+ 1" because of format string
53 ASSERT_EQ(n + 1,
54 computed_sizes.size()) << "Unexpected number of format arguments: '"
55 << format << "'";
56 for (unsigned i = 0; i < n; ++i)
57 EXPECT_EQ(va_arg(expected_sizes, unsigned), computed_sizes[i + 1])
58 << "Unexpect write size for argument " << i << ", format string '"
59 << format << "'";
62 static const char test_buf[] = "Test string.";
63 static const size_t test_buf_size = sizeof(test_buf);
65 static const unsigned SCANF_ARGS_MAX = 16;
67 static void testScanf3(void *ctx, int result, bool allowGnuMalloc,
68 const char *format, ...) {
69 va_list ap;
70 va_start(ap, format);
71 scanf_common(ctx, result, allowGnuMalloc, format, ap);
72 va_end(ap);
75 static void testScanf2(const char *format, int scanf_result,
76 bool allowGnuMalloc, unsigned n,
77 va_list expected_sizes) {
78 std::vector<unsigned> scanf_sizes;
79 // 16 args should be enough.
80 testScanf3((void *)&scanf_sizes, scanf_result, allowGnuMalloc, format,
81 test_buf, test_buf, test_buf, test_buf, test_buf, test_buf,
82 test_buf, test_buf, test_buf, test_buf, test_buf, test_buf,
83 test_buf, test_buf, test_buf, test_buf);
84 verifyFormatResults(format, n, scanf_sizes, expected_sizes);
87 static void testScanf(const char *format, unsigned n, ...) {
88 va_list ap;
89 va_start(ap, n);
90 testScanf2(format, SCANF_ARGS_MAX, /* allowGnuMalloc */ true, n, ap);
91 va_end(ap);
94 static void testScanfPartial(const char *format, int scanf_result, unsigned n,
95 ...) {
96 va_list ap;
97 va_start(ap, n);
98 testScanf2(format, scanf_result, /* allowGnuMalloc */ true, n, ap);
99 va_end(ap);
102 static void testScanfNoGnuMalloc(const char *format, unsigned n, ...) {
103 va_list ap;
104 va_start(ap, n);
105 testScanf2(format, SCANF_ARGS_MAX, /* allowGnuMalloc */ false, n, ap);
106 va_end(ap);
109 TEST(SanitizerCommonInterceptors, Scanf) {
110 testScanf("%d", 1, I);
111 testScanf("%d%d%d", 3, I, I, I);
112 testScanf("ab%u%dc", 2, I, I);
113 testScanf("%ld", 1, L);
114 testScanf("%llu", 1, LL);
115 testScanf("%qd", 1, LL);
116 testScanf("a %hd%hhx", 2, S, C);
117 testScanf("%c", 1, C);
118 testScanf("%lc", 1, LC);
120 testScanf("%%", 0);
121 testScanf("a%%", 0);
122 testScanf("a%%b", 0);
123 testScanf("a%%%%b", 0);
124 testScanf("a%%b%%", 0);
125 testScanf("a%%%%%%b", 0);
126 testScanf("a%%%%%b", 0);
127 testScanf("a%%%%%f", 1, F);
128 testScanf("a%%%lxb", 1, L);
129 testScanf("a%lf%%%lxb", 2, D, L);
130 testScanf("%nf", 1, I);
132 testScanf("%10s", 1, 11);
133 testScanf("%10c", 1, 10);
134 testScanf("%10ls", 1, 11 * LC);
135 testScanf("%10lc", 1, 10 * LC);
136 testScanf("%%10s", 0);
137 testScanf("%*10s", 0);
138 testScanf("%*d", 0);
140 testScanf("%4d%8f%c", 3, I, F, C);
141 testScanf("%s%d", 2, test_buf_size, I);
142 testScanf("%[abc]", 1, test_buf_size);
143 testScanf("%4[bcdef]", 1, 5);
144 testScanf("%[]]", 1, test_buf_size);
145 testScanf("%8[^]%d0-9-]%c", 2, 9, C);
147 testScanf("%*[^:]%n:%d:%1[ ]%n", 4, I, I, 2, I);
149 testScanf("%*d%u", 1, I);
151 testScanf("%c%d", 2, C, I);
152 testScanf("%A%lf", 2, F, D);
154 testScanf("%ms %Lf", 2, P, LD);
155 testScanf("s%Las", 1, LD);
156 testScanf("%ar", 1, F);
158 // In the cases with std::min below the format spec can be interpreted as
159 // either floating-something, or (GNU extension) callee-allocated string.
160 // Our conservative implementation reports one of the two possibilities with
161 // the least store range.
162 testScanf("%a[", 0);
163 testScanf("%a[]", 0);
164 testScanf("%a[]]", 1, std::min(F, P));
165 testScanf("%a[abc]", 1, std::min(F, P));
166 testScanf("%a[^abc]", 1, std::min(F, P));
167 testScanf("%a[ab%c] %d", 0);
168 testScanf("%a[^ab%c] %d", 0);
169 testScanf("%as", 1, std::min(F, P));
170 testScanf("%aS", 1, std::min(F, P));
171 testScanf("%a13S", 1, std::min(F, P));
172 testScanf("%alS", 1, std::min(F, P));
174 testScanfNoGnuMalloc("s%Las", 1, LD);
175 testScanfNoGnuMalloc("%ar", 1, F);
176 testScanfNoGnuMalloc("%a[", 1, F);
177 testScanfNoGnuMalloc("%a[]", 1, F);
178 testScanfNoGnuMalloc("%a[]]", 1, F);
179 testScanfNoGnuMalloc("%a[abc]", 1, F);
180 testScanfNoGnuMalloc("%a[^abc]", 1, F);
181 testScanfNoGnuMalloc("%a[ab%c] %d", 3, F, C, I);
182 testScanfNoGnuMalloc("%a[^ab%c] %d", 3, F, C, I);
183 testScanfNoGnuMalloc("%as", 1, F);
184 testScanfNoGnuMalloc("%aS", 1, F);
185 testScanfNoGnuMalloc("%a13S", 1, F);
186 testScanfNoGnuMalloc("%alS", 1, F);
188 testScanf("%5$d", 0);
189 testScanf("%md", 0);
190 testScanf("%m10s", 0);
192 testScanfPartial("%d%d%d%d //1\n", 1, 1, I);
193 testScanfPartial("%d%d%d%d //2\n", 2, 2, I, I);
194 testScanfPartial("%d%d%d%d //3\n", 3, 3, I, I, I);
195 testScanfPartial("%d%d%d%d //4\n", 4, 4, I, I, I, I);
197 testScanfPartial("%d%n%n%d //1\n", 1, 3, I, I, I);
198 testScanfPartial("%d%n%n%d //2\n", 2, 4, I, I, I, I);
200 testScanfPartial("%d%n%n%d %s %s", 3, 5, I, I, I, I, test_buf_size);
201 testScanfPartial("%d%n%n%d %s %s", 4, 6, I, I, I, I, test_buf_size,
202 test_buf_size);
205 static void testPrintf3(void *ctx, const char *format, ...) {
206 va_list ap;
207 va_start(ap, format);
208 printf_common(ctx, format, ap);
209 va_end(ap);
212 static void testPrintf2(const char *format, unsigned n,
213 va_list expected_sizes) {
214 std::vector<unsigned> printf_sizes;
215 // 16 args should be enough.
216 testPrintf3((void *)&printf_sizes, format,
217 test_buf, test_buf, test_buf, test_buf, test_buf, test_buf,
218 test_buf, test_buf, test_buf, test_buf, test_buf, test_buf,
219 test_buf, test_buf, test_buf, test_buf);
220 verifyFormatResults(format, n, printf_sizes, expected_sizes);
223 static void testPrintf(const char *format, unsigned n, ...) {
224 va_list ap;
225 va_start(ap, n);
226 testPrintf2(format, n, ap);
227 va_end(ap);
230 TEST(SanitizerCommonInterceptors, Printf) {
231 // Only test functionality which differs from scanf
233 // Indexed arguments
234 testPrintf("%5$d", 0);
235 testPrintf("%.*5$d", 0);
237 // errno
238 testPrintf("%0-m", 0);
240 // Dynamic width
241 testPrintf("%*n", 1, I);
242 testPrintf("%*.10n", 1, I);
244 // Precision
245 testPrintf("%10.10n", 1, I);
246 testPrintf("%.3s", 1, 3);
247 testPrintf("%.20s", 1, test_buf_size);
249 // Dynamic precision
250 testPrintf("%.*n", 1, I);
251 testPrintf("%10.*n", 1, I);
253 // Dynamic precision for strings is not implemented yet.
254 testPrintf("%.*s", 1, 0);
256 // Checks for wide-character strings are not implemented yet.
257 testPrintf("%ls", 1, 0);