From fbb131da737fdabe4908558bd839c468410ab3fc Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 28 Oct 2019 14:44:28 +0000 Subject: memalign/posix_memalign proposal for libdislocator --- libdislocator/libdislocator.so.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'libdislocator/libdislocator.so.c') diff --git a/libdislocator/libdislocator.so.c b/libdislocator/libdislocator.so.c index d172f7a2..f1972797 100644 --- a/libdislocator/libdislocator.so.c +++ b/libdislocator/libdislocator.so.c @@ -264,6 +264,36 @@ void* realloc(void* ptr, size_t len) { } +/* posix_memalign we mainly check the proper alignment argument + if the requested size fits within the alignment we do + a normal request */ + +int posix_memalign(void** ptr, size_t align, size_t len) { + if (!ptr) FATAL("null pointer on posix_memalign()"); + if ((align % 2) || (align % sizeof(void *))) FATAL("bad alignment on posix_memalign()"); + if (align >= 4 * sizeof(size_t)) { + + len += align -1; + + } + + *ptr = malloc(len); + + DEBUGF("posix_memalign(%p %zu, %zu)", ptr, len, align); + + return 0; +} + +/* just the non-posix fashion */ + +void *memalign(size_t align, size_t len) { + void* ret; + + posix_memalign(&ret, align, len); + + return ret; +} + __attribute__((constructor)) void __dislocator_init(void) { u8* tmp = getenv("AFL_LD_LIMIT_MB"); -- cgit 1.4.1 From 80359685167309639562ece25a77782cb6ccfd54 Mon Sep 17 00:00:00 2001 From: hexcoder- Date: Mon, 28 Oct 2019 16:32:26 +0100 Subject: silence some compiler warnings --- libdislocator/libdislocator.so.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'libdislocator/libdislocator.so.c') diff --git a/libdislocator/libdislocator.so.c b/libdislocator/libdislocator.so.c index f1972797..5246af40 100644 --- a/libdislocator/libdislocator.so.c +++ b/libdislocator/libdislocator.so.c @@ -279,7 +279,7 @@ int posix_memalign(void** ptr, size_t align, size_t len) { *ptr = malloc(len); - DEBUGF("posix_memalign(%p %zu, %zu)", ptr, len, align); + DEBUGF("posix_memalign(%p %zu, %zu)", ptr, align, len); return 0; } @@ -287,9 +287,11 @@ int posix_memalign(void** ptr, size_t align, size_t len) { /* just the non-posix fashion */ void *memalign(size_t align, size_t len) { - void* ret; + void* ret = NULL; - posix_memalign(&ret, align, len); + if (posix_memalign(&ret, align, len)) { + DEBUGF("memalign(%zu, %zu) failed", align, len); + } return ret; } -- cgit 1.4.1 From 6238df88a26d8498b4a821897f030a866dafdc24 Mon Sep 17 00:00:00 2001 From: van Hauser Date: Mon, 28 Oct 2019 22:36:29 +0100 Subject: fixed warning and return --- libdislocator/libdislocator.so.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'libdislocator/libdislocator.so.c') diff --git a/libdislocator/libdislocator.so.c b/libdislocator/libdislocator.so.c index 5246af40..b3a90366 100644 --- a/libdislocator/libdislocator.so.c +++ b/libdislocator/libdislocator.so.c @@ -269,13 +269,11 @@ void* realloc(void* ptr, size_t len) { a normal request */ int posix_memalign(void** ptr, size_t align, size_t len) { - if (!ptr) FATAL("null pointer on posix_memalign()"); - if ((align % 2) || (align % sizeof(void *))) FATAL("bad alignment on posix_memalign()"); - if (align >= 4 * sizeof(size_t)) { - - len += align -1; - - } + if ((char*)ptr == NULL || *ptr == NULL) + return -1; // why would we do: FATAL("null pointer on posix_memalign()"); + if ((align % 2) || (align % sizeof(void *))) + return -1; // why would we do: FATAL("bad alignment on posix_memalign()"); + if (align >= 4 * sizeof(size_t)) len += align -1; *ptr = malloc(len); -- cgit 1.4.1 From 87b599f4a8b875c0ff8c81aff39ebecfd34e29fc Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 29 Oct 2019 08:09:43 +0000 Subject: adding aligned_alloc + little changes proposal for posix_memalign --- libdislocator/libdislocator.so.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'libdislocator/libdislocator.so.c') diff --git a/libdislocator/libdislocator.so.c b/libdislocator/libdislocator.so.c index b3a90366..e27efc0f 100644 --- a/libdislocator/libdislocator.so.c +++ b/libdislocator/libdislocator.so.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "config.h" @@ -272,7 +273,11 @@ int posix_memalign(void** ptr, size_t align, size_t len) { if ((char*)ptr == NULL || *ptr == NULL) return -1; // why would we do: FATAL("null pointer on posix_memalign()"); if ((align % 2) || (align % sizeof(void *))) - return -1; // why would we do: FATAL("bad alignment on posix_memalign()"); + return EINVAL; // why would we do: FATAL("bad alignment on posix_memalign()"); + if (len == 0) { + *ptr = NULL; + return 0; + } if (align >= 4 * sizeof(size_t)) len += align -1; *ptr = malloc(len); @@ -294,6 +299,20 @@ void *memalign(size_t align, size_t len) { return ret; } +/* sort of C11 alias of memalign only more severe, alignment-wise */ + +void *aligned_alloc(size_t align, size_t len) { + void *ret = NULL; + + if ((len % align)) return NULL; + + if (posix_memalign(&ret, align, len)) { + DEBUGF("aligned_alloc(%zu, %zu) failed", align, len); + } + + return ret; +} + __attribute__((constructor)) void __dislocator_init(void) { u8* tmp = getenv("AFL_LD_LIMIT_MB"); -- cgit 1.4.1 From ccbb0d37b33a83a0ea1bdb6128cb6c8900802944 Mon Sep 17 00:00:00 2001 From: van Hauser Date: Tue, 29 Oct 2019 10:44:57 +0100 Subject: removed warning --- libdislocator/libdislocator.so.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'libdislocator/libdislocator.so.c') diff --git a/libdislocator/libdislocator.so.c b/libdislocator/libdislocator.so.c index e27efc0f..7fe40afa 100644 --- a/libdislocator/libdislocator.so.c +++ b/libdislocator/libdislocator.so.c @@ -270,10 +270,10 @@ void* realloc(void* ptr, size_t len) { a normal request */ int posix_memalign(void** ptr, size_t align, size_t len) { - if ((char*)ptr == NULL || *ptr == NULL) - return -1; // why would we do: FATAL("null pointer on posix_memalign()"); + if (*ptr == NULL) + return EINVAL; if ((align % 2) || (align % sizeof(void *))) - return EINVAL; // why would we do: FATAL("bad alignment on posix_memalign()"); + return EINVAL; if (len == 0) { *ptr = NULL; return 0; -- cgit 1.4.1