add configuration template system

This commit is contained in:
Daniel Micay 2022-01-09 11:08:56 -05:00
parent 052b756840
commit b3372e1576
5 changed files with 85 additions and 33 deletions

View File

@ -1,25 +1,17 @@
CONFIG_WERROR := true VARIANT := default
CONFIG_NATIVE := true
CONFIG_CXX_ALLOCATOR := true ifneq ($(VARIANT),)
CONFIG_UBSAN := false CONFIG_FILE := config/$(VARIANT).mk
CONFIG_SEAL_METADATA := false include config/$(VARIANT).mk
CONFIG_ZERO_ON_FREE := true endif
CONFIG_WRITE_AFTER_FREE_CHECK := true
CONFIG_SLOT_RANDOMIZE := true ifeq ($(VARIANT),default)
CONFIG_SLAB_CANARY := true SUFFIX :=
CONFIG_SLAB_QUARANTINE_RANDOM_LENGTH := 1 else
CONFIG_SLAB_QUARANTINE_QUEUE_LENGTH := 1 SUFFIX := -$(VARIANT)
CONFIG_EXTENDED_SIZE_CLASSES := true endif
CONFIG_LARGE_SIZE_CLASSES := true
CONFIG_GUARD_SLABS_INTERVAL := 1 OUT := out$(SUFFIX)
CONFIG_GUARD_SIZE_DIVISOR := 2
CONFIG_REGION_QUARANTINE_RANDOM_LENGTH := 256
CONFIG_REGION_QUARANTINE_QUEUE_LENGTH := 1024
CONFIG_REGION_QUARANTINE_SKIP_THRESHOLD := 33554432 # 32MiB
CONFIG_FREE_SLABS_QUARANTINE_RANDOM_LENGTH := 32
CONFIG_CLASS_REGION_SIZE := 34359738368 # 32GiB
CONFIG_N_ARENA := 4
CONFIG_STATS := false
define safe_flag define safe_flag
$(shell $(CC) $(if $(filter clang,$(CC)),-Werror=unknown-warning-option) -E $1 - </dev/null >/dev/null 2>&1 && echo $1 || echo $2) $(shell $(CC) $(if $(filter clang,$(CC)),-Werror=unknown-warning-option) -E $1 - </dev/null >/dev/null 2>&1 && echo $1 || echo $2)
@ -54,6 +46,8 @@ ifeq ($(CONFIG_CXX_ALLOCATOR),true)
OBJECTS += new.o OBJECTS += new.o
endif endif
OBJECTS := $(addprefix $(OUT)/,$(OBJECTS))
ifeq ($(CONFIG_UBSAN),true) ifeq ($(CONFIG_UBSAN),true)
CFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined CFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
CXXFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined CXXFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
@ -111,16 +105,26 @@ CPPFLAGS += \
-DN_ARENA=$(CONFIG_N_ARENA) \ -DN_ARENA=$(CONFIG_N_ARENA) \
-DCONFIG_STATS=$(CONFIG_STATS) -DCONFIG_STATS=$(CONFIG_STATS)
libhardened_malloc.so: $(OBJECTS) $(OUT)/libhardened_malloc$(SUFFIX).so: $(OBJECTS) | $(OUT)
$(CC) $(CFLAGS) $(LDFLAGS) -shared $^ $(LDLIBS) -o $@ $(CC) $(CFLAGS) $(LDFLAGS) -shared $^ $(LDLIBS) -o $@
chacha.o: chacha.c chacha.h util.h $(OUT):
h_malloc.o: h_malloc.c include/h_malloc.h mutex.h memory.h pages.h random.h util.h mkdir -p $(OUT)
memory.o: memory.c memory.h util.h
new.o: new.cc include/h_malloc.h util.h $(OUT)/chacha.o: chacha.c chacha.h util.h $(CONFIG_FILE) | $(OUT)
pages.o: pages.c pages.h memory.h util.h $(COMPILE.c) $(OUTPUT_OPTION) $<
random.o: random.c random.h chacha.h util.h $(OUT)/h_malloc.o: h_malloc.c include/h_malloc.h mutex.h memory.h pages.h random.h util.h $(CONFIG_FILE) | $(OUT)
util.o: util.c util.h $(COMPILE.c) $(OUTPUT_OPTION) $<
$(OUT)/memory.o: memory.c memory.h util.h $(CONFIG_FILE) | $(OUT)
$(COMPILE.c) $(OUTPUT_OPTION) $<
$(OUT)/new.o: new.cc include/h_malloc.h util.h $(CONFIG_FILE) | $(OUT)
$(COMPILE.cc) $(OUTPUT_OPTION) $<
$(OUT)/pages.o: pages.c pages.h memory.h util.h $(CONFIG_FILE) | $(OUT)
$(COMPILE.c) $(OUTPUT_OPTION) $<
$(OUT)/random.o: random.c random.h chacha.h util.h $(CONFIG_FILE) | $(OUT)
$(COMPILE.c) $(OUTPUT_OPTION) $<
$(OUT)/util.o: util.c util.h $(CONFIG_FILE) | $(OUT)
$(COMPILE.c) $(OUTPUT_OPTION) $<
check: tidy check: tidy
@ -129,10 +133,10 @@ tidy:
clang-tidy --extra-arg=-std=c++17 $(filter %.cc,$(SOURCES)) -- $(CPPFLAGS) clang-tidy --extra-arg=-std=c++17 $(filter %.cc,$(SOURCES)) -- $(CPPFLAGS)
clean: clean:
rm -f libhardened_malloc.so $(OBJECTS) rm -f $(OUT)/libhardened_malloc.so $(OBJECTS)
make -C test/ clean make -C test/ clean
test: libhardened_malloc.so test: $(OUT)/libhardened_malloc$(SUFFIX).so
make -C test/ make -C test/
python3 -m unittest discover --start-directory test/ python3 -m unittest discover --start-directory test/

22
config/default.mk Normal file
View File

@ -0,0 +1,22 @@
CONFIG_WERROR := true
CONFIG_NATIVE := true
CONFIG_CXX_ALLOCATOR := true
CONFIG_UBSAN := false
CONFIG_SEAL_METADATA := false
CONFIG_ZERO_ON_FREE := true
CONFIG_WRITE_AFTER_FREE_CHECK := true
CONFIG_SLOT_RANDOMIZE := true
CONFIG_SLAB_CANARY := true
CONFIG_SLAB_QUARANTINE_RANDOM_LENGTH := 1
CONFIG_SLAB_QUARANTINE_QUEUE_LENGTH := 1
CONFIG_EXTENDED_SIZE_CLASSES := true
CONFIG_LARGE_SIZE_CLASSES := true
CONFIG_GUARD_SLABS_INTERVAL := 1
CONFIG_GUARD_SIZE_DIVISOR := 2
CONFIG_REGION_QUARANTINE_RANDOM_LENGTH := 256
CONFIG_REGION_QUARANTINE_QUEUE_LENGTH := 1024
CONFIG_REGION_QUARANTINE_SKIP_THRESHOLD := 33554432 # 32MiB
CONFIG_FREE_SLABS_QUARANTINE_RANDOM_LENGTH := 32
CONFIG_CLASS_REGION_SIZE := 34359738368 # 32GiB
CONFIG_N_ARENA := 4
CONFIG_STATS := false

22
config/light.mk Normal file
View File

@ -0,0 +1,22 @@
CONFIG_WERROR := true
CONFIG_NATIVE := true
CONFIG_CXX_ALLOCATOR := true
CONFIG_UBSAN := false
CONFIG_SEAL_METADATA := false
CONFIG_ZERO_ON_FREE := true
CONFIG_WRITE_AFTER_FREE_CHECK := false
CONFIG_SLOT_RANDOMIZE := false
CONFIG_SLAB_CANARY := true
CONFIG_SLAB_QUARANTINE_RANDOM_LENGTH := 0
CONFIG_SLAB_QUARANTINE_QUEUE_LENGTH := 0
CONFIG_EXTENDED_SIZE_CLASSES := true
CONFIG_LARGE_SIZE_CLASSES := true
CONFIG_GUARD_SLABS_INTERVAL := 8
CONFIG_GUARD_SIZE_DIVISOR := 2
CONFIG_REGION_QUARANTINE_RANDOM_LENGTH := 256
CONFIG_REGION_QUARANTINE_QUEUE_LENGTH := 1024
CONFIG_REGION_QUARANTINE_SKIP_THRESHOLD := 33554432 # 32MiB
CONFIG_FREE_SLABS_QUARANTINE_RANDOM_LENGTH := 32
CONFIG_CLASS_REGION_SIZE := 34359738368 # 32GiB
CONFIG_N_ARENA := 4
CONFIG_STATS := false

View File

@ -1,6 +1,10 @@
CONFIG_SLAB_CANARY := true CONFIG_SLAB_CANARY := true
CONFIG_EXTENDED_SIZE_CLASSES := true CONFIG_EXTENDED_SIZE_CLASSES := true
ifneq ($(VARIANT),)
$(error testing non-default variants not yet supported)
endif
ifeq (,$(filter $(CONFIG_SLAB_CANARY),true false)) 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

View File

@ -13,7 +13,7 @@ CPPFLAGS += -D_GNU_SOURCE \
LDLIBS := -lhardened_malloc LDLIBS := -lhardened_malloc
LDFLAGS := -Wl,-L$(dir)../../,-R,$(dir)../../ LDFLAGS := -Wl,-L$(dir)../../out,-R,$(dir)../../out
EXECUTABLES := \ EXECUTABLES := \
double_free_large \ double_free_large \