1*e9c8d0ffSKevin Athey // RUN: %clangxx_msan -O0 %s -o %t -lresolv && %run %t
2*e9c8d0ffSKevin Athey // RUN: not %run %t NTOP_READ 2>&1 | FileCheck %s --check-prefix=NTOP_READ
3*e9c8d0ffSKevin Athey // RUN: not %run %t PTON_READ 2>&1 | FileCheck %s --check-prefix=PTON_READ
4*e9c8d0ffSKevin Athey 
5*e9c8d0ffSKevin Athey #include <assert.h>
6*e9c8d0ffSKevin Athey #include <resolv.h>
7*e9c8d0ffSKevin Athey #include <stdlib.h>
8*e9c8d0ffSKevin Athey #include <string.h>
9*e9c8d0ffSKevin Athey #include <sys/types.h>
10*e9c8d0ffSKevin Athey 
11*e9c8d0ffSKevin Athey #include <sanitizer/msan_interface.h>
12*e9c8d0ffSKevin Athey 
13*e9c8d0ffSKevin Athey int main(int iArgc, char *szArgv[]) {
14*e9c8d0ffSKevin Athey   char* test = nullptr;
15*e9c8d0ffSKevin Athey   if (iArgc > 1) {
16*e9c8d0ffSKevin Athey     test = szArgv[1];
17*e9c8d0ffSKevin Athey   }
18*e9c8d0ffSKevin Athey 
19*e9c8d0ffSKevin Athey   if (test == nullptr) {
20*e9c8d0ffSKevin Athey     // Check NTOP writing
21*e9c8d0ffSKevin Athey     const char *src = "base64 test data";
22*e9c8d0ffSKevin Athey     size_t src_len = strlen(src);
23*e9c8d0ffSKevin Athey     size_t dst_len = ((src_len + 2) / 3) * 4 + 1;
24*e9c8d0ffSKevin Athey     char dst[dst_len];
25*e9c8d0ffSKevin Athey     int res = b64_ntop(reinterpret_cast<const unsigned char *>(src), src_len,
26*e9c8d0ffSKevin Athey                        dst, dst_len);
27*e9c8d0ffSKevin Athey     assert(res >= 0);
28*e9c8d0ffSKevin Athey     __msan_check_mem_is_initialized(dst, res);
29*e9c8d0ffSKevin Athey 
30*e9c8d0ffSKevin Athey     // Check PTON writing
31*e9c8d0ffSKevin Athey     unsigned char target[dst_len];
32*e9c8d0ffSKevin Athey     res = b64_pton(dst, target, dst_len);
33*e9c8d0ffSKevin Athey     assert(res >= 0);
34*e9c8d0ffSKevin Athey     __msan_check_mem_is_initialized(target, res);
35*e9c8d0ffSKevin Athey 
36*e9c8d0ffSKevin Athey     // Check NTOP writing for zero length src
37*e9c8d0ffSKevin Athey     src = "";
38*e9c8d0ffSKevin Athey     src_len = strlen(src);
39*e9c8d0ffSKevin Athey     assert(((src_len + 2) / 3) * 4 + 1 < dst_len);
40*e9c8d0ffSKevin Athey     res = b64_ntop(reinterpret_cast<const unsigned char *>(src), src_len,
41*e9c8d0ffSKevin Athey                        dst, dst_len);
42*e9c8d0ffSKevin Athey     assert(res >= 0);
43*e9c8d0ffSKevin Athey     __msan_check_mem_is_initialized(dst, res);
44*e9c8d0ffSKevin Athey 
45*e9c8d0ffSKevin Athey     // Check PTON writing for zero length src
46*e9c8d0ffSKevin Athey     dst[0] = '\0';
47*e9c8d0ffSKevin Athey     res = b64_pton(dst, target, dst_len);
48*e9c8d0ffSKevin Athey     assert(res >= 0);
49*e9c8d0ffSKevin Athey     __msan_check_mem_is_initialized(target, res);
50*e9c8d0ffSKevin Athey 
51*e9c8d0ffSKevin Athey     return 0;
52*e9c8d0ffSKevin Athey   }
53*e9c8d0ffSKevin Athey 
54*e9c8d0ffSKevin Athey   if (strcmp(test, "NTOP_READ") == 0) {
55*e9c8d0ffSKevin Athey     // Check NTOP reading
56*e9c8d0ffSKevin Athey     size_t src_len = 80;
57*e9c8d0ffSKevin Athey     char src[src_len];
58*e9c8d0ffSKevin Athey     __msan_poison(src, src_len);
59*e9c8d0ffSKevin Athey     size_t dst_len = ((src_len + 2) / 3) * 4 + 1;
60*e9c8d0ffSKevin Athey     char dst[dst_len];
61*e9c8d0ffSKevin Athey     int res = b64_ntop(reinterpret_cast<const unsigned char *>(src), src_len,
62*e9c8d0ffSKevin Athey                        dst, dst_len);
63*e9c8d0ffSKevin Athey     // NTOP_READ: Uninitialized bytes in __interceptor___b64_ntop
64*e9c8d0ffSKevin Athey     return 0;
65*e9c8d0ffSKevin Athey   }
66*e9c8d0ffSKevin Athey 
67*e9c8d0ffSKevin Athey   if (strcmp(test, "PTON_READ") == 0) {
68*e9c8d0ffSKevin Athey     // Check PTON reading
69*e9c8d0ffSKevin Athey     size_t src_len = 80;
70*e9c8d0ffSKevin Athey     char src[src_len];
71*e9c8d0ffSKevin Athey     strcpy(src, "junk longer than zero");
72*e9c8d0ffSKevin Athey     // pretend it is uninitialized
73*e9c8d0ffSKevin Athey     __msan_poison(src, src_len);
74*e9c8d0ffSKevin Athey     unsigned char target[src_len];
75*e9c8d0ffSKevin Athey     int res = b64_pton(src, target, src_len);
76*e9c8d0ffSKevin Athey     // PTON_READ: Uninitialized bytes in __interceptor___b64_pton
77*e9c8d0ffSKevin Athey     return 0;
78*e9c8d0ffSKevin Athey   }
79*e9c8d0ffSKevin Athey 
80*e9c8d0ffSKevin Athey   return 0;
81*e9c8d0ffSKevin Athey }
82