string_override.c 950 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <stddef.h>
  3. /*
  4. * Override the "basic" built-in string helpers so that they can be used in
  5. * guest code. KVM selftests don't support dynamic loading in guest code and
  6. * will jump into the weeds if the compiler decides to insert an out-of-line
  7. * call via the PLT.
  8. */
  9. int memcmp(const void *cs, const void *ct, size_t count)
  10. {
  11. const unsigned char *su1, *su2;
  12. int res = 0;
  13. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) {
  14. if ((res = *su1 - *su2) != 0)
  15. break;
  16. }
  17. return res;
  18. }
  19. void *memcpy(void *dest, const void *src, size_t count)
  20. {
  21. char *tmp = dest;
  22. const char *s = src;
  23. while (count--)
  24. *tmp++ = *s++;
  25. return dest;
  26. }
  27. void *memset(void *s, int c, size_t count)
  28. {
  29. char *xs = s;
  30. while (count--)
  31. *xs++ = c;
  32. return s;
  33. }
  34. size_t strnlen(const char *s, size_t count)
  35. {
  36. const char *sc;
  37. for (sc = s; count-- && *sc != '\0'; ++sc)
  38. /* nothing */;
  39. return sc - s;
  40. }