From 9514bd7bbb370b3aeb4dbe66230d46cabf2ccc65 Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Tue, 18 Nov 2025 15:45:54 -0500 Subject: [PATCH 1/4] Misused posix_memalign results in memory leak and returning wrong lib index --- ddprof-lib/src/main/cpp/arch_dd.h | 26 ++++++++++++++++++++++++++ ddprof-lib/src/main/cpp/codeCache.cpp | 3 ++- ddprof-lib/src/main/cpp/codeCache.h | 4 +++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ddprof-lib/src/main/cpp/arch_dd.h b/ddprof-lib/src/main/cpp/arch_dd.h index 8bddcdd2c..01d030612 100644 --- a/ddprof-lib/src/main/cpp/arch_dd.h +++ b/ddprof-lib/src/main/cpp/arch_dd.h @@ -6,6 +6,8 @@ #define COMMA , #include +#include +#include constexpr int DEFAULT_CACHE_LINE_SIZE = 64; @@ -43,4 +45,28 @@ static inline void storeRelease(volatile T& var, T value) { return __atomic_store_n(&var, value, __ATOMIC_RELEASE); } +inline bool is_power_of_2(size_t size) { + return size > 0 && (size & (size - 1)) == 0; +} + +template +inline bool is_aligned(const T* ptr, size_t alignment) noexcept { + assert(is_power_of_2(alignment)); + // Convert the pointer to an integer type + auto iptr = reinterpret_cast(ptr); + + // Check if the integer value is a multiple of the alignment + return (iptr & ~(alignment - 1) == 0); +} + +inline size_t align_down(size_t size, size_t alignment) noexcept { + assert(is_power_of_2(alignment)); + return size & ~(alignment - 1); +} + +inline size_t align_up(size_t size, size_t alignment) noexcept { + assert(is_power_of_2(alignment)); + return align_down(size + alignment - 1, alignment); +} + #endif // _ARCH_DD_H diff --git a/ddprof-lib/src/main/cpp/codeCache.cpp b/ddprof-lib/src/main/cpp/codeCache.cpp index 5fb17d964..7e2769be5 100644 --- a/ddprof-lib/src/main/cpp/codeCache.cpp +++ b/ddprof-lib/src/main/cpp/codeCache.cpp @@ -12,7 +12,8 @@ #include char *NativeFunc::create(const char *name, short lib_index) { - NativeFunc *f = (NativeFunc *)malloc(sizeof(NativeFunc) + 1 + strlen(name)); + size_t size = align_up(sizeof(NativeFunc) + 1 + strlen(name), sizeof(NativeFunc*)); + NativeFunc *f = (NativeFunc *)aligned_alloc(sizeof(NativeFunc*), size); f->_lib_index = lib_index; f->_mark = 0; // cppcheck-suppress memleak diff --git a/ddprof-lib/src/main/cpp/codeCache.h b/ddprof-lib/src/main/cpp/codeCache.h index 233da56c1..c38d52a07 100644 --- a/ddprof-lib/src/main/cpp/codeCache.h +++ b/ddprof-lib/src/main/cpp/codeCache.h @@ -6,6 +6,8 @@ #ifndef _CODECACHE_H #define _CODECACHE_H +#include "arch_dd.h" + #include #include #include @@ -62,7 +64,7 @@ class NativeFunc { static short libIndex(const char *name) { NativeFunc* func = from(name); - if (posix_memalign((void**)(&func), sizeof(NativeFunc*), sizeof(NativeFunc)) != 0) { + if (!is_aligned(func, sizeof(func))) { return -1; } return func->_lib_index; From 6936cbab1f2bdeb898ca3b551eca0d333a9b89e2 Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Tue, 18 Nov 2025 20:24:33 -0500 Subject: [PATCH 2/4] Move to new file --- ddprof-lib/src/main/cpp/arch_dd.h | 26 ---------------------- ddprof-lib/src/main/cpp/codeCache.h | 2 +- ddprof-lib/src/main/cpp/utils.h | 34 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 ddprof-lib/src/main/cpp/utils.h diff --git a/ddprof-lib/src/main/cpp/arch_dd.h b/ddprof-lib/src/main/cpp/arch_dd.h index 01d030612..8bddcdd2c 100644 --- a/ddprof-lib/src/main/cpp/arch_dd.h +++ b/ddprof-lib/src/main/cpp/arch_dd.h @@ -6,8 +6,6 @@ #define COMMA , #include -#include -#include constexpr int DEFAULT_CACHE_LINE_SIZE = 64; @@ -45,28 +43,4 @@ static inline void storeRelease(volatile T& var, T value) { return __atomic_store_n(&var, value, __ATOMIC_RELEASE); } -inline bool is_power_of_2(size_t size) { - return size > 0 && (size & (size - 1)) == 0; -} - -template -inline bool is_aligned(const T* ptr, size_t alignment) noexcept { - assert(is_power_of_2(alignment)); - // Convert the pointer to an integer type - auto iptr = reinterpret_cast(ptr); - - // Check if the integer value is a multiple of the alignment - return (iptr & ~(alignment - 1) == 0); -} - -inline size_t align_down(size_t size, size_t alignment) noexcept { - assert(is_power_of_2(alignment)); - return size & ~(alignment - 1); -} - -inline size_t align_up(size_t size, size_t alignment) noexcept { - assert(is_power_of_2(alignment)); - return align_down(size + alignment - 1, alignment); -} - #endif // _ARCH_DD_H diff --git a/ddprof-lib/src/main/cpp/codeCache.h b/ddprof-lib/src/main/cpp/codeCache.h index c38d52a07..3398ad029 100644 --- a/ddprof-lib/src/main/cpp/codeCache.h +++ b/ddprof-lib/src/main/cpp/codeCache.h @@ -6,7 +6,7 @@ #ifndef _CODECACHE_H #define _CODECACHE_H -#include "arch_dd.h" +#include "utils.h" #include #include diff --git a/ddprof-lib/src/main/cpp/utils.h b/ddprof-lib/src/main/cpp/utils.h new file mode 100644 index 000000000..2e4e594f6 --- /dev/null +++ b/ddprof-lib/src/main/cpp/utils.h @@ -0,0 +1,34 @@ +#ifndef _UTILS_H +#define _UTILS_H + +#include +#include + +inline bool is_power_of_2(size_t size) { + return size > 0 && (size & (size - 1)) == 0; +} + +template +inline bool is_aligned(const T* ptr, size_t alignment) noexcept { + assert(is_power_of_2(alignment)); + // Convert the pointer to an integer type + auto iptr = reinterpret_cast(ptr); + + // Check if the integer value is a multiple of the alignment + return (iptr & ~(alignment - 1) == 0); +} + +inline size_t align_down(size_t size, size_t alignment) noexcept { + assert(is_power_of_2(alignment)); + return size & ~(alignment - 1); +} + +inline size_t align_up(size_t size, size_t alignment) noexcept { + assert(is_power_of_2(alignment)); + return align_down(size + alignment - 1, alignment); +} + + + + +#endif // _UTILS_H \ No newline at end of file From 066fc1c99eb0f56af0256afabacd54ef3a2422da Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Tue, 18 Nov 2025 20:29:29 -0500 Subject: [PATCH 3/4] Fix include --- ddprof-lib/src/main/cpp/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddprof-lib/src/main/cpp/utils.h b/ddprof-lib/src/main/cpp/utils.h index 2e4e594f6..a29a29e0e 100644 --- a/ddprof-lib/src/main/cpp/utils.h +++ b/ddprof-lib/src/main/cpp/utils.h @@ -2,7 +2,7 @@ #define _UTILS_H #include -#include +#include inline bool is_power_of_2(size_t size) { return size > 0 && (size & (size - 1)) == 0; From fb769d00a0f4339f9c2400565543de963f55285a Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Tue, 18 Nov 2025 20:35:31 -0500 Subject: [PATCH 4/4] Fix include --- ddprof-lib/src/main/cpp/codeCache.cpp | 1 + ddprof-lib/src/main/cpp/utils.h | 1 + 2 files changed, 2 insertions(+) diff --git a/ddprof-lib/src/main/cpp/codeCache.cpp b/ddprof-lib/src/main/cpp/codeCache.cpp index 7e2769be5..14796d701 100644 --- a/ddprof-lib/src/main/cpp/codeCache.cpp +++ b/ddprof-lib/src/main/cpp/codeCache.cpp @@ -6,6 +6,7 @@ #include "codeCache.h" #include "dwarf_dd.h" #include "os_dd.h" + #include #include #include diff --git a/ddprof-lib/src/main/cpp/utils.h b/ddprof-lib/src/main/cpp/utils.h index a29a29e0e..47d58eb96 100644 --- a/ddprof-lib/src/main/cpp/utils.h +++ b/ddprof-lib/src/main/cpp/utils.h @@ -3,6 +3,7 @@ #include #include +#include inline bool is_power_of_2(size_t size) { return size > 0 && (size & (size - 1)) == 0;