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