aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/sanitizer.h
blob: 658d82812305bf8250a177fa52d102a30fb5bb1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef _included_clib_sanitizer_h
#define _included_clib_sanitizer_h

#ifdef CLIB_SANITIZE_ADDR

#include <sanitizer/asan_interface.h>
#include <vppinfra/clib.h>
#include <vppinfra/error_bootstrap.h>

typedef struct
{
  size_t shadow_scale;
  size_t shadow_offset;
} clib_sanitizer_main_t;

extern clib_sanitizer_main_t sanitizer_main;

#define CLIB_NOSANITIZE_ADDR    __attribute__((no_sanitize_address))
#define CLIB_MEM_POISON(a, s)   ASAN_POISON_MEMORY_REGION((a), (s))
#define CLIB_MEM_UNPOISON(a, s) ASAN_UNPOISON_MEMORY_REGION((a), (s))

#define CLIB_MEM_OVERFLOW_MAX 64

static_always_inline void
sanitizer_unpoison__ (u64 *restrict *shadow_ptr, size_t *shadow_len,
		      const void *ptr, size_t len)
{
  size_t scale, off;

  if (PREDICT_FALSE (~0 == sanitizer_main.shadow_scale))
    __asan_get_shadow_mapping (&sanitizer_main.shadow_scale,
			       &sanitizer_main.shadow_offset);

  scale = sanitizer_main.shadow_scale;
  off = sanitizer_main.shadow_offset;

  /* compute the shadow address and length */
  *shadow_len = len >> scale;
  ASSERT (*shadow_len <= CLIB_MEM_OVERFLOW_MAX);
  *shadow_ptr = (void *) (((clib_address_t) ptr >> scale) + off);
}

static_always_inline CLIB_NOSANITIZE_ADDR void
sanitizer_unpoison_push__ (u64 *restrict shadow, const void *ptr, size_t len)
{
  u64 *restrict shadow_ptr;
  size_t shadow_len;
  int i;

  sanitizer_unpoison__ (&shadow_ptr, &shadow_len, ptr, len);

  /* save the shadow area */
  for (i = 0; i < shadow_len; i++)
    shadow[i] = shadow_ptr[i];

  /* unpoison */
  for (i = 0; i < shadow_len; i++)
    shadow_ptr[i] = 0;
}

static_always_inline CLIB_NOSANITIZE_ADDR void
sanitizer_unpoison_pop__ (const u64 *restrict shadow, const void *ptr,
			  size_t len)
{
  u64 *restrict shadow_ptr;
  size_t shadow_len;
  int i;

  sanitizer_unpoison__ (&shadow_ptr, &shadow_len, ptr, len);

  /* restore the shadow area */
  for (i = 0; i < shadow_len; i++)
    {
      ASSERT (0 == shadow_ptr[i]);
      shadow_ptr[i] = shadow[i];
    }
}

#define CLIB_MEM_OVERFLOW(f, src, n)                                          \
  ({                                                                          \
    typeof (f) clib_mem_overflow_ret__;                                       \
    const void *clib_mem_overflow_src__ = (src);                              \
    size_t clib_mem_overflow_n__ = (n);                                       \
    u64 clib_mem_overflow_shadow__[CLIB_MEM_OVERFLOW_MAX];                    \
    sanitizer_unpoison_push__ (clib_mem_overflow_shadow__,                    \
			       clib_mem_overflow_src__,                       \
			       clib_mem_overflow_n__);                        \
    clib_mem_overflow_ret__ = f;                                              \
    sanitizer_unpoison_pop__ (clib_mem_overflow_shadow__,                     \
			      clib_mem_overflow_src__,                        \
			      clib_mem_overflow_n__);                         \
    clib_mem_overflow_ret__;                                                  \
  })

#define CLIB_MEM_OVERFLOW_LOAD(f, src) \
  ({ \
   typeof(src) clib_mem_overflow_load_src__ = (src); \
   CLIB_MEM_OVERFLOW(f(clib_mem_overflow_load_src__), clib_mem_overflow_load_src__, sizeof(typeof(f(clib_mem_overflow_load_src__)))); \
   })

static_always_inline void
CLIB_MEM_POISON_LEN (void *src, size_t oldlen, size_t newlen)
{
  if (oldlen > newlen)
    CLIB_MEM_POISON (src + newlen, oldlen - newlen);
  else if (newlen > oldlen)
    CLIB_MEM_UNPOISON (src + oldlen, newlen - oldlen);
}

#else /* CLIB_SANITIZE_ADDR */

#define CLIB_NOSANITIZE_ADDR
#define CLIB_MEM_POISON(a, s)                   (void)(a)
#define CLIB_MEM_UNPOISON(a, s)                 (void)(a)
#define CLIB_MEM_OVERFLOW(a, b, c)              a
#define CLIB_MEM_OVERFLOW_LOAD(f, src)          f(src)
#define CLIB_MEM_POISON_LEN(a, b, c)

#endif /* CLIB_SANITIZE_ADDR */

/*
 * clang tends to force alignment of all sections when compiling for address
 * sanitizer. This confuse VPP plugin infra, prevent clang to do that
 * On the contrary, GCC does not support this kind of attribute on sections
 * sigh.
 */
#ifdef __clang__
#define CLIB_NOSANITIZE_PLUGIN_REG_SECTION      CLIB_NOSANITIZE_ADDR
#else
#define CLIB_NOSANITIZE_PLUGIN_REG_SECTION
#endif

#endif /* _included_clib_sanitizer_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */