/* * Copyright (c) 2016 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** \file Optimized string handling code, including c11-compliant "safe C library" variants. */ #ifndef included_clib_string_h #define included_clib_string_h #include /* for CLIB_LINUX_KERNEL */ #include #ifdef CLIB_LINUX_KERNEL #include #endif #ifdef CLIB_UNIX #include #endif #ifdef CLIB_STANDALONE #include #endif #if _x86_64_ #include #endif /* Exchanges source and destination. */ void clib_memswap (void *_a, void *_b, uword bytes); /* * the vector unit memcpy variants confuse coverity * so don't let it anywhere near them. */ #ifndef __COVERITY__ #if __AVX512F__ #include #elif __AVX2__ #include #elif __SSSE3__ #include #else #define clib_memcpy_fast(a,b,c) memcpy(a,b,c) #endif #else /* __COVERITY__ */ #define clib_memcpy_fast(a,b,c) memcpy(a,b,c) #endif /* c-11 string manipulation variants */ #ifndef EOK #define EOK 0 #endif #ifndef EINVAL #define EINVAL 22 #endif #ifndef ESRCH #define ESRCH 3 #endif #ifndef EOVERFLOW #define EOVERFLOW 75 #endif /* * In order to provide smooth mapping from unsafe string API to the clib string * macro, we often have to improvise s1max and s2max due to the additional * arguments are required for implementing the safe API. This macro is used * to provide the s1max/s2max. It is not perfect because the actual * s1max/s2max may be greater than 4k and the mapping from the unsafe API to * the macro would cause a regression. However, it is not terribly likely. * So I bet against the odds. */ #define CLIB_STRING_MACRO_MAX 4096 typedef int errno_t; typedef uword rsize_t; void clib_c11_violation (const char *s); errno_t memcpy_s (void *__restrict__ dest, rsize_t dmax, const void *__restrict__ src, rsize_t n); always_inline errno_t memcpy_s_inline (void *__restrict__ dest, rsize_t dmax, const void *__restrict__ src, rsize_t n) { uword low, hi; u8 bad; /* * Optimize constant-number-of-bytes calls without asking * "too many questions for someone from New Jersey" */ if (__builtin_constant_p (n)) { clib_memcpy_fast (dest, src, n); return EOK; } /* * call bogus if: src or dst NULL, trying to copy * more data than we have space in dst, or src == dst. * n == 0 isn't really "bad", so check first in the * "wall-of-shame" department... */ bad = (dest == 0) + (src == 0) + (n > dmax) + (dest == src) + (n == 0); if (PREDICT_FALSE (bad != 0)) { /* Not actually trying to copy anything is OK */ if (n == 0) return EOK; if (dest == NULL) clib_c11_violation ("dest NULL"); if (src == NULL) clib_c11_violation ("src NULL"); if (n > dmax) clib_c11_violation ("n > dmax"); if (dest == src) clib_c11_violation ("dest == src"); return EINVAL; } /* Check for src/dst overlap, which is not allowed */ low = (uword) (src < dest ? src : dest); hi = (uword) (src < dest ? dest : src); if (PREDICT_FALSE (low + (n - 1) >= hi)) { clib_c11_violation ("src/dest overlap"); return EINVAL; } clib_memcpy_fast (dest, src, n); return EOK; } /* * Note: $$$ This macro is a crutch. Folks need to manually * inspect every extant clib_memcpy(...) call and * attempt to provide a real destination buffer size * argument... */ #define clib_memcpy(d,s,n) memcpy_s_inline(d,n,s,n) errno_t memset_s (void *s, rsize_t smax, int c, rsize_t n); always_inline errno_t memset_s_inline (void *s, rsize_t smax, int c, rsize_t n) { u8 bad; bad = (s == 0) + (n > smax); if (PREDICT_FALSE (bad != 0)) { if (s == 0) clib_c11_violation ("s NULL"); if (n > smax) clib_c11_violation ("n > smax"); return (EINVAL); } memset (s, c, n); return (EOK); } /* * This macro is not [so much of] a crutch. * It's super-typical to write: * * ep = pool_get (); * clib_memset(ep, 0, sizeof (*ep)); * * The compiler should delete the not-so useful * (n > smax) test. TBH the NULL pointer check isn't * so useful in this case, but so be it. */ #define clib_memset(s,c,n) memset_s_inline(s,n,c,n) static_always_inline void clib_memcpy_le (u8 * dst, u8 * src, u8 len, u8 max_len) { #if defined (CLIB_HAVE_VEC256) u8x32 s0, s1, d0, d1; u8x32 mask = { 0, 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 }; u8x32 lv = u8x32_splat (len); u8x32 add = u8x32_splat (32); s0 = u8x32_load_unaligned (src); s1 = u8x32_load_unaligned (src + 32); d0 = u8x32_load_unaligned (dst); d1 = u8x32_load_unaligned (dst + 32); d0 = u8x32_blend (d0, s0, u8x32_is_greater (lv, mask)); u8x32_store_unaligned (d0, dst); if (max_len <= 32) return; mask += add; d1 = u8x32_blend (d1, s1, u8x32_is_greater (lv, mask)); u8x32_store_unaligned (d1, dst + 32); #elif defined (CLIB_HAVE_VEC128) u8x16 s0, s1, s2, s3, d0, d1, d2, d3; u8x16 mask = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; u8x16 lv = u8x16_splat (len); u8x16 add = u8x16_splat (16); s0 = u8x16_load_unaligned (src); s1 = u8x16_load_unaligned (src + 16); s2 = u8x16_load_unaligned (src + 32); s3 = u8x16_load_unaligned (src + 48); d0 = u8x16_load_unaligned (dst); d1 = u8x16_load_unaligned (dst + 16); d2 = u8x16_load_unaligned (dst + 32); d3 = u8x16_load_unaligned (dst + 48); d0 = u8x16_blend (d0, s0, u8x16_is_greater (lv, mask)); u8x16_store_unaligned (d0, dst); if (max_len <= 16) return; mask += add; d1 = u8x16_blend (d1, s1, u8x16_is_greater (lv, mask)); u8x16_store_unaligned (d1, dst + 16); if (max_len <= 32) return; mask += add; d2 = u8x16_blend (d2, s2, u8x16_is_greater (lv, mask)); u8x16_store_unaligned (d2, dst + 32); mask += add; d3 = u8x16_blend (d3, s3, u8x16_is_greater (lv, mask)); u8x16_store_unaligned (d3, dst + 48); #else memmove (dst, src, len); #endif } static_always_inline void clib_memcpy_le64 (u8 * dst, u8 * src, u8 len) { clib_memcpy_le (dst, src, len, 64); } static_always_inline void clib_memcpy_le32 (u8 * dst, u8 * src, u8 len) { clib_memcpy_le (dst, src, len, 32); } static_always_inline void clib_memset_u64 (void *p, u64 val, uword count) { u64 *ptr = p; #if defined(CLIB_HAVE_VEC512) u64x8 v512 = u64x8_splat (val); while (count >= 8) { u64x8_store_unaligned (v512, ptr); ptr += 8; count -= 8; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC256) u64x4 v256 = u64x4_splat (val); while (count >= 4) { u64x4_store_unaligned (v256, ptr); ptr += 4; count -= 4; } if (count == 0) return; #else while (count >= 4) { ptr[0] = ptr[1] = ptr[2] = ptr[3] = val; ptr += 4; count -= 4; } #endif while (count--) ptr++[0] = val; } static_always_inline void clib_memset_u32 (void *p, u32 val, uword count) { u32 *ptr = p; #if defined(CLIB_HAVE_VEC512) u32x16 v512 = u32x16_splat (val); while (count >= 16) { u32x16_store_unaligned (v512, ptr); ptr += 16; count -= 16; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC256) u32x8 v256 = u32x8_splat (val); while (count >= 8) { u32x8_store_unaligned (v256, ptr); ptr += 8; count -= 8; } if (count == 0) return; #endif #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE) u32x4 v128 = u32x4_splat (val); while (count >= 4) { u32x4_store_unaligned (v128, ptr); ptr += 4; count -
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
  Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <vppinfra/format.h>
#include <vppinfra/bitmap.h>

static u32 known_random_sequence[] = {
  0x00000000, 0x3c6ef35f, 0x47502932, 0xd1ccf6e9,
  0xaaf95334, 0x6252e503, 0x9f2ec686, 0x57fe6c2d,
  0xa3d95fa8, 0x81fdbee7, 0x94f0af1a, 0xcbf633b1,
};


int
test_random_main (unformat_input_t * input)
{
  uword n_iterations;
  uword i, repeat_count;
  uword *bitmap = 0;
  uword print;
  u32 seed;
  u32 *seedp = &seed;

  /* first, check known sequence from Numerical Recipes in C, 2nd ed.
     page 284 */
  seed = known_random_sequence[0];
  for (i = 0; i < ARRAY_LEN (known_random_sequence) - 1; i++)
    {
      u32 rv;
      rv = random_u32 (seedp);
      if (rv != known_random_sequence[i + 1])
	{
	  fformat (stderr, "known sequence check FAILS at index %d", i + 1);
	  break;
	}
    }

  clib_warning ("known sequence check passes");

  n_iterations = 1000;
  seed = 0;
  print = 1 << 24;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (0 == unformat (input, "iter %d", &n_iterations)
	  && 0 == unformat (input, "print %d", &print)
	  && 0 == unformat (input, "seed %d", &seed))
	clib_error ("unknown input `%U'", format_unformat_error, input);
    }

  if (!seed)
    seed = random_default_seed ();

  if (n_iterations == 0)
    n_iterations = random_u32_max ();

  clib_warning ("%d iterations, seed %d\n", n_iterations, seed);

  repeat_count = 0;
  for (i = 0; i < n_iterations; i++)
    {
      uword r = random_u32 (&seed);
      uword b, ri, rj;

      ri = r / BITS (bitmap[0]);
      rj = (uword) 1 << (r % BITS (bitmap[0]));

      vec_validate (bitmap, ri);
      b = bitmap[ri];

      if (b & rj)
	goto repeat;
      b |= rj;
      bitmap[ri] = b;

      if (0 == (i & (print - 1)))
	fformat (stderr, "0x%08x iterations %d repeats\n", i, repeat_count);
      continue;

    repeat:
      fformat (stderr, "repeat found at iteration  %d/%d\n", i, n_iterations);
      repeat_count++;
      continue;
    }

  return 0;
}

#ifdef CLIB_UNIX
int
main (int argc, char *argv[])
{
  unformat_input_t i;
  int ret;

  clib_mem_init (0, 3ULL << 30);

  unformat_init_command_line (&i, argv);
  ret = test_random_main (&i);
  unformat_free (&i);

  return ret;
}
#endif /* CLIB_UNIX */


/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
are encouraged to use the cool C11 strstr_s API to avoid * this problem. */ #define clib_strstr(s1,s2) \ ({ char * __substring = 0; \ strstr_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, CLIB_STRING_MACRO_MAX, \ &__substring); \ __substring; \ }) errno_t strstr_s (char *s1, rsize_t s1max, const char *s2, rsize_t s2max, char **substring); always_inline errno_t strstr_s_inline (char *s1, rsize_t s1max, const char *s2, rsize_t s2max, char **substring) { u8 bad; size_t s1_size, s2_size; bad = (s1 == 0) + (s2 == 0) + (substring == 0) + (s1max == 0) + (s2max == 0) + (s1 && s1max && (s1[clib_strnlen (s1, s1max)] != '\0')) + (s2 && s2max && (s2[clib_strnlen (s2, s2max)] != '\0')); if (PREDICT_FALSE (bad != 0)) { if (s1 == 0) clib_c11_violation ("s1 NULL"); if (s2 == 0) clib_c11_violation ("s2 NULL"); if (s1max == 0) clib_c11_violation ("s1max 0"); if (s2max == 0) clib_c11_violation ("s2max 0"); if (substring == 0) clib_c11_violation ("substring NULL"); if (s1 && s1max && (s1[clib_strnlen (s1, s1max)] != '\0')) clib_c11_violation ("s1 unterminated"); if (s2 && s2max && (s2[clib_strnlen (s2, s1max)] != '\0')) clib_c11_violation ("s2 unterminated"); return EINVAL; } /* * s2 points to a string with zero length, or s2 equals s1, return s1 */ if (PREDICT_FALSE (*s2 == '\0' || s1 == s2)) { *substring = s1; return EOK; } /* * s2_size > s1_size, it won't find match. */ s1_size = clib_strnlen (s1, s1max); s2_size = clib_strnlen (s2, s2max); if (PREDICT_FALSE (s2_size > s1_size)) return ESRCH; *substring = strstr (s1, s2); if (*substring == 0) return ESRCH; return EOK; } #endif /* included_clib_string_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */