From c512b2f054ce14321c8f6fae66696f880a74bbf1 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 22 Mar 2017 08:50:09 +0000 Subject: [PATCH] Add heap fault injection generator into tests This builds a dynamic library which can cause heap allocation (malloc) faliures after a specified number of calls. This is useful to allow tesing of memory failure allocation paths within netsurf test suites. An example test for core strings test has been added which incrementaly fails alloctions allowing all error paths in initialisation to be exercised. --- test/Makefile | 89 ++++++++++++++++++++++++-------------------- test/corestrings.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++ test/malloc_fig.c | 58 +++++++++++++++++++++++++++++ test/malloc_fig.h | 34 +++++++++++++++++ 4 files changed, 233 insertions(+), 41 deletions(-) create mode 100644 test/corestrings.c create mode 100644 test/malloc_fig.c create mode 100644 test/malloc_fig.h diff --git a/test/Makefile b/test/Makefile index 65d74a8ce..833e7d68e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -11,33 +11,32 @@ TESTS := \ utils \ messages \ time \ - mimesniff #llcache + mimesniff \ + corestrings #llcache + +# sources necessary to use nsurl functionality +NSURL_SOURCES := utils/nsurl/nsurl.c utils/nsurl/parse.c utils/idna.c \ + utils/punycode.c # nsurl sources -nsurl_SRCS := utils/corestrings.c utils/nsurl/nsurl.c \ - utils/nsurl/parse.c \ - utils/idna.c utils/punycode.c \ - test/log.c test/nsurl.c +nsurl_SRCS := $(NSURL_SOURCES) utils/corestrings.c test/log.c test/nsurl.c # url database test sources -urldbtest_SRCS := content/urldb.c \ - utils/idna.c utils/bloom.c utils/nsoption.c \ - utils/nsurl/nsurl.c utils/nsurl/parse.c \ - utils/corestrings.c utils/punycode.c \ - utils/hashtable.c utils/messages.c utils/time.c utils/utils.c \ - test/log.c test/urldbtest.c +urldbtest_SRCS := $(NSURL_SOURCES) \ + utils/bloom.c utils/nsoption.c utils/corestrings.c utils/time.c \ + utils/hashtable.c utils/messages.c utils/utils.c \ + content/urldb.c \ + test/log.c test/urldbtest.c # low level cache sources llcache_SRCS := content/fetch.c content/fetchers/curl.c \ - content/fetchers/about.c content/fetchers/data.c \ - content/fetchers/resource.c content/llcache.c \ - content/urldb.c \ - image/image_cache.c \ - utils/base64.c utils/corestrings.c utils/hashtable.c \ - utils/nsurl/nsurl.c utils/nsurl/parse.c \ - utils/messages.c utils/url.c utils/useragent.c \ - utils/utils.c \ - test/log.c test/llcache.c + content/fetchers/about.c content/fetchers/data.c \ + content/fetchers/resource.c content/llcache.c \ + content/urldb.c \ + image/image_cache.c \ + $(NSURL_SOURCES) utils/base64.c utils/corestrings.c utils/hashtable.c \ + utils/messages.c utils/url.c utils/useragent.c utils/utils.c \ + test/log.c test/llcache.c # messages test sources messages_SRCS := utils/messages.c utils/hashtable.c test/log.c test/messages.c @@ -55,22 +54,26 @@ hashtable_SRCS := utils/hashtable.c test/log.c test/hashtable.c urlescape_SRCS := utils/url.c test/log.c test/urlescape.c # utility test sources -utils_SRCS := utils/utils.c utils/messages.c utils/hashtable.c \ - utils/corestrings.c utils/nsurl/nsurl.c \ - utils/nsurl/parse.c utils/idna.c utils/punycode.c \ - test/log.c test/utils.c +utils_SRCS := $(NSURL_SOURCES) utils/utils.c utils/messages.c \ + utils/hashtable.c utils/corestrings.c \ + test/log.c test/utils.c # time test sources time_SRCS := utils/time.c test/log.c test/time.c # mimesniff test sources -mimesniff_SRCS := utils/hashtable.c utils/nsurl/nsurl.c utils/hashtable.c \ - utils/nsurl/parse.c utils/corestrings.c utils/idna.c utils/punycode.c \ +mimesniff_SRCS := $(NSURL_SOURCES) utils/hashtable.c utils/corestrings.c \ utils/http/generics.c utils/http/content-type.c \ utils/http/primitives.c utils/messages.c utils/http/parameter.c \ content/mimesniff.c \ test/log.c test/mimesniff.c +# corestrings test sources +corestrings_SRCS := $(NSURL_SOURCES) utils/corestrings.c \ + test/log.c test/corestrings.c +corestrings_LD := -lmalloc_fig + + # Coverage builds need additional flags COV_ROOT := build/$(HOST)-coverage ifeq ($(MAKECMDGOALS),coverage) @@ -121,20 +124,24 @@ ifneq ($(CC_MAJOR),2) endif TESTCFLAGS := -std=c99 -g \ - $(COMMON_WARNFLAGS) \ - -D_BSD_SOURCE \ - -D_POSIX_C_SOURCE=200809L \ - -D_XOPEN_SOURCE=600 \ - -Itest -Iinclude -Icontent/handlers -Ifrontends -I. -I.. \ - -Dnsgtk \ - $(shell pkg-config --cflags libcurl libparserutils libwapcaplet libdom libnsutils libutf8proc) \ - $(LIB_CFLAGS) \ - $(COV_CFLAGS) + $(COMMON_WARNFLAGS) \ + -D_BSD_SOURCE \ + -D_POSIX_C_SOURCE=200809L \ + -D_XOPEN_SOURCE=600 \ + -Itest -Iinclude -Icontent/handlers -Ifrontends -I. -I.. \ + -Dnsgtk \ + $(shell pkg-config --cflags libcurl libparserutils libwapcaplet libdom libnsutils libutf8proc) \ + $(LIB_CFLAGS) \ + $(COV_CFLAGS) -TESTLDFLAGS := $(shell pkg-config --libs libcurl libparserutils libwapcaplet libdom libnsutils libutf8proc) -lz \ - $(LIB_LDFLAGS)\ - $(COV_LDFLAGS) +TESTLDFLAGS := -L$(TESTROOT) \ + $(shell pkg-config --libs libcurl libparserutils libwapcaplet libdom libnsutils libutf8proc) -lz \ + $(LIB_LDFLAGS)\ + $(COV_LDFLAGS) +# malloc faliure injection generator +$(TESTROOT)/libmalloc_fig.so:test/malloc_fig.c + $(CC) -shared -fPIC -I. -std=c99 $(COMMON_WARNFLAGS) $^ -o $@ # Source files for all tests being compiled TESTSOURCES := @@ -144,13 +151,13 @@ GCOV ?= gcov define gen_test_target $$(TESTROOT)/$(1): $$(sort $$(addprefix $$(TESTROOT)/,$$(subst /,_,$$(patsubst %.c,%.o,$$(patsubst %.cpp,%.o,$$(patsubst %.m,%.o,$$(patsubst %.s,%.o,$$($(1)_SRCS)))))))) $$(VQ)echo "LINKTEST: $$@" - $$(Q)$$(CC) $$(TESTCFLAGS) $$^ -o $$@ $$(TESTLDFLAGS) + $$(Q)$$(CC) $$(TESTCFLAGS) $$^ -o $$@ $$($(1)_LD) $$(TESTLDFLAGS) .PHONY:$(1)_test $(1)_test:$$(TESTROOT)/$(1) $$(VQ)echo "RUN TEST: $(1)" - $$(Q)$$(TESTROOT)/$(1) + $$(Q)LD_LIBRARY_PATH=$$(TESTROOT)/ $$(TESTROOT)/$(1) TESTSOURCES += $$($(1)_SRCS) @@ -174,7 +181,7 @@ $(eval $(foreach SOURCE,$(sort $(filter %.c,$(TESTSOURCES))), \ .PHONY:test coverage -test: $(TESTROOT)/created $(addsuffix _test,$(TESTS)) +test: $(TESTROOT)/created $(TESTROOT)/libmalloc_fig.so $(addsuffix _test,$(TESTS)) coverage: test diff --git a/test/corestrings.c b/test/corestrings.c new file mode 100644 index 000000000..e8f68c0a6 --- /dev/null +++ b/test/corestrings.c @@ -0,0 +1,93 @@ +/* + * Copyright 2016 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file + * Tests for corestrings. + */ + +#include "utils/config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "utils/corestrings.h" + +#include "test/malloc_fig.h" + +START_TEST(corestrings_test) +{ + nserror ires; + nserror res; + + malloc_limit(_i); + + ires = corestrings_init(); + res = corestrings_fini(); + + malloc_limit(UINT_MAX); + + ck_assert_int_eq(ires, NSERROR_NOMEM); + ck_assert_int_eq(res, NSERROR_OK); +} +END_TEST + + +static TCase *corestrings_case_create(void) +{ + TCase *tc; + tc = tcase_create("corestrings"); + + tcase_add_loop_test(tc, corestrings_test, 0, 437); + + return tc; +} + + +/* + * corestrings test suite creation + */ +static Suite *corestrings_suite_create(void) +{ + Suite *s; + s = suite_create("Corestrings API"); + + suite_add_tcase(s, corestrings_case_create()); + + return s; +} + +int main(int argc, char **argv) +{ + int number_failed; + SRunner *sr; + + sr = srunner_create(corestrings_suite_create()); + + srunner_run_all(sr, CK_ENV); + + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/test/malloc_fig.c b/test/malloc_fig.c new file mode 100644 index 000000000..19ff91e76 --- /dev/null +++ b/test/malloc_fig.c @@ -0,0 +1,58 @@ +/* + * Copyright 2017 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file + * + * heap fault injection generation. + * + * This library inject allocation faults into NetSurf tests + */ + +#define _GNU_SOURCE +#include +#include +#include +#include + +#include "test/malloc_fig.h" + +static unsigned int count = UINT_MAX; + +void malloc_limit(unsigned int newcount) +{ + count = newcount; + //fprintf(stderr, "malloc_limit %d\n", count); +} + +void* malloc(size_t size) +{ + static void* (*real_malloc)(size_t) = NULL; + void *p = NULL; + + if (real_malloc == NULL) { + real_malloc = dlsym(RTLD_NEXT, "malloc"); + } + + if (count > 0) { + p = real_malloc(size); + count--; + } + //fprintf(stderr, "malloc(%d) = %p remian:%d\n", size, p, count); + return p; +} diff --git a/test/malloc_fig.h b/test/malloc_fig.h new file mode 100644 index 000000000..3871f62a8 --- /dev/null +++ b/test/malloc_fig.h @@ -0,0 +1,34 @@ +/* + * Copyright 2017 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file + * + * Interface to heap fault injection generation. + * + * This library is used to inject allocation faults into NetSurf tests + */ + +#ifndef NETSURF_TEST_MALLOC_FIG_H +#define NETSURF_TEST_MALLOC_FIG_H + +void malloc_limit(unsigned int count); + +void* malloc(size_t size); + +#endif