[msan] Intercept (v)asprintf.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@184545 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/msan/msan_interceptors.cc b/lib/msan/msan_interceptors.cc
index ea868c7..1c713b0 100644
--- a/lib/msan/msan_interceptors.cc
+++ b/lib/msan/msan_interceptors.cc
@@ -311,11 +311,30 @@
return res;
}
+INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap) {
+ ENSURE_MSAN_INITED();
+ int res = REAL(vasprintf)(strp, format, ap);
+ if (res >= 0 && !__msan_has_dynamic_component()) {
+ __msan_unpoison(strp, sizeof(*strp));
+ __msan_unpoison(*strp, res + 1);
+ }
+ return res;
+}
+
+INTERCEPTOR(int, asprintf, char **strp, const char *format, ...) { // NOLINT
+ ENSURE_MSAN_INITED();
+ va_list ap;
+ va_start(ap, format);
+ int res = vasprintf(strp, format, ap); // NOLINT
+ va_end(ap);
+ return res;
+}
+
INTERCEPTOR(int, vsnprintf, char *str, uptr size,
const char *format, va_list ap) {
ENSURE_MSAN_INITED();
int res = REAL(vsnprintf)(str, size, format, ap);
- if (!__msan_has_dynamic_component()) {
+ if (res >= 0 && !__msan_has_dynamic_component()) {
__msan_unpoison(str, res + 1);
}
return res;
@@ -324,7 +343,7 @@
INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap) {
ENSURE_MSAN_INITED();
int res = REAL(vsprintf)(str, format, ap);
- if (!__msan_has_dynamic_component()) {
+ if (res >= 0 && !__msan_has_dynamic_component()) {
__msan_unpoison(str, res + 1);
}
return res;
@@ -333,7 +352,7 @@
INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
ENSURE_MSAN_INITED();
int res = REAL(vswprintf)(str, size, format, ap);
- if (!__msan_has_dynamic_component()) {
+ if (res >= 0 && !__msan_has_dynamic_component()) {
__msan_unpoison(str, 4 * (res + 1));
}
return res;
@@ -1144,6 +1163,8 @@
INTERCEPT_FUNCTION(strtod);
INTERCEPT_FUNCTION(strtof);
INTERCEPT_FUNCTION(strtold);
+ INTERCEPT_FUNCTION(vasprintf);
+ INTERCEPT_FUNCTION(asprintf);
INTERCEPT_FUNCTION(vsprintf);
INTERCEPT_FUNCTION(vsnprintf);
INTERCEPT_FUNCTION(vswprintf);
diff --git a/lib/msan/tests/msan_test.cc b/lib/msan/tests/msan_test.cc
index 3028812..53106ac 100644
--- a/lib/msan/tests/msan_test.cc
+++ b/lib/msan/tests/msan_test.cc
@@ -1055,6 +1055,20 @@
EXPECT_POISONED(buff[8]);
}
+TEST(MemorySanitizer, asprintf) { // NOLINT
+ char *pbuf;
+ EXPECT_POISONED(pbuf);
+ int res = asprintf(&pbuf, "%d", 1234567); // NOLINT
+ assert(res == 7);
+ EXPECT_NOT_POISONED(pbuf);
+ assert(pbuf[0] == '1');
+ assert(pbuf[1] == '2');
+ assert(pbuf[2] == '3');
+ assert(pbuf[6] == '7');
+ assert(pbuf[7] == 0);
+ free(pbuf);
+}
+
TEST(MemorySanitizer, wcstombs) {
const wchar_t *x = L"abc";
char buff[10];