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