diff --git a/Makefile b/Makefile index 87964c0..04f4667 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,7 @@ endef CPPFLAGS := $(CPPFLAGS) -D_GNU_SOURCE -I include SHARED_FLAGS := -O3 -flto -fPIC -fvisibility=hidden -fno-plt \ - $(call safe_flag,-fstack-clash-protection) -fstack-protector-strong -pipe -Wall -Wextra \ - $(call safe_flag,-Wcast-align=strict,-Wcast-align) -Wcast-qual -Wwrite-strings + $(call safe_flag,-Wcast-align) -Wcast-qual -Wwrite-strings ifeq ($(CONFIG_WERROR),true) SHARED_FLAGS += -Werror @@ -32,7 +31,8 @@ endif CFLAGS := $(CFLAGS) -std=c11 $(SHARED_FLAGS) -Wmissing-prototypes CXXFLAGS := $(CXXFLAGS) -std=c++17 $(SHARED_FLAGS) -LDFLAGS := $(LDFLAGS) -Wl,--as-needed,-z,defs,-z,relro,-z,now,-z,nodlopen,-z,text + +LDFLAGS := $(LDFLAGS) -Wl SOURCES := chacha.c h_malloc.c memory.c pages.c random.c util.c OBJECTS := $(SOURCES:.c=.o) @@ -40,7 +40,7 @@ OBJECTS := $(SOURCES:.c=.o) ifeq ($(CONFIG_CXX_ALLOCATOR),true) # make sure LTO is compatible in case CC and CXX don't match (such as clang and g++) CXX := $(CC) - LDLIBS += -lstdc++ -lgcc_s + LDLIBS += -lstdc++ SOURCES += new.cc OBJECTS += new.o diff --git a/config/default.mk b/config/default.mk index 67a267b..9392347 100644 --- a/config/default.mk +++ b/config/default.mk @@ -1,5 +1,5 @@ CONFIG_WERROR := true -CONFIG_NATIVE := true +CONFIG_NATIVE := false CONFIG_CXX_ALLOCATOR := true CONFIG_UBSAN := false CONFIG_SEAL_METADATA := false diff --git a/h_malloc.c b/h_malloc.c index f03a74e..684101a 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -6,10 +6,9 @@ #include #include #include -#include -#include #include + #include #include "third_party/libdivide.h" @@ -61,8 +60,9 @@ static_assert(N_ARENA <= 256, "maximum number of arenas is currently 256"); #define CACHELINE_SIZE 64 #if N_ARENA > 1 -__attribute__((tls_model("initial-exec"))) -static thread_local unsigned thread_arena = N_ARENA; +// use -ftls-model flag for now +//__attribute__((tls_model("initial-exec"))) +static __thread unsigned thread_arena = N_ARENA; static atomic_uint thread_arena_counter = 0; #else static const unsigned thread_arena = 0; diff --git a/include/h_malloc.h b/include/h_malloc.h index 5824abb..4374c7a 100644 --- a/include/h_malloc.h +++ b/include/h_malloc.h @@ -3,8 +3,6 @@ #include -#include - #ifdef __cplusplus extern "C" { #endif diff --git a/memory.c b/memory.c index 2d995ef..3cc04d0 100644 --- a/memory.c +++ b/memory.c @@ -1,7 +1,6 @@ #include #include -#include #ifndef PR_SET_VMA #define PR_SET_VMA 0x53564d41 diff --git a/new.cc b/new.cc index e46e302..ce1d59f 100644 --- a/new.cc +++ b/new.cc @@ -1,4 +1,3 @@ -#include #include #include "h_malloc.h" diff --git a/random.c b/random.c index 8883531..c4dbeef 100644 --- a/random.c +++ b/random.c @@ -12,7 +12,11 @@ static void get_random_seed(void *buf, size_t size) { ssize_t r; do { - r = getrandom(buf, size, 0); + #ifdef __APPLE__ + r = getentropy(buf, size); + #else + r = getrandom(buf, size, 0); + #endif } while (r == -1 && errno == EINTR); if (r <= 0) { diff --git a/test/Makefile b/test/Makefile index f9dbf45..bd5bc02 100644 --- a/test/Makefile +++ b/test/Makefile @@ -9,8 +9,6 @@ ifeq (,$(filter $(CONFIG_SLAB_CANARY),true false)) $(error CONFIG_SLAB_CANARY must be true or false) endif -LDLIBS := -lpthread - CPPFLAGS += \ -DSLAB_CANARY=$(CONFIG_SLAB_CANARY) \ -DCONFIG_EXTENDED_SIZE_CLASSES=$(CONFIG_EXTENDED_SIZE_CLASSES) diff --git a/test/mallinfo.c b/test/mallinfo.c index d38f92f..75ed42e 100644 --- a/test/mallinfo.c +++ b/test/mallinfo.c @@ -1,6 +1,5 @@ #include - -#include +#include #include "test_util.h" diff --git a/test/mallinfo2.c b/test/mallinfo2.c index 66c3445..70a2d7b 100644 --- a/test/mallinfo2.c +++ b/test/mallinfo2.c @@ -1,6 +1,5 @@ #include - -#include +#include #include "test_util.h" diff --git a/test/malloc_info.c b/test/malloc_info.c index 3eda5e3..edfc18b 100644 --- a/test/malloc_info.c +++ b/test/malloc_info.c @@ -1,7 +1,6 @@ #include #include - -#include +#include #include "test_util.h" #include "../util.h" diff --git a/test/simple-memory-corruption/impossibly_large_malloc.c b/test/simple-memory-corruption/impossibly_large_malloc.c index 3341ea5..7473308 100644 --- a/test/simple-memory-corruption/impossibly_large_malloc.c +++ b/test/simple-memory-corruption/impossibly_large_malloc.c @@ -1,4 +1,4 @@ -#include +#include #include #include "../test_util.h" diff --git a/test/simple-memory-corruption/invalid_malloc_object_size_small.c b/test/simple-memory-corruption/invalid_malloc_object_size_small.c index 126ff2b..5deef8b 100644 --- a/test/simple-memory-corruption/invalid_malloc_object_size_small.c +++ b/test/simple-memory-corruption/invalid_malloc_object_size_small.c @@ -1,4 +1,4 @@ -#include +#include #include "../test_util.h" diff --git a/test/simple-memory-corruption/invalid_malloc_object_size_small_quarantine.c b/test/simple-memory-corruption/invalid_malloc_object_size_small_quarantine.c index 168dc23..84ee1d1 100644 --- a/test/simple-memory-corruption/invalid_malloc_object_size_small_quarantine.c +++ b/test/simple-memory-corruption/invalid_malloc_object_size_small_quarantine.c @@ -1,4 +1,4 @@ -#include +#include #include "../test_util.h" diff --git a/test/simple-memory-corruption/invalid_malloc_usable_size_small.c b/test/simple-memory-corruption/invalid_malloc_usable_size_small.c index f200564..b12f7ee 100644 --- a/test/simple-memory-corruption/invalid_malloc_usable_size_small.c +++ b/test/simple-memory-corruption/invalid_malloc_usable_size_small.c @@ -1,4 +1,5 @@ -#include +#include +#include #include "../test_util.h" @@ -8,6 +9,6 @@ OPTNONE int main(void) { return 1; } char *q = p + 4096 * 4; - malloc_usable_size(q); + malloc_size(q); return 0; } diff --git a/test/simple-memory-corruption/invalid_malloc_usable_size_small_quarantine.c b/test/simple-memory-corruption/invalid_malloc_usable_size_small_quarantine.c index ae0a8c6..6dafa97 100644 --- a/test/simple-memory-corruption/invalid_malloc_usable_size_small_quarantine.c +++ b/test/simple-memory-corruption/invalid_malloc_usable_size_small_quarantine.c @@ -1,4 +1,5 @@ -#include +#include +#include #include "../test_util.h" @@ -8,6 +9,6 @@ OPTNONE int main(void) { return 1; } free(p); - malloc_usable_size(p); + malloc_size(p); return 0; } diff --git a/test/simple-memory-corruption/malloc_object_size.c b/test/simple-memory-corruption/malloc_object_size.c index 04e6350..3a951d6 100644 --- a/test/simple-memory-corruption/malloc_object_size.c +++ b/test/simple-memory-corruption/malloc_object_size.c @@ -1,5 +1,5 @@ #include -#include +#include #include "../test_util.h" diff --git a/test/simple-memory-corruption/malloc_object_size_offset.c b/test/simple-memory-corruption/malloc_object_size_offset.c index 2bc16b6..01a1656 100644 --- a/test/simple-memory-corruption/malloc_object_size_offset.c +++ b/test/simple-memory-corruption/malloc_object_size_offset.c @@ -1,5 +1,5 @@ #include -#include +#include #include "../test_util.h" diff --git a/test/simple-memory-corruption/overflow_large_1_byte.c b/test/simple-memory-corruption/overflow_large_1_byte.c index b759654..9368dc3 100644 --- a/test/simple-memory-corruption/overflow_large_1_byte.c +++ b/test/simple-memory-corruption/overflow_large_1_byte.c @@ -1,4 +1,3 @@ -#include #include #include "../test_util.h" @@ -8,7 +7,7 @@ OPTNONE int main(void) { if (!p) { return 1; } - size_t size = malloc_usable_size(p); + size_t size = malloc_size(p); *(p + size) = 0; free(p); return 0; diff --git a/test/simple-memory-corruption/overflow_large_8_byte.c b/test/simple-memory-corruption/overflow_large_8_byte.c index a067420..892ec66 100644 --- a/test/simple-memory-corruption/overflow_large_8_byte.c +++ b/test/simple-memory-corruption/overflow_large_8_byte.c @@ -1,4 +1,4 @@ -#include +#include #include #include "../test_util.h" @@ -8,7 +8,7 @@ OPTNONE int main(void) { if (!p) { return 1; } - size_t size = malloc_usable_size(p); + size_t size = malloc_size(p); *(p + size + 7) = 0; free(p); return 0; diff --git a/test/simple-memory-corruption/overflow_small_1_byte.c b/test/simple-memory-corruption/overflow_small_1_byte.c index 3aa8206..61d7e1e 100644 --- a/test/simple-memory-corruption/overflow_small_1_byte.c +++ b/test/simple-memory-corruption/overflow_small_1_byte.c @@ -1,4 +1,4 @@ -#include +#include #include #include "../test_util.h" @@ -8,7 +8,7 @@ OPTNONE int main(void) { if (!p) { return 1; } - size_t size = malloc_usable_size(p); + size_t size = malloc_size(p); *(p + size) = 1; free(p); return 0; diff --git a/test/simple-memory-corruption/overflow_small_8_byte.c b/test/simple-memory-corruption/overflow_small_8_byte.c index fd9666e..3ba0063 100644 --- a/test/simple-memory-corruption/overflow_small_8_byte.c +++ b/test/simple-memory-corruption/overflow_small_8_byte.c @@ -1,4 +1,4 @@ -#include +#include #include #include "../test_util.h" @@ -8,7 +8,7 @@ OPTNONE int main(void) { if (!p) { return 1; } - size_t size = malloc_usable_size(p); + size_t size = malloc_size(p); // XOR is used to avoid the test having a 1/256 chance to fail *(p + size + 7) ^= 1; free(p); diff --git a/test/simple-memory-corruption/string_overflow.c b/test/simple-memory-corruption/string_overflow.c index 7f54a63..3b460f0 100644 --- a/test/simple-memory-corruption/string_overflow.c +++ b/test/simple-memory-corruption/string_overflow.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include "../test_util.h" @@ -12,7 +12,7 @@ OPTNONE int main(void) { return 1; } - size_t size = malloc_usable_size(p); + size_t size = malloc_size(p); memset(p, 'a', size); printf("overflow by %zu bytes\n", strlen(p) - size); diff --git a/test/simple-memory-corruption/unaligned_malloc_usable_size_small.c b/test/simple-memory-corruption/unaligned_malloc_usable_size_small.c index 2bbbf8a..743fae0 100644 --- a/test/simple-memory-corruption/unaligned_malloc_usable_size_small.c +++ b/test/simple-memory-corruption/unaligned_malloc_usable_size_small.c @@ -1,4 +1,5 @@ -#include +#include +#include #include "../test_util.h" @@ -7,6 +8,6 @@ OPTNONE int main(void) { if (!p) { return 1; } - malloc_usable_size(p + 1); + malloc_size(p + 1); return 0; } diff --git a/test/simple-memory-corruption/uninitialized_malloc_usable_size.c b/test/simple-memory-corruption/uninitialized_malloc_usable_size.c index d0644c4..0a6166f 100644 --- a/test/simple-memory-corruption/uninitialized_malloc_usable_size.c +++ b/test/simple-memory-corruption/uninitialized_malloc_usable_size.c @@ -1,8 +1,8 @@ -#include #include "../test_util.h" +#include OPTNONE int main(void) { - malloc_usable_size((void *)1); + malloc_size((void *)1); return 0; } diff --git a/util.h b/util.h index 4c7ac9f..0df05ce 100644 --- a/util.h +++ b/util.h @@ -25,8 +25,13 @@ #define UNUSED __attribute__((unused)) #define EXPORT __attribute__((visibility("default"))) +#define ALIAS(f) + +#ifndef __APPLE__ #define STRINGIFY(s) #s #define ALIAS(f) __attribute__((alias(STRINGIFY(f)))) +#endif + static inline int ffzl(unsigned long x) { return __builtin_ffsl(~x);