Kostya Serebryany | 16e0075 | 2012-05-31 13:42:53 +0000 | [diff] [blame] | 1 | //===-- sanitizer_libc.h ----------------------------------------*- C++ -*-===// |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 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. |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 14 | // FIXME: Some of functions declared in this file are in fact POSIX, not libc. |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 16 | #ifndef SANITIZER_LIBC_H |
| 17 | #define SANITIZER_LIBC_H |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 18 | |
Alexey Samsonov | a92c0cb | 2012-06-19 08:52:02 +0000 | [diff] [blame] | 19 | // ----------- ATTENTION ------------- |
| 20 | // This header should NOT include any other headers from sanitizer runtime. |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 21 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | 3836ff2 | 2012-06-04 10:30:16 +0000 | [diff] [blame] | 22 | |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 23 | namespace __sanitizer { |
| 24 | |
Alexey Samsonov | 3836ff2 | 2012-06-04 10:30:16 +0000 | [diff] [blame] | 25 | // internal_X() is a custom implementation of X() for use in RTL. |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 26 | |
| 27 | // String functions |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 28 | s64 internal_atoll(const char *nptr); |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 29 | void *internal_memchr(const void *s, int c, uptr n); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 30 | void *internal_memrchr(const void *s, int c, uptr n); |
Alexey Samsonov | 327c1c1 | 2012-06-14 14:04:54 +0000 | [diff] [blame] | 31 | int internal_memcmp(const void* s1, const void* s2, uptr n); |
Alexey Samsonov | f7667cc | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 32 | void *internal_memcpy(void *dest, const void *src, uptr n); |
Alexander Potapenko | f1673e6 | 2012-10-15 15:34:41 +0000 | [diff] [blame] | 33 | void *internal_memmove(void *dest, const void *src, uptr n); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 34 | // Set [s, s + n) to 0. Both s and n should be 16-aligned. |
| 35 | void internal_bzero_aligned16(void *s, uptr n); |
Alexey Samsonov | 4fac148 | 2012-06-08 14:11:12 +0000 | [diff] [blame] | 36 | // Should not be used in performance-critical places. |
| 37 | void *internal_memset(void *s, int c, uptr n); |
Alexey Samsonov | 88207ab | 2012-06-15 12:24:07 +0000 | [diff] [blame] | 38 | char* internal_strchr(const char *s, int c); |
Alexey Samsonov | 72870db | 2013-09-03 13:09:28 +0000 | [diff] [blame] | 39 | char *internal_strchrnul(const char *s, int c); |
Alexey Samsonov | c0d78c1 | 2012-06-04 13:27:49 +0000 | [diff] [blame] | 40 | int internal_strcmp(const char *s1, const char *s2); |
Alexey Samsonov | 2511347 | 2012-08-21 09:26:26 +0000 | [diff] [blame] | 41 | uptr internal_strcspn(const char *s, const char *reject); |
Alexey Samsonov | f7667cc | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 42 | char *internal_strdup(const char *s); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 43 | char *internal_strndup(const char *s, uptr n); |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 44 | uptr internal_strlen(const char *s); |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 45 | char *internal_strncat(char *dst, const char *src, uptr n); |
Alexey Samsonov | 8cd0df7 | 2012-06-18 14:34:59 +0000 | [diff] [blame] | 46 | int internal_strncmp(const char *s1, const char *s2, uptr n); |
Alexey Samsonov | 3836ff2 | 2012-06-04 10:30:16 +0000 | [diff] [blame] | 47 | char *internal_strncpy(char *dst, const char *src, uptr n); |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 48 | uptr internal_strnlen(const char *s, uptr maxlen); |
Alexey Samsonov | 4fac148 | 2012-06-08 14:11:12 +0000 | [diff] [blame] | 49 | char *internal_strrchr(const char *s, int c); |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 50 | // This is O(N^2), but we are not using it in hot places. |
| 51 | char *internal_strstr(const char *haystack, const char *needle); |
| 52 | // Works only for base=10 and doesn't set errno. |
| 53 | s64 internal_simple_strtoll(const char *nptr, char **endptr, int base); |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 54 | int internal_snprintf(char *buffer, uptr length, const char *format, ...); |
Alexey Samsonov | 3836ff2 | 2012-06-04 10:30:16 +0000 | [diff] [blame] | 55 | |
Kostya Serebryany | eb28093 | 2012-12-28 15:24:16 +0000 | [diff] [blame] | 56 | // Return true if all bytes in [mem, mem+size) are zero. |
| 57 | // Optimized for the case when the result is true. |
| 58 | bool mem_is_zero(const char *mem, uptr size); |
| 59 | |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 60 | // I/O |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 61 | const fd_t kInvalidFd = (fd_t)-1; |
Alexey Samsonov | f3457cb | 2012-11-02 09:38:47 +0000 | [diff] [blame] | 62 | const fd_t kStdinFd = 0; |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 63 | const fd_t kStdoutFd = (fd_t)1; |
| 64 | const fd_t kStderrFd = (fd_t)2; |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 65 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 66 | uptr internal_ftruncate(fd_t fd, uptr size); |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 67 | |
| 68 | // OS |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 69 | void NORETURN internal__exit(int exitcode); |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 70 | |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 71 | uptr internal_getpid(); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 72 | uptr internal_getppid(); |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 73 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 74 | // Threading |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 75 | uptr internal_sched_yield(); |
| 76 | |
| 77 | // Error handling |
| 78 | bool internal_iserror(uptr retval, int *rverrno = 0); |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 79 | |
Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 80 | } // namespace __sanitizer |
| 81 | |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 82 | #endif // SANITIZER_LIBC_H |