History

string: use __builtin_memcpy() in strlcpy/strlcat

lib/string.c is built with -ffreestanding, which prevents the compiler from replacing certain functions with calls to their library versions. On the other hand, this also prevents Clang and GCC from instrumenting calls to memcpy() when building with KASAN, KCSAN or KMSAN: - KASAN normally replaces memcpy() with __asan_memcpy() with the additional cc-param,asan-kernel-mem-intrinsic-prefix=1; - KCSAN and KMSAN replace memcpy() with __tsan_memcpy() and __msan_memcpy() by default. To let the tools catch memory accesses from strlcpy/strlcat, replace the calls to memcpy() with __builtin_memcpy(), which KASAN, KCSAN and KMSAN are able to replace even in -ffreestanding mode. This preserves the behavior in normal builds (__builtin_memcpy() ends up being replaced with memcpy()), and does not introduce new instrumentation in unwanted places, as strlcpy/strlcat are already instrumented.

Alexander Potapenko committed 3 years ago

Commit f9cfb191 parent 2af4aa31 file · +2 −2Browse files

Showing the changes to lib/string.c (1 of 1 file). Show all

Unified · Split

lib/string.c+2−2
@@ -110,7 +110,7 @@ size_t strlcpy(char *dest, const char *src, size_t size) · 109 unmodified lines
110 110
111 if (size) {111 if (size) {
112 size_t len = (ret >= size) ? size - 1 : ret;112 size_t len = (ret >= size) ? size - 1 : ret;
113 memcpy(dest, src, len);113 __builtin_memcpy(dest, src, len);
114 dest[len] = '\0';114 dest[len] = '\0';
115 }115 }
116 return ret;116 return ret;
@@ -260,7 +260,7 @@ size_t strlcat(char *dest, const char *src, size_t count) · 143 unmodified lines
260 count -= dsize;260 count -= dsize;
261 if (len >= count)261 if (len >= count)
262 len = count-1;262 len = count-1;
263 memcpy(dest, src, len);263 __builtin_memcpy(dest, src, len);
264 dest[len] = 0;264 dest[len] = 0;
265 return res;265 return res;
266}266}