aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/sanitizer.h
blob: db4daea978d8929cdd7615cc2612cd5818a636e9 (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
#ifndef _included_clib_sanitizer_h
#define _included_clib_sanitizer_h

#ifdef CLIB_SANITIZE_ADDR

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

#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(f, src, n) \
  ({ \
   typeof (f) clib_mem_overflow_ret__; \
   const void *clib_mem_overflow_src__ = (src); \
   size_t clib_mem_overflow_n__ = (n); \
   const void *clib_mem_overflow_start__ = __asan_region_is_poisoned((void *)clib_mem_overflow_src__, clib_mem_overflow_n__); \
   clib_mem_overflow_n__ -= (size_t)(clib_mem_overflow_start__ - clib_mem_overflow_src__); \
   if (clib_mem_overflow_start__) \
     CLIB_MEM_UNPOISON(clib_mem_overflow_start__, clib_mem_overflow_n__); \
   clib_mem_overflow_ret__ = f; \
   if (clib_mem_overflow_start__) \
     CLIB_MEM_POISON(clib_mem_overflow_start__, 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:
 */
class="n">nh = ip_address_initializer; u32 sw_if_index, nh_table_id; clib_error_t *error = NULL; int rv; sw_if_index = ~0; nh_table_id = 0; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnet_get_main (), &sw_if_index)) ; else if (unformat (line_input, "peer %U", unformat_ip_address, &peer)) ; else if (unformat (line_input, "nh-table-id %d", &nh_table_id)) ; else if (unformat (line_input, "nh %U", unformat_ip_address, &nh)) ; else { error = clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); goto done; } } if (~0 == sw_if_index) { error = clib_error_return (0, "interface required'", format_unformat_error, line_input); goto done; } if (ip_address_is_zero (&peer)) { error = clib_error_return (0, "peer required'", format_unformat_error, line_input); goto done; } if (ip_address_is_zero (&nh)) { error = clib_error_return (0, "next-hop required'", format_unformat_error, line_input); goto done; } rv = teib_entry_add (sw_if_index, &peer, nh_table_id, &nh); if (rv) { error = clib_error_return_code (NULL, rv, 0, "TEIB error", format_unformat_error, line_input); } done: unformat_free (line_input); return error; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (teib_create_command, static) = { .path = "create teib", .short_help = "create teib <interface> peer <addr> nh <addr> [nh-table-id <ID>]", .function = teib_add, }; /* *INDENT-ON* */ static clib_error_t * teib_del (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; ip_address_t peer = ip_address_initializer; clib_error_t *error = NULL; u32 sw_if_index; int rv; sw_if_index = ~0; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnet_get_main (), &sw_if_index)) ; else if (unformat (line_input, "peer %U", unformat_ip_address, &peer)) ; else { error = clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); goto done; } } if (~0 == sw_if_index) { error = clib_error_return (0, "interface required'", format_unformat_error, line_input); } if (ip_address_is_zero (&peer)) { error = clib_error_return (0, "peer required'", format_unformat_error, line_input); goto done; } rv = teib_entry_del (sw_if_index, &peer); if (rv) { error = clib_error_return_code (NULL, rv, 0, "TEIB error", format_unformat_error, line_input); } done: unformat_free (line_input); return error; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (teib_delete_command, static) = { .path = "delete teib", .short_help = "delete teib <interface> peer <addr>", .function = teib_del, }; /* *INDENT-ON* */ static walk_rc_t teib_show_one (index_t nei, void *ctx) { vlib_cli_output (ctx, "%U", format_teib_entry, nei); return (WALK_CONTINUE); } static clib_error_t * teib_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { teib_walk (teib_show_one, vm); return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (teib_show_command, static) = { .path = "show teib", .short_help = "show teib", .function = teib_show, }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */