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
23namespace __sanitizer {
24
25// internal_X() is a custom implementation of X() for use in RTL.
26
27extern "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.
30SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memcpy(void *dest,
31 const void *src,
32 uptr n);
33SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memmove(
34 void *dest, const void *src, uptr n);
35SANITIZER_INTERFACE_ATTRIBUTE void *__sanitizer_internal_memset(void *s, int c,
36 uptr n);
37} // extern "C"
38
39// String functions
40s64 internal_atoll(const char *nptr);
41void *internal_memchr(const void *s, int c, uptr n);
42void *internal_memrchr(const void *s, int c, uptr n);
43int internal_memcmp(const void* s1, const void* s2, uptr n);
44ALWAYS_INLINE void *internal_memcpy(void *dest, const void *src, uptr n) {
45 return __sanitizer_internal_memcpy(dest, src, n);
46}
47ALWAYS_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.
51ALWAYS_INLINE void *internal_memset(void *s, int c, uptr n) {
52 return __sanitizer_internal_memset(s, c, n);
53}
54char* internal_strchr(const char *s, int c);
55char *internal_strchrnul(const char *s, int c);
56int internal_strcmp(const char *s1, const char *s2);
57uptr internal_strcspn(const char *s, const char *reject);
58char *internal_strdup(const char *s);
59uptr internal_strlen(const char *s);
60uptr internal_strlcat(char *dst, const char *src, uptr maxlen);
61char *internal_strncat(char *dst, const char *src, uptr n);
62int internal_strncmp(const char *s1, const char *s2, uptr n);
63uptr internal_strlcpy(char *dst, const char *src, uptr maxlen);
64char *internal_strncpy(char *dst, const char *src, uptr n);
65uptr internal_strnlen(const char *s, uptr maxlen);
66char *internal_strrchr(const char *s, int c);
67char *internal_strstr(const char *haystack, const char *needle);
68// Works only for base=10 and doesn't set errno.
69s64 internal_simple_strtoll(const char *nptr, const char **endptr, int base);
70int internal_snprintf(char *buffer, uptr length, const char *format, ...)
71 FORMAT(3, 4);
72uptr internal_wcslen(const wchar_t *s);
73uptr internal_wcsnlen(const wchar_t *s, uptr maxlen);
74wchar_t *internal_wcscpy(wchar_t *dst, const wchar_t *src);
75wchar_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.
78bool 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
88uptr internal_ftruncate(fd_t fd, uptr size);
89
90// OS
91void NORETURN internal__exit(int exitcode);
92void internal_sleep(unsigned seconds);
93void internal_usleep(u64 useconds);
94
95uptr internal_getpid();
96uptr internal_getppid();
97
98int internal_dlinfo(void *handle, int request, void *p);
99
100// Threading
101uptr internal_sched_yield();
102
103// Error handling
104bool internal_iserror(uptr retval, int *rverrno = nullptr);
105
106} // namespace __sanitizer
107
108#endif // SANITIZER_LIBC_H
109