Skip to content

Commit f333486

Browse files
authored
Assert on proper alignment in dlmalloc (#14634)
Fixes #10072
1 parent 6309a5d commit f333486

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

system/lib/dlmalloc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@
2424
#define USE_SPIN_LOCKS 0 // Ensure we use pthread_mutex_t.
2525
#endif
2626

27+
#ifndef MALLOC_ALIGNMENT
28+
#include <stddef.h>
29+
/* `malloc`ed pointers must be aligned at least as strictly as max_align_t. */
30+
#define MALLOC_ALIGNMENT (__alignof__(max_align_t))
31+
/*
32+
Emscripten aligns even float128 to 64-bits, to save size and increase speed.
33+
See https://github.com/emscripten-core/emscripten/issues/10072
34+
*/
35+
_Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8");
2736
#endif
2837

38+
#endif // __EMSCRIPTEN__
39+
2940

3041
#define __THROW
3142
#define __attribute_malloc__

system/lib/emmalloc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* malloc.
4040
*/
4141

42+
#include <stddef.h>
4243
#include <stdint.h>
4344
#include <unistd.h>
4445
#include <memory.h>
@@ -60,7 +61,8 @@ extern "C"
6061

6162
// Configuration: specifies the minimum alignment that malloc()ed memory outputs. Allocation requests with smaller alignment
6263
// than this will yield an allocation with this much alignment.
63-
#define MALLOC_ALIGNMENT 8
64+
#define MALLOC_ALIGNMENT alignof(max_align_t)
65+
_Static_assert(alignof(max_align_t) == 8, "max_align_t must be correct");
6466

6567
#define EMMALLOC_EXPORT __attribute__((weak, __visibility__("default")))
6668

0 commit comments

Comments
 (0)