16fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//===-- sanitizer_linux.h ---------------------------------------*- C++ -*-===//
26fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//
36fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//                     The LLVM Compiler Infrastructure
46fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//
56fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany// This file is distributed under the University of Illinois Open Source
66fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany// License. See LICENSE.TXT for details.
76fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//
86fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//===----------------------------------------------------------------------===//
96fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//
106fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany// Linux-specific syscall wrappers and classes.
116fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//
126fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany//===----------------------------------------------------------------------===//
136fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany#ifndef SANITIZER_LINUX_H
146fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany#define SANITIZER_LINUX_H
156fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany
1610f3ab775088bec69dd9e7b611f9b4e152f629bcAlexey Samsonov#include "sanitizer_common.h"
176fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany#include "sanitizer_internal_defs.h"
186fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany
192e75ac983cb233daa1abfa35fb33d2bafffe2ab1Peter Collingbournestruct link_map;  // Opaque type returned by dlopen().
206fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanystruct sigaltstack;
216fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany
226fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanynamespace __sanitizer {
236fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany// Dirent structure for getdents(). Note that this structure is different from
246fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany// the one in <dirent.h>, which is used by readdir().
256fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanystruct linux_dirent;
266fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany
276fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany// Syscall wrappers.
289578a3ecfc35a264ede1135033398e2a77a6cad6Peter Collingbourneuptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
299578a3ecfc35a264ede1135033398e2a77a6cad6Peter Collingbourneuptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5);
309578a3ecfc35a264ede1135033398e2a77a6cad6Peter Collingbourneuptr internal_sigaltstack(const struct sigaltstack* ss,
319578a3ecfc35a264ede1135033398e2a77a6cad6Peter Collingbourne                          struct sigaltstack* oss);
326fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany
336fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany// This class reads thread IDs from /proc/<pid>/task using only syscalls.
346fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryanyclass ThreadLister {
356fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany public:
366fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  explicit ThreadLister(int pid);
376fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  ~ThreadLister();
386fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  // GetNextTID returns -1 if the list of threads is exhausted, or if there has
396fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  // been an error.
406fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  int GetNextTID();
416fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  void Reset();
426fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  bool error();
436fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany
446fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany private:
456fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  bool GetDirectoryEntries();
466fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany
476fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  int pid_;
486fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  int descriptor_;
4910f3ab775088bec69dd9e7b611f9b4e152f629bcAlexey Samsonov  InternalScopedBuffer<char> buffer_;
506fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  bool error_;
516fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  struct linux_dirent* entry_;
526fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany  int bytes_read_;
536fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany};
54b9bf700ae7fe59e25976e0abe9636150f3a39cd2Evgeniy Stepanov
55b9bf700ae7fe59e25976e0abe9636150f3a39cd2Evgeniy Stepanovvoid AdjustStackSizeLinux(void *attr, int verbosity);
56b9bf700ae7fe59e25976e0abe9636150f3a39cd2Evgeniy Stepanov
5724323de08fa8850712d56621bff29dbbbd0285a9Sergey Matveev// Exposed for testing.
5824323de08fa8850712d56621bff29dbbbd0285a9Sergey Matveevuptr ThreadDescriptorSize();
594c086441ff9d06e53fa941671781008d453dba49Sergey Matveevuptr ThreadSelf();
604c086441ff9d06e53fa941671781008d453dba49Sergey Matveevuptr ThreadSelfOffset();
6124323de08fa8850712d56621bff29dbbbd0285a9Sergey Matveev
623de0086409d143a612a54a7a0ed809e286656188Sergey Matveev// Matches a library's file name against a base name (stripping path and version
633de0086409d143a612a54a7a0ed809e286656188Sergey Matveev// information).
643de0086409d143a612a54a7a0ed809e286656188Sergey Matveevbool LibraryNameIs(const char *full_name, const char *base_name);
653de0086409d143a612a54a7a0ed809e286656188Sergey Matveev
6651c963a812ca2fd24a72e17d1b5c127f4f9290bdPeter Collingbourne// Read the name of the current binary from /proc/self/exe.
6751c963a812ca2fd24a72e17d1b5c127f4f9290bdPeter Collingbourneuptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
6851c963a812ca2fd24a72e17d1b5c127f4f9290bdPeter Collingbourne
692e75ac983cb233daa1abfa35fb33d2bafffe2ab1Peter Collingbourne// Call cb for each region mapped by map.
702e75ac983cb233daa1abfa35fb33d2bafffe2ab1Peter Collingbournevoid ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr));
712e75ac983cb233daa1abfa35fb33d2bafffe2ab1Peter Collingbourne
726fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany}  // namespace __sanitizer
736fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany
746fb47af2d2d305adbfc3d41bea589d1527a364a9Kostya Serebryany#endif  // SANITIZER_LINUX_H
75