xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_libc.h (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 
75 // Return true if all bytes in [mem, mem+size) are zero.
76 // Optimized for the case when the result is true.
77 bool mem_is_zero(const char *mem, uptr size);
78 
79 // I/O
80 // Define these as macros so we can use them in linker initialized global
81 // structs without dynamic initialization.
82 #define kInvalidFd ((fd_t)-1)
83 #define kStdinFd ((fd_t)0)
84 #define kStdoutFd ((fd_t)1)
85 #define kStderrFd ((fd_t)2)
86 
87 uptr internal_ftruncate(fd_t fd, uptr size);
88 
89 // OS
90 void NORETURN internal__exit(int exitcode);
91 void internal_sleep(unsigned seconds);
92 void internal_usleep(u64 useconds);
93 
94 uptr internal_getpid();
95 uptr internal_getppid();
96 
97 int internal_dlinfo(void *handle, int request, void *p);
98 
99 // Threading
100 uptr internal_sched_yield();
101 
102 // Error handling
103 bool internal_iserror(uptr retval, int *rverrno = nullptr);
104 
105 } // namespace __sanitizer
106 
107 #endif // SANITIZER_LIBC_H
108