xref: /freebsd/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan.h (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 //===-- dfsan.h -------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of DataFlowSanitizer.
10 //
11 // Private DFSan header.
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef DFSAN_H
15 #define DFSAN_H
16 
17 #include "sanitizer_common/sanitizer_internal_defs.h"
18 
19 #include "dfsan_platform.h"
20 
21 using __sanitizer::u32;
22 using __sanitizer::u8;
23 using __sanitizer::uptr;
24 
25 // Copy declarations from public sanitizer/dfsan_interface.h header here.
26 typedef u8 dfsan_label;
27 typedef u32 dfsan_origin;
28 
29 extern "C" {
30 void dfsan_add_label(dfsan_label label, void *addr, uptr size);
31 void dfsan_set_label(dfsan_label label, void *addr, uptr size);
32 dfsan_label dfsan_read_label(const void *addr, uptr size);
33 dfsan_label dfsan_union(dfsan_label l1, dfsan_label l2);
34 // Zero out [offset, offset+size) from __dfsan_arg_tls.
35 void dfsan_clear_arg_tls(uptr offset, uptr size);
36 // Zero out the TLS storage.
37 void dfsan_clear_thread_local_state();
38 
39 // Set DFSan label and origin TLS of argument for a call.
40 // Note that offset may not correspond with argument number.
41 // Some arguments (aggregate/array) will use several offsets.
42 void dfsan_set_arg_tls(uptr offset, dfsan_label label);
43 void dfsan_set_arg_origin_tls(uptr offset, dfsan_origin o);
44 
45 // Return the origin associated with the first taint byte in the size bytes
46 // from the address addr.
47 dfsan_origin dfsan_read_origin_of_first_taint(const void *addr, uptr size);
48 
49 // Set the data within [addr, addr+size) with label and origin.
50 void dfsan_set_label_origin(dfsan_label label, dfsan_origin origin, void *addr,
51                             uptr size);
52 
53 // Copy or move the origins of the len bytes from src to dst.
54 void dfsan_mem_origin_transfer(const void *dst, const void *src, uptr len);
55 
56 // Copy shadow bytes from src to dst.
57 // Note this preserves distinct taint labels at specific offsets.
58 void dfsan_mem_shadow_transfer(void *dst, const void *src, uptr len);
59 }  // extern "C"
60 
61 template <typename T>
62 void dfsan_set_label(dfsan_label label, T &data) {
63   dfsan_set_label(label, (void *)&data, sizeof(T));
64 }
65 
66 namespace __dfsan {
67 
68 extern bool dfsan_inited;
69 extern bool dfsan_init_is_running;
70 
71 void initialize_interceptors();
72 
73 inline dfsan_label *shadow_for(void *ptr) {
74   return (dfsan_label *)MEM_TO_SHADOW(ptr);
75 }
76 
77 inline const dfsan_label *shadow_for(const void *ptr) {
78   return shadow_for(const_cast<void *>(ptr));
79 }
80 
81 inline uptr unaligned_origin_for(uptr ptr) { return MEM_TO_ORIGIN(ptr); }
82 
83 inline dfsan_origin *origin_for(void *ptr) {
84   auto aligned_addr = unaligned_origin_for(reinterpret_cast<uptr>(ptr)) &
85                       ~(sizeof(dfsan_origin) - 1);
86   return reinterpret_cast<dfsan_origin *>(aligned_addr);
87 }
88 
89 inline const dfsan_origin *origin_for(const void *ptr) {
90   return origin_for(const_cast<void *>(ptr));
91 }
92 
93 void dfsan_copy_memory(void *dst, const void *src, uptr size);
94 
95 void dfsan_allocator_init();
96 void dfsan_deallocate(void *ptr);
97 
98 void *dfsan_malloc(uptr size);
99 void *dfsan_calloc(uptr nmemb, uptr size);
100 void *dfsan_realloc(void *ptr, uptr size);
101 void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size);
102 void *dfsan_valloc(uptr size);
103 void *dfsan_pvalloc(uptr size);
104 void *dfsan_aligned_alloc(uptr alignment, uptr size);
105 void *dfsan_memalign(uptr alignment, uptr size);
106 int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size);
107 
108 void dfsan_init();
109 
110 }  // namespace __dfsan
111 
112 #endif  // DFSAN_H
113