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 50 // Copy shadow bytes from src to dst. 51 // Note this preserves distinct taint labels at specific offsets. 52 void dfsan_mem_shadow_transfer(void *dst, const void *src, uptr len); 53 } // extern "C" 54 55 template <typename T> 56 void dfsan_set_label(dfsan_label label, T &data) { 57 dfsan_set_label(label, (void *)&data, sizeof(T)); 58 } 59 60 namespace __dfsan { 61 62 extern bool dfsan_inited; 63 extern bool dfsan_init_is_running; 64 65 void initialize_interceptors(); 66 67 inline dfsan_label *shadow_for(void *ptr) { 68 return (dfsan_label *)MEM_TO_SHADOW(ptr); 69 } 70 71 inline const dfsan_label *shadow_for(const void *ptr) { 72 return shadow_for(const_cast<void *>(ptr)); 73 } 74 75 inline uptr unaligned_origin_for(uptr ptr) { return MEM_TO_ORIGIN(ptr); } 76 77 inline dfsan_origin *origin_for(void *ptr) { 78 auto aligned_addr = unaligned_origin_for(reinterpret_cast<uptr>(ptr)) & 79 ~(sizeof(dfsan_origin) - 1); 80 return reinterpret_cast<dfsan_origin *>(aligned_addr); 81 } 82 83 inline const dfsan_origin *origin_for(const void *ptr) { 84 return origin_for(const_cast<void *>(ptr)); 85 } 86 87 void dfsan_copy_memory(void *dst, const void *src, uptr size); 88 89 void dfsan_allocator_init(); 90 void dfsan_deallocate(void *ptr); 91 92 void *dfsan_malloc(uptr size); 93 void *dfsan_calloc(uptr nmemb, uptr size); 94 void *dfsan_realloc(void *ptr, uptr size); 95 void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size); 96 void *dfsan_valloc(uptr size); 97 void *dfsan_pvalloc(uptr size); 98 void *dfsan_aligned_alloc(uptr alignment, uptr size); 99 void *dfsan_memalign(uptr alignment, uptr size); 100 int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size); 101 102 void dfsan_init(); 103 104 } // namespace __dfsan 105 106 #endif // DFSAN_H 107