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
21using __sanitizer::u32;
22using __sanitizer::u8;
23using __sanitizer::uptr;
24
25// Copy declarations from public sanitizer/dfsan_interface.h header here.
26typedef u8 dfsan_label;
27typedef u32 dfsan_origin;
28
29extern "C" {
30void dfsan_add_label(dfsan_label label, void *addr, uptr size);
31void dfsan_set_label(dfsan_label label, void *addr, uptr size);
32dfsan_label dfsan_read_label(const void *addr, uptr size);
33dfsan_label dfsan_union(dfsan_label l1, dfsan_label l2);
34// Zero out [offset, offset+size) from __dfsan_arg_tls.
35void dfsan_clear_arg_tls(uptr offset, uptr size);
36// Zero out the TLS storage.
37void 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.
42void dfsan_set_arg_tls(uptr offset, dfsan_label label);
43void 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.
47dfsan_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.
50void 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.
54void 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.
58void dfsan_mem_shadow_transfer(void *dst, const void *src, uptr len);
59} // extern "C"
60
61template <typename T>
62void dfsan_set_label(dfsan_label label, T &data) {
63 dfsan_set_label(label, addr: (void *)&data, size: sizeof(T));
64}
65
66namespace __dfsan {
67
68extern bool dfsan_inited;
69extern bool dfsan_init_is_running;
70
71void initialize_interceptors();
72
73inline dfsan_label *shadow_for(void *ptr) {
74 return (dfsan_label *)MEM_TO_SHADOW(ptr);
75}
76
77inline const dfsan_label *shadow_for(const void *ptr) {
78 return shadow_for(ptr: const_cast<void *>(ptr));
79}
80
81inline uptr unaligned_origin_for(uptr ptr) { return MEM_TO_ORIGIN(ptr); }
82
83inline dfsan_origin *origin_for(void *ptr) {
84 auto aligned_addr = unaligned_origin_for(ptr: reinterpret_cast<uptr>(ptr)) &
85 ~(sizeof(dfsan_origin) - 1);
86 return reinterpret_cast<dfsan_origin *>(aligned_addr);
87}
88
89inline const dfsan_origin *origin_for(const void *ptr) {
90 return origin_for(ptr: const_cast<void *>(ptr));
91}
92
93void dfsan_copy_memory(void *dst, const void *src, uptr size);
94
95void dfsan_allocator_init();
96void dfsan_deallocate(void *ptr);
97
98void *dfsan_malloc(uptr size);
99void *dfsan_calloc(uptr nmemb, uptr size);
100void *dfsan_realloc(void *ptr, uptr size);
101void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size);
102void *dfsan_valloc(uptr size);
103void *dfsan_pvalloc(uptr size);
104void *dfsan_aligned_alloc(uptr alignment, uptr size);
105void *dfsan_memalign(uptr alignment, uptr size);
106int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size);
107
108void dfsan_init();
109
110} // namespace __dfsan
111
112#endif // DFSAN_H
113