2013-02-27 11:22:40 +00:00
|
|
|
//===-- sanitizer_linux.h ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-02-27 11:22:40 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Linux-specific syscall wrappers and classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_LINUX_H
|
|
|
|
#define SANITIZER_LINUX_H
|
|
|
|
|
2013-09-10 14:36:16 +00:00
|
|
|
#include "sanitizer_platform.h"
|
2023-11-27 23:17:03 +01:00
|
|
|
#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
|
2025-04-11 16:21:00 -04:00
|
|
|
SANITIZER_SOLARIS || SANITIZER_HAIKU
|
2023-11-27 23:17:03 +01:00
|
|
|
# include "sanitizer_common.h"
|
|
|
|
# include "sanitizer_internal_defs.h"
|
|
|
|
# include "sanitizer_platform_limits_freebsd.h"
|
|
|
|
# include "sanitizer_platform_limits_netbsd.h"
|
|
|
|
# include "sanitizer_platform_limits_posix.h"
|
|
|
|
# include "sanitizer_platform_limits_solaris.h"
|
|
|
|
# include "sanitizer_posix.h"
|
2013-02-27 11:22:40 +00:00
|
|
|
|
2013-07-29 19:09:49 +00:00
|
|
|
struct link_map; // Opaque type returned by dlopen().
|
2020-03-26 13:35:09 +01:00
|
|
|
struct utsname;
|
2013-02-27 11:22:40 +00:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
// Dirent structure for getdents(). Note that this structure is different from
|
|
|
|
// the one in <dirent.h>, which is used by readdir().
|
|
|
|
struct linux_dirent;
|
|
|
|
|
2025-04-11 16:21:00 -04:00
|
|
|
# if SANITIZER_HAIKU
|
|
|
|
struct MemoryMappingLayoutData {
|
|
|
|
long signed int cookie;
|
|
|
|
};
|
|
|
|
# else
|
Removed platform-specific ifdefs from sanitizer_procmaps.h
Summary: Removed platform-specific ifdefs for linux, mac, freebsd and netbsd from sanitizer_procmaps.h
Patch by Yicheng Wang <yichengfb@fb.com>
Reviewers: kcc, kubamracek, alekseyshl, fjricci, vitalybuka
Reviewed By: fjricci, vitalybuka
Subscribers: vitalybuka, emaste, krytarowski, llvm-commits
Differential Revision: https://reviews.llvm.org/D38098
llvm-svn: 313999
2017-09-22 17:48:24 +00:00
|
|
|
struct ProcSelfMapsBuff {
|
|
|
|
char *data;
|
|
|
|
uptr mmaped_size;
|
|
|
|
uptr len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MemoryMappingLayoutData {
|
|
|
|
ProcSelfMapsBuff proc_self_maps;
|
|
|
|
const char *current;
|
|
|
|
};
|
|
|
|
|
|
|
|
void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
|
2025-04-12 00:49:23 -04:00
|
|
|
# endif // SANITIZER_HAIKU
|
Removed platform-specific ifdefs from sanitizer_procmaps.h
Summary: Removed platform-specific ifdefs for linux, mac, freebsd and netbsd from sanitizer_procmaps.h
Patch by Yicheng Wang <yichengfb@fb.com>
Reviewers: kcc, kubamracek, alekseyshl, fjricci, vitalybuka
Reviewed By: fjricci, vitalybuka
Subscribers: vitalybuka, emaste, krytarowski, llvm-commits
Differential Revision: https://reviews.llvm.org/D38098
llvm-svn: 313999
2017-09-22 17:48:24 +00:00
|
|
|
|
2013-02-27 11:22:40 +00:00
|
|
|
// Syscall wrappers.
|
2013-05-08 14:43:49 +00:00
|
|
|
uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
|
2023-11-27 23:17:03 +01:00
|
|
|
uptr internal_sigaltstack(const void *ss, void *oss);
|
2023-09-26 21:10:10 -07:00
|
|
|
uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
|
2023-11-27 23:17:03 +01:00
|
|
|
__sanitizer_sigset_t *oldset);
|
2023-09-26 21:10:10 -07:00
|
|
|
|
|
|
|
void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset);
|
|
|
|
void BlockSignals(__sanitizer_sigset_t *oldset = nullptr);
|
|
|
|
struct ScopedBlockSignals {
|
|
|
|
explicit ScopedBlockSignals(__sanitizer_sigset_t *copy);
|
|
|
|
~ScopedBlockSignals();
|
|
|
|
|
|
|
|
ScopedBlockSignals &operator=(const ScopedBlockSignals &) = delete;
|
|
|
|
ScopedBlockSignals(const ScopedBlockSignals &) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
__sanitizer_sigset_t saved_;
|
|
|
|
};
|
2021-11-08 18:45:22 -08:00
|
|
|
|
|
|
|
# if SANITIZER_GLIBC
|
2017-12-13 16:23:54 +00:00
|
|
|
uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp);
|
2023-11-27 23:17:03 +01:00
|
|
|
# endif
|
2014-03-07 10:03:54 +00:00
|
|
|
|
|
|
|
// Linux-only syscalls.
|
2023-11-27 23:17:03 +01:00
|
|
|
# if SANITIZER_LINUX
|
2014-03-07 10:03:54 +00:00
|
|
|
uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5);
|
2022-07-13 19:04:38 -07:00
|
|
|
# if defined(__x86_64__)
|
|
|
|
uptr internal_arch_prctl(int option, uptr arg2);
|
|
|
|
# endif
|
2014-01-31 11:29:51 +00:00
|
|
|
// Used only by sanitizer_stoptheworld. Signal handlers that are actually used
|
|
|
|
// (like the process-wide error reporting SEGV handler) must use
|
|
|
|
// internal_sigaction instead.
|
|
|
|
int internal_sigaction_norestorer(int signum, const void *act, void *oldact);
|
2014-03-07 10:03:54 +00:00
|
|
|
void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
|
2022-12-08 10:08:45 +08:00
|
|
|
# if defined(__x86_64__) || defined(__mips__) || defined(__aarch64__) || \
|
|
|
|
defined(__powerpc64__) || defined(__s390__) || defined(__i386__) || \
|
|
|
|
defined(__arm__) || SANITIZER_RISCV64 || SANITIZER_LOONGARCH64
|
2013-09-02 11:36:19 +00:00
|
|
|
uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
|
|
|
|
int *parent_tidptr, void *newtls, int *child_tidptr);
|
2023-11-27 23:17:03 +01:00
|
|
|
# endif
|
2020-05-30 10:13:07 +02:00
|
|
|
int internal_uname(struct utsname *buf);
|
2023-11-27 23:17:03 +01:00
|
|
|
# elif SANITIZER_FREEBSD
|
2022-06-08 08:55:10 +01:00
|
|
|
uptr internal_procctl(int type, int id, int cmd, void *data);
|
2018-10-04 20:58:18 +00:00
|
|
|
void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
|
2023-11-27 23:17:03 +01:00
|
|
|
# elif SANITIZER_NETBSD
|
Add NetBSD LSan support
Summary:
Combine few relatively small changes into one:
- implement internal_ptrace() and internal_clone() for NetBSD
- add support for stoptheworld based on the ptrace(2) API
- define COMPILER_RT_HAS_LSAN for NetBSD
- enable tests for NetBSD/amd64
Inspired by the original implementation by Christos Zoulas in netbsd/src for GCC.
The implementation is in theory CPU independent through well defined macros
across all NetBSD ports, however only the x86_64 version was tested.
Reviewers: mgorny, dvyukov, vitalybuka, joerg, jfb
Reviewed By: vitalybuka
Subscribers: dexonsmith, jfb, srhines, kubamracek, llvm-commits, christos
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64057
llvm-svn: 365735
2019-07-11 06:22:35 +00:00
|
|
|
void internal_sigdelset(__sanitizer_sigset_t *set, int signum);
|
|
|
|
uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
|
2023-11-27 23:17:03 +01:00
|
|
|
# endif // SANITIZER_LINUX
|
2013-02-27 11:22:40 +00:00
|
|
|
|
|
|
|
// This class reads thread IDs from /proc/<pid>/task using only syscalls.
|
|
|
|
class ThreadLister {
|
|
|
|
public:
|
2018-05-09 21:21:26 +00:00
|
|
|
explicit ThreadLister(pid_t pid);
|
2018-05-10 04:02:59 +00:00
|
|
|
enum Result {
|
|
|
|
Error,
|
|
|
|
Incomplete,
|
|
|
|
Ok,
|
|
|
|
};
|
|
|
|
Result ListThreads(InternalMmapVector<tid_t> *threads);
|
2024-10-10 20:56:25 -07:00
|
|
|
const char *LoadStatus(tid_t tid);
|
2013-02-27 11:22:40 +00:00
|
|
|
|
|
|
|
private:
|
2024-10-10 20:56:25 -07:00
|
|
|
bool IsAlive(tid_t tid);
|
2018-05-10 04:02:59 +00:00
|
|
|
|
2024-10-10 11:23:11 -07:00
|
|
|
InternalScopedString task_path_;
|
|
|
|
InternalScopedString status_path_;
|
2018-05-07 05:56:36 +00:00
|
|
|
InternalMmapVector<char> buffer_;
|
2013-02-27 11:22:40 +00:00
|
|
|
};
|
2013-03-19 09:30:52 +00:00
|
|
|
|
2013-05-07 14:41:43 +00:00
|
|
|
// Exposed for testing.
|
|
|
|
uptr ThreadDescriptorSize();
|
2013-05-29 13:07:42 +00:00
|
|
|
uptr ThreadSelf();
|
2013-05-07 14:41:43 +00:00
|
|
|
|
2013-05-14 13:24:46 +00:00
|
|
|
// Matches a library's file name against a base name (stripping path and version
|
|
|
|
// information).
|
|
|
|
bool LibraryNameIs(const char *full_name, const char *base_name);
|
|
|
|
|
2013-07-29 19:09:49 +00:00
|
|
|
// Call cb for each region mapped by map.
|
|
|
|
void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr));
|
2017-05-02 15:13:36 +00:00
|
|
|
|
2019-01-19 01:54:09 +00:00
|
|
|
// Releases memory pages entirely within the [beg, end] address range.
|
|
|
|
// The pages no longer count toward RSS; reads are guaranteed to return 0.
|
|
|
|
// Requires (but does not verify!) that pages are MAP_PRIVATE.
|
2020-09-17 16:04:50 +02:00
|
|
|
inline void ReleaseMemoryPagesToOSAndZeroFill(uptr beg, uptr end) {
|
2019-01-19 01:54:09 +00:00
|
|
|
// man madvise on Linux promises zero-fill for anonymous private pages.
|
|
|
|
// Testing shows the same behaviour for private (but not anonymous) mappings
|
|
|
|
// of shm_open() files, as long as the underlying file is untouched.
|
|
|
|
CHECK(SANITIZER_LINUX);
|
|
|
|
ReleaseMemoryPagesToOS(beg, end);
|
|
|
|
}
|
|
|
|
|
2023-11-27 23:17:03 +01:00
|
|
|
# if SANITIZER_ANDROID
|
|
|
|
|
|
|
|
# if defined(__aarch64__)
|
|
|
|
# define __get_tls() \
|
|
|
|
({ \
|
|
|
|
void **__v; \
|
|
|
|
__asm__("mrs %0, tpidr_el0" : "=r"(__v)); \
|
|
|
|
__v; \
|
|
|
|
})
|
|
|
|
# elif defined(__arm__)
|
|
|
|
# define __get_tls() \
|
|
|
|
({ \
|
|
|
|
void **__v; \
|
|
|
|
__asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__v)); \
|
|
|
|
__v; \
|
|
|
|
})
|
|
|
|
# elif defined(__mips__)
|
2017-05-02 15:13:36 +00:00
|
|
|
// On mips32r1, this goes via a kernel illegal instruction trap that's
|
|
|
|
// optimized for v1.
|
2023-11-27 23:17:03 +01:00
|
|
|
# define __get_tls() \
|
|
|
|
({ \
|
|
|
|
register void **__v asm("v1"); \
|
|
|
|
__asm__( \
|
|
|
|
".set push\n" \
|
|
|
|
".set mips32r2\n" \
|
|
|
|
"rdhwr %0,$29\n" \
|
|
|
|
".set pop\n" \
|
|
|
|
: "=r"(__v)); \
|
|
|
|
__v; \
|
|
|
|
})
|
|
|
|
# elif defined(__riscv)
|
|
|
|
# define __get_tls() \
|
|
|
|
({ \
|
|
|
|
void **__v; \
|
|
|
|
__asm__("mv %0, tp" : "=r"(__v)); \
|
|
|
|
__v; \
|
|
|
|
})
|
|
|
|
# elif defined(__i386__)
|
|
|
|
# define __get_tls() \
|
|
|
|
({ \
|
|
|
|
void **__v; \
|
|
|
|
__asm__("movl %%gs:0, %0" : "=r"(__v)); \
|
|
|
|
__v; \
|
|
|
|
})
|
|
|
|
# elif defined(__x86_64__)
|
|
|
|
# define __get_tls() \
|
|
|
|
({ \
|
|
|
|
void **__v; \
|
|
|
|
__asm__("mov %%fs:0, %0" : "=r"(__v)); \
|
|
|
|
__v; \
|
|
|
|
})
|
|
|
|
# else
|
|
|
|
# error "Unsupported architecture."
|
|
|
|
# endif
|
2017-05-02 15:13:36 +00:00
|
|
|
|
2019-01-05 00:45:14 +00:00
|
|
|
// The Android Bionic team has allocated a TLS slot for sanitizers starting
|
|
|
|
// with Q, given that Android currently doesn't support ELF TLS. It is used to
|
|
|
|
// store sanitizer thread specific data.
|
|
|
|
static const int TLS_SLOT_SANITIZER = 6;
|
2017-05-02 15:13:36 +00:00
|
|
|
|
|
|
|
ALWAYS_INLINE uptr *get_android_tls_ptr() {
|
2019-01-05 00:45:14 +00:00
|
|
|
return reinterpret_cast<uptr *>(&__get_tls()[TLS_SLOT_SANITIZER]);
|
2017-05-02 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 23:17:03 +01:00
|
|
|
# endif // SANITIZER_ANDROID
|
2017-05-02 15:13:36 +00:00
|
|
|
|
2013-02-27 11:22:40 +00:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2018-03-16 22:29:29 +00:00
|
|
|
#endif
|
2013-02-27 11:22:40 +00:00
|
|
|
#endif // SANITIZER_LINUX_H
|