Skip to contentSkip to conversation

bits_clear and bits_set: Track bits cleared and set inside a function

PATCH In review Harshvardhan Jha 2 participants 2 messages Jul 22, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bits_clear and bits_set: Track bits cleared and set inside a function

Whenever a bit is set or cleared using bitwise operations inside a funciton body, the states are set using database entries.

---
 Makefile                  |  2 +
 check_list.h              |  2 +
 smatch.h                  | 19 ++++++--
 smatch_bits.c             | 67 +++++++++++++++++++++++----
 smatch_param_bits_clear.c | 96 +++++++++++++++++++++++++++++++++++++++
 smatch_param_bits_set.c   | 76 +++++++++++++++++++++++++++++++
 6 files changed, 251 insertions(+), 11 deletions(-)
 create mode 100644 smatch_param_bits_clear.c
 create mode 100644 smatch_param_bits_set.c
Makefile +2 -0
index 8855c974..3169b49e 100644
@@ -290,6 +290,8 @@ SMATCH_OBJS += smatch_mtag_data.o
 SMATCH_OBJS += smatch_mtag_map.o
 SMATCH_OBJS += smatch_mtag.o
 SMATCH_OBJS += smatch_nul_terminator.o
+SMATCH_OBJS += smatch_param_bits_set.o
+SMATCH_OBJS += smatch_param_bits_clear.o
 SMATCH_OBJS += smatch_param_cleared.o
 SMATCH_OBJS += smatch_param_compare_limit.o
 SMATCH_OBJS += smatch_parameter_names.o
check_list.h +2 -0
index fd205269..a57715ea 100644
@@ -169,6 +169,8 @@ CK(check_atomic_test)
 CK(check_checking_for_null_instead_of_err_ptr)
 CK(check_syscall_arg_type)
 CK(check_trinity_generator)
+CK(register_param_bits_set)
+CK(register_param_bits_clear)
 
 /* <- your test goes here */
 /* CK(register_template) */
smatch.h +16 -3
index 882e3927..bf88d887 100644
@@ -72,7 +72,7 @@ struct sm_state {
 	unsigned short owner;
 	unsigned short merged:1;
 	unsigned int line;
-  	struct smatch_state *state;
+	struct smatch_state *state;
 	struct stree *pool;
 	struct sm_state *left;
 	struct sm_state *right;
@@ -302,8 +302,8 @@ do {                                                           \
 		sm_printf("parse error: ");		       \
 		sm_nr_errors++;				       \
 	}						       \
-        sm_printf(msg);                                        \
-        sm_printf("\n");                                       \
+	sm_printf(msg);					       \
+	sm_printf("\n");				       \
 } while (0)
 
 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
@@ -848,6 +848,10 @@ enum info_type {
 	FRESH_ALLOC	= 1044,
 	ALLOCATOR	= 1045,
 	FUNC_TIME	= 1047,
+	BIT_SET		= 1051,
+	BIT_CLEAR	= 1052,
+	BIT_IS_SET	= 1053,
+	BIT_IS_CLEAR	= 1054,
 
 	/* put random temporary stuff in the 7000-7999 range for testing */
 	USER_DATA	= 8017,
@@ -1317,6 +1321,15 @@ struct bit_info *alloc_bit_info(unsigned long long set, unsigned long long possi
 struct smatch_state *alloc_bstate(unsigned long long set, unsigned long long possible);
 struct smatch_state *merge_bstates(struct smatch_state *one_state, struct smatch_state *two_state);
 
+/* smatch_param_bits_set.c */
+void __set_param_modified_helper(struct expression *expr, struct  smatch_state *state);
+void __set_param_modified_helper_sym(const char *name, struct symbol *sym,
+				     struct smatch_state *state);
+
+/* smatch_param_bits_clear.c */
+void __set_param_modified_helper_clear(struct expression *expr, struct smatch_state *state);
+void __set_param_modified_helper_sym_clear(const char *name, struct symbol *sym,
+					   struct smatch_state *state);
 
 /* smatch_bit_info.c */
 struct bit_info *rl_to_binfo(struct range_list *rl);
smatch_bits.c +59 -8

114 lines not on this page · raw message

smatch_param_bits_clear.c new file +96 -0

96 lines not on this page · raw message

smatch_param_bits_set.c new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
2.32.0
#1 lore raw
Quoted 14 lines · Harshvardhan Jha
smatch.h +1 -1
index 882e3927..bf88d887 100644
@@ -72,7 +72,7 @@ struct sm_state {
 	unsigned short owner;
 	unsigned short merged:1;
 	unsigned int line;
-  	struct smatch_state *state;
+	struct smatch_state *state;
 	struct stree *pool;
 	struct sm_state *left;
 	struct sm_state *right;

smatch.h do { \ · line 306

Quoted 7 lines
smatch.h +2 -2
@@ -302,8 +302,8 @@ do {                                                           \
 		sm_printf("parse error: ");		       \
 		sm_nr_errors++;				       \
 	}						       \
-        sm_printf(msg);                                        \
-        sm_printf("\n");                                       \
+	sm_printf(msg);					       \
+	sm_printf("\n");				       \

These changes are correct but unrelated.

Quoted 63 lines
smatch.h +13 -0
 } while (0)
 
 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
@@ -848,6 +848,10 @@ enum info_type {
 	FRESH_ALLOC	= 1044,
 	ALLOCATOR	= 1045,
 	FUNC_TIME	= 1047,
+	BIT_SET		= 1051,
+	BIT_CLEAR	= 1052,
+	BIT_IS_SET	= 1053,
+	BIT_IS_CLEAR	= 1054,
 
 	/* put random temporary stuff in the 7000-7999 range for testing */
 	USER_DATA	= 8017,
@@ -1317,6 +1321,15 @@ struct bit_info *alloc_bit_info(unsigned long long set, unsigned long long possi
 struct smatch_state *alloc_bstate(unsigned long long set, unsigned long long possible);
 struct smatch_state *merge_bstates(struct smatch_state *one_state, struct smatch_state *two_state);
 
+/* smatch_param_bits_set.c */
+void __set_param_modified_helper(struct expression *expr, struct  smatch_state *state);
+void __set_param_modified_helper_sym(const char *name, struct symbol *sym,
+				     struct smatch_state *state);
+
+/* smatch_param_bits_clear.c */
+void __set_param_modified_helper_clear(struct expression *expr, struct smatch_state *state);
+void __set_param_modified_helper_sym_clear(const char *name, struct symbol *sym,
+					   struct smatch_state *state);
 
 /* smatch_bit_info.c */
 struct bit_info *rl_to_binfo(struct range_list *rl);
smatch_bits.c +13 -1
index 2f92ecd7..555e2c14 100644
@@ -41,6 +41,17 @@ struct bit_info *alloc_bit_info(unsigned long long set, unsigned long long possi
 	return bit_info;
 }
 
+void set_bits_modified_expr(struct expression *expr, struct smatch_state *state)
+{
+	__set_param_modified_helper(expr, state);
+	set_state_expr(my_id, expr, state);
+}
+
+void set_bits_modified_expr_sym(const char *name, struct symbol *sym, struct smatch_state *state)
+{
+	__set_param_modified_helper_sym(name, sym, state);
+	set_state(my_id, name, sym, state);
+}
 struct smatch_state *alloc_bstate(unsigned long long set, unsigned long long possible)
 {
 	struct smatch_state *state;
@@ -151,7 +162,8 @@ static void match_modify(struct sm_state *sm, struct expression *mod_expr)
 
 	if (handled_by_assign_hook(mod_expr))
 		return;
-	set_state(my_id, sm->name, sm->sym, alloc_bstate(0, -1ULL));
+
+	set_bits_modified_expr_sym(sm->name, sm->sym, alloc_bstate(0, -1ULL));
 }
 
 int binfo_equiv(struct bit_info *one, struct bit_info *two)

smatch_bits.c struct bit_info *get_bit_info(struct expression *expr) · line 264

Quoted 8 lines
smatch_bits.c +2 -2
@@ -247,9 +259,9 @@ struct bit_info *get_bit_info(struct expression *expr)
 	sval_t known;
 
 	expr = strip_parens(expr);
-
-	if (get_implied_value(expr, &known))
+	if (get_implied_value(expr, &known)) {
 		return alloc_bit_info(known.value, known.value);
+	}

Extra curly braces.

Quoted 12 lines
smatch_bits.c +1 -1
 	if (expr->type == EXPR_BINOP) {
 		if (expr->op == '&')
@@ -334,15 +346,17 @@ static void match_assign(struct expression *expr)
 		if (is_unknown_binfo(get_type(expr->left), binfo))
 			return;
 
-		set_state_expr(my_id, expr->left, alloc_bstate(binfo->set, binfo->possible));
+		set_bits_modified_expr(expr->left, alloc_bstate(binfo->set, binfo->possible));
 	} else if (expr->op == SPECIAL_OR_ASSIGN) {
 		start = get_bit_info(expr->left);
 		new = alloc_bstate(start->set | binfo->set, start->possible | binfo->possible);

smatch_bits.c static void match_assign(struct expression *expr) · line 360

Quoted 10 lines
smatch_bits.c +4 -2
-		set_state_expr(my_id, expr->left, new);
+		set_bits_modified_expr(expr->left, new);
+
 	} else if (expr->op == SPECIAL_AND_ASSIGN) {
 		start = get_bit_info(expr->left);
 		new = alloc_bstate(start->set & binfo->set, start->possible & binfo->possible);
-		set_state_expr(my_id, expr->left, new);
+		set_bits_modified_expr(expr->left, new);
+
 	}

No blank line before a curly brace.

smatch_bits.c static void match_condition(struct expression *expr) · line 372

Quoted 7 lines
smatch_bits.c +0 -1
 }
 
@@ -369,7 +383,6 @@ static void match_condition(struct expression *expr)
 
 	true_info.possible &= right.uvalue;
 	false_info.possible &= ~right.uvalue;
-

Unrelated.

smatch_bits.c static void set_param_bits(const char *name, struct symbol *sym, char *key, char

Quoted 4 lines
smatch_bits.c +0 -0
 	set_true_false_states_expr(my_id, expr->left,
 				   alloc_bstate(true_info.set, true_info.possible),
 				   alloc_bstate(false_info.set, false_info.possible));
@@ -454,7 +467,42 @@ static void set_param_bits(const char *name, struct symbol *sym, char *key, char

Just fix those few small things and resend.

regards,
dan carpenter