sanitizer_linux.h revision 6eb836f9bd2b96a321ddc7fb5ea08aa65131e61f
1//===-- sanitizer_linux.h ---------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Linux-specific syscall wrappers and classes.
11//
12//===----------------------------------------------------------------------===//
13#ifndef SANITIZER_LINUX_H
14#define SANITIZER_LINUX_H
15
16#include "sanitizer_platform.h"
17#if SANITIZER_LINUX
18#include "sanitizer_common.h"
19#include "sanitizer_internal_defs.h"
20
21struct link_map;  // Opaque type returned by dlopen().
22struct sigaltstack;
23
24namespace __sanitizer {
25// Dirent structure for getdents(). Note that this structure is different from
26// the one in <dirent.h>, which is used by readdir().
27struct linux_dirent;
28
29// Syscall wrappers.
30uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
31uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5);
32uptr internal_sigaltstack(const struct sigaltstack* ss,
33                          struct sigaltstack* oss);
34#ifdef __x86_64__
35uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
36                    int *parent_tidptr, void *newtls, int *child_tidptr);
37#endif
38
39// This class reads thread IDs from /proc/<pid>/task using only syscalls.
40class ThreadLister {
41 public:
42  explicit ThreadLister(int pid);
43  ~ThreadLister();
44  // GetNextTID returns -1 if the list of threads is exhausted, or if there has
45  // been an error.
46  int GetNextTID();
47  void Reset();
48  bool error();
49
50 private:
51  bool GetDirectoryEntries();
52
53  int pid_;
54  int descriptor_;
55  InternalScopedBuffer<char> buffer_;
56  bool error_;
57  struct linux_dirent* entry_;
58  int bytes_read_;
59};
60
61void AdjustStackSizeLinux(void *attr, int verbosity);
62
63// Exposed for testing.
64uptr ThreadDescriptorSize();
65uptr ThreadSelf();
66uptr ThreadSelfOffset();
67
68// Matches a library's file name against a base name (stripping path and version
69// information).
70bool LibraryNameIs(const char *full_name, const char *base_name);
71
72// Read the name of the current binary from /proc/self/exe.
73uptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
74// Cache the value of /proc/self/exe.
75void CacheBinaryName();
76
77// Call cb for each region mapped by map.
78void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr));
79
80// PTHREAD_DESTRUCTOR_ITERATIONS from glibc.
81const uptr kPthreadDestructorIterations = 4;
82}  // namespace __sanitizer
83
84#endif  // SANITIZER_LINUX
85#endif  // SANITIZER_LINUX_H
86