xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_libc.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 //===-- sanitizer_libc.h ----------------------------------------*- C++ -*-===//
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 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 // These tools can not use some of the libc functions directly because those
12 // functions are intercepted. Instead, we implement a tiny subset of libc here.
13 // FIXME: Some of functions declared in this file are in fact POSIX, not libc.
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef SANITIZER_LIBC_H
17 #define SANITIZER_LIBC_H
18 
19 // ----------- ATTENTION -------------
20 // This header should NOT include any other headers from sanitizer runtime.
21 #include "sanitizer_internal_defs.h"
22 
23 namespace __sanitizer {
24 
25 // internal_X() is a custom implementation of X() for use in RTL.
26 
27 extern "C" {
28 // These are used as builtin replacements; see sanitizer_redefine_builtins.h.
29 // In normal runtime code, use the __sanitizer::internal_X() aliases instead.
30 SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memcpy(void *dest,
31                                                                 const void *src,
32                                                                 uptr n);
33 SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memmove(
34     void *dest, const void *src, uptr n);
35 SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memset(void *s, int c,
36                                                                 uptr n);
37 }  // extern "C"
38 
39 // String functions
40 s64 internal_atoll(const char *nptr);
41 void *internal_memchr(const void *s, int c, uptr n);
42 void *internal_memrchr(const void *s, int c, uptr n);
43 int internal_memcmp(const void* s1, const void* s2, uptr n);
44 ALWAYS_INLINE void *internal_memcpy(void *dest, const void *src, uptr n) {
45   return __sanitizer_internal_memcpy(dest, src, n);
46 }
47 ALWAYS_INLINE void *internal_memmove(void *dest, const void *src, uptr n) {
48   return __sanitizer_internal_memmove(dest, src, n);
49 }
50 // Should not be used in performance-critical places.
51 ALWAYS_INLINE void *internal_memset(void *s, int c, uptr n) {
52   return __sanitizer_internal_memset(s, c, n);
53 }
54 char* internal_strchr(const char *s, int c);
55 char *internal_strchrnul(const char *s, int c);
56 int internal_strcmp(const char *s1, const char *s2);
57 uptr internal_strcspn(const char *s, const char *reject);
58 char *internal_strdup(const char *s);
59 uptr internal_strlen(const char *s);
60 uptr internal_strlcat(char *dst, const char *src, uptr maxlen);
61 char *internal_strncat(char *dst, const char *src, uptr n);
62 int internal_strncmp(const char *s1, const char *s2, uptr n);
63 uptr internal_strlcpy(char *dst, const char *src, uptr maxlen);
64 char *internal_strncpy(char *dst, const char *src, uptr n);
65 uptr internal_strnlen(const char *s, uptr maxlen);
66 char *internal_strrchr(const char *s, int c);
67 char *internal_strstr(const char *haystack, const char *needle);
68 // Works only for base=10 and doesn't set errno.
69 s64 internal_simple_strtoll(const char *nptr, const char **endptr, int base);
70 int internal_snprintf(char *buffer, uptr length, const char *format, ...)
71     FORMAT(3, 4);
72 uptr internal_wcslen(const wchar_t *s);
73 uptr internal_wcsnlen(const wchar_t *s, uptr maxlen);
74 wchar_t *internal_wcscpy(wchar_t *dst, const wchar_t *src);
75 wchar_t *internal_wcsncpy(wchar_t *dst, const wchar_t *src, uptr maxlen);
76 // Return true if all bytes in [mem, mem+size) are zero.
77 // Optimized for the case when the result is true.
78 bool mem_is_zero(const char *mem, uptr size);
79 
80 // I/O
81 // Define these as macros so we can use them in linker initialized global
82 // structs without dynamic initialization.
83 #define kInvalidFd ((fd_t)-1)
84 #define kStdinFd ((fd_t)0)
85 #define kStdoutFd ((fd_t)1)
86 #define kStderrFd ((fd_t)2)
87 
88 uptr internal_ftruncate(fd_t fd, uptr size);
89 
90 // OS
91 void NORETURN internal__exit(int exitcode);
92 void internal_sleep(unsigned seconds);
93 void internal_usleep(u64 useconds);
94 
95 uptr internal_getpid();
96 uptr internal_getppid();
97 
98 int internal_dlinfo(void *handle, int request, void *p);
99 
100 // Threading
101 uptr internal_sched_yield();
102 
103 // Error handling
104 bool internal_iserror(uptr retval, int *rverrno = nullptr);
105 
106 } // namespace __sanitizer
107 
108 #endif // SANITIZER_LIBC_H
109