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