• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
11 //
12 // Information about the process mappings.
13 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_PROCMAPS_H
15 #define SANITIZER_PROCMAPS_H
16 
17 #include "sanitizer_internal_defs.h"
18 
19 namespace __sanitizer {
20 
21 #ifdef _WIN32
22 class MemoryMappingLayout {
23  public:
MemoryMappingLayout()24   MemoryMappingLayout() {}
GetObjectNameAndOffset(uptr addr,uptr * offset,char filename[],uptr filename_size)25   bool GetObjectNameAndOffset(uptr addr, uptr *offset,
26                               char filename[], uptr filename_size) {
27     UNIMPLEMENTED();
28     return false;
29   }
30 };
31 
32 #else  // _WIN32
33 class MemoryMappingLayout {
34  public:
35   MemoryMappingLayout();
36   bool Next(uptr *start, uptr *end, uptr *offset,
37             char filename[], uptr filename_size);
38   void Reset();
39   // Gets the object file name and the offset in that object for a given
40   // address 'addr'. Returns true on success.
41   bool GetObjectNameAndOffset(uptr addr, uptr *offset,
42                               char filename[], uptr filename_size);
43   ~MemoryMappingLayout();
44 
45  private:
46   // Default implementation of GetObjectNameAndOffset.
47   // Quite slow, because it iterates through the whole process map for each
48   // lookup.
49   bool IterateForObjectNameAndOffset(uptr addr, uptr *offset,
50                                      char filename[], uptr filename_size) {
51     Reset();
52     uptr start, end, file_offset;
53     for (int i = 0; Next(&start, &end, &file_offset, filename, filename_size);
54          i++) {
55       if (addr >= start && addr < end) {
56         // Don't subtract 'start' for the first entry:
57         // * If a binary is compiled w/o -pie, then the first entry in
58         //   process maps is likely the binary itself (all dynamic libs
59         //   are mapped higher in address space). For such a binary,
60         //   instruction offset in binary coincides with the actual
61         //   instruction address in virtual memory (as code section
62         //   is mapped to a fixed memory range).
63         // * If a binary is compiled with -pie, all the modules are
64         //   mapped high at address space (in particular, higher than
65         //   shadow memory of the tool), so the module can't be the
66         //   first entry.
67         *offset = (addr - (i ? start : 0)) + file_offset;
68         return true;
69       }
70     }
71     if (filename_size)
72       filename[0] = '\0';
73     return false;
74   }
75 
76 # if defined __linux__
77   char *proc_self_maps_buff_;
78   uptr proc_self_maps_buff_mmaped_size_;
79   uptr proc_self_maps_buff_len_;
80   char *current_;
81 # elif defined __APPLE__
82   template<u32 kLCSegment, typename SegmentCommand>
83   bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
84                        char filename[], uptr filename_size);
85   int current_image_;
86   u32 current_magic_;
87   int current_load_cmd_count_;
88   char *current_load_cmd_addr_;
89 # endif
90 };
91 
92 #endif  // _WIN32
93 
94 }  // namespace __sanitizer
95 
96 #endif  // SANITIZER_PROCMAPS_H
97