annotate libsanitizer/sanitizer_common/sanitizer_stackdepot.cc @ 138:fc828634a951

merge
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:17:14 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- sanitizer_stackdepot.cc -------------------------------------------===//
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // This file is shared between AddressSanitizer and ThreadSanitizer
kono
parents:
diff changeset
9 // run-time libraries.
kono
parents:
diff changeset
10 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 #include "sanitizer_stackdepot.h"
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 #include "sanitizer_common.h"
kono
parents:
diff changeset
15 #include "sanitizer_stackdepotbase.h"
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 namespace __sanitizer {
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 struct StackDepotNode {
kono
parents:
diff changeset
20 StackDepotNode *link;
kono
parents:
diff changeset
21 u32 id;
kono
parents:
diff changeset
22 atomic_uint32_t hash_and_use_count; // hash_bits : 12; use_count : 20;
kono
parents:
diff changeset
23 u32 size;
kono
parents:
diff changeset
24 u32 tag;
kono
parents:
diff changeset
25 uptr stack[1]; // [size]
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 static const u32 kTabSizeLog = 20;
kono
parents:
diff changeset
28 // Lower kTabSizeLog bits are equal for all items in one bucket.
kono
parents:
diff changeset
29 // We use these bits to store the per-stack use counter.
kono
parents:
diff changeset
30 static const u32 kUseCountBits = kTabSizeLog;
kono
parents:
diff changeset
31 static const u32 kMaxUseCount = 1 << kUseCountBits;
kono
parents:
diff changeset
32 static const u32 kUseCountMask = (1 << kUseCountBits) - 1;
kono
parents:
diff changeset
33 static const u32 kHashMask = ~kUseCountMask;
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 typedef StackTrace args_type;
kono
parents:
diff changeset
36 bool eq(u32 hash, const args_type &args) const {
kono
parents:
diff changeset
37 u32 hash_bits =
kono
parents:
diff changeset
38 atomic_load(&hash_and_use_count, memory_order_relaxed) & kHashMask;
kono
parents:
diff changeset
39 if ((hash & kHashMask) != hash_bits || args.size != size || args.tag != tag)
kono
parents:
diff changeset
40 return false;
kono
parents:
diff changeset
41 uptr i = 0;
kono
parents:
diff changeset
42 for (; i < size; i++) {
kono
parents:
diff changeset
43 if (stack[i] != args.trace[i]) return false;
kono
parents:
diff changeset
44 }
kono
parents:
diff changeset
45 return true;
kono
parents:
diff changeset
46 }
kono
parents:
diff changeset
47 static uptr storage_size(const args_type &args) {
kono
parents:
diff changeset
48 return sizeof(StackDepotNode) + (args.size - 1) * sizeof(uptr);
kono
parents:
diff changeset
49 }
kono
parents:
diff changeset
50 static u32 hash(const args_type &args) {
kono
parents:
diff changeset
51 // murmur2
kono
parents:
diff changeset
52 const u32 m = 0x5bd1e995;
kono
parents:
diff changeset
53 const u32 seed = 0x9747b28c;
kono
parents:
diff changeset
54 const u32 r = 24;
kono
parents:
diff changeset
55 u32 h = seed ^ (args.size * sizeof(uptr));
kono
parents:
diff changeset
56 for (uptr i = 0; i < args.size; i++) {
kono
parents:
diff changeset
57 u32 k = args.trace[i];
kono
parents:
diff changeset
58 k *= m;
kono
parents:
diff changeset
59 k ^= k >> r;
kono
parents:
diff changeset
60 k *= m;
kono
parents:
diff changeset
61 h *= m;
kono
parents:
diff changeset
62 h ^= k;
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64 h ^= h >> 13;
kono
parents:
diff changeset
65 h *= m;
kono
parents:
diff changeset
66 h ^= h >> 15;
kono
parents:
diff changeset
67 return h;
kono
parents:
diff changeset
68 }
kono
parents:
diff changeset
69 static bool is_valid(const args_type &args) {
kono
parents:
diff changeset
70 return args.size > 0 && args.trace;
kono
parents:
diff changeset
71 }
kono
parents:
diff changeset
72 void store(const args_type &args, u32 hash) {
kono
parents:
diff changeset
73 atomic_store(&hash_and_use_count, hash & kHashMask, memory_order_relaxed);
kono
parents:
diff changeset
74 size = args.size;
kono
parents:
diff changeset
75 tag = args.tag;
kono
parents:
diff changeset
76 internal_memcpy(stack, args.trace, size * sizeof(uptr));
kono
parents:
diff changeset
77 }
kono
parents:
diff changeset
78 args_type load() const {
kono
parents:
diff changeset
79 return args_type(&stack[0], size, tag);
kono
parents:
diff changeset
80 }
kono
parents:
diff changeset
81 StackDepotHandle get_handle() { return StackDepotHandle(this); }
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 typedef StackDepotHandle handle_type;
kono
parents:
diff changeset
84 };
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 COMPILER_CHECK(StackDepotNode::kMaxUseCount == (u32)kStackDepotMaxUseCount);
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 u32 StackDepotHandle::id() { return node_->id; }
kono
parents:
diff changeset
89 int StackDepotHandle::use_count() {
kono
parents:
diff changeset
90 return atomic_load(&node_->hash_and_use_count, memory_order_relaxed) &
kono
parents:
diff changeset
91 StackDepotNode::kUseCountMask;
kono
parents:
diff changeset
92 }
kono
parents:
diff changeset
93 void StackDepotHandle::inc_use_count_unsafe() {
kono
parents:
diff changeset
94 u32 prev =
kono
parents:
diff changeset
95 atomic_fetch_add(&node_->hash_and_use_count, 1, memory_order_relaxed) &
kono
parents:
diff changeset
96 StackDepotNode::kUseCountMask;
kono
parents:
diff changeset
97 CHECK_LT(prev + 1, StackDepotNode::kMaxUseCount);
kono
parents:
diff changeset
98 }
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 // FIXME(dvyukov): this single reserved bit is used in TSan.
kono
parents:
diff changeset
101 typedef StackDepotBase<StackDepotNode, 1, StackDepotNode::kTabSizeLog>
kono
parents:
diff changeset
102 StackDepot;
kono
parents:
diff changeset
103 static StackDepot theDepot;
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 StackDepotStats *StackDepotGetStats() {
kono
parents:
diff changeset
106 return theDepot.GetStats();
kono
parents:
diff changeset
107 }
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 u32 StackDepotPut(StackTrace stack) {
kono
parents:
diff changeset
110 StackDepotHandle h = theDepot.Put(stack);
kono
parents:
diff changeset
111 return h.valid() ? h.id() : 0;
kono
parents:
diff changeset
112 }
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 StackDepotHandle StackDepotPut_WithHandle(StackTrace stack) {
kono
parents:
diff changeset
115 return theDepot.Put(stack);
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 StackTrace StackDepotGet(u32 id) {
kono
parents:
diff changeset
119 return theDepot.Get(id);
kono
parents:
diff changeset
120 }
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 void StackDepotLockAll() {
kono
parents:
diff changeset
123 theDepot.LockAll();
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 void StackDepotUnlockAll() {
kono
parents:
diff changeset
127 theDepot.UnlockAll();
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 bool StackDepotReverseMap::IdDescPair::IdComparator(
kono
parents:
diff changeset
131 const StackDepotReverseMap::IdDescPair &a,
kono
parents:
diff changeset
132 const StackDepotReverseMap::IdDescPair &b) {
kono
parents:
diff changeset
133 return a.id < b.id;
kono
parents:
diff changeset
134 }
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 StackDepotReverseMap::StackDepotReverseMap()
kono
parents:
diff changeset
137 : map_(StackDepotGetStats()->n_uniq_ids + 100) {
kono
parents:
diff changeset
138 for (int idx = 0; idx < StackDepot::kTabSize; idx++) {
kono
parents:
diff changeset
139 atomic_uintptr_t *p = &theDepot.tab[idx];
kono
parents:
diff changeset
140 uptr v = atomic_load(p, memory_order_consume);
kono
parents:
diff changeset
141 StackDepotNode *s = (StackDepotNode*)(v & ~1);
kono
parents:
diff changeset
142 for (; s; s = s->link) {
kono
parents:
diff changeset
143 IdDescPair pair = {s->id, s};
kono
parents:
diff changeset
144 map_.push_back(pair);
kono
parents:
diff changeset
145 }
kono
parents:
diff changeset
146 }
kono
parents:
diff changeset
147 InternalSort(&map_, map_.size(), IdDescPair::IdComparator);
kono
parents:
diff changeset
148 }
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 StackTrace StackDepotReverseMap::Get(u32 id) {
kono
parents:
diff changeset
151 if (!map_.size())
kono
parents:
diff changeset
152 return StackTrace();
kono
parents:
diff changeset
153 IdDescPair pair = {id, nullptr};
kono
parents:
diff changeset
154 uptr idx =
kono
parents:
diff changeset
155 InternalLowerBound(map_, 0, map_.size(), pair, IdDescPair::IdComparator);
kono
parents:
diff changeset
156 if (idx > map_.size() || map_[idx].id != id)
kono
parents:
diff changeset
157 return StackTrace();
kono
parents:
diff changeset
158 return map_[idx].desc->load();
kono
parents:
diff changeset
159 }
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 } // namespace __sanitizer