diff options
author | Eric Andersen <andersen@codepoet.org> | 2002-08-14 02:38:04 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2002-08-14 02:38:04 +0000 |
commit | 8c2c5e549eec9eec63c85e2d21a53a71a369e557 (patch) | |
tree | a6f09ae74a425d493fbab0d4e453da7db956c9ab /test/malloc | |
parent | 8966b4965d7d6ae29d957950d064382c79669246 (diff) | |
download | uClibc-alpine-8c2c5e549eec9eec63c85e2d21a53a71a369e557.tar.bz2 uClibc-alpine-8c2c5e549eec9eec63c85e2d21a53a71a369e557.tar.xz |
Move all malloc tests under test/malloc/
Diffstat (limited to 'test/malloc')
-rw-r--r-- | test/malloc/.cvsignore | 7 | ||||
-rw-r--r-- | test/malloc/Makefile | 62 | ||||
-rw-r--r-- | test/malloc/malloc.c | 1 | ||||
-rw-r--r-- | test/malloc/mallocbug.c | 67 | ||||
-rw-r--r-- | test/malloc/testmalloc.c | 98 |
5 files changed, 235 insertions, 0 deletions
diff --git a/test/malloc/.cvsignore b/test/malloc/.cvsignore new file mode 100644 index 000000000..3a3bea67b --- /dev/null +++ b/test/malloc/.cvsignore @@ -0,0 +1,7 @@ +malloc +testmalloc +testmalloc.o +testmalloc_glibc +testmalloc_glibc.o +mallocbug +mallocbug_glibc diff --git a/test/malloc/Makefile b/test/malloc/Makefile index b1f61cdc9..aaa2976d6 100644 --- a/test/malloc/Makefile +++ b/test/malloc/Makefile @@ -21,6 +21,8 @@ include $(TESTDIR)/Rules.mak TARGETS=malloc +TARGETS+=testmalloc testmalloc_glibc +TARGETS+=mallocbug mallocbug_glibc all: $(TARGETS) malloc: malloc.c Makefile $(TESTDIR)/Config $(TESTDIR)/Rules.mak $(CC) @@ -34,6 +36,66 @@ malloc: malloc.c Makefile $(TESTDIR)/Config $(TESTDIR)/Rules.mak $(CC) -./$@ -@ echo " " +testmalloc_source: + -@ echo "-------" + -@ echo "testmalloc.c source: " + -@ echo " " + -@ cat testmalloc.c + -@ echo " " + +testmalloc: testmalloc.c Makefile $(TESTDIR)/Config $(TESTDIR)/Rules.mak $(CC) + -@ echo "-------" + -@ echo " " + -@ echo "Compiling vs uClibc: " + -@ echo " " + $(CC) $(CFLAGS) -c $< -o $@.o + $(CC) $(LDFLAGS) $@.o -o $@ $(EXTRA_LIBS) + $(STRIPTOOL) -x -R .note -R .comment $@ + -$(LDD) $@ + ls -l $@ + -./$@ + -@ echo " " + +testmalloc_glibc: testmalloc.c Makefile + -@ echo "-------" + -@ echo " " + -@ echo "Compiling vs GNU libc: " + -@ echo " " + $(HOST_CC) $(GLIBC_CFLAGS) -c $< -o $@.o + $(HOST_CC) $(GLIBC_LDFLAGS) $@.o -o $@ + $(STRIPTOOL) -x -R .note -R .comment $@ + -$(LDD) $@ + ls -l $@ + -./$@ + -@ echo " " + +mallocbug: mallocbug.c Makefile $(TESTDIR)/Config $(TESTDIR)/Rules.mak $(CC) + -@ echo "-------" + -@ echo " " + -@ echo "Compiling vs uClibc: " + -@ echo " " + $(CC) $(CFLAGS) -c $< -o $@.o + $(CC) $(LDFLAGS) $@.o -o $@ $(EXTRA_LIBS) + $(STRIPTOOL) -x -R .note -R .comment $@ + -$(LDD) $@ + ls -l $@ + -./$@ + -@ echo " " + +mallocbug_glibc: mallocbug.c Makefile + -@ echo "-------" + -@ echo " " + -@ echo "Compiling vs GNU libc: " + -@ echo " " + $(HOST_CC) $(GLIBC_CFLAGS) -c $< -o $@.o + $(HOST_CC) $(GLIBC_LDFLAGS) $@.o -o $@ + $(STRIPTOOL) -x -R .note -R .comment $@ + -$(LDD) $@ + ls -l $@ + -./$@ + -@ echo " " + + clean: rm -f *.[oa] *~ core $(TARGETS) diff --git a/test/malloc/malloc.c b/test/malloc/malloc.c index ac40ec6a2..8e93cf205 100644 --- a/test/malloc/malloc.c +++ b/test/malloc/malloc.c @@ -17,6 +17,7 @@ int main(int argc,char *argv[]) { test1(); test2(); + return 0; } void test1(void) diff --git a/test/malloc/mallocbug.c b/test/malloc/mallocbug.c new file mode 100644 index 000000000..84a638795 --- /dev/null +++ b/test/malloc/mallocbug.c @@ -0,0 +1,67 @@ +/* Reproduce a GNU malloc bug. */ +#include <malloc.h> +#include <stdio.h> +#include <string.h> + +#define size_t unsigned int + +int +main (int argc, char *argv[]) +{ + char *dummy0; + char *dummy1; + char *fill_info_table1; + char *over_top; + size_t over_top_size = 0x3000; + char *over_top_dup; + size_t over_top_dup_size = 0x7000; + char *x; + size_t i; + + /* Here's what memory is supposed to look like (hex): + size contents + 3000 original_info_table, later fill_info_table1 + 3fa000 dummy0 + 3fa000 dummy1 + 6000 info_table_2 + 3000 over_top + + */ + /* mem: original_info_table */ + dummy0 = malloc (0x3fa000); + /* mem: original_info_table, dummy0 */ + dummy1 = malloc (0x3fa000); + /* mem: free, dummy0, dummy1, info_table_2 */ + fill_info_table1 = malloc (0x3000); + /* mem: fill_info_table1, dummy0, dummy1, info_table_2 */ + + x = malloc (0x1000); + free (x); + /* mem: fill_info_table1, dummy0, dummy1, info_table_2, freexx */ + + /* This is what loses; info_table_2 and freexx get combined unbeknownst + to mmalloc, and mmalloc puts over_top in a section of memory which + is on the free list as part of another block (where info_table_2 had + been). */ + over_top = malloc (over_top_size); + over_top_dup = malloc (over_top_dup_size); + memset (over_top, 0, over_top_size); + memset (over_top_dup, 1, over_top_dup_size); + + for (i = 0; i < over_top_size; ++i) + if (over_top[i] != 0) + { + printf ("FAIL: malloc expands info table\n"); + return 0; + } + + for (i = 0; i < over_top_dup_size; ++i) + if (over_top_dup[i] != 1) + { + printf ("FAIL: malloc expands info table\n"); + return 0; + } + + printf ("PASS: malloc expands info table\n"); + return 0; +} diff --git a/test/malloc/testmalloc.c b/test/malloc/testmalloc.c new file mode 100644 index 000000000..158bf4236 --- /dev/null +++ b/test/malloc/testmalloc.c @@ -0,0 +1,98 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +struct list { + struct list *next; +}; + +int main(void) +{ + int z=999; + int *y=&z; + int *x=NULL; + struct list *save; + struct list *lp; + int i; + + + printf("pointer to x is %p\n", x); + printf("pointer to y is %p\n", y); + x=malloc(sizeof(int)*2000); + printf("pointer to x is %p\n", x); + y=malloc(sizeof(int)*100); + printf("pointer to y is %p\n", y); + free(x); + free(y); + printf("about to free(0)\n"); + free(0); + + x=malloc(13); + printf("x = %p\n", x); + memcpy(x, "Small string", 13); + printf("0x%p test string1: %s\n", x, (char *)x); + y = realloc(x, 36); + printf("0x%p test string1: %s\n", y, (char *)y); + memcpy(y, "********** Larger string **********", 36); + printf("0x%p test string2: %s\n", y, (char *)y); + free(y); + + + printf("Allocate 100 nodes 500 bytes each\n"); + save = 0; + for (i=0; i<100; i++) { + lp = malloc(500); + if (lp == 0) { + printf("loop 1: malloc returned 0\n"); + goto Failed; + } + lp->next = save; + save = lp; + } + + printf("freeing 100 nodes\n"); + while (save) { + lp = save; + save = save->next; + free(lp); + } + + printf("try realloc 100 times \n"); + lp = 0; + for (i=1; i<=100; i++) { + lp = realloc(lp, i*200); + if (lp == 0) { + printf("loop 3: realloc returned 0\n"); + goto Failed; + } + } + realloc(lp, 0); + + printf("Allocate another 100 nodes 600 bytes each\n"); + save = 0; + for (i=0; i<100; i++) { + lp = malloc(600); + if (lp == 0) { + printf("loop 2: malloc returned 0\n"); + goto Failed; + } + lp->next = save; + save = lp; + } + + printf("freeing 100 nodes\n"); + while (save) { + lp = save; + save = save->next; + free(lp); + } + + + printf("alloc test PASSED\n"); + exit(0); + +Failed: + printf("!!!!!!!!!!!! alloc test FAILED. !!!!!!!!!!!!!!!\n"); + exit(1); +} |