make source & tests compile on osx

This commit is contained in:
iraizo 2022-01-12 13:25:22 +01:00
parent b3372e1576
commit be80191b46
26 changed files with 47 additions and 45 deletions

View file

@ -19,8 +19,7 @@ endef
CPPFLAGS := $(CPPFLAGS) -D_GNU_SOURCE -I include CPPFLAGS := $(CPPFLAGS) -D_GNU_SOURCE -I include
SHARED_FLAGS := -O3 -flto -fPIC -fvisibility=hidden -fno-plt \ 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) -Wcast-qual -Wwrite-strings
$(call safe_flag,-Wcast-align=strict,-Wcast-align) -Wcast-qual -Wwrite-strings
ifeq ($(CONFIG_WERROR),true) ifeq ($(CONFIG_WERROR),true)
SHARED_FLAGS += -Werror SHARED_FLAGS += -Werror
@ -32,7 +31,8 @@ endif
CFLAGS := $(CFLAGS) -std=c11 $(SHARED_FLAGS) -Wmissing-prototypes CFLAGS := $(CFLAGS) -std=c11 $(SHARED_FLAGS) -Wmissing-prototypes
CXXFLAGS := $(CXXFLAGS) -std=c++17 $(SHARED_FLAGS) 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 SOURCES := chacha.c h_malloc.c memory.c pages.c random.c util.c
OBJECTS := $(SOURCES:.c=.o) OBJECTS := $(SOURCES:.c=.o)
@ -40,7 +40,7 @@ OBJECTS := $(SOURCES:.c=.o)
ifeq ($(CONFIG_CXX_ALLOCATOR),true) ifeq ($(CONFIG_CXX_ALLOCATOR),true)
# make sure LTO is compatible in case CC and CXX don't match (such as clang and g++) # make sure LTO is compatible in case CC and CXX don't match (such as clang and g++)
CXX := $(CC) CXX := $(CC)
LDLIBS += -lstdc++ -lgcc_s LDLIBS += -lstdc++
SOURCES += new.cc SOURCES += new.cc
OBJECTS += new.o OBJECTS += new.o

View file

@ -1,5 +1,5 @@
CONFIG_WERROR := true CONFIG_WERROR := true
CONFIG_NATIVE := true CONFIG_NATIVE := false
CONFIG_CXX_ALLOCATOR := true CONFIG_CXX_ALLOCATOR := true
CONFIG_UBSAN := false CONFIG_UBSAN := false
CONFIG_SEAL_METADATA := false CONFIG_SEAL_METADATA := false

View file

@ -6,10 +6,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <threads.h>
#include <malloc.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include "third_party/libdivide.h" #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 #define CACHELINE_SIZE 64
#if N_ARENA > 1 #if N_ARENA > 1
__attribute__((tls_model("initial-exec"))) // use -ftls-model flag for now
static thread_local unsigned thread_arena = N_ARENA; //__attribute__((tls_model("initial-exec")))
static __thread unsigned thread_arena = N_ARENA;
static atomic_uint thread_arena_counter = 0; static atomic_uint thread_arena_counter = 0;
#else #else
static const unsigned thread_arena = 0; static const unsigned thread_arena = 0;

View file

@ -3,8 +3,6 @@
#include <stdio.h> #include <stdio.h>
#include <malloc.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View file

@ -1,7 +1,6 @@
#include <errno.h> #include <errno.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/prctl.h>
#ifndef PR_SET_VMA #ifndef PR_SET_VMA
#define PR_SET_VMA 0x53564d41 #define PR_SET_VMA 0x53564d41

1
new.cc
View file

@ -1,4 +1,3 @@
#include <bits/functexcept.h>
#include <new> #include <new>
#include "h_malloc.h" #include "h_malloc.h"

View file

@ -12,7 +12,11 @@ static void get_random_seed(void *buf, size_t size) {
ssize_t r; ssize_t r;
do { do {
#ifdef __APPLE__
r = getentropy(buf, size);
#else
r = getrandom(buf, size, 0); r = getrandom(buf, size, 0);
#endif
} while (r == -1 && errno == EINTR); } while (r == -1 && errno == EINTR);
if (r <= 0) { if (r <= 0) {

View file

@ -9,8 +9,6 @@ ifeq (,$(filter $(CONFIG_SLAB_CANARY),true false))
$(error CONFIG_SLAB_CANARY must be true or false) $(error CONFIG_SLAB_CANARY must be true or false)
endif endif
LDLIBS := -lpthread
CPPFLAGS += \ CPPFLAGS += \
-DSLAB_CANARY=$(CONFIG_SLAB_CANARY) \ -DSLAB_CANARY=$(CONFIG_SLAB_CANARY) \
-DCONFIG_EXTENDED_SIZE_CLASSES=$(CONFIG_EXTENDED_SIZE_CLASSES) -DCONFIG_EXTENDED_SIZE_CLASSES=$(CONFIG_EXTENDED_SIZE_CLASSES)

View file

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "test_util.h" #include "test_util.h"

View file

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "test_util.h" #include "test_util.h"

View file

@ -1,7 +1,6 @@
#include <pthread.h> #include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "test_util.h" #include "test_util.h"
#include "../util.h" #include "../util.h"

View file

@ -1,4 +1,4 @@
#include <malloc.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#include "../test_util.h" #include "../test_util.h"

View file

@ -1,4 +1,4 @@
#include <malloc.h> #include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"

View file

@ -1,4 +1,4 @@
#include <malloc.h> #include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"

View file

@ -1,4 +1,5 @@
#include <malloc.h> #include <malloc/malloc.h>
#include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"
@ -8,6 +9,6 @@ OPTNONE int main(void) {
return 1; return 1;
} }
char *q = p + 4096 * 4; char *q = p + 4096 * 4;
malloc_usable_size(q); malloc_size(q);
return 0; return 0;
} }

View file

@ -1,4 +1,5 @@
#include <malloc.h> #include <malloc/malloc.h>
#include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"
@ -8,6 +9,6 @@ OPTNONE int main(void) {
return 1; return 1;
} }
free(p); free(p);
malloc_usable_size(p); malloc_size(p);
return 0; return 0;
} }

View file

@ -1,5 +1,5 @@
#include <stdbool.h> #include <stdbool.h>
#include <malloc.h> #include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"

View file

@ -1,5 +1,5 @@
#include <stdbool.h> #include <stdbool.h>
#include <malloc.h> #include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"

View file

@ -1,4 +1,3 @@
#include <malloc.h>
#include <stdlib.h> #include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"
@ -8,7 +7,7 @@ OPTNONE int main(void) {
if (!p) { if (!p) {
return 1; return 1;
} }
size_t size = malloc_usable_size(p); size_t size = malloc_size(p);
*(p + size) = 0; *(p + size) = 0;
free(p); free(p);
return 0; return 0;

View file

@ -1,4 +1,4 @@
#include <malloc.h> #include <malloc/malloc.h>
#include <stdlib.h> #include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"
@ -8,7 +8,7 @@ OPTNONE int main(void) {
if (!p) { if (!p) {
return 1; return 1;
} }
size_t size = malloc_usable_size(p); size_t size = malloc_size(p);
*(p + size + 7) = 0; *(p + size + 7) = 0;
free(p); free(p);
return 0; return 0;

View file

@ -1,4 +1,4 @@
#include <malloc.h> #include <malloc/malloc.h>
#include <stdlib.h> #include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"
@ -8,7 +8,7 @@ OPTNONE int main(void) {
if (!p) { if (!p) {
return 1; return 1;
} }
size_t size = malloc_usable_size(p); size_t size = malloc_size(p);
*(p + size) = 1; *(p + size) = 1;
free(p); free(p);
return 0; return 0;

View file

@ -1,4 +1,4 @@
#include <malloc.h> #include <malloc/malloc.h>
#include <stdlib.h> #include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"
@ -8,7 +8,7 @@ OPTNONE int main(void) {
if (!p) { if (!p) {
return 1; 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 // XOR is used to avoid the test having a 1/256 chance to fail
*(p + size + 7) ^= 1; *(p + size + 7) ^= 1;
free(p); free(p);

View file

@ -2,7 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc/malloc.h>
#include "../test_util.h" #include "../test_util.h"
@ -12,7 +12,7 @@ OPTNONE int main(void) {
return 1; return 1;
} }
size_t size = malloc_usable_size(p); size_t size = malloc_size(p);
memset(p, 'a', size); memset(p, 'a', size);
printf("overflow by %zu bytes\n", strlen(p) - size); printf("overflow by %zu bytes\n", strlen(p) - size);

View file

@ -1,4 +1,5 @@
#include <malloc.h> #include <malloc/malloc.h>
#include <stdlib.h>
#include "../test_util.h" #include "../test_util.h"
@ -7,6 +8,6 @@ OPTNONE int main(void) {
if (!p) { if (!p) {
return 1; return 1;
} }
malloc_usable_size(p + 1); malloc_size(p + 1);
return 0; return 0;
} }

View file

@ -1,8 +1,8 @@
#include <malloc.h>
#include "../test_util.h" #include "../test_util.h"
#include <malloc/malloc.h>
OPTNONE int main(void) { OPTNONE int main(void) {
malloc_usable_size((void *)1); malloc_size((void *)1);
return 0; return 0;
} }

5
util.h
View file

@ -25,8 +25,13 @@
#define UNUSED __attribute__((unused)) #define UNUSED __attribute__((unused))
#define EXPORT __attribute__((visibility("default"))) #define EXPORT __attribute__((visibility("default")))
#define ALIAS(f)
#ifndef __APPLE__
#define STRINGIFY(s) #s #define STRINGIFY(s) #s
#define ALIAS(f) __attribute__((alias(STRINGIFY(f)))) #define ALIAS(f) __attribute__((alias(STRINGIFY(f))))
#endif
static inline int ffzl(unsigned long x) { static inline int ffzl(unsigned long x) {
return __builtin_ffsl(~x); return __builtin_ffsl(~x);