Skip to content

Commit c0edd33

Browse files
committed
MDEV-39141 MariaDB crashes in THD::THD() due to misalignment
fix my_malloc() to return 16-aligned pointers (type_assoc_array.sp-assoc-array-64bit prints changes in memory_used, and my_malloc() wastes less memory now)
1 parent 660f0cf commit c0edd33

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

mysys/my_malloc.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@
1919
#include "mysys_err.h"
2020
#include <m_string.h>
2121

22+
/*
23+
when values won't fit in the provided bits anymore, the options are:
24+
* m_size has 2 unused bits because of alignment
25+
* m_owner has 4 unused bits because of alignment
26+
* if these 6 bits won't help either, HEADER_SIZE has to become 32
27+
*/
28+
#define HEADER_M_SIZE_WIDTH 48 /* good for up to 256TB in one malloc */
29+
#define HEADER_M_KEY_WIDTH 16
30+
#define HEADER_SIZE 16
31+
2232
struct my_memory_header
2333
{
2434
PSI_thread *m_owner;
25-
size_t m_size;
26-
PSI_memory_key m_key;
35+
ulonglong m_size:HEADER_M_SIZE_WIDTH;
36+
uint m_key:HEADER_M_KEY_WIDTH;
2737
};
2838
typedef struct my_memory_header my_memory_header;
29-
#define HEADER_SIZE 24
3039

3140
#define USER_TO_HEADER(P) ((my_memory_header*)((char *)(P) - HEADER_SIZE))
3241
#define HEADER_TO_USER(P) ((char*)(P) + HEADER_SIZE)
@@ -73,6 +82,7 @@ void *my_malloc(PSI_memory_key key, size_t size, myf my_flags)
7382
void *point;
7483
DBUG_ENTER("my_malloc");
7584
DBUG_PRINT("my",("size: %zu flags: %lu", size, my_flags));
85+
DBUG_ASSERT(key < 1ULL << HEADER_M_KEY_WIDTH);
7686
compile_time_assert(sizeof(my_memory_header) <= HEADER_SIZE);
7787

7888
if (!(my_flags & (MY_WME | MY_FAE)))
@@ -81,7 +91,7 @@ void *my_malloc(PSI_memory_key key, size_t size, myf my_flags)
8191
/* Safety */
8292
if (!size)
8393
size=1;
84-
if (size > SIZE_T_MAX - 1024L*1024L*16L) /* Wrong call */
94+
if (size > 1ULL << HEADER_M_SIZE_WIDTH) /* Wrong call */
8595
DBUG_RETURN(0);
8696

8797
/* We have to align size as we store MY_THREAD_SPECIFIC flag in the LSB */

plugin/type_assoc_array/mysql-test/type_assoc_array/sp-assoc-array-64bit.result

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ END;
4444
/
4545
CALL p1;
4646
id diff
47-
p0 176
47+
p0 152
4848
id diff
49-
p1 376
49+
p1 328
5050
id diff
51-
p2 2624
51+
p2 2552
5252
id diff
53-
p2upd 2624
53+
p2upd 2552
5454
id diff
55-
p2del 376
55+
p2del 328
5656
diff1
5757
0
5858
DROP PROCEDURE p1;

0 commit comments

Comments
 (0)