xref: /freebsd/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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 // Return the origin associated with the first taint byte in the size bytes
40 // from the address addr.
41 dfsan_origin dfsan_read_origin_of_first_taint(const void *addr, uptr size);
42 
43 // Set the data within [addr, addr+size) with label and origin.
44 void dfsan_set_label_origin(dfsan_label label, dfsan_origin origin, void *addr,
45                             uptr size);
46 
47 // Copy or move the origins of the len bytes from src to dst.
48 void dfsan_mem_origin_transfer(const void *dst, const void *src, uptr len);
49 }  // extern "C"
50 
51 template <typename T>
52 void dfsan_set_label(dfsan_label label, T &data) {  // NOLINT
53   dfsan_set_label(label, (void *)&data, sizeof(T));
54 }
55 
56 namespace __dfsan {
57 
58 extern bool dfsan_inited;
59 extern bool dfsan_init_is_running;
60 
61 void initialize_interceptors();
62 
63 inline dfsan_label *shadow_for(void *ptr) {
64   return (dfsan_label *)MEM_TO_SHADOW(ptr);
65 }
66 
67 inline const dfsan_label *shadow_for(const void *ptr) {
68   return shadow_for(const_cast<void *>(ptr));
69 }
70 
71 inline uptr unaligned_origin_for(uptr ptr) { return MEM_TO_ORIGIN(ptr); }
72 
73 inline dfsan_origin *origin_for(void *ptr) {
74   auto aligned_addr = unaligned_origin_for(reinterpret_cast<uptr>(ptr)) &
75                       ~(sizeof(dfsan_origin) - 1);
76   return reinterpret_cast<dfsan_origin *>(aligned_addr);
77 }
78 
79 inline const dfsan_origin *origin_for(const void *ptr) {
80   return origin_for(const_cast<void *>(ptr));
81 }
82 
83 void dfsan_copy_memory(void *dst, const void *src, uptr size);
84 
85 void dfsan_allocator_init();
86 void dfsan_deallocate(void *ptr);
87 
88 void *dfsan_malloc(uptr size);
89 void *dfsan_calloc(uptr nmemb, uptr size);
90 void *dfsan_realloc(void *ptr, uptr size);
91 void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size);
92 void *dfsan_valloc(uptr size);
93 void *dfsan_pvalloc(uptr size);
94 void *dfsan_aligned_alloc(uptr alignment, uptr size);
95 void *dfsan_memalign(uptr alignment, uptr size);
96 int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size);
97 
98 void dfsan_init();
99 
100 }  // namespace __dfsan
101 
102 #endif  // DFSAN_H
103