xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_posix.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- sanitizer_posix.h -------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is shared between AddressSanitizer and ThreadSanitizer
100b57cec5SDimitry Andric // run-time libraries and declares some useful POSIX-specific functions.
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric #ifndef SANITIZER_POSIX_H
130b57cec5SDimitry Andric #define SANITIZER_POSIX_H
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric // ----------- ATTENTION -------------
160b57cec5SDimitry Andric // This header should NOT include any other headers from sanitizer runtime.
170b57cec5SDimitry Andric #include "sanitizer_internal_defs.h"
180b57cec5SDimitry Andric #include "sanitizer_platform_limits_freebsd.h"
190b57cec5SDimitry Andric #include "sanitizer_platform_limits_netbsd.h"
200b57cec5SDimitry Andric #include "sanitizer_platform_limits_posix.h"
210b57cec5SDimitry Andric #include "sanitizer_platform_limits_solaris.h"
220b57cec5SDimitry Andric 
23349cc55cSDimitry Andric #if SANITIZER_POSIX
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace __sanitizer {
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric // I/O
280b57cec5SDimitry Andric // Don't use directly, use __sanitizer::OpenFile() instead.
290b57cec5SDimitry Andric uptr internal_open(const char *filename, int flags);
300b57cec5SDimitry Andric uptr internal_open(const char *filename, int flags, u32 mode);
310b57cec5SDimitry Andric uptr internal_close(fd_t fd);
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric uptr internal_read(fd_t fd, void *buf, uptr count);
340b57cec5SDimitry Andric uptr internal_write(fd_t fd, const void *buf, uptr count);
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric // Memory
370b57cec5SDimitry Andric uptr internal_mmap(void *addr, uptr length, int prot, int flags,
38480093f4SDimitry Andric                    int fd, u64 offset);
390b57cec5SDimitry Andric uptr internal_munmap(void *addr, uptr length);
40fe6060f1SDimitry Andric #if SANITIZER_LINUX
41fe6060f1SDimitry Andric uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
42fe6060f1SDimitry Andric                      void *new_address);
43fe6060f1SDimitry Andric #endif
440b57cec5SDimitry Andric int internal_mprotect(void *addr, uptr length, int prot);
45e8d8bef9SDimitry Andric int internal_madvise(uptr addr, uptr length, int advice);
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric // OS
480b57cec5SDimitry Andric uptr internal_filesize(fd_t fd);  // -1 on error.
490b57cec5SDimitry Andric uptr internal_stat(const char *path, void *buf);
500b57cec5SDimitry Andric uptr internal_lstat(const char *path, void *buf);
510b57cec5SDimitry Andric uptr internal_fstat(fd_t fd, void *buf);
520b57cec5SDimitry Andric uptr internal_dup(int oldfd);
530b57cec5SDimitry Andric uptr internal_dup2(int oldfd, int newfd);
540b57cec5SDimitry Andric uptr internal_readlink(const char *path, char *buf, uptr bufsize);
550b57cec5SDimitry Andric uptr internal_unlink(const char *path);
560b57cec5SDimitry Andric uptr internal_rename(const char *oldpath, const char *newpath);
570b57cec5SDimitry Andric uptr internal_lseek(fd_t fd, OFF_T offset, int whence);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric #if SANITIZER_NETBSD
600b57cec5SDimitry Andric uptr internal_ptrace(int request, int pid, void *addr, int data);
610b57cec5SDimitry Andric #else
620b57cec5SDimitry Andric uptr internal_ptrace(int request, int pid, void *addr, void *data);
630b57cec5SDimitry Andric #endif
640b57cec5SDimitry Andric uptr internal_waitpid(int pid, int *status, int options);
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric int internal_fork();
675ffd83dbSDimitry Andric fd_t internal_spawn(const char *argv[], const char *envp[], pid_t *pid);
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
700b57cec5SDimitry Andric                     uptr *oldlenp, const void *newp, uptr newlen);
710b57cec5SDimitry Andric int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
720b57cec5SDimitry Andric                           const void *newp, uptr newlen);
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric // These functions call appropriate pthread_ functions directly, bypassing
750b57cec5SDimitry Andric // the interceptor. They are weak and may not be present in some tools.
760b57cec5SDimitry Andric SANITIZER_WEAK_ATTRIBUTE
77*0fca6ea1SDimitry Andric int internal_pthread_create(void *th, void *attr, void *(*callback)(void *),
780b57cec5SDimitry Andric                             void *param);
790b57cec5SDimitry Andric SANITIZER_WEAK_ATTRIBUTE
80*0fca6ea1SDimitry Andric int internal_pthread_join(void *th, void **ret);
810b57cec5SDimitry Andric 
82*0fca6ea1SDimitry Andric #  define DEFINE_INTERNAL_PTHREAD_FUNCTIONS                               \
830b57cec5SDimitry Andric     namespace __sanitizer {                                               \
84*0fca6ea1SDimitry Andric     int internal_pthread_create(void *th, void *attr,                     \
85*0fca6ea1SDimitry Andric                                 void *(*callback)(void *), void *param) { \
860b57cec5SDimitry Andric       return REAL(pthread_create)(th, attr, callback, param);             \
870b57cec5SDimitry Andric     }                                                                     \
88*0fca6ea1SDimitry Andric     int internal_pthread_join(void *th, void **ret) {                     \
890b57cec5SDimitry Andric       return REAL(pthread_join(th, ret));                                 \
900b57cec5SDimitry Andric     }                                                                     \
910b57cec5SDimitry Andric     }  // namespace __sanitizer
920b57cec5SDimitry Andric 
9306c3fb27SDimitry Andric int internal_pthread_attr_getstack(void *attr, void **addr, uptr *size);
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric // A routine named real_sigaction() must be implemented by each sanitizer in
960b57cec5SDimitry Andric // order for internal_sigaction() to bypass interceptors.
970b57cec5SDimitry Andric int internal_sigaction(int signum, const void *act, void *oldact);
980b57cec5SDimitry Andric void internal_sigfillset(__sanitizer_sigset_t *set);
990b57cec5SDimitry Andric void internal_sigemptyset(__sanitizer_sigset_t *set);
1000b57cec5SDimitry Andric bool internal_sigismember(__sanitizer_sigset_t *set, int signum);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric uptr internal_execve(const char *filename, char *const argv[],
1030b57cec5SDimitry Andric                      char *const envp[]);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric bool IsStateDetached(int state);
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric // Move the fd out of {0, 1, 2} range.
1080b57cec5SDimitry Andric fd_t ReserveStandardFds(fd_t fd);
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric bool ShouldMockFailureToOpen(const char *path);
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric // Create a non-file mapping with a given /proc/self/maps name.
1130b57cec5SDimitry Andric uptr MmapNamed(void *addr, uptr length, int prot, int flags, const char *name);
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric // Platforms should implement at most one of these.
1160b57cec5SDimitry Andric // 1. Provide a pre-decorated file descriptor to use instead of an anonymous
1170b57cec5SDimitry Andric // mapping.
1180b57cec5SDimitry Andric int GetNamedMappingFd(const char *name, uptr size, int *flags);
1190b57cec5SDimitry Andric // 2. Add name to an existing anonymous mapping. The caller must keep *name
1200b57cec5SDimitry Andric // alive at least as long as the mapping exists.
1210b57cec5SDimitry Andric void DecorateMapping(uptr addr, uptr size, const char *name);
1220b57cec5SDimitry Andric 
12306c3fb27SDimitry Andric #  if !SANITIZER_FREEBSD
12406c3fb27SDimitry Andric #    define __sanitizer_dirsiz(dp) ((dp)->d_reclen)
12506c3fb27SDimitry Andric #  endif
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric }  // namespace __sanitizer
1280b57cec5SDimitry Andric 
129349cc55cSDimitry Andric #endif  // SANITIZER_POSIX
130349cc55cSDimitry Andric 
1310b57cec5SDimitry Andric #endif  // SANITIZER_POSIX_H
132