Adding the malloc NULL pointer checker.
Subsystems (2)
Christopher Li
maintainerPATCH 1/1 Adding the malloc NULL pointer checker.
Changelog:
- Using inline annotation.
- Using pseudo user list to find out the calling function. It doesn't scan instruction one by one any more.
This patch add the kmalloc null pointer checking. It also try to track the double free as well. It knows a few kmalloc like functions and kfree etc.
Signed-off-by Christopher Li<[email protected]>
blobhash.c +92 -0
@@ -0,0 +1,92 @@ +/* +* Copyright (C) 2006 Christopher Li <[email protected]> +* Copyright (C) 2003 Transmeta Corp. +* 2003 Linus Torvalds +* +* Licensed under the Open Software License version 1.1 +*/ +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <ctype.h> +#include <unistd.h> +#include <fcntl.h> + +#include "lib.h" +#include "allocate.h" +#include "token.h" +#include "parse.h" +#include "symbol.h" +#include "expression.h" +#include "linearize.h" +#include "storage.h" +#include "checker.h" + +/* +* steal from tokenize.c +*/ +#define BLOB_HASH_BITS (13) +#define BLOB_HASH_SIZE (1<<BLOB_HASH_BITS) +#define BLOB_HASH_MASK (BLOB_HASH_SIZE-1) + +#define blob_hash_init (c) (c) +#define blob_hash_add (oldhash,c) ((oldhash)*11 + (c)) +#define blob_hash_end (hash) ((((hash) >> BLOB_HASH_BITS) + (hash)) & BLOB_HASH_MASK) + +ALLOCATOR (blob,"pseudo state blob" ); +ALLOCATOR (bb_state,"basic block state" ); +ALLOCATOR (state,"one state" ); + + +static struct blob_list *blob_hash_table[BLOB_HASH_SIZE ]; + +static unsigned long blob_hash (const unsigned char *data,int len) +{ +unsigned long hash; +const unsigned char *p = data; + + hash =blob_hash_init (0 ); +while (len--) { +unsigned int i = *p++; + hash =blob_hash_add (hash, i); + } +return blob_hash_end (hash); +} + +struct blob *lookup_blob (const unsigned char *data,int len) +{ +struct blob_list *list = blob_hash_table[blob_hash (data, len)]; +struct blob *blob; + + FOR_EACH_PTR(list, blob) { +if (blob->len == len && !memcmp (blob->data, data, len)) +return blob; + }END_FOR_EACH_PTR (blob); +return NULL ; +} + +struct blob *create_hashed_blob (const unsigned char *data,int len) +{ +struct blob_list **list = blob_hash_table +blob_hash (data, len); +struct blob *blob; + FOR_EACH_PTR(*list, blob) { +if (blob->len == len && !memcmp (blob->data, data, len)) +return blob; + }END_FOR_EACH_PTR (blob); + blob =alloc_blob (len); +memcpy (blob->data, data, len); +add_blob (list, blob); +return blob; +} + +void free_blob (void ) +{ +int i; + +for (i =0 ; i <BLOB_HASH_SIZE ; i++) { +free_ptr_list (blob_hash_table + i); + } +clear_blob_alloc (); +} +
ptrlist.h +2 -0
@@ -45,6 +45,8 @@ extern void concat_ptr_list(struct ptr_lextern void __free_ptr_list (struct ptr_list **);extern int ptr_list_size (struct ptr_list *);extern int linearize_ptr_list (struct ptr_list *,void **,int ); +extern int find_ptr_in_list (struct ptr_list * list,void *ptr); +extern int find_ptr_index (struct ptr_list * list,void *ptr);/* * Hey, who said that you can't do overloading in C?
lib.c +2 -0
@@ -190,6 +190,7 @@ int Waddress_space = 1;int Wenum_mismatch =1 ;int Wdo_while =1 ;int Wuninitialized =1 ; +int Wmalloc =1 ;int dbg_entry =0 ;int dbg_dead =0 ;
@@ -339,6 +340,7 @@ static const struct warning { { "enum-mismatch", &Wenum_mismatch }, { "do-while", &Wdo_while }, { "uninitialized", &Wuninitialized }, + { "malloc", &Wmalloc}, }; enum {
Makefile +5 -2
@@ -28,12 +28,13 @@ INST_PROGRAMS=sparse cgcc LIB_H= token.h parse.h lib.h symbol.h scope.h expression.h target.h \ linearize.h bitmap.h ident-list.h compat.h flow.h allocate.h \ - storage.h ptrlist.h dissect.h + storage.h ptrlist.h dissect.h checker.h LIB_OBJS= target.o parse.o tokenize.o pre-process.o symbol.o lib.o scope.o \ expression.o show-parse.o evaluate.o expand.o inline.o linearize.o \ sort.o allocate.o compat-$(OS).o ptrlist.o \ - flow.o cse.o simplify.o memops.o liveness.o storage.o unssa.o dissect.o + flow.o cse.o simplify.o memops.o liveness.o storage.o unssa.o dissect.o \ + blobhash.o check-nullptr.o LIB_FILE= libsparse.a SLIB_FILE= libsparse.so
@@ -136,6 +137,8 @@ example.o: $(LIB_H) storage.o: $(LIB_H) dissect.o: $(LIB_H) graph.o: $(LIB_H) +blobstate.o: $(LIB_H) +check-nullptr.o: $(LIB_H) compat-linux.o: compat/strtold.c compat/mmap-blob.c \ $(LIB_H)
checker.h +142 -0
142 lines not on this page · raw message
expand.c +2 -2
@@ -29,8 +29,8 @@/* Random cost numbers */ #define SIDE_EFFECTS 10000/* The expression has side effects */ #define UNSAFE 100/* The expression may be "infinitely costly" due to exceptions */ -#define SELECT_COST 20/* Cut-off for turning a conditional into a select */ -#define BRANCH_COST 10/* Cost of a conditional branch */ +#define SELECT_COST 0/* Cut-off for turning a conditional into a select */ +#define BRANCH_COST 0/* Cost of a conditional branch */ static int expand_expression (struct expression *);static int expand_statement (struct statement *);
lib.h +1 -0
@@ -96,6 +96,7 @@ extern int Wshadow;extern int Wcast_truncate;extern int Wdo_while;extern int Wuninitialized; +extern int Wmalloc;extern int dbg_entry;extern int dbg_dead;
sparse.c +7 -0
@@ -23,6 +23,8 @@#include "symbol.h" #include "expression.h" #include "linearize.h" +#include "storage.h" +#include "checker.h" static int context_increase (struct basic_block *bb,int entry) {
@@ -269,6 +271,8 @@ static void check_symbols(struct symbol_show_entry (ep);check_context (ep); +if (Wmalloc) +check_null_ptr (ep); } }END_FOR_EACH_PTR (sym); }
@@ -280,6 +284,9 @@ int main(int argc, char **argv)// Expand, linearize and show it. check_symbols (sparse_initialize (argc, argv, &filelist)); + +check_null_ptr_init (); +check_interrupt_init (); FOR_EACH_PTR_NOTAG(filelist, file) {check_symbols (sparse (file)); }END_FOR_EACH_PTR_NOTAG (file);
linearize.h +9 -2
@@ -30,7 +30,9 @@ enum pseudo_type {struct pseudo {int nr; -enum pseudo_type type; +enum pseudo_type type:8 ; +unsigned state_index:16 ; +unsigned tracking:1 ;struct pseudo_user_list *users;struct ident *ident;union {
@@ -213,14 +215,19 @@ enum opcode {/* Needed to translate SSA back to normal form */ OP_COPY , + OP_LAST, };struct basic_block_list ;struct instruction_list ; +struct bb_state ;struct basic_block {struct position pos; -unsigned long generation; +union { +unsigned long generation; +struct bb_state *state; + };int context;struct entrypoint *ep;struct basic_block_list *parents;/* sources */
check-nullptr.c +266 -0
266 lines not on this page · raw message
ptrlist.c +26 -0
@@ -246,3 +246,29 @@ void __free_ptr_list(struct ptr_list **l *listp =NULL ; } + +int find_ptr_in_list (struct ptr_list * list,void *ptr) +{ +void *p; + FOR_EACH_PTR(list, p) { +if (p == ptr) +return 1 ; + }END_FOR_EACH_PTR (p); +return 0 ; +} + +int find_ptr_index (struct ptr_list * list,void *ptr) +{ +void *p; +int i =0 ; + FOR_EACH_PTR(list, p) { +if (p == ptr) +return i; + i++; + }END_FOR_EACH_PTR (p); +return -1 ; +} + + + +