[compiler-rt] [compiler-rt][asan] _aligned_malloc/_aligned_free interception. (PR #82049)
David CARLIER via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 22 11:13:40 PDT 2024
https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/82049
>From b44305bacaa1c24c43c8eb761560ba6b008a91f2 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 16 Feb 2024 22:08:25 +0000
Subject: [PATCH] [compiler-rt][asan] _aligned_malloc/_aligned_free
interception.
---
compiler-rt/lib/asan/asan_malloc_win.cpp | 20 +++++++++++++++++++
.../asan/TestCases/Windows/aligned_alloc.cpp | 20 +++++++++++++++++++
2 files changed, 40 insertions(+)
create mode 100644 compiler-rt/test/asan/TestCases/Windows/aligned_alloc.cpp
diff --git a/compiler-rt/lib/asan/asan_malloc_win.cpp b/compiler-rt/lib/asan/asan_malloc_win.cpp
index 3278f072198769..213eb6587b52bd 100644
--- a/compiler-rt/lib/asan/asan_malloc_win.cpp
+++ b/compiler-rt/lib/asan/asan_malloc_win.cpp
@@ -142,6 +142,26 @@ __declspec(noinline) void *_recalloc_base(void *p, size_t n, size_t elem_size) {
return _recalloc(p, n, elem_size);
}
+__declspec(noinline) void *_aligned_malloc(size_t alignment, size_t size) {
+ GET_STACK_TRACE_MALLOC;
+ return asan_aligned_alloc(alignment, size, &stack);
+}
+
+__declspec(noinline) void *_aligned_realloc(void *p, size_t alignment,
+ size_t size) {
+ GET_STACK_TRACE_MALLOC;
+ void *n = asan_aligned_alloc(alignment, size, &stack);
+ if (n) {
+ size_t osize = _msize(p);
+ REAL(memcpy)(n, p, Min<size_t>(osize, size));
+ free(p);
+ }
+
+ return n;
+}
+
+__declspec(noinline) void *_aligned_free(void *p) { free(p); }
+
__declspec(noinline) void *_expand(void *memblock, size_t size) {
// _expand is used in realloc-like functions to resize the buffer if possible.
// We don't want memory to stand still while resizing buffers, so return 0.
diff --git a/compiler-rt/test/asan/TestCases/Windows/aligned_alloc.cpp b/compiler-rt/test/asan/TestCases/Windows/aligned_alloc.cpp
new file mode 100644
index 00000000000000..569e3616354015
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/Windows/aligned_alloc.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cl_asan %Od %s %Fe%t
+// RUN: %run %t
+
+#include <windows.h>
+
+#define CHECK_ALIGNED(ptr, alignment) \
+ do { \
+ if (((uintptr_t)(ptr) % (alignment)) != 0) \
+ return __LINE__; \
+ } while (0)
+
+int main(void) {
+ char *p = reinterpret_cast<char *>(_aligned_malloc(16, 8);
+ CHECK_ALIGNED(p, 8);
+ char *n = reinterpret_cast<char *>(_aligned_realloc(32, 16));
+ CHECK_ALIGNED(n, 16);
+ _aligned_free(n);
+
+ return 0;
+}
More information about the llvm-commits
mailing list