]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/dfsan/dfsan.cc
Upgrade our copy of clang and llvm to 3.5.1 release. This is a bugfix
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / dfsan / dfsan.cc
1 //===-- dfsan.cc ----------------------------------------------------------===//
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 a part of DataFlowSanitizer.
11 //
12 // DataFlowSanitizer runtime.  This file defines the public interface to
13 // DataFlowSanitizer as well as the definition of certain runtime functions
14 // called automatically by the compiler (specifically the instrumentation pass
15 // in llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp).
16 //
17 // The public interface is defined in include/sanitizer/dfsan_interface.h whose
18 // functions are prefixed dfsan_ while the compiler interface functions are
19 // prefixed __dfsan_.
20 //===----------------------------------------------------------------------===//
21
22 #include "sanitizer_common/sanitizer_atomic.h"
23 #include "sanitizer_common/sanitizer_common.h"
24 #include "sanitizer_common/sanitizer_flags.h"
25 #include "sanitizer_common/sanitizer_libc.h"
26
27 #include "dfsan/dfsan.h"
28
29 using namespace __dfsan;
30
31 typedef atomic_uint16_t atomic_dfsan_label;
32 static const dfsan_label kInitializingLabel = -1;
33
34 static const uptr kNumLabels = 1 << (sizeof(dfsan_label) * 8);
35
36 static atomic_dfsan_label __dfsan_last_label;
37 static dfsan_label_info __dfsan_label_info[kNumLabels];
38
39 Flags __dfsan::flags_data;
40
41 SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL dfsan_label __dfsan_retval_tls;
42 SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL dfsan_label __dfsan_arg_tls[64];
43
44 // On Linux/x86_64, memory is laid out as follows:
45 //
46 // +--------------------+ 0x800000000000 (top of memory)
47 // | application memory |
48 // +--------------------+ 0x700000008000 (kAppAddr)
49 // |                    |
50 // |       unused       |
51 // |                    |
52 // +--------------------+ 0x200200000000 (kUnusedAddr)
53 // |    union table     |
54 // +--------------------+ 0x200000000000 (kUnionTableAddr)
55 // |   shadow memory    |
56 // +--------------------+ 0x000000010000 (kShadowAddr)
57 // | reserved by kernel |
58 // +--------------------+ 0x000000000000
59 //
60 // To derive a shadow memory address from an application memory address,
61 // bits 44-46 are cleared to bring the address into the range
62 // [0x000000008000,0x100000000000).  Then the address is shifted left by 1 to
63 // account for the double byte representation of shadow labels and move the
64 // address into the shadow memory range.  See the function shadow_for below.
65
66 // On Linux/MIPS64, memory is laid out as follows:
67 //
68 // +--------------------+ 0x10000000000 (top of memory)
69 // | application memory |
70 // +--------------------+ 0xF000008000 (kAppAddr)
71 // |                    |
72 // |       unused       |
73 // |                    |
74 // +--------------------+ 0x2200000000 (kUnusedAddr)
75 // |    union table     |
76 // +--------------------+ 0x2000000000 (kUnionTableAddr)
77 // |   shadow memory    |
78 // +--------------------+ 0x0000010000 (kShadowAddr)
79 // | reserved by kernel |
80 // +--------------------+ 0x0000000000
81
82 typedef atomic_dfsan_label dfsan_union_table_t[kNumLabels][kNumLabels];
83
84 #if defined(__x86_64__)
85 static const uptr kShadowAddr = 0x10000;
86 static const uptr kUnionTableAddr = 0x200000000000;
87 static const uptr kUnusedAddr = kUnionTableAddr + sizeof(dfsan_union_table_t);
88 static const uptr kAppAddr = 0x700000008000;
89 #elif defined(__mips64)
90 static const uptr kShadowAddr = 0x10000;
91 static const uptr kUnionTableAddr = 0x2000000000;
92 static const uptr kUnusedAddr = kUnionTableAddr + sizeof(dfsan_union_table_t);
93 static const uptr kAppAddr = 0xF000008000;
94 #else
95 # error "DFSan not supported for this platform!"
96 #endif
97
98 static atomic_dfsan_label *union_table(dfsan_label l1, dfsan_label l2) {
99   return &(*(dfsan_union_table_t *) kUnionTableAddr)[l1][l2];
100 }
101
102 // Checks we do not run out of labels.
103 static void dfsan_check_label(dfsan_label label) {
104   if (label == kInitializingLabel) {
105     Report("FATAL: DataFlowSanitizer: out of labels\n");
106     Die();
107   }
108 }
109
110 // Resolves the union of two unequal labels.  Nonequality is a precondition for
111 // this function (the instrumentation pass inlines the equality test).
112 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
113 dfsan_label __dfsan_union(dfsan_label l1, dfsan_label l2) {
114   DCHECK_NE(l1, l2);
115
116   if (l1 == 0)
117     return l2;
118   if (l2 == 0)
119     return l1;
120
121   if (l1 > l2)
122     Swap(l1, l2);
123
124   atomic_dfsan_label *table_ent = union_table(l1, l2);
125   // We need to deal with the case where two threads concurrently request
126   // a union of the same pair of labels.  If the table entry is uninitialized,
127   // (i.e. 0) use a compare-exchange to set the entry to kInitializingLabel
128   // (i.e. -1) to mark that we are initializing it.
129   dfsan_label label = 0;
130   if (atomic_compare_exchange_strong(table_ent, &label, kInitializingLabel,
131                                      memory_order_acquire)) {
132     // Check whether l2 subsumes l1.  We don't need to check whether l1
133     // subsumes l2 because we are guaranteed here that l1 < l2, and (at least
134     // in the cases we are interested in) a label may only subsume labels
135     // created earlier (i.e. with a lower numerical value).
136     if (__dfsan_label_info[l2].l1 == l1 ||
137         __dfsan_label_info[l2].l2 == l1) {
138       label = l2;
139     } else {
140       label =
141         atomic_fetch_add(&__dfsan_last_label, 1, memory_order_relaxed) + 1;
142       dfsan_check_label(label);
143       __dfsan_label_info[label].l1 = l1;
144       __dfsan_label_info[label].l2 = l2;
145     }
146     atomic_store(table_ent, label, memory_order_release);
147   } else if (label == kInitializingLabel) {
148     // Another thread is initializing the entry.  Wait until it is finished.
149     do {
150       internal_sched_yield();
151       label = atomic_load(table_ent, memory_order_acquire);
152     } while (label == kInitializingLabel);
153   }
154   return label;
155 }
156
157 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
158 dfsan_label __dfsan_union_load(const dfsan_label *ls, uptr n) {
159   dfsan_label label = ls[0];
160   for (uptr i = 1; i != n; ++i) {
161     dfsan_label next_label = ls[i];
162     if (label != next_label)
163       label = __dfsan_union(label, next_label);
164   }
165   return label;
166 }
167
168 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
169 void __dfsan_unimplemented(char *fname) {
170   if (flags().warn_unimplemented)
171     Report("WARNING: DataFlowSanitizer: call to uninstrumented function %s\n",
172            fname);
173 }
174
175 // Use '-mllvm -dfsan-debug-nonzero-labels' and break on this function
176 // to try to figure out where labels are being introduced in a nominally
177 // label-free program.
178 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_nonzero_label() {
179   if (flags().warn_nonzero_labels)
180     Report("WARNING: DataFlowSanitizer: saw nonzero label\n");
181 }
182
183 // Indirect call to an uninstrumented vararg function. We don't have a way of
184 // handling these at the moment.
185 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
186 __dfsan_vararg_wrapper(const char *fname) {
187   Report("FATAL: DataFlowSanitizer: unsupported indirect call to vararg "
188          "function %s\n", fname);
189   Die();
190 }
191
192 // Like __dfsan_union, but for use from the client or custom functions.  Hence
193 // the equality comparison is done here before calling __dfsan_union.
194 SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
195 dfsan_union(dfsan_label l1, dfsan_label l2) {
196   if (l1 == l2)
197     return l1;
198   return __dfsan_union(l1, l2);
199 }
200
201 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
202 dfsan_label dfsan_create_label(const char *desc, void *userdata) {
203   dfsan_label label =
204     atomic_fetch_add(&__dfsan_last_label, 1, memory_order_relaxed) + 1;
205   dfsan_check_label(label);
206   __dfsan_label_info[label].l1 = __dfsan_label_info[label].l2 = 0;
207   __dfsan_label_info[label].desc = desc;
208   __dfsan_label_info[label].userdata = userdata;
209   return label;
210 }
211
212 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
213 void __dfsan_set_label(dfsan_label label, void *addr, uptr size) {
214   for (dfsan_label *labelp = shadow_for(addr); size != 0; --size, ++labelp) {
215     // Don't write the label if it is already the value we need it to be.
216     // In a program where most addresses are not labeled, it is common that
217     // a page of shadow memory is entirely zeroed.  The Linux copy-on-write
218     // implementation will share all of the zeroed pages, making a copy of a
219     // page when any value is written.  The un-sharing will happen even if
220     // the value written does not change the value in memory.  Avoiding the
221     // write when both |label| and |*labelp| are zero dramatically reduces
222     // the amount of real memory used by large programs.
223     if (label == *labelp)
224       continue;
225
226     *labelp = label;
227   }
228 }
229
230 SANITIZER_INTERFACE_ATTRIBUTE
231 void dfsan_set_label(dfsan_label label, void *addr, uptr size) {
232   __dfsan_set_label(label, addr, size);
233 }
234
235 SANITIZER_INTERFACE_ATTRIBUTE
236 void dfsan_add_label(dfsan_label label, void *addr, uptr size) {
237   for (dfsan_label *labelp = shadow_for(addr); size != 0; --size, ++labelp)
238     if (*labelp != label)
239       *labelp = __dfsan_union(*labelp, label);
240 }
241
242 // Unlike the other dfsan interface functions the behavior of this function
243 // depends on the label of one of its arguments.  Hence it is implemented as a
244 // custom function.
245 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
246 __dfsw_dfsan_get_label(long data, dfsan_label data_label,
247                        dfsan_label *ret_label) {
248   *ret_label = 0;
249   return data_label;
250 }
251
252 SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
253 dfsan_read_label(const void *addr, uptr size) {
254   if (size == 0)
255     return 0;
256   return __dfsan_union_load(shadow_for(addr), size);
257 }
258
259 SANITIZER_INTERFACE_ATTRIBUTE
260 const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label) {
261   return &__dfsan_label_info[label];
262 }
263
264 extern "C" SANITIZER_INTERFACE_ATTRIBUTE int
265 dfsan_has_label(dfsan_label label, dfsan_label elem) {
266   if (label == elem)
267     return true;
268   const dfsan_label_info *info = dfsan_get_label_info(label);
269   if (info->l1 != 0) {
270     return dfsan_has_label(info->l1, elem) || dfsan_has_label(info->l2, elem);
271   } else {
272     return false;
273   }
274 }
275
276 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
277 dfsan_has_label_with_desc(dfsan_label label, const char *desc) {
278   const dfsan_label_info *info = dfsan_get_label_info(label);
279   if (info->l1 != 0) {
280     return dfsan_has_label_with_desc(info->l1, desc) ||
281            dfsan_has_label_with_desc(info->l2, desc);
282   } else {
283     return internal_strcmp(desc, info->desc) == 0;
284   }
285 }
286
287 extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr
288 dfsan_get_label_count(void) {
289   dfsan_label max_label_allocated =
290       atomic_load(&__dfsan_last_label, memory_order_relaxed);
291
292   return static_cast<uptr>(max_label_allocated);
293 }
294
295 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
296 dfsan_dump_labels(int fd) {
297   dfsan_label last_label =
298       atomic_load(&__dfsan_last_label, memory_order_relaxed);
299
300   for (uptr l = 1; l <= last_label; ++l) {
301     char buf[64];
302     internal_snprintf(buf, sizeof(buf), "%u %u %u ", l,
303                       __dfsan_label_info[l].l1, __dfsan_label_info[l].l2);
304     internal_write(fd, buf, internal_strlen(buf));
305     if (__dfsan_label_info[l].l1 == 0 && __dfsan_label_info[l].desc) {
306       internal_write(fd, __dfsan_label_info[l].desc,
307                      internal_strlen(__dfsan_label_info[l].desc));
308     }
309     internal_write(fd, "\n", 1);
310   }
311 }
312
313 static void InitializeFlags(Flags &f, const char *env) {
314   f.warn_unimplemented = true;
315   f.warn_nonzero_labels = false;
316   f.strict_data_dependencies = true;
317   f.dump_labels_at_exit = "";
318
319   ParseFlag(env, &f.warn_unimplemented, "warn_unimplemented", "");
320   ParseFlag(env, &f.warn_nonzero_labels, "warn_nonzero_labels", "");
321   ParseFlag(env, &f.strict_data_dependencies, "strict_data_dependencies", "");
322   ParseFlag(env, &f.dump_labels_at_exit, "dump_labels_at_exit", "");
323 }
324
325 static void dfsan_fini() {
326   if (internal_strcmp(flags().dump_labels_at_exit, "") != 0) {
327     fd_t fd = OpenFile(flags().dump_labels_at_exit, true /* write */);
328     if (fd == kInvalidFd) {
329       Report("WARNING: DataFlowSanitizer: unable to open output file %s\n",
330              flags().dump_labels_at_exit);
331       return;
332     }
333
334     Report("INFO: DataFlowSanitizer: dumping labels to %s\n",
335            flags().dump_labels_at_exit);
336     dfsan_dump_labels(fd);
337     internal_close(fd);
338   }
339 }
340
341 #ifdef DFSAN_NOLIBC
342 extern "C" void dfsan_init() {
343 #else
344 static void dfsan_init(int argc, char **argv, char **envp) {
345 #endif
346   MmapFixedNoReserve(kShadowAddr, kUnusedAddr - kShadowAddr);
347
348   // Protect the region of memory we don't use, to preserve the one-to-one
349   // mapping from application to shadow memory. But if ASLR is disabled, Linux
350   // will load our executable in the middle of our unused region. This mostly
351   // works so long as the program doesn't use too much memory. We support this
352   // case by disabling memory protection when ASLR is disabled.
353   uptr init_addr = (uptr)&dfsan_init;
354   if (!(init_addr >= kUnusedAddr && init_addr < kAppAddr))
355     Mprotect(kUnusedAddr, kAppAddr - kUnusedAddr);
356
357   InitializeFlags(flags(), GetEnv("DFSAN_OPTIONS"));
358
359   InitializeInterceptors();
360
361   // Register the fini callback to run when the program terminates successfully
362   // or it is killed by the runtime.
363   Atexit(dfsan_fini);
364   SetDieCallback(dfsan_fini);
365
366   __dfsan_label_info[kInitializingLabel].desc = "<init label>";
367 }
368
369 #if !defined(DFSAN_NOLIBC) && SANITIZER_CAN_USE_PREINIT_ARRAY
370 __attribute__((section(".preinit_array"), used))
371 static void (*dfsan_init_ptr)(int, char **, char **) = dfsan_init;
372 #endif