Skip to content

Commit

Permalink
[HWASan] Leave pointer tagged when calling memmove.
Browse files Browse the repository at this point in the history
Fixes a false positive that occurs when a user-implemented memmove is
instrumented by HWASan.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D118180
  • Loading branch information
morehouse committed Jan 28, 2022
1 parent e9768a2 commit f7c2833
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler-rt/lib/hwasan/hwasan_memintrinsics.cpp
Expand Up @@ -40,5 +40,5 @@ void *__hwasan_memmove(void *to, const void *from, uptr size) {
reinterpret_cast<uptr>(to), size);
CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
reinterpret_cast<uptr>(from), size);
return memmove(UntagPtr(to), UntagPtr(from), size);
return memmove(to, from, size);
}
39 changes: 39 additions & 0 deletions compiler-rt/test/hwasan/TestCases/custom-memmove.c
@@ -0,0 +1,39 @@
// Test that custom memmove implementations instrumented by HWASan do not cause
// false positives.

// RUN: %clang_hwasan %s -o %t
// RUN: %run %t

#include <assert.h>
#include <sanitizer/hwasan_interface.h>
#include <stdlib.h>

void *memmove(void *Dest, const void *Src, size_t N) {
char *Tmp = (char *)malloc(N);
char *D = (char *)Dest;
const char *S = (const char *)Src;

for (size_t I = 0; I < N; ++I)
Tmp[I] = S[I];
for (size_t I = 0; I < N; ++I)
D[I] = Tmp[I];

free(Tmp);
return Dest;
}

int main() {
__hwasan_enable_allocator_tagging();

const size_t BufSize = 64;
char *Buf = (char *)malloc(BufSize);

for (size_t I = 0; I < BufSize; ++I)
Buf[I] = I;
memmove(Buf + BufSize / 2, Buf, BufSize / 2);
for (size_t I = 0; I < BufSize; ++I)
assert(Buf[I] == I % (BufSize / 2));

free(Buf);
return 0;
}

0 comments on commit f7c2833

Please sign in to comment.