blob: 94e3871af9a285adb47a99e21a23f7b523d88372 [file] [log] [blame]
Alexey Samsonov28a98952012-06-07 06:15:12 +00001//===-- 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
Alexey Samsonov64ffa592013-12-25 08:39:38 +000017#include "sanitizer_common.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000018#include "sanitizer_internal_defs.h"
Alexander Potapenko78114252012-12-01 02:39:45 +000019#include "sanitizer_mutex.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000020
21namespace __sanitizer {
22
Alexey Samsonovd52b9ba2014-03-06 08:58:24 +000023#if SANITIZER_FREEBSD || SANITIZER_LINUX
Alexander Potapenkoe2b6d082012-12-03 21:21:22 +000024struct ProcSelfMapsBuff {
25 char *data;
26 uptr mmaped_size;
27 uptr len;
28};
Viktor Kutuzova37ad092014-08-06 10:16:52 +000029
30// Reads process memory map in an OS-specific way.
31void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
Alexey Samsonovd52b9ba2014-03-06 08:58:24 +000032#endif // SANITIZER_FREEBSD || SANITIZER_LINUX
Alexander Potapenkoe2b6d082012-12-03 21:21:22 +000033
Alexey Samsonovcc622112012-08-27 13:48:48 +000034class MemoryMappingLayout {
Alexey Samsonov28a98952012-06-07 06:15:12 +000035 public:
Alexander Potapenkof8109dd2013-03-26 10:34:37 +000036 explicit MemoryMappingLayout(bool cache_enabled);
Alexey Samsonov64ffa592013-12-25 08:39:38 +000037 ~MemoryMappingLayout();
Alexey Samsonov28a98952012-06-07 06:15:12 +000038 bool Next(uptr *start, uptr *end, uptr *offset,
Alexey Samsonov06d3aa42013-03-13 06:51:02 +000039 char filename[], uptr filename_size, uptr *protection);
Alexey Samsonov28a98952012-06-07 06:15:12 +000040 void Reset();
Alexander Potapenko78114252012-12-01 02:39:45 +000041 // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
42 // to obtain the memory mappings. It should fall back to pre-cached data
43 // instead of aborting.
44 static void CacheMemoryMappings();
Alexey Samsonov64ffa592013-12-25 08:39:38 +000045
46 // Stores the list of mapped objects into an array.
47 uptr DumpListOfModules(LoadedModule *modules, uptr max_modules,
48 string_predicate_t filter);
Kostya Serebryany98390d02012-06-20 15:19:17 +000049
Alexey Samsonov06d3aa42013-03-13 06:51:02 +000050 // Memory protection masks.
51 static const uptr kProtectionRead = 1;
52 static const uptr kProtectionWrite = 2;
53 static const uptr kProtectionExecute = 4;
54 static const uptr kProtectionShared = 8;
55
Alexey Samsonov28a98952012-06-07 06:15:12 +000056 private:
Alexander Potapenko78114252012-12-01 02:39:45 +000057 void LoadFromCache();
Alexey Samsonov28a98952012-06-07 06:15:12 +000058
Alexey Samsonov2f392d22013-12-25 08:01:16 +000059 // FIXME: Hide implementation details for different platforms in
60 // platform-specific files.
Alexey Samsonovd52b9ba2014-03-06 08:58:24 +000061# if SANITIZER_FREEBSD || SANITIZER_LINUX
Alexander Potapenkoe2b6d082012-12-03 21:21:22 +000062 ProcSelfMapsBuff proc_self_maps_;
Viktor Kutuzova37ad092014-08-06 10:16:52 +000063 const char *current_;
Alexander Potapenko78114252012-12-01 02:39:45 +000064
65 // Static mappings cache.
Alexander Potapenkoe2b6d082012-12-03 21:21:22 +000066 static ProcSelfMapsBuff cached_proc_self_maps_;
67 static StaticSpinMutex cache_lock_; // protects cached_proc_self_maps_.
Alexey Samsonova0e28a72013-04-03 07:24:35 +000068# elif SANITIZER_MAC
Alexey Samsonov28a98952012-06-07 06:15:12 +000069 template<u32 kLCSegment, typename SegmentCommand>
70 bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
Alexey Samsonov91f833a2013-03-13 07:39:25 +000071 char filename[], uptr filename_size,
72 uptr *protection);
Alexey Samsonov28a98952012-06-07 06:15:12 +000073 int current_image_;
74 u32 current_magic_;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +000075 u32 current_filetype_;
Alexey Samsonov28a98952012-06-07 06:15:12 +000076 int current_load_cmd_count_;
77 char *current_load_cmd_addr_;
Alexey Samsonovcae486c2012-08-28 07:22:24 +000078# endif
Alexey Samsonov28a98952012-06-07 06:15:12 +000079};
80
Alexander Potapenko7e1c5192013-09-03 11:09:16 +000081typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
82 /*out*/uptr *stats, uptr stats_size);
83
84// Parse the contents of /proc/self/smaps and generate a memory profile.
85// |cb| is a tool-specific callback that fills the |stats| array containing
86// |stats_size| elements.
87void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
88
Dmitry Vyukov4e9c0912013-09-21 21:41:08 +000089// Returns code range for the specified module.
90bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
91
Viktor Kutuzova37ad092014-08-06 10:16:52 +000092bool IsDecimal(char c);
93uptr ParseDecimal(const char **p);
94bool IsHex(char c);
95uptr ParseHex(const char **p);
96
Alexey Samsonov28a98952012-06-07 06:15:12 +000097} // namespace __sanitizer
98
99#endif // SANITIZER_PROCMAPS_H