dfsan.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Set DFSan label and origin TLS of argument for a call.
  33. // Note that offset may not correspond with argument number.
  34. // Some arguments (aggregate/array) will use several offsets.
  35. void dfsan_set_arg_tls(uptr offset, dfsan_label label);
  36. void dfsan_set_arg_origin_tls(uptr offset, dfsan_origin o);
  37. // Return the origin associated with the first taint byte in the size bytes
  38. // from the address addr.
  39. dfsan_origin dfsan_read_origin_of_first_taint(const void *addr, uptr size);
  40. // Set the data within [addr, addr+size) with label and origin.
  41. void dfsan_set_label_origin(dfsan_label label, dfsan_origin origin, void *addr,
  42. uptr size);
  43. // Copy or move the origins of the len bytes from src to dst.
  44. void dfsan_mem_origin_transfer(const void *dst, const void *src, uptr len);
  45. // Copy shadow bytes from src to dst.
  46. // Note this preserves distinct taint labels at specific offsets.
  47. void dfsan_mem_shadow_transfer(void *dst, const void *src, uptr len);
  48. } // extern "C"
  49. template <typename T>
  50. void dfsan_set_label(dfsan_label label, T &data) {
  51. dfsan_set_label(label, (void *)&data, sizeof(T));
  52. }
  53. namespace __dfsan {
  54. extern bool dfsan_inited;
  55. extern bool dfsan_init_is_running;
  56. void initialize_interceptors();
  57. inline dfsan_label *shadow_for(void *ptr) {
  58. return (dfsan_label *)MEM_TO_SHADOW(ptr);
  59. }
  60. inline const dfsan_label *shadow_for(const void *ptr) {
  61. return shadow_for(const_cast<void *>(ptr));
  62. }
  63. inline uptr unaligned_origin_for(uptr ptr) { return MEM_TO_ORIGIN(ptr); }
  64. inline dfsan_origin *origin_for(void *ptr) {
  65. auto aligned_addr = unaligned_origin_for(reinterpret_cast<uptr>(ptr)) &
  66. ~(sizeof(dfsan_origin) - 1);
  67. return reinterpret_cast<dfsan_origin *>(aligned_addr);
  68. }
  69. inline const dfsan_origin *origin_for(const void *ptr) {
  70. return origin_for(const_cast<void *>(ptr));
  71. }
  72. void dfsan_copy_memory(void *dst, const void *src, uptr size);
  73. void dfsan_allocator_init();
  74. void dfsan_deallocate(void *ptr);
  75. void *dfsan_malloc(uptr size);
  76. void *dfsan_calloc(uptr nmemb, uptr size);
  77. void *dfsan_realloc(void *ptr, uptr size);
  78. void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size);
  79. void *dfsan_valloc(uptr size);
  80. void *dfsan_pvalloc(uptr size);
  81. void *dfsan_aligned_alloc(uptr alignment, uptr size);
  82. void *dfsan_memalign(uptr alignment, uptr size);
  83. int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size);
  84. void dfsan_init();
  85. } // namespace __dfsan
  86. #endif // DFSAN_H