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