[Sanitizer] extend internal libc with stat/fstat/lstat functions
[blocksruntime.git] / lib / sanitizer_common / sanitizer_procmaps.h
blob1b8ea7aff1653d157dafd232950db5e7d8fd3fa2
1 //===-- sanitizer_procmaps.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 // This file is shared between AddressSanitizer and ThreadSanitizer.
12 // Information about the process mappings.
13 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_PROCMAPS_H
15 #define SANITIZER_PROCMAPS_H
17 #include "sanitizer_internal_defs.h"
18 #include "sanitizer_mutex.h"
20 namespace __sanitizer {
22 #ifdef _WIN32
23 class MemoryMappingLayout {
24 public:
25 MemoryMappingLayout() {}
26 bool GetObjectNameAndOffset(uptr addr, uptr *offset,
27 char filename[], uptr filename_size) {
28 UNIMPLEMENTED();
32 #else // _WIN32
33 #if defined(__linux__)
34 struct ProcSelfMapsBuff {
35 char *data;
36 uptr mmaped_size;
37 uptr len;
39 #endif // defined(__linux__)
41 class MemoryMappingLayout {
42 public:
43 MemoryMappingLayout();
44 bool Next(uptr *start, uptr *end, uptr *offset,
45 char filename[], uptr filename_size);
46 void Reset();
47 // Gets the object file name and the offset in that object for a given
48 // address 'addr'. Returns true on success.
49 bool GetObjectNameAndOffset(uptr addr, uptr *offset,
50 char filename[], uptr filename_size);
51 // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
52 // to obtain the memory mappings. It should fall back to pre-cached data
53 // instead of aborting.
54 static void CacheMemoryMappings();
55 ~MemoryMappingLayout();
57 private:
58 void LoadFromCache();
59 // Default implementation of GetObjectNameAndOffset.
60 // Quite slow, because it iterates through the whole process map for each
61 // lookup.
62 bool IterateForObjectNameAndOffset(uptr addr, uptr *offset,
63 char filename[], uptr filename_size) {
64 Reset();
65 uptr start, end, file_offset;
66 for (int i = 0; Next(&start, &end, &file_offset, filename, filename_size);
67 i++) {
68 if (addr >= start && addr < end) {
69 // Don't subtract 'start' for the first entry:
70 // * If a binary is compiled w/o -pie, then the first entry in
71 // process maps is likely the binary itself (all dynamic libs
72 // are mapped higher in address space). For such a binary,
73 // instruction offset in binary coincides with the actual
74 // instruction address in virtual memory (as code section
75 // is mapped to a fixed memory range).
76 // * If a binary is compiled with -pie, all the modules are
77 // mapped high at address space (in particular, higher than
78 // shadow memory of the tool), so the module can't be the
79 // first entry.
80 *offset = (addr - (i ? start : 0)) + file_offset;
81 return true;
84 if (filename_size)
85 filename[0] = '\0';
86 return false;
89 # if defined __linux__
90 ProcSelfMapsBuff proc_self_maps_;
91 char *current_;
93 // Static mappings cache.
94 static ProcSelfMapsBuff cached_proc_self_maps_;
95 static StaticSpinMutex cache_lock_; // protects cached_proc_self_maps_.
96 # elif defined __APPLE__
97 template<u32 kLCSegment, typename SegmentCommand>
98 bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
99 char filename[], uptr filename_size);
100 int current_image_;
101 u32 current_magic_;
102 u32 current_filetype_;
103 int current_load_cmd_count_;
104 char *current_load_cmd_addr_;
105 # endif
108 #endif // _WIN32
110 } // namespace __sanitizer
112 #endif // SANITIZER_PROCMAPS_H