dfsan.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef DFSAN_H
  14. #define DFSAN_H
  15. #include "sanitizer_common/sanitizer_internal_defs.h"
  16. #include "dfsan_platform.h"
  17. using __sanitizer::u32;
  18. using __sanitizer::u8;
  19. using __sanitizer::uptr;
  20. // Copy declarations from public sanitizer/dfsan_interface.h header here.
  21. typedef u8 dfsan_label;
  22. typedef u32 dfsan_origin;
  23. extern "C" {
  24. void dfsan_add_label(dfsan_label label, void *addr, uptr size);
  25. void dfsan_set_label(dfsan_label label, void *addr, uptr size);
  26. dfsan_label dfsan_read_label(const void *addr, uptr size);
  27. dfsan_label dfsan_union(dfsan_label l1, dfsan_label l2);
  28. // Zero out [offset, offset+size) from __dfsan_arg_tls.
  29. void dfsan_clear_arg_tls(uptr offset, uptr size);
  30. // Zero out the TLS storage.
  31. void dfsan_clear_thread_local_state();
  32. // Return the origin associated with the first taint byte in the size bytes
  33. // from the address addr.
  34. dfsan_origin dfsan_read_origin_of_first_taint(const void *addr, uptr size);
  35. // Set the data within [addr, addr+size) with label and origin.
  36. void dfsan_set_label_origin(dfsan_label label, dfsan_origin origin, void *addr,
  37. uptr size);
  38. // Copy or move the origins of the len bytes from src to dst.
  39. void dfsan_mem_origin_transfer(const void *dst, const void *src, uptr len);
  40. // Copy shadow bytes from src to dst.
  41. // Note this preserves distinct taint labels at specific offsets.
  42. void dfsan_mem_shadow_transfer(void *dst, const void *src, uptr len);
  43. } // extern "C"
  44. template <typename T>
  45. void dfsan_set_label(dfsan_label label, T &data) {
  46. dfsan_set_label(label, (void *)&data, sizeof(T));
  47. }
  48. namespace __dfsan {
  49. extern bool dfsan_inited;
  50. extern bool dfsan_init_is_running;
  51. void initialize_interceptors();
  52. inline dfsan_label *shadow_for(void *ptr) {
  53. return (dfsan_label *)MEM_TO_SHADOW(ptr);
  54. }
  55. inline const dfsan_label *shadow_for(const void *ptr) {
  56. return shadow_for(const_cast<void *>(ptr));
  57. }
  58. inline uptr unaligned_origin_for(uptr ptr) { return MEM_TO_ORIGIN(ptr); }
  59. inline dfsan_origin *origin_for(void *ptr) {
  60. auto aligned_addr = unaligned_origin_for(reinterpret_cast<uptr>(ptr)) &
  61. ~(sizeof(dfsan_origin) - 1);
  62. return reinterpret_cast<dfsan_origin *>(aligned_addr);
  63. }
  64. inline const dfsan_origin *origin_for(const void *ptr) {
  65. return origin_for(const_cast<void *>(ptr));
  66. }
  67. void dfsan_copy_memory(void *dst, const void *src, uptr size);
  68. void dfsan_allocator_init();
  69. void dfsan_deallocate(void *ptr);
  70. void *dfsan_malloc(uptr size);
  71. void *dfsan_calloc(uptr nmemb, uptr size);
  72. void *dfsan_realloc(void *ptr, uptr size);
  73. void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size);
  74. void *dfsan_valloc(uptr size);
  75. void *dfsan_pvalloc(uptr size);
  76. void *dfsan_aligned_alloc(uptr alignment, uptr size);
  77. void *dfsan_memalign(uptr alignment, uptr size);
  78. int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size);
  79. void dfsan_init();
  80. } // namespace __dfsan
  81. #endif // DFSAN_H