1 |
commit: 767becaac8ccf0a271fc7633fafe635bf8126f3e |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Dec 19 06:41:36 2015 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Dec 19 06:41:36 2015 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=767becaa |
7 |
|
8 |
libsandbox: fix memory alignment |
9 |
|
10 |
Some targets (like sparc32) have higher alignment requirements for 64-bit |
11 |
values than size_t (which is 4 bytes on sparc32). If we happen to return |
12 |
4 byte aligned memory which is used to hold a 64-bit, we get bus errors. |
13 |
Use the same algorithm that dlmalloc does. |
14 |
|
15 |
URL: https://bugs.gentoo.org/565630 |
16 |
Reported-by: Denis Kaganovich <mahatma <AT> eu.by> |
17 |
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org> |
18 |
|
19 |
libsandbox/memory.c | 9 ++++++--- |
20 |
1 file changed, 6 insertions(+), 3 deletions(-) |
21 |
|
22 |
diff --git a/libsandbox/memory.c b/libsandbox/memory.c |
23 |
index 5609208..8581128 100644 |
24 |
--- a/libsandbox/memory.c |
25 |
+++ b/libsandbox/memory.c |
26 |
@@ -15,6 +15,9 @@ |
27 |
#include "libsandbox.h" |
28 |
#include "sbutil.h" |
29 |
|
30 |
+/* Pick a value to guarantee alignment requirements. #565630 */ |
31 |
+#define MIN_ALIGN (2 * sizeof(void *)) |
32 |
+ |
33 |
/* Well screw me sideways, someone decided to override mmap() #290249 |
34 |
* We probably don't need to include the exact sym version ... |
35 |
*/ |
36 |
@@ -35,14 +38,14 @@ static int sb_munmap(void *addr, size_t length) |
37 |
} |
38 |
#define munmap sb_munmap |
39 |
|
40 |
-#define SB_MALLOC_TO_MMAP(ptr) ((void*)(((size_t*)ptr) - 1)) |
41 |
-#define SB_MMAP_TO_MALLOC(ptr) ((void*)(((size_t*)ptr) + 1)) |
42 |
+#define SB_MALLOC_TO_MMAP(ptr) ((void*)((uintptr_t)(ptr) - MIN_ALIGN)) |
43 |
+#define SB_MMAP_TO_MALLOC(ptr) ((void*)((uintptr_t)(ptr) + MIN_ALIGN)) |
44 |
#define SB_MALLOC_TO_SIZE(ptr) (*((size_t*)SB_MALLOC_TO_MMAP(ptr))) |
45 |
|
46 |
void *malloc(size_t size) |
47 |
{ |
48 |
size_t *ret; |
49 |
- size += sizeof(size_t); |
50 |
+ size += MIN_ALIGN; |
51 |
ret = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
52 |
if (ret == MAP_FAILED) |
53 |
return NULL; |