1 //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
{
23 class MemoryMappingLayout
{
25 MemoryMappingLayout() {}
26 bool GetObjectNameAndOffset(uptr addr
, uptr
*offset
,
27 char filename
[], uptr filename_size
) {
33 #if defined(__linux__)
34 struct ProcSelfMapsBuff
{
39 #endif // defined(__linux__)
41 class MemoryMappingLayout
{
43 MemoryMappingLayout();
44 bool Next(uptr
*start
, uptr
*end
, uptr
*offset
,
45 char filename
[], uptr filename_size
);
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();
59 // Default implementation of GetObjectNameAndOffset.
60 // Quite slow, because it iterates through the whole process map for each
62 bool IterateForObjectNameAndOffset(uptr addr
, uptr
*offset
,
63 char filename
[], uptr filename_size
) {
65 uptr start
, end
, file_offset
;
66 for (int i
= 0; Next(&start
, &end
, &file_offset
, filename
, filename_size
);
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
80 *offset
= (addr
- (i
? start
: 0)) + file_offset
;
89 # if defined __linux__
90 ProcSelfMapsBuff proc_self_maps_
;
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
);
102 u32 current_filetype_
;
103 int current_load_cmd_count_
;
104 char *current_load_cmd_addr_
;
110 } // namespace __sanitizer
112 #endif // SANITIZER_PROCMAPS_H