Peter Collingbourne | eee71ae | 2013-08-07 22:47:26 +0000 | [diff] [blame] | 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 | |
| 17 | #include <stddef.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sanitizer/common_interface_defs.h> |
| 20 | |
| 21 | #ifdef __cplusplus |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
| 25 | typedef uint16_t dfsan_label; |
| 26 | |
| 27 | /// Stores information associated with a specific label identifier. A label |
| 28 | /// may be a base label created using dfsan_create_label, with associated |
| 29 | /// text description and user data, or an automatically created union label, |
| 30 | /// which represents the union of two label identifiers (which may themselves |
| 31 | /// be base or union labels). |
| 32 | struct dfsan_label_info { |
| 33 | // Fields for union labels, set to 0 for base labels. |
| 34 | dfsan_label l1; |
| 35 | dfsan_label l2; |
| 36 | |
| 37 | // Fields for base labels. |
| 38 | const char *desc; |
| 39 | void *userdata; |
| 40 | }; |
| 41 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 42 | /// Signature of the callback argument to dfsan_set_write_callback(). |
| 43 | typedef void (*dfsan_write_callback_t)(int fd, const void *buf, size_t count); |
| 44 | |
Peter Collingbourne | 249cdca | 2013-08-12 23:47:37 +0000 | [diff] [blame] | 45 | /// Computes the union of \c l1 and \c l2, possibly creating a union label in |
| 46 | /// the process. |
| 47 | dfsan_label dfsan_union(dfsan_label l1, dfsan_label l2); |
| 48 | |
Peter Collingbourne | eee71ae | 2013-08-07 22:47:26 +0000 | [diff] [blame] | 49 | /// Creates and returns a base label with the given description and user data. |
| 50 | dfsan_label dfsan_create_label(const char *desc, void *userdata); |
| 51 | |
| 52 | /// Sets the label for each address in [addr,addr+size) to \c label. |
| 53 | void dfsan_set_label(dfsan_label label, void *addr, size_t size); |
| 54 | |
| 55 | /// Sets the label for each address in [addr,addr+size) to the union of the |
| 56 | /// current label for that address and \c label. |
| 57 | void dfsan_add_label(dfsan_label label, void *addr, size_t size); |
| 58 | |
| 59 | /// Retrieves the label associated with the given data. |
| 60 | /// |
| 61 | /// The type of 'data' is arbitrary. The function accepts a value of any type, |
| 62 | /// which can be truncated or extended (implicitly or explicitly) as necessary. |
| 63 | /// The truncation/extension operations will preserve the label of the original |
| 64 | /// value. |
| 65 | dfsan_label dfsan_get_label(long data); |
| 66 | |
Peter Collingbourne | 1528c6c | 2013-08-13 22:15:40 +0000 | [diff] [blame] | 67 | /// Retrieves the label associated with the data at the given address. |
| 68 | dfsan_label dfsan_read_label(const void *addr, size_t size); |
| 69 | |
Peter Collingbourne | eee71ae | 2013-08-07 22:47:26 +0000 | [diff] [blame] | 70 | /// Retrieves a pointer to the dfsan_label_info struct for the given label. |
| 71 | const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label); |
| 72 | |
| 73 | /// Returns whether the given label label contains the label elem. |
| 74 | int dfsan_has_label(dfsan_label label, dfsan_label elem); |
| 75 | |
| 76 | /// If the given label label contains a label with the description desc, returns |
| 77 | /// that label, else returns 0. |
| 78 | dfsan_label dfsan_has_label_with_desc(dfsan_label label, const char *desc); |
| 79 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 80 | /// Returns the number of labels allocated. |
| 81 | size_t dfsan_get_label_count(void); |
| 82 | |
| 83 | /// Sets a callback to be invoked on calls to write(). The callback is invoked |
| 84 | /// before the write is done. The write is not guaranteed to succeed when the |
| 85 | /// callback executes. Pass in NULL to remove any callback. |
| 86 | void dfsan_set_write_callback(dfsan_write_callback_t labeled_write_callback); |
| 87 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 88 | /// Writes the labels currently used by the program to the given file |
| 89 | /// descriptor. The lines of the output have the following format: |
| 90 | /// |
| 91 | /// <label> <parent label 1> <parent label 2> <label description if any> |
| 92 | void dfsan_dump_labels(int fd); |
| 93 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 94 | /// Interceptor hooks. |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 95 | /// Whenever a dfsan's custom function is called the corresponding |
| 96 | /// hook is called it non-zero. The hooks should be defined by the user. |
| 97 | /// The primary use case is taint-guided fuzzing, where the fuzzer |
| 98 | /// needs to see the parameters of the function and the labels. |
| 99 | /// FIXME: implement more hooks. |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 100 | void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2, |
| 101 | size_t n, dfsan_label s1_label, |
| 102 | dfsan_label s2_label, dfsan_label n_label); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 103 | void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2, |
| 104 | size_t n, dfsan_label s1_label, |
| 105 | dfsan_label s2_label, dfsan_label n_label); |
Peter Collingbourne | eee71ae | 2013-08-07 22:47:26 +0000 | [diff] [blame] | 106 | #ifdef __cplusplus |
| 107 | } // extern "C" |
| 108 | |
| 109 | template <typename T> |
Dmitry Vyukov | 734647c | 2013-08-13 16:52:07 +0000 | [diff] [blame] | 110 | void dfsan_set_label(dfsan_label label, T &data) { // NOLINT |
Peter Collingbourne | eee71ae | 2013-08-07 22:47:26 +0000 | [diff] [blame] | 111 | dfsan_set_label(label, (void *)&data, sizeof(T)); |
| 112 | } |
| 113 | |
| 114 | #endif |
| 115 | |
| 116 | #endif // DFSAN_INTERFACE_H |