diff --git a/Android.bp b/Android.bp index 5e2a44a..4ab69df 100644 --- a/Android.bp +++ b/Android.bp @@ -29,6 +29,7 @@ common_cflags = [ "-DCONFIG_CLASS_REGION_SIZE=34359738368", // 32GiB "-DN_ARENA=1", "-DCONFIG_STATS=true", + "-DCONFIG_SELF_INIT=false", ] cc_defaults { diff --git a/Makefile b/Makefile index 6254138..e5551c5 100644 --- a/Makefile +++ b/Makefile @@ -84,6 +84,10 @@ ifeq (,$(filter $(CONFIG_STATS),true false)) $(error CONFIG_STATS must be true or false) endif +ifeq (,$(filter $(CONFIG_SELF_INIT),true false)) + $(error CONFIG_SELF_INIT must be true or false) +endif + CPPFLAGS += \ -DCONFIG_SEAL_METADATA=$(CONFIG_SEAL_METADATA) \ -DZERO_ON_FREE=$(CONFIG_ZERO_ON_FREE) \ @@ -102,7 +106,8 @@ CPPFLAGS += \ -DFREE_SLABS_QUARANTINE_RANDOM_LENGTH=$(CONFIG_FREE_SLABS_QUARANTINE_RANDOM_LENGTH) \ -DCONFIG_CLASS_REGION_SIZE=$(CONFIG_CLASS_REGION_SIZE) \ -DN_ARENA=$(CONFIG_N_ARENA) \ - -DCONFIG_STATS=$(CONFIG_STATS) + -DCONFIG_STATS=$(CONFIG_STATS) \ + -DCONFIG_SELF_INIT=$(CONFIG_SELF_INIT) $(OUT)/libhardened_malloc$(SUFFIX).so: $(OBJECTS) | $(OUT) $(CC) $(CFLAGS) $(LDFLAGS) -shared $^ $(LDLIBS) -o $@ diff --git a/config/default.mk b/config/default.mk index 67a267b..71b1cc4 100644 --- a/config/default.mk +++ b/config/default.mk @@ -20,3 +20,4 @@ CONFIG_FREE_SLABS_QUARANTINE_RANDOM_LENGTH := 32 CONFIG_CLASS_REGION_SIZE := 34359738368 # 32GiB CONFIG_N_ARENA := 4 CONFIG_STATS := false +CONFIG_SELF_INIT := true diff --git a/config/light.mk b/config/light.mk index 397f85e..88a0e1f 100644 --- a/config/light.mk +++ b/config/light.mk @@ -20,3 +20,4 @@ CONFIG_FREE_SLABS_QUARANTINE_RANDOM_LENGTH := 32 CONFIG_CLASS_REGION_SIZE := 34359738368 # 32GiB CONFIG_N_ARENA := 4 CONFIG_STATS := false +CONFIG_SELF_INIT := true diff --git a/h_malloc.c b/h_malloc.c index 39ba84d..4b68841 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -1183,11 +1183,13 @@ static inline unsigned init(void) { return arena; } +#if CONFIG_SELF_INIT // trigger early initialization to set up pthread_atfork and protect state as soon as possible COLD __attribute__((constructor(101))) static void trigger_early_init(void) { // avoid calling init directly to skip it if this isn't the malloc implementation h_free(h_malloc(16)); } +#endif // Returns 0 on overflow. static size_t get_large_size_class(size_t size) {