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