From 195bc8c92a0ab2c9c5f39e40cd0cfe4738e9f855 Mon Sep 17 00:00:00 2001 From: rwarr627 Date: Mon, 8 Jun 2020 19:21:17 -0700 Subject: [PATCH] added tests for malloc_object_size LDFLAGS is on single line --- test/simple-memory-corruption/.gitignore | 2 ++ test/simple-memory-corruption/Makefile | 18 +++++++++++++++++- .../malloc_object_size.c | 11 +++++++++++ .../malloc_object_size_offset.c | 11 +++++++++++ test/simple-memory-corruption/test_smc.py | 7 +++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/simple-memory-corruption/malloc_object_size.c create mode 100644 test/simple-memory-corruption/malloc_object_size_offset.c diff --git a/test/simple-memory-corruption/.gitignore b/test/simple-memory-corruption/.gitignore index 7a9ba96..97915d1 100644 --- a/test/simple-memory-corruption/.gitignore +++ b/test/simple-memory-corruption/.gitignore @@ -26,4 +26,6 @@ write_zero_size unaligned_malloc_usable_size_small invalid_malloc_usable_size_small invalid_malloc_usable_size_small_quarantine +malloc_object_size +malloc_object_size_offset __pycache__/ diff --git a/test/simple-memory-corruption/Makefile b/test/simple-memory-corruption/Makefile index 2fa4daf..49a3015 100644 --- a/test/simple-memory-corruption/Makefile +++ b/test/simple-memory-corruption/Makefile @@ -1,3 +1,17 @@ +dir=$(dir $(realpath $(firstword $(MAKEFILE_LIST)))) + +CONFIG_SLAB_CANARY := true + +ifeq (,$(filter $(CONFIG_SLAB_CANARY),true false)) + $(error CONFIG_SLAB_CANARY must be true or false) +endif + +CFLAGS += -DSLAB_CANARY=$(CONFIG_SLAB_CANARY) + +LDLIBS := -lhardened_malloc + +LDFLAGS := -Wl,-L$(dir)../../,-R,$(dir)../../ + EXECUTABLES := \ double_free_large \ double_free_large_delayed \ @@ -26,7 +40,9 @@ EXECUTABLES := \ delete_type_size_mismatch \ unaligned_malloc_usable_size_small \ invalid_malloc_usable_size_small \ - invalid_malloc_usable_size_small_quarantine + invalid_malloc_usable_size_small_quarantine \ + malloc_object_size \ + malloc_object_size_offset all: $(EXECUTABLES) diff --git a/test/simple-memory-corruption/malloc_object_size.c b/test/simple-memory-corruption/malloc_object_size.c new file mode 100644 index 0000000..630134d --- /dev/null +++ b/test/simple-memory-corruption/malloc_object_size.c @@ -0,0 +1,11 @@ +#include +#include + +size_t malloc_object_size(void *ptr); + +__attribute__((optimize(0))) +int main(void) { + char *p = malloc(16); + size_t size = malloc_object_size(p); + return size != (SLAB_CANARY ? 24 : 32); +} diff --git a/test/simple-memory-corruption/malloc_object_size_offset.c b/test/simple-memory-corruption/malloc_object_size_offset.c new file mode 100644 index 0000000..3d389a0 --- /dev/null +++ b/test/simple-memory-corruption/malloc_object_size_offset.c @@ -0,0 +1,11 @@ +#include +#include + +size_t malloc_object_size(void *ptr); + +__attribute__((optimize(0))) +int main(void) { + char *p = malloc(16); + size_t size = malloc_object_size(p + 5); + return size != (SLAB_CANARY ? 19 : 27); +} diff --git a/test/simple-memory-corruption/test_smc.py b/test/simple-memory-corruption/test_smc.py index 006bdfa..83aa247 100644 --- a/test/simple-memory-corruption/test_smc.py +++ b/test/simple-memory-corruption/test_smc.py @@ -181,6 +181,13 @@ class TestSimpleMemoryCorruption(unittest.TestCase): _stdout, _stderr, returncode = self.run_test("write_zero_size") self.assertEqual(returncode, -11) + def test_malloc_object_size(self): + _stdout, _stderr, returncode = self.run_test("malloc_object_size") + self.assertEqual(returncode, 0) + + def test_malloc_object_size_offset(self): + _stdout, _stderr, returncode = self.run_test("malloc_object_size_offset") + self.assertEqual(returncode, 0) if __name__ == '__main__': unittest.main()