dfsan_interface.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===-- dfsan_interface.h -------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file is a part of DataFlowSanitizer.
  11. //
  12. // Public interface header.
  13. //===----------------------------------------------------------------------===//
  14. #ifndef DFSAN_INTERFACE_H
  15. #define DFSAN_INTERFACE_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <sanitizer/common_interface_defs.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. typedef uint16_t dfsan_label;
  23. /// Stores information associated with a specific label identifier. A label
  24. /// may be a base label created using dfsan_create_label, with associated
  25. /// text description and user data, or an automatically created union label,
  26. /// which represents the union of two label identifiers (which may themselves
  27. /// be base or union labels).
  28. struct dfsan_label_info {
  29. // Fields for union labels, set to 0 for base labels.
  30. dfsan_label l1;
  31. dfsan_label l2;
  32. // Fields for base labels.
  33. const char *desc;
  34. void *userdata;
  35. };
  36. /// Signature of the callback argument to dfsan_set_write_callback().
  37. typedef void (*dfsan_write_callback_t)(int fd, const void *buf, size_t count);
  38. /// Computes the union of \c l1 and \c l2, possibly creating a union label in
  39. /// the process.
  40. dfsan_label dfsan_union(dfsan_label l1, dfsan_label l2);
  41. /// Creates and returns a base label with the given description and user data.
  42. dfsan_label dfsan_create_label(const char *desc, void *userdata);
  43. /// Sets the label for each address in [addr,addr+size) to \c label.
  44. void dfsan_set_label(dfsan_label label, void *addr, size_t size);
  45. /// Sets the label for each address in [addr,addr+size) to the union of the
  46. /// current label for that address and \c label.
  47. void dfsan_add_label(dfsan_label label, void *addr, size_t size);
  48. /// Retrieves the label associated with the given data.
  49. ///
  50. /// The type of 'data' is arbitrary. The function accepts a value of any type,
  51. /// which can be truncated or extended (implicitly or explicitly) as necessary.
  52. /// The truncation/extension operations will preserve the label of the original
  53. /// value.
  54. dfsan_label dfsan_get_label(long data);
  55. /// Retrieves the label associated with the data at the given address.
  56. dfsan_label dfsan_read_label(const void *addr, size_t size);
  57. /// Retrieves a pointer to the dfsan_label_info struct for the given label.
  58. const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label);
  59. /// Returns whether the given label label contains the label elem.
  60. int dfsan_has_label(dfsan_label label, dfsan_label elem);
  61. /// If the given label label contains a label with the description desc, returns
  62. /// that label, else returns 0.
  63. dfsan_label dfsan_has_label_with_desc(dfsan_label label, const char *desc);
  64. /// Returns the number of labels allocated.
  65. size_t dfsan_get_label_count(void);
  66. /// Sets a callback to be invoked on calls to write(). The callback is invoked
  67. /// before the write is done. The write is not guaranteed to succeed when the
  68. /// callback executes. Pass in NULL to remove any callback.
  69. void dfsan_set_write_callback(dfsan_write_callback_t labeled_write_callback);
  70. /// Writes the labels currently used by the program to the given file
  71. /// descriptor. The lines of the output have the following format:
  72. ///
  73. /// <label> <parent label 1> <parent label 2> <label description if any>
  74. void dfsan_dump_labels(int fd);
  75. /// Interceptor hooks.
  76. /// Whenever a dfsan's custom function is called the corresponding
  77. /// hook is called it non-zero. The hooks should be defined by the user.
  78. /// The primary use case is taint-guided fuzzing, where the fuzzer
  79. /// needs to see the parameters of the function and the labels.
  80. /// FIXME: implement more hooks.
  81. void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
  82. size_t n, dfsan_label s1_label,
  83. dfsan_label s2_label, dfsan_label n_label);
  84. void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
  85. size_t n, dfsan_label s1_label,
  86. dfsan_label s2_label, dfsan_label n_label);
  87. #ifdef __cplusplus
  88. } // extern "C"
  89. template <typename T>
  90. void dfsan_set_label(dfsan_label label, T &data) { // NOLINT
  91. dfsan_set_label(label, (void *)&data, sizeof(T));
  92. }
  93. #endif
  94. #endif // DFSAN_INTERFACE_H