xref: /openbsd-src/gnu/llvm/compiler-rt/lib/sanitizer_common/tests/sanitizer_printf_test.cpp (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
1 //===-- sanitizer_printf_test.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Tests for sanitizer_printf.cpp
10 //
11 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common/sanitizer_common.h"
13 #include "sanitizer_common/sanitizer_libc.h"
14 #include "gtest/gtest.h"
15 
16 #include <string.h>
17 #include <limits.h>
18 
19 #ifdef __x86_64__
20 #  include <emmintrin.h>
21 #endif
22 
23 namespace __sanitizer {
24 
25 TEST(Printf, Basic) {
26   char buf[1024];
27   uptr len = internal_snprintf(
28       buf, sizeof(buf), "a%db%zdc%ue%zuf%xh%zxq%pe%sr", (int)-1, (uptr)-2,
29       (unsigned)-4, (uptr)5, (unsigned)10, (uptr)11, (void *)0x123, "_string_");
30   EXPECT_EQ(len, strlen(buf));
31 
32   std::string expectedString = "a-1b-2c4294967292e5fahbq0x";
33   expectedString += std::string(SANITIZER_POINTER_FORMAT_LENGTH - 3, '0');
34   expectedString += "123e_string_r";
35   EXPECT_STREQ(expectedString.c_str(), buf);
36 }
37 
38 TEST(Printf, OverflowStr) {
39   char buf[] = "123456789";
40   uptr len = internal_snprintf(buf, 4, "%s", "abcdef");  // NOLINT
41   EXPECT_EQ(len, (uptr)6);
42   EXPECT_STREQ("abc", buf);
43   EXPECT_EQ(buf[3], 0);
44   EXPECT_EQ(buf[4], '5');
45   EXPECT_EQ(buf[5], '6');
46   EXPECT_EQ(buf[6], '7');
47   EXPECT_EQ(buf[7], '8');
48   EXPECT_EQ(buf[8], '9');
49   EXPECT_EQ(buf[9], 0);
50 }
51 
52 TEST(Printf, OverflowInt) {
53   char buf[] = "123456789";
54   internal_snprintf(buf, 4, "%d", -123456789);  // NOLINT
55   EXPECT_STREQ("-12", buf);
56   EXPECT_EQ(buf[3], 0);
57   EXPECT_EQ(buf[4], '5');
58   EXPECT_EQ(buf[5], '6');
59   EXPECT_EQ(buf[6], '7');
60   EXPECT_EQ(buf[7], '8');
61   EXPECT_EQ(buf[8], '9');
62   EXPECT_EQ(buf[9], 0);
63 }
64 
65 TEST(Printf, OverflowUint) {
66   char buf[] = "123456789";
67   uptr val;
68   if (sizeof(val) == 4) {
69     val = (uptr)0x12345678;
70   } else {
71     val = (uptr)0x123456789ULL;
72   }
73   internal_snprintf(buf, 4, "a%zx", val);  // NOLINT
74   EXPECT_STREQ("a12", buf);
75   EXPECT_EQ(buf[3], 0);
76   EXPECT_EQ(buf[4], '5');
77   EXPECT_EQ(buf[5], '6');
78   EXPECT_EQ(buf[6], '7');
79   EXPECT_EQ(buf[7], '8');
80   EXPECT_EQ(buf[8], '9');
81   EXPECT_EQ(buf[9], 0);
82 }
83 
84 TEST(Printf, OverflowPtr) {
85   char buf[] = "123456789";
86   void *p;
87   if (sizeof(p) == 4) {
88     p = (void*)0x1234567;
89   } else {
90     p = (void*)0x123456789ULL;
91   }
92   internal_snprintf(buf, 4, "%p", p);  // NOLINT
93   EXPECT_STREQ("0x0", buf);
94   EXPECT_EQ(buf[3], 0);
95   EXPECT_EQ(buf[4], '5');
96   EXPECT_EQ(buf[5], '6');
97   EXPECT_EQ(buf[6], '7');
98   EXPECT_EQ(buf[7], '8');
99   EXPECT_EQ(buf[8], '9');
100   EXPECT_EQ(buf[9], 0);
101 }
102 
103 #if defined(_WIN32)
104 // Oh well, MSVS headers don't define snprintf.
105 # define snprintf _snprintf
106 #endif
107 
108 template<typename T>
109 static void TestAgainstLibc(const char *fmt, T arg1, T arg2) {
110   char buf[1024];
111   uptr len = internal_snprintf(buf, sizeof(buf), fmt, arg1, arg2);
112   char buf2[1024];
113   snprintf(buf2, sizeof(buf2), fmt, arg1, arg2);
114   EXPECT_EQ(len, strlen(buf));
115   EXPECT_STREQ(buf2, buf);
116 }
117 
118 TEST(Printf, MinMax) {
119   TestAgainstLibc<int>("%d-%d", INT_MIN, INT_MAX);
120   TestAgainstLibc<unsigned>("%u-%u", 0, UINT_MAX);
121   TestAgainstLibc<unsigned>("%x-%x", 0, UINT_MAX);
122 #if !defined(_WIN32)
123   // %z* format doesn't seem to be supported by MSVS.
124   TestAgainstLibc<long>("%zd-%zd", LONG_MIN, LONG_MAX);
125   TestAgainstLibc<unsigned long>("%zu-%zu", 0, ULONG_MAX);
126   TestAgainstLibc<unsigned long>("%zx-%zx", 0, ULONG_MAX);
127 #endif
128 }
129 
130 TEST(Printf, Padding) {
131   TestAgainstLibc<int>("%3d - %3d", 1, 0);
132   TestAgainstLibc<int>("%3d - %3d", -1, 123);
133   TestAgainstLibc<int>("%3d - %3d", -1, -123);
134   TestAgainstLibc<int>("%3d - %3d", 12, 1234);
135   TestAgainstLibc<int>("%3d - %3d", -12, -1234);
136   TestAgainstLibc<int>("%03d - %03d", 1, 0);
137   TestAgainstLibc<int>("%03d - %03d", -1, 123);
138   TestAgainstLibc<int>("%03d - %03d", -1, -123);
139   TestAgainstLibc<int>("%03d - %03d", 12, 1234);
140   TestAgainstLibc<int>("%03d - %03d", -12, -1234);
141 }
142 
143 TEST(Printf, Precision) {
144   char buf[1024];
145   uptr len = internal_snprintf(buf, sizeof(buf), "%.*s", 3, "12345");
146   EXPECT_EQ(3U, len);
147   EXPECT_STREQ("123", buf);
148   len = internal_snprintf(buf, sizeof(buf), "%.*s", 6, "12345");
149   EXPECT_EQ(5U, len);
150   EXPECT_STREQ("12345", buf);
151   len = internal_snprintf(buf, sizeof(buf), "%-6s", "12345");
152   EXPECT_EQ(6U, len);
153   EXPECT_STREQ("12345 ", buf);
154   // Check that width does not overflow the smaller buffer, although
155   // 10 chars is requested, it stops at the buffer size, 8.
156   len = internal_snprintf(buf, 8, "%-10s", "12345");  // NOLINT
157   EXPECT_EQ(10U, len);  // The required size reported.
158   EXPECT_STREQ("12345  ", buf);
159 }
160 
161 #ifdef __x86_64__
162 TEST(Printf, M128) {
163   __m128i v = _mm_set_epi32(0x12345678, 0x0a0a0a0a, 0xb0b0b0b0, 0xaabbccdd);
164   char buf[128];
165   internal_snprintf(buf, sizeof(buf), "%V", PRINTF_128(v));
166   EXPECT_STREQ("ddccbbaab0b0b0b00a0a0a0a78563412", buf);
167   v = _mm_cvtsi32_si128(0x12345678);
168   internal_snprintf(buf, sizeof(buf), "%V", PRINTF_128(v));
169   EXPECT_STREQ("78563412000000000000000000000000", buf);
170   internal_snprintf(buf, sizeof(buf), "%d %V", 0, PRINTF_128(v));
171   EXPECT_STREQ("0 78563412000000000000000000000000", buf);
172 }
173 #endif
174 
175 }  // namespace __sanitizer
176