sanitizer_posix.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===-- sanitizer_posix.h -------------------------------------------------===//
  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 and declares some useful POSIX-specific functions.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_POSIX_H
  13. #define SANITIZER_POSIX_H
  14. // ----------- ATTENTION -------------
  15. // This header should NOT include any other headers from sanitizer runtime.
  16. #include "sanitizer_internal_defs.h"
  17. #include "sanitizer_platform_limits_freebsd.h"
  18. #include "sanitizer_platform_limits_netbsd.h"
  19. #include "sanitizer_platform_limits_posix.h"
  20. #include "sanitizer_platform_limits_solaris.h"
  21. #if SANITIZER_POSIX
  22. namespace __sanitizer {
  23. // I/O
  24. // Don't use directly, use __sanitizer::OpenFile() instead.
  25. uptr internal_open(const char *filename, int flags);
  26. uptr internal_open(const char *filename, int flags, u32 mode);
  27. uptr internal_close(fd_t fd);
  28. uptr internal_read(fd_t fd, void *buf, uptr count);
  29. uptr internal_write(fd_t fd, const void *buf, uptr count);
  30. // Memory
  31. uptr internal_mmap(void *addr, uptr length, int prot, int flags,
  32. int fd, u64 offset);
  33. uptr internal_munmap(void *addr, uptr length);
  34. #if SANITIZER_LINUX
  35. uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
  36. void *new_address);
  37. #endif
  38. int internal_mprotect(void *addr, uptr length, int prot);
  39. int internal_madvise(uptr addr, uptr length, int advice);
  40. // OS
  41. uptr internal_filesize(fd_t fd); // -1 on error.
  42. uptr internal_stat(const char *path, void *buf);
  43. uptr internal_lstat(const char *path, void *buf);
  44. uptr internal_fstat(fd_t fd, void *buf);
  45. uptr internal_dup(int oldfd);
  46. uptr internal_dup2(int oldfd, int newfd);
  47. uptr internal_readlink(const char *path, char *buf, uptr bufsize);
  48. uptr internal_unlink(const char *path);
  49. uptr internal_rename(const char *oldpath, const char *newpath);
  50. uptr internal_lseek(fd_t fd, OFF_T offset, int whence);
  51. #if SANITIZER_NETBSD
  52. uptr internal_ptrace(int request, int pid, void *addr, int data);
  53. #else
  54. uptr internal_ptrace(int request, int pid, void *addr, void *data);
  55. #endif
  56. uptr internal_waitpid(int pid, int *status, int options);
  57. int internal_fork();
  58. fd_t internal_spawn(const char *argv[], const char *envp[], pid_t *pid);
  59. int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
  60. uptr *oldlenp, const void *newp, uptr newlen);
  61. int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
  62. const void *newp, uptr newlen);
  63. // These functions call appropriate pthread_ functions directly, bypassing
  64. // the interceptor. They are weak and may not be present in some tools.
  65. SANITIZER_WEAK_ATTRIBUTE
  66. int real_pthread_create(void *th, void *attr, void *(*callback)(void *),
  67. void *param);
  68. SANITIZER_WEAK_ATTRIBUTE
  69. int real_pthread_join(void *th, void **ret);
  70. #define DEFINE_REAL_PTHREAD_FUNCTIONS \
  71. namespace __sanitizer { \
  72. int real_pthread_create(void *th, void *attr, void *(*callback)(void *), \
  73. void *param) { \
  74. return REAL(pthread_create)(th, attr, callback, param); \
  75. } \
  76. int real_pthread_join(void *th, void **ret) { \
  77. return REAL(pthread_join(th, ret)); \
  78. } \
  79. } // namespace __sanitizer
  80. int my_pthread_attr_getstack(void *attr, void **addr, uptr *size);
  81. // A routine named real_sigaction() must be implemented by each sanitizer in
  82. // order for internal_sigaction() to bypass interceptors.
  83. int internal_sigaction(int signum, const void *act, void *oldact);
  84. void internal_sigfillset(__sanitizer_sigset_t *set);
  85. void internal_sigemptyset(__sanitizer_sigset_t *set);
  86. bool internal_sigismember(__sanitizer_sigset_t *set, int signum);
  87. uptr internal_execve(const char *filename, char *const argv[],
  88. char *const envp[]);
  89. bool IsStateDetached(int state);
  90. // Move the fd out of {0, 1, 2} range.
  91. fd_t ReserveStandardFds(fd_t fd);
  92. bool ShouldMockFailureToOpen(const char *path);
  93. // Create a non-file mapping with a given /proc/self/maps name.
  94. uptr MmapNamed(void *addr, uptr length, int prot, int flags, const char *name);
  95. // Platforms should implement at most one of these.
  96. // 1. Provide a pre-decorated file descriptor to use instead of an anonymous
  97. // mapping.
  98. int GetNamedMappingFd(const char *name, uptr size, int *flags);
  99. // 2. Add name to an existing anonymous mapping. The caller must keep *name
  100. // alive at least as long as the mapping exists.
  101. void DecorateMapping(uptr addr, uptr size, const char *name);
  102. } // namespace __sanitizer
  103. #endif // SANITIZER_POSIX
  104. #endif // SANITIZER_POSIX_H