Skip to content

Commit f6b38d2

Browse files
committed
fix build
1 parent f36e4fe commit f6b38d2

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ ALLOC_SANITY = -DALLOC_SANITY=0
108108
## not require ALLOC_SANITY is enabled. On MacOS you need
109109
## to set FORTIFY_SOURCE to 0. Leave these commented if
110110
## you aren't enabling them.
111-
#MEMCPY_SANITY = -DMEMCPY_SANITY=0 -D_FORTIFY_SOURCE=0
112-
#MEMSET_SANITY = -DMEMSET_SANITY=0 -D_FORTIFY_SOURCE=0
111+
MEMCPY_SANITY = -DMEMCPY_SANITY=0
112+
MEMSET_SANITY = -DMEMSET_SANITY=0
113113

114114
## Enable the userfaultfd based uninitialized read detection
115115
## feature. This samples calls to malloc, and allocates raw
@@ -181,6 +181,9 @@ UNAME := $(shell uname)
181181
ifeq ($(UNAME), Darwin)
182182
OS_FLAGS = -framework Security
183183
LIBNAME = libisoalloc.dylib
184+
ifeq ($(MEMSET_SANITY), -DMEMSET_SANITY=1) || ($(MEMCPY_SANITY), -DMEMCPY_SANITY=1)
185+
CFLAGS += -D_FORTIFY_SOURCE=0
186+
endif
184187
endif
185188

186189
ifeq ($(UNAME), Linux)
@@ -218,7 +221,7 @@ BUILD_ERROR_FLAGS := $(BUILD_ERROR_FLAGS) -Werror -pedantic
218221
else
219222
BUILD_ERROR_FLAGS := $(BUILD_ERROR_FLAGS) -Wno-attributes -Wno-unused-variable
220223
endif
221-
CFLAGS = $(COMMON_CFLAGS) $(SECURITY_FLAGS) $(BUILD_ERROR_FLAGS) $(HOOKS) $(HEAP_PROFILER) -fvisibility=hidden \
224+
CFLAGS += $(COMMON_CFLAGS) $(SECURITY_FLAGS) $(BUILD_ERROR_FLAGS) $(HOOKS) $(HEAP_PROFILER) -fvisibility=hidden \
222225
-std=c11 $(SANITIZER_SUPPORT) $(ALLOC_SANITY) $(MEMCPY_SANITY) $(UNINIT_READ_SANITY) $(CPU_PIN) $(SCHED_GETCPU) \
223226
$(EXPERIMENTAL) $(UAF_PTR_PAGE) $(VERIFY_BIT_SLOT_CACHE) $(NAMED_MAPPINGS) $(ABORT_ON_NULL) $(NO_ZERO_ALLOCATIONS) \
224227
$(ABORT_NO_ENTROPY) $(ISO_DTOR_CLEANUP) $(SHUFFLE_BIT_SLOT_CACHE) $(USE_SPINLOCK) $(HUGE_PAGES) $(USE_MLOCK) \
@@ -313,6 +316,7 @@ tests: clean library_debug_unit_tests
313316
libc_sanity_tests: clean library_debug_unit_tests
314317
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/memset_sanity.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/memset_sanity $(LDFLAGS)
315318
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/memcpy_sanity.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/memcpy_sanity $(LDFLAGS)
319+
build/memset_sanity ; build/memcpy_sanity
316320

317321
fuzz_test: clean library_debug_unit_tests
318322
@echo "make fuzz_test"
@@ -335,6 +339,9 @@ perf_tests: clean
335339
## compared to the same malloc/free operations
336340
malloc_cmp_test: clean
337341
@echo "make malloc_cmp_test"
342+
ifeq ($(MEMSET_SANITY), -DMEMSET_SANITY=1) || ($(MEMCPY_SANITY), -DMEMCPY_SANITY=1)
343+
$(error "Please unset MEMSET_SANITY/MEMCPY_SANITY before running this test")
344+
endif
338345
$(CC) $(CFLAGS) $(C_SRCS) $(OPTIMIZE) $(EXE_CFLAGS) $(OS_FLAGS) tests/tests.c -o $(BUILD_DIR)/tests
339346
$(CC) $(CFLAGS) $(OPTIMIZE) $(EXE_CFLAGS) $(OS_FLAGS) -DMALLOC_PERF_TEST $(ISO_ALLOC_PRINTF_SRC) tests/tests.c -o $(BUILD_DIR)/malloc_tests
340347
echo "Running IsoAlloc Performance Test"

src/iso_alloc_sanity.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#include "iso_alloc_internal.h"
55

6+
#if MEMCPY_SANITY || MEMSET_SANITY
7+
#define MEM_SANITY_CHK(p, start, sz) (start <= p && (start + ZONE_USER_SIZE) - n > p && n > sz)
8+
#endif
9+
610
#if ALLOC_SANITY
711

812
#if THREAD_SUPPORT
@@ -18,10 +22,6 @@ int32_t _sane_sampled;
1822
uint8_t _sane_cache[SANE_CACHE_SIZE];
1923
_sane_allocation_t _sane_allocations[MAX_SANE_SAMPLES];
2024

21-
#if MEMCPY_SANITY || MEMSET_SANITY
22-
#define MEM_SANITY_CHK(p) (user_pages_start <= p && (user_pages_start + ZONE_USER_SIZE) - n > p && n > zone->chunk_size)
23-
#endif
24-
2525
#if UNINIT_READ_SANITY
2626
pthread_t _page_fault_thread;
2727
struct uffdio_api _uffd_api;
@@ -304,14 +304,14 @@ INTERNAL_HIDDEN void *_iso_alloc_memcpy(void *restrict dest, const void *restric
304304
iso_alloc_zone_t *zone = search_chunk_lookup_table(dest);
305305
void *user_pages_start = UNMASK_USER_PTR(zone);
306306

307-
if(MEM_SANITY_CHK(dest)) {
307+
if(MEM_SANITY_CHK(dest, user_pages_start, zone->chunk_size)) {
308308
LOG_AND_ABORT("Detected an out of bounds write memcpy: dest=0x%p (%d bytes) src=0x%p size=%d", dest, zone->chunk_size, src, n);
309309
}
310310

311311
zone = search_chunk_lookup_table(src);
312312
user_pages_start = UNMASK_USER_PTR(zone);
313313

314-
if(MEM_SANITY_CHK(src)) {
314+
if(MEM_SANITY_CHK(src, user_pages_start, zone->chunk_size)) {
315315
LOG_AND_ABORT("Detected an out of bounds read memcpy: dest=0x%p src=0x%p (%d bytes) size=%d", dest, src, zone->chunk_size, n);
316316
}
317317
}
@@ -337,7 +337,7 @@ INTERNAL_HIDDEN void *_iso_alloc_memset(void *dest, int b, size_t n) {
337337
iso_alloc_zone_t *zone = search_chunk_lookup_table(dest);
338338
void *user_pages_start = UNMASK_USER_PTR(zone);
339339

340-
if(MEM_SANITY_CHK(dest)) {
340+
if(MEM_SANITY_CHK(dest, user_pages_start, zone->chunk_size)) {
341341
LOG_AND_ABORT("Detected an out of bounds write memset: dest=0x%p (%d bytes) size=%d", dest, zone->chunk_size, n);
342342
}
343343
}

0 commit comments

Comments
 (0)