• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- sanitizer_libc.h ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 // These tools can not use some of the libc functions directly because those
13 // functions are intercepted. Instead, we implement a tiny subset of libc here.
14 //===----------------------------------------------------------------------===//
15 #ifndef SANITIZER_LIBC_H
16 #define SANITIZER_LIBC_H
17 
18 // ----------- ATTENTION -------------
19 // This header should NOT include any other headers from sanitizer runtime.
20 #include "sanitizer_internal_defs.h"
21 
22 namespace __sanitizer {
23 
24 // internal_X() is a custom implementation of X() for use in RTL.
25 
26 // String functions
27 s64 internal_atoll(const char *nptr);
28 void *internal_memchr(const void *s, int c, uptr n);
29 int internal_memcmp(const void* s1, const void* s2, uptr n);
30 void *internal_memcpy(void *dest, const void *src, uptr n);
31 void *internal_memmove(void *dest, const void *src, uptr n);
32 // Set [s, s + n) to 0. Both s and n should be 16-aligned.
33 void internal_bzero_aligned16(void *s, uptr n);
34 // Should not be used in performance-critical places.
35 void *internal_memset(void *s, int c, uptr n);
36 char* internal_strchr(const char *s, int c);
37 char *internal_strchrnul(const char *s, int c);
38 int internal_strcmp(const char *s1, const char *s2);
39 uptr internal_strcspn(const char *s, const char *reject);
40 char *internal_strdup(const char *s);
41 uptr internal_strlen(const char *s);
42 char *internal_strncat(char *dst, const char *src, uptr n);
43 int internal_strncmp(const char *s1, const char *s2, uptr n);
44 char *internal_strncpy(char *dst, const char *src, uptr n);
45 uptr internal_strnlen(const char *s, uptr maxlen);
46 char *internal_strrchr(const char *s, int c);
47 // This is O(N^2), but we are not using it in hot places.
48 char *internal_strstr(const char *haystack, const char *needle);
49 // Works only for base=10 and doesn't set errno.
50 s64 internal_simple_strtoll(const char *nptr, char **endptr, int base);
51 int internal_snprintf(char *buffer, uptr length, const char *format, ...);
52 
53 // Return true if all bytes in [mem, mem+size) are zero.
54 // Optimized for the case when the result is true.
55 bool mem_is_zero(const char *mem, uptr size);
56 
57 
58 // Memory
59 uptr internal_mmap(void *addr, uptr length, int prot, int flags,
60                    int fd, u64 offset);
61 uptr internal_munmap(void *addr, uptr length);
62 
63 // I/O
64 const fd_t kInvalidFd = -1;
65 const fd_t kStdinFd = 0;
66 const fd_t kStdoutFd = 1;
67 const fd_t kStderrFd = 2;
68 uptr internal_close(fd_t fd);
69 int internal_isatty(fd_t fd);
70 
71 // Use __sanitizer::OpenFile() instead.
72 uptr internal_open(const char *filename, int flags);
73 uptr internal_open(const char *filename, int flags, u32 mode);
74 
75 uptr internal_read(fd_t fd, void *buf, uptr count);
76 uptr internal_write(fd_t fd, const void *buf, uptr count);
77 uptr internal_ftruncate(fd_t fd, uptr size);
78 
79 // OS
80 uptr internal_filesize(fd_t fd);  // -1 on error.
81 uptr internal_stat(const char *path, void *buf);
82 uptr internal_lstat(const char *path, void *buf);
83 uptr internal_fstat(fd_t fd, void *buf);
84 uptr internal_dup2(int oldfd, int newfd);
85 uptr internal_readlink(const char *path, char *buf, uptr bufsize);
86 uptr internal_unlink(const char *path);
87 uptr internal_rename(const char *oldpath, const char *newpath);
88 void NORETURN internal__exit(int exitcode);
89 uptr internal_lseek(fd_t fd, OFF_T offset, int whence);
90 
91 uptr internal_ptrace(int request, int pid, void *addr, void *data);
92 uptr internal_waitpid(int pid, int *status, int options);
93 uptr internal_getpid();
94 uptr internal_getppid();
95 
96 int internal_fork();
97 
98 // Threading
99 uptr internal_sched_yield();
100 
101 // Error handling
102 bool internal_iserror(uptr retval, int *rverrno = 0);
103 
104 int internal_sigaction(int signum, const void *act, void *oldact);
105 
106 }  // namespace __sanitizer
107 
108 #endif  // SANITIZER_LIBC_H
109