asan_globals.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //===-- asan_globals.cc ---------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of AddressSanitizer, an address sanity checker.
  9. //
  10. // Handle globals.
  11. //===----------------------------------------------------------------------===//
  12. #include "asan_interceptors.h"
  13. #include "asan_internal.h"
  14. #include "asan_mapping.h"
  15. #include "asan_poisoning.h"
  16. #include "asan_report.h"
  17. #include "asan_stack.h"
  18. #include "asan_stats.h"
  19. #include "asan_thread.h"
  20. #include "sanitizer_common/sanitizer_common.h"
  21. #include "sanitizer_common/sanitizer_mutex.h"
  22. #include "sanitizer_common/sanitizer_placement_new.h"
  23. #include "sanitizer_common/sanitizer_stackdepot.h"
  24. namespace __asan {
  25. typedef __asan_global Global;
  26. struct ListOfGlobals {
  27. const Global *g;
  28. ListOfGlobals *next;
  29. };
  30. static BlockingMutex mu_for_globals(LINKER_INITIALIZED);
  31. static LowLevelAllocator allocator_for_globals;
  32. static ListOfGlobals *list_of_all_globals;
  33. static const int kDynamicInitGlobalsInitialCapacity = 512;
  34. struct DynInitGlobal {
  35. Global g;
  36. bool initialized;
  37. };
  38. typedef InternalMmapVector<DynInitGlobal> VectorOfGlobals;
  39. // Lazy-initialized and never deleted.
  40. static VectorOfGlobals *dynamic_init_globals;
  41. // We want to remember where a certain range of globals was registered.
  42. struct GlobalRegistrationSite {
  43. u32 stack_id;
  44. Global *g_first, *g_last;
  45. };
  46. typedef InternalMmapVector<GlobalRegistrationSite> GlobalRegistrationSiteVector;
  47. static GlobalRegistrationSiteVector *global_registration_site_vector;
  48. ALWAYS_INLINE void PoisonShadowForGlobal(const Global *g, u8 value) {
  49. FastPoisonShadow(g->beg, g->size_with_redzone, value);
  50. }
  51. ALWAYS_INLINE void PoisonRedZones(const Global &g) {
  52. uptr aligned_size = RoundUpTo(g.size, SHADOW_GRANULARITY);
  53. FastPoisonShadow(g.beg + aligned_size, g.size_with_redzone - aligned_size,
  54. kAsanGlobalRedzoneMagic);
  55. if (g.size != aligned_size) {
  56. FastPoisonShadowPartialRightRedzone(
  57. g.beg + RoundDownTo(g.size, SHADOW_GRANULARITY),
  58. g.size % SHADOW_GRANULARITY,
  59. SHADOW_GRANULARITY,
  60. kAsanGlobalRedzoneMagic);
  61. }
  62. }
  63. const uptr kMinimalDistanceFromAnotherGlobal = 64;
  64. bool IsAddressNearGlobal(uptr addr, const __asan_global &g) {
  65. if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
  66. if (addr >= g.beg + g.size_with_redzone) return false;
  67. return true;
  68. }
  69. static void ReportGlobal(const Global &g, const char *prefix) {
  70. Report("%s Global[%p]: beg=%p size=%zu/%zu name=%s module=%s dyn_init=%zu\n",
  71. prefix, &g, (void *)g.beg, g.size, g.size_with_redzone, g.name,
  72. g.module_name, g.has_dynamic_init);
  73. if (g.location) {
  74. Report(" location (%p): name=%s[%p], %d %d\n", g.location,
  75. g.location->filename, g.location->filename, g.location->line_no,
  76. g.location->column_no);
  77. }
  78. }
  79. static bool DescribeOrGetInfoIfGlobal(uptr addr, uptr size, bool print,
  80. Global *output_global) {
  81. if (!flags()->report_globals) return false;
  82. BlockingMutexLock lock(&mu_for_globals);
  83. bool res = false;
  84. for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
  85. const Global &g = *l->g;
  86. if (print) {
  87. if (flags()->report_globals >= 2)
  88. ReportGlobal(g, "Search");
  89. res |= DescribeAddressRelativeToGlobal(addr, size, g);
  90. } else {
  91. if (IsAddressNearGlobal(addr, g)) {
  92. CHECK(output_global);
  93. *output_global = g;
  94. return true;
  95. }
  96. }
  97. }
  98. return res;
  99. }
  100. bool DescribeAddressIfGlobal(uptr addr, uptr size) {
  101. return DescribeOrGetInfoIfGlobal(addr, size, /* print */ true,
  102. /* output_global */ nullptr);
  103. }
  104. bool GetInfoForAddressIfGlobal(uptr addr, AddressDescription *descr) {
  105. Global g = {};
  106. if (DescribeOrGetInfoIfGlobal(addr, /* size */ 1, /* print */ false, &g)) {
  107. internal_strncpy(descr->name, g.name, descr->name_size);
  108. descr->region_address = g.beg;
  109. descr->region_size = g.size;
  110. descr->region_kind = "global";
  111. return true;
  112. }
  113. return false;
  114. }
  115. u32 FindRegistrationSite(const Global *g) {
  116. CHECK(global_registration_site_vector);
  117. for (uptr i = 0, n = global_registration_site_vector->size(); i < n; i++) {
  118. GlobalRegistrationSite &grs = (*global_registration_site_vector)[i];
  119. if (g >= grs.g_first && g <= grs.g_last)
  120. return grs.stack_id;
  121. }
  122. return 0;
  123. }
  124. // Register a global variable.
  125. // This function may be called more than once for every global
  126. // so we store the globals in a map.
  127. static void RegisterGlobal(const Global *g) {
  128. CHECK(asan_inited);
  129. if (flags()->report_globals >= 2)
  130. ReportGlobal(*g, "Added");
  131. CHECK(flags()->report_globals);
  132. CHECK(AddrIsInMem(g->beg));
  133. CHECK(AddrIsAlignedByGranularity(g->beg));
  134. CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
  135. // This "ODR violation" detection is fundamentally incompatible with
  136. // how GCC registers globals. Disable as useless until rewritten upstream.
  137. if (0 && flags()->detect_odr_violation) {
  138. // Try detecting ODR (One Definition Rule) violation, i.e. the situation
  139. // where two globals with the same name are defined in different modules.
  140. if (__asan_region_is_poisoned(g->beg, g->size_with_redzone)) {
  141. // This check may not be enough: if the first global is much larger
  142. // the entire redzone of the second global may be within the first global.
  143. for (ListOfGlobals *l = list_of_all_globals; l; l = l->next) {
  144. if (g->beg == l->g->beg &&
  145. (flags()->detect_odr_violation >= 2 || g->size != l->g->size))
  146. ReportODRViolation(g, FindRegistrationSite(g),
  147. l->g, FindRegistrationSite(l->g));
  148. }
  149. }
  150. }
  151. if (flags()->poison_heap)
  152. PoisonRedZones(*g);
  153. ListOfGlobals *l = new(allocator_for_globals) ListOfGlobals;
  154. l->g = g;
  155. l->next = list_of_all_globals;
  156. list_of_all_globals = l;
  157. if (g->has_dynamic_init) {
  158. if (dynamic_init_globals == 0) {
  159. dynamic_init_globals = new(allocator_for_globals)
  160. VectorOfGlobals(kDynamicInitGlobalsInitialCapacity);
  161. }
  162. DynInitGlobal dyn_global = { *g, false };
  163. dynamic_init_globals->push_back(dyn_global);
  164. }
  165. }
  166. static void UnregisterGlobal(const Global *g) {
  167. CHECK(asan_inited);
  168. CHECK(flags()->report_globals);
  169. CHECK(AddrIsInMem(g->beg));
  170. CHECK(AddrIsAlignedByGranularity(g->beg));
  171. CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
  172. if (flags()->poison_heap)
  173. PoisonShadowForGlobal(g, 0);
  174. // We unpoison the shadow memory for the global but we do not remove it from
  175. // the list because that would require O(n^2) time with the current list
  176. // implementation. It might not be worth doing anyway.
  177. }
  178. void StopInitOrderChecking() {
  179. BlockingMutexLock lock(&mu_for_globals);
  180. if (!flags()->check_initialization_order || !dynamic_init_globals)
  181. return;
  182. flags()->check_initialization_order = false;
  183. for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
  184. DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
  185. const Global *g = &dyn_g.g;
  186. // Unpoison the whole global.
  187. PoisonShadowForGlobal(g, 0);
  188. // Poison redzones back.
  189. PoisonRedZones(*g);
  190. }
  191. }
  192. } // namespace __asan
  193. // ---------------------- Interface ---------------- {{{1
  194. using namespace __asan; // NOLINT
  195. // Register an array of globals.
  196. void __asan_register_globals(__asan_global *globals, uptr n) {
  197. if (!flags()->report_globals) return;
  198. GET_STACK_TRACE_FATAL_HERE;
  199. u32 stack_id = StackDepotPut(stack);
  200. BlockingMutexLock lock(&mu_for_globals);
  201. if (!global_registration_site_vector)
  202. global_registration_site_vector =
  203. new(allocator_for_globals) GlobalRegistrationSiteVector(128);
  204. GlobalRegistrationSite site = {stack_id, &globals[0], &globals[n - 1]};
  205. global_registration_site_vector->push_back(site);
  206. if (flags()->report_globals >= 2) {
  207. PRINT_CURRENT_STACK();
  208. Printf("=== ID %d; %p %p\n", stack_id, &globals[0], &globals[n - 1]);
  209. }
  210. for (uptr i = 0; i < n; i++) {
  211. RegisterGlobal(&globals[i]);
  212. }
  213. }
  214. // Unregister an array of globals.
  215. // We must do this when a shared objects gets dlclosed.
  216. void __asan_unregister_globals(__asan_global *globals, uptr n) {
  217. if (!flags()->report_globals) return;
  218. BlockingMutexLock lock(&mu_for_globals);
  219. for (uptr i = 0; i < n; i++) {
  220. UnregisterGlobal(&globals[i]);
  221. }
  222. }
  223. // This method runs immediately prior to dynamic initialization in each TU,
  224. // when all dynamically initialized globals are unpoisoned. This method
  225. // poisons all global variables not defined in this TU, so that a dynamic
  226. // initializer can only touch global variables in the same TU.
  227. void __asan_before_dynamic_init(const char *module_name) {
  228. if (!flags()->check_initialization_order ||
  229. !flags()->poison_heap)
  230. return;
  231. bool strict_init_order = flags()->strict_init_order;
  232. CHECK(dynamic_init_globals);
  233. CHECK(module_name);
  234. CHECK(asan_inited);
  235. BlockingMutexLock lock(&mu_for_globals);
  236. if (flags()->report_globals >= 3)
  237. Printf("DynInitPoison module: %s\n", module_name);
  238. for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
  239. DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
  240. const Global *g = &dyn_g.g;
  241. if (dyn_g.initialized)
  242. continue;
  243. if (g->module_name != module_name)
  244. PoisonShadowForGlobal(g, kAsanInitializationOrderMagic);
  245. else if (!strict_init_order)
  246. dyn_g.initialized = true;
  247. }
  248. }
  249. // This method runs immediately after dynamic initialization in each TU, when
  250. // all dynamically initialized globals except for those defined in the current
  251. // TU are poisoned. It simply unpoisons all dynamically initialized globals.
  252. void __asan_after_dynamic_init() {
  253. if (!flags()->check_initialization_order ||
  254. !flags()->poison_heap)
  255. return;
  256. CHECK(asan_inited);
  257. BlockingMutexLock lock(&mu_for_globals);
  258. // FIXME: Optionally report that we're unpoisoning globals from a module.
  259. for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {
  260. DynInitGlobal &dyn_g = (*dynamic_init_globals)[i];
  261. const Global *g = &dyn_g.g;
  262. if (!dyn_g.initialized) {
  263. // Unpoison the whole global.
  264. PoisonShadowForGlobal(g, 0);
  265. // Poison redzones back.
  266. PoisonRedZones(*g);
  267. }
  268. }
  269. }