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