1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-08 11:22:35 +03:00

MDEV-28836 fixup

On GNU/Linux, even though the C11 aligned_alloc() appeared in
GNU libc early on, some custom memory allocators did not
implement it until recently. For example, before
gperftools/gperftools@d406f22853
the free() in tcmalloc would fail to free memory that was
returned by aligned_alloc(), because the latter would map to the
built-in allocator of libc. The Linux specific memalign() has a
similar interface and is safer to use, because it has been
available for a longer time. For AddressSanitizer, we will use
aligned_alloc() so that the constraint on size can be enforced.

buf_tmp_reserve_compression_buf(): When HAVE_ALIGNED_ALLOC holds,
round up the size to be an integer multiple of the alignment.

pfs_malloc(): In the unit test stub, round up the size to be an
integer multiple of the alignment.
This commit is contained in:
Marko Mäkelä
2022-06-22 08:23:32 +03:00
parent 3794673111
commit 0fa19fdebf
4 changed files with 22 additions and 3 deletions

View File

@@ -324,7 +324,14 @@ ENDIF()
CHECK_FUNCTION_EXISTS (accept4 HAVE_ACCEPT4) CHECK_FUNCTION_EXISTS (accept4 HAVE_ACCEPT4)
CHECK_FUNCTION_EXISTS (access HAVE_ACCESS) CHECK_FUNCTION_EXISTS (access HAVE_ACCESS)
CHECK_FUNCTION_EXISTS (alarm HAVE_ALARM) CHECK_FUNCTION_EXISTS (alarm HAVE_ALARM)
CHECK_FUNCTION_EXISTS (aligned_alloc HAVE_ALIGNED_ALLOC) IF (CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT WITH_ASAN)
# When an old custom memory allocator library is used, aligned_alloc()
# could invoke the built-in allocator in libc, not matching
# the overriden free() in the custom memory allocator.
SET(HAVE_ALIGNED_ALLOC 0)
ELSE()
CHECK_FUNCTION_EXISTS (aligned_alloc HAVE_ALIGNED_ALLOC)
ENDIF()
SET(HAVE_ALLOCA 1) SET(HAVE_ALLOCA 1)
CHECK_FUNCTION_EXISTS (backtrace HAVE_BACKTRACE) CHECK_FUNCTION_EXISTS (backtrace HAVE_BACKTRACE)
CHECK_FUNCTION_EXISTS (backtrace_symbols HAVE_BACKTRACE_SYMBOLS) CHECK_FUNCTION_EXISTS (backtrace_symbols HAVE_BACKTRACE_SYMBOLS)

View File

@@ -14,12 +14,19 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
#ifdef HAVE_ALIGNED_ALLOC
#elif defined __linux__
# include <malloc.h>
#endif
inline void *aligned_malloc(size_t size, size_t alignment) inline void *aligned_malloc(size_t size, size_t alignment)
{ {
#ifdef _WIN32 #ifdef _WIN32
return _aligned_malloc(size, alignment); return _aligned_malloc(size, alignment);
#elif defined HAVE_ALIGNED_ALLOC #elif defined HAVE_ALIGNED_ALLOC
return aligned_alloc(alignment, size); return aligned_alloc(alignment, size);
#elif defined __linux__
return memalign(alignment, size);
#else #else
void *result; void *result;
if (posix_memalign(&result, alignment, size)) if (posix_memalign(&result, alignment, size))

View File

@@ -576,9 +576,12 @@ static void buf_tmp_reserve_compression_buf(buf_tmp_buffer_t* slot)
buffer be bigger than input buffer. Adjust the allocated size. */ buffer be bigger than input buffer. Adjust the allocated size. */
ulint size= srv_page_size; ulint size= srv_page_size;
#ifdef HAVE_LZO #ifdef HAVE_LZO
size+= LZO1X_1_15_MEM_COMPRESS; size= size + LZO1X_1_15_MEM_COMPRESS;
#elif defined HAVE_SNAPPY #elif defined HAVE_SNAPPY
size= snappy_max_compressed_length(size); size= snappy_max_compressed_length(size);
#endif
#if defined HAVE_ALIGNED_ALLOC && (defined HAVE_LZO || defined HAVE_SNAPPY)
size= MY_ALIGN(size, srv_page_size);
#endif #endif
slot->comp_buf= static_cast<byte*>(aligned_malloc(size, srv_page_size)); slot->comp_buf= static_cast<byte*>(aligned_malloc(size, srv_page_size));
} }

View File

@@ -26,6 +26,7 @@
#include <pfs_global.h> #include <pfs_global.h>
#include <string.h> #include <string.h>
#include "aligned.h" #include "aligned.h"
#include "assume_aligned.h"
bool pfs_initialized= false; bool pfs_initialized= false;
size_t pfs_allocated_memory_size= 0; size_t pfs_allocated_memory_size= 0;
@@ -47,9 +48,10 @@ void *pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf)
if (--stub_alloc_fails_after_count <= 0) if (--stub_alloc_fails_after_count <= 0)
return NULL; return NULL;
size= MY_ALIGN(size, CPU_LEVEL1_DCACHE_LINESIZE);
void *ptr= aligned_malloc(size, CPU_LEVEL1_DCACHE_LINESIZE); void *ptr= aligned_malloc(size, CPU_LEVEL1_DCACHE_LINESIZE);
if (ptr != NULL) if (ptr != NULL)
memset(ptr, 0, size); memset_aligned<CPU_LEVEL1_DCACHE_LINESIZE>(ptr, 0, size);
return ptr; return ptr;
} }