papi: Use CMSG_SPACE for sizing ancillary buffer space
[vpp.git] / src / vppinfra / test_vec.c
index ee17ef1..9f336a0 100644 (file)
@@ -124,7 +124,7 @@ uword g_call_stats[OP_MAX];
 
 
 /* A structure for both vector headers and vector elements might be useful to
-   uncover potential alignement issues. */
+   uncover potential alignment issues. */
 
 typedef struct
 {
@@ -207,33 +207,35 @@ dump_call_stats (uword * stats)
    more sensible value later. */
 #define MAX_VEC_LEN 10
 
-#define create_random_vec_wh(elt_type, len, hdr_bytes, seed)                   \
-({                                                                             \
-  elt_type * _v(v) = NULL;                                                     \
-  uword _v(l) = (len);                                                         \
-  uword _v(h) = (hdr_bytes);                                                   \
-  u8 * _v(hdr);                                                                        \
-                                                                               \
-  if (_v(l) == 0)                                                              \
-    goto __done__;                                                             \
-                                                                               \
-  /* ~0 means select random length between 0 and MAX_VEC_LEN. */               \
-  if (_v(l) == ~0)                                                             \
-    _v(l) = bounded_random_u32 (&(seed), 0, MAX_VEC_LEN);                      \
-                                                                               \
-  _v(v) = _vec_resize (NULL, _v(l), _v(l) * sizeof (elt_type), _v(h), 0);      \
-  fill_with_random_data (_v(v), vec_bytes (_v(v)), (seed));                    \
-                                                                               \
-  /* Fill header with random data as well. */                                  \
-  if (_v(h) > 0)                                                               \
-    {                                                                          \
-      _v(hdr) = vec_header (_v(v), _v(h));                                     \
-      fill_with_random_data (_v(hdr), _v(h), (seed));                          \
-    }                                                                          \
-                                                                               \
-__done__:                                                                      \
-  _v(v);                                                                       \
-})
+#define create_random_vec_wh(elt_type, len, hdr_bytes, seed)                  \
+  ({                                                                          \
+    elt_type *_v (v) = NULL;                                                  \
+    uword _v (l) = (len);                                                     \
+    vec_attr_t _v (attr) = { .hdr_sz = (hdr_bytes),                           \
+                            .elt_sz = sizeof (elt_type) };                   \
+    uword _v (h) = (hdr_bytes);                                               \
+    u8 *_v (hdr);                                                             \
+                                                                              \
+    if (_v (l) == 0)                                                          \
+      goto __done__;                                                          \
+                                                                              \
+    /* ~0 means select random length between 0 and MAX_VEC_LEN. */            \
+    if (_v (l) == ~0)                                                         \
+      _v (l) = bounded_random_u32 (&(seed), 0, MAX_VEC_LEN);                  \
+                                                                              \
+    _v (v) = _vec_alloc_internal (_v (l), &_v (attr));                        \
+    fill_with_random_data (_v (v), vec_bytes (_v (v)), (seed));               \
+                                                                              \
+    /* Fill header with random data as well. */                               \
+    if (_v (h) > 0)                                                           \
+      {                                                                       \
+       _v (hdr) = vec_header (_v (v));                                       \
+       fill_with_random_data (_v (hdr), _v (h), (seed));                     \
+      }                                                                       \
+                                                                              \
+  __done__:                                                                   \
+    _v (v);                                                                   \
+  })
 
 #define create_random_vec(elt_type, len, seed) \
 create_random_vec_wh (elt_type, len, 0, seed)
@@ -258,7 +260,7 @@ validate_vec_free (elt_t * vec)
 static elt_t *
 validate_vec_free_h (elt_t * vec, uword hdr_bytes)
 {
-  vec_free_h (vec, hdr_bytes);
+  vec_free (vec);
   ASSERT (vec == NULL);
   return vec;
 }
@@ -274,8 +276,8 @@ validate_vec_hdr (elt_t * vec, uword hdr_bytes)
     return;
 
   vh = _vec_find (vec);
-  hdr = vec_header (vec, hdr_bytes);
-  hdr_end = vec_header_end (hdr, hdr_bytes);
+  hdr = vec_header (vec);
+  hdr_end = vec_header_end (hdr);
 
   ASSERT (hdr_end == (u8 *) vec);
   ASSERT ((u8 *) vh - (u8 *) hdr >= hdr_bytes);
@@ -335,8 +337,7 @@ validate_vec (elt_t * vec, uword hdr_bytes)
   else
     {
       if (hdr_bytes > 0)
-       VERBOSE3 ("Header: %U\n",
-                 format_hex_bytes, vec_header (vec, sizeof (vec[0])),
+       VERBOSE3 ("Header: %U\n", format_hex_bytes, vec_header (vec),
                  sizeof (vec[0]));
 
       VERBOSE3 ("%U\n\n",
@@ -371,7 +372,7 @@ validate_vec_resize_h (elt_t * vec, uword num_elts, uword hdr_bytes)
   len1 = vec_len (vec);
 
   if (vec)
-    hdr = vec_header (vec, hdr_bytes);
+    hdr = vec_header (vec);
 
   hash = compute_vec_hash (0, vec);
   hdr_hash = compute_mem_hash (0, hdr, hdr_bytes);
@@ -391,7 +392,7 @@ validate_vec_resize_h (elt_t * vec, uword num_elts, uword hdr_bytes)
     }
 
   if (vec)
-    hdr = vec_header (vec, hdr_bytes);
+    hdr = vec_header (vec);
 
   ASSERT (compute_vec_hash (hash, vec) == 0);
   ASSERT (compute_mem_hash (hdr_hash, hdr, hdr_bytes) == 0);
@@ -677,7 +678,7 @@ validate_vec_init_h (uword num_elts, uword hdr_bytes)
   uword len;
   elt_t *new;
 
-  new = vec_new_ha (elt_t, num_elts, hdr_bytes, 0);
+  new = vec_new_generic (elt_t, num_elts, hdr_bytes, 0, 0);
   len = vec_len (new);
 
   ASSERT (len == num_elts);
@@ -687,7 +688,7 @@ validate_vec_init_h (uword num_elts, uword hdr_bytes)
     {
       if (i == 0)
        {
-         ptr = (u8 *) vec_header (new, hdr_bytes);
+         ptr = (u8 *) vec_header (new);
          end = ptr + hdr_bytes;
        }
       else
@@ -799,7 +800,7 @@ run_validator_wh (uword iter)
        {
        case OP_IS_VEC_INIT_H:
          num_elts = bounded_random_u32 (&g_seed, 0, MAX_CHANGE);
-         vec_free_h (vec, sizeof (hdr_t));
+         vec_free (vec);
          VERBOSE2 ("vec_init_h(), new elts %d\n", num_elts);
          vec = validate_vec_init_h (num_elts, sizeof (hdr_t));
          break;
@@ -840,7 +841,7 @@ run_validator_wh (uword iter)
     }
 
   validate_vec (vec, sizeof (hdr_t));
-  vec_free_h (vec, sizeof (hdr_t));
+  vec_free (vec);
 }
 
 static void
@@ -1079,6 +1080,7 @@ test_vec_main (unformat_input_t * input)
   uword help = 0;
   uword big = 0;
   uword align = 0;
+  uword ugly = 0;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -1089,6 +1091,7 @@ test_vec_main (unformat_input_t * input)
          && 0 == unformat (input, "dump %d", &g_dump_period)
          && 0 == unformat (input, "help %=", &help, 1)
          && 0 == unformat (input, "big %=", &big, 1)
+         && 0 == unformat (input, "ugly %d", &ugly)
          && 0 == unformat (input, "align %=", &align, 1))
        {
          clib_error ("unknown input `%U'", format_unformat_error, input);
@@ -1096,6 +1099,19 @@ test_vec_main (unformat_input_t * input)
        }
     }
 
+  /* Cause a deliberate heap botch */
+  if (ugly)
+    {
+      u8 *overrun_me = 0;
+      int i;
+
+      vec_validate (overrun_me, 31);
+      for (i = 0; i < vec_len (overrun_me) + ugly; i++)
+       overrun_me[i] = i;
+
+      vec_free (overrun_me);
+    }
+
   if (big)
     {
       u8 *bigboy = 0;
@@ -1134,10 +1150,6 @@ test_vec_main (unformat_input_t * input)
     dump_call_stats (g_call_stats);
   prob_free ();
 
-  if (verbose)
-    {
-      memory_snap ();
-    }
   return 0;
 
 usage:
@@ -1158,8 +1170,6 @@ main (int argc, char *argv[])
 
   clib_mem_init (0, 3ULL << 30);
 
-  //  mheap_alloc (0, (uword) 10ULL << 30);
-
   verbose = (argc > 1);
   unformat_init_command_line (&i, argv);
   ret = test_vec_main (&i);