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