Jelajahi Sumber

Random quick-and-dirty evil malloc checker.

David Schleef 23 tahun lalu
induk
melakukan
6072c7f7ea
2 mengubah file dengan 65 tambahan dan 0 penghapusan
  1. 22 0
      test/malloc/Makefile
  2. 43 0
      test/malloc/malloc.c

+ 22 - 0
test/malloc/Makefile

@@ -0,0 +1,22 @@
+TESTDIR=../
+include $(TESTDIR)/Rules.mak
+
+
+TARGETS=malloc
+all: $(TARGETS)
+
+malloc: malloc.c Makefile $(TESTDIR)/Config $(TESTDIR)/Rules.mak $(TESTCC)
+	-@ echo "-------"
+	-@ echo " "
+	-@ echo "Compiling vs uClibc: "
+	-@ echo " "
+	$(TESTCC) $(CFLAGS) -c $< -o $@.o
+	$(TESTCC) $(LDFLAGS) $@.o -o $@ $(EXTRA_LIBS)
+	$(STRIPTOOL) -x -R .note -R .comment $@
+	-./$@
+	-@ echo " "
+
+clean:
+	rm -f *.[oa] *~ core $(TARGETS)
+
+

+ 43 - 0
test/malloc/malloc.c

@@ -0,0 +1,43 @@
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#define N_PTRS 1000
+#define N_ALLOCS 10000
+#define MAX_SIZE 0x10000
+
+#define random_size()	(random()%MAX_SIZE)
+#define random_ptr()	(random()%N_PTRS)
+
+int main(int argc,char *argv[])
+{
+	void **ptrs;
+	int i,j;
+	int size;
+
+	srandom(0x19730929);
+
+	ptrs = malloc(N_PTRS*sizeof(void *));
+
+	for(i=0;i<N_PTRS;i++){
+		ptrs[i]=malloc(random_size());
+	}
+	for(i=0;i<N_ALLOCS;i++){
+		j=random_ptr();
+		free(ptrs[j]);
+
+		size=random_size();
+		ptrs[j]=malloc(size);
+		if(!ptrs[j]){
+			printf("malloc failed! %d\n",i);
+		}
+		memset(ptrs[j],0,size);
+	}
+	for(i=0;i<N_PTRS;i++){
+		free(ptrs[i]);
+	}
+
+	return 0;
+}
+