Home | History | Annotate | Download | only in sanitizer_common
      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 // Should not be used in performance-critical places.
     33 void *internal_memset(void *s, int c, uptr n);
     34 char* internal_strchr(const char *s, int c);
     35 int internal_strcmp(const char *s1, const char *s2);
     36 uptr internal_strcspn(const char *s, const char *reject);
     37 char *internal_strdup(const char *s);
     38 uptr internal_strlen(const char *s);
     39 char *internal_strncat(char *dst, const char *src, uptr n);
     40 int internal_strncmp(const char *s1, const char *s2, uptr n);
     41 char *internal_strncpy(char *dst, const char *src, uptr n);
     42 uptr internal_strnlen(const char *s, uptr maxlen);
     43 char *internal_strrchr(const char *s, int c);
     44 // This is O(N^2), but we are not using it in hot places.
     45 char *internal_strstr(const char *haystack, const char *needle);
     46 // Works only for base=10 and doesn't set errno.
     47 s64 internal_simple_strtoll(const char *nptr, char **endptr, int base);
     48 int internal_snprintf(char *buffer, uptr length, const char *format, ...);
     49 
     50 // Return true if all bytes in [mem, mem+size) are zero.
     51 // Optimized for the case when the result is true.
     52 bool mem_is_zero(const char *mem, uptr size);
     53 
     54 
     55 // Memory
     56 uptr internal_mmap(void *addr, uptr length, int prot, int flags,
     57                    int fd, u64 offset);
     58 uptr internal_munmap(void *addr, uptr length);
     59 
     60 // I/O
     61 const fd_t kInvalidFd = -1;
     62 const fd_t kStdinFd = 0;
     63 const fd_t kStdoutFd = 1;
     64 const fd_t kStderrFd = 2;
     65 uptr internal_close(fd_t fd);
     66 int internal_isatty(fd_t fd);
     67 
     68 // Use __sanitizer::OpenFile() instead.
     69 uptr internal_open(const char *filename, int flags);
     70 uptr internal_open(const char *filename, int flags, u32 mode);
     71 
     72 uptr internal_read(fd_t fd, void *buf, uptr count);
     73 uptr internal_write(fd_t fd, const void *buf, uptr count);
     74 
     75 // OS
     76 uptr internal_filesize(fd_t fd);  // -1 on error.
     77 uptr internal_stat(const char *path, void *buf);
     78 uptr internal_lstat(const char *path, void *buf);
     79 uptr internal_fstat(fd_t fd, void *buf);
     80 uptr internal_dup2(int oldfd, int newfd);
     81 uptr internal_readlink(const char *path, char *buf, uptr bufsize);
     82 uptr internal_unlink(const char *path);
     83 void NORETURN internal__exit(int exitcode);
     84 uptr internal_lseek(fd_t fd, OFF_T offset, int whence);
     85 
     86 uptr internal_ptrace(int request, int pid, void *addr, void *data);
     87 uptr internal_waitpid(int pid, int *status, int options);
     88 uptr internal_getpid();
     89 uptr internal_getppid();
     90 
     91 // Threading
     92 uptr internal_sched_yield();
     93 
     94 // Error handling
     95 bool internal_iserror(uptr retval, int *rverrno = 0);
     96 
     97 }  // namespace __sanitizer
     98 
     99 #endif  // SANITIZER_LIBC_H
    100