sanitizer_libc.h 3.9 KB

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