extra: Fix false output of handle_AND_op and handle_AND_condition
PATCH 1/2 extra: Fix false output of handle_AND_op and handle_AND_condition
handle_AND_condition and handle_AND_op gave false outputs. This could be seen in the test case in validation/sm_bits1.c the expected output was 0x1 for possible and 0x0 for definitely set. However, in the previous state 0x0 was output for both possibly set and definitely set.
--- smatch_expressions.c | 10 ++++++ smatch_extra.c | 72 +++++++++---------------------------------- validation/sm_bits1.c | 23 ++++++++++++++ 3 files changed, 48 insertions(+), 57 deletions(-) create mode 100644 validation/sm_bits1.c
smatch_expressions.c +10 -0
@@ -61,6 +61,16 @@ struct expression *value_expr(long long val)return expr; } +struct expression *value_expr_sval (sval_t sval) +{ +struct expression *expr; + + expr =alloc_tmp_expression (get_cur_pos (),EXPR_VALUE ); + expr->value = sval.value; + expr->ctype = sval.type; +return expr; +} +struct expression *member_expression (struct expression *deref,int op,struct ident *member) {struct expression *expr;
smatch_extra.c +15 -57
98 lines not on this page · raw message
validation/sm_bits1.c new file +23 -0
@@ -0,0 +1,23 @@ +#include "../check_debug.h" + +unsigned int frob (); + +void test (void ) +{ +unsigned int x =frob (); +if (x & ~1 ) +return ; +__smatch_bits (x); +__smatch_implied (x); +} + + +/* +* check-name: smatch bits 1 +* check-command: smatch sm_bits1.c +* +* check-output-start +sm_bits1.c:10 test() bit info 'x': definitely set 0x0. possibly set 0x1. +sm_bits1.c:11 test() implied: x = '0-1' +* check-output-end +*/
Signature · 1 line
2.32.0
PATCH 2/2 Fix handle_bit_test so that null set condition is taken care of
The handle_bit_test condition wasn't setting a false range_list for the false state and hence the implied rl was coming out to be false. The false and true rls have been calculating using rl_intersection and rl_filter commands.
--- smatch_extra.c | 17 +++++++++-------- validation/sm_bits2.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 validation/sm_bits2.c
smatch_extra.c +9 -8
@@ -2061,7 +2061,7 @@ static void match_comparison(struct expression *expr)static bool handle_bit_test (struct expression *expr) { -struct range_list *orig_rl, *rl; +struct range_list *orig_rl, *rl, *true_rl, *false_rl;struct expression *shift, *mask, *var;struct bit_info *bit_info;sval_t sval;
@@ -2083,23 +2083,24 @@ static bool handle_bit_test(struct expression *expr) bit_info =get_bit_info (mask);if (!bit_info)return false; -if (!bit_info->possible) +if (!bit_info->possible){ +set_true_false_states_expr (my_id, var,alloc_estate_empty (),NULL );return false; + }get_absolute_rl (var, &orig_rl);if (sval_is_negative (rl_min (orig_rl)) ||rl_max (orig_rl).uvalue >type_bits (get_type (shift->left)))return false; - low.value =ffsll (bit_info->possible); - high.value =sm_fls64 (bit_info->possible); + low.value =ffsll (bit_info->possible) -1 ; + high.value =sm_fls64 (bit_info->possible) -1 ; rl =alloc_rl (low, high); rl =cast_rl (get_type (var), rl); - rl =rl_intersection (orig_rl, rl); -if (!rl) -return false; + true_rl =rl_intersection (orig_rl, rl); + false_rl =rl_filter (orig_rl, rl); -set_extra_expr_true_false (shift->right,alloc_estate_rl (rl),NULL ); +set_extra_expr_true_false (shift->right,alloc_estate_rl (true_rl),alloc_estate_rl (false_rl));return true; }
validation/sm_bits2.c new file +28 -0
@@ -0,0 +1,28 @@ +#include "../check_debug.h" + +unsigned int frob (); + +void test (void ) +{ +unsigned int mask =0xff0 ; +unsigned int x =frob (); + +if (x <4 || x >11 ) +return ; + +if ((1 << x) & mask) { +__smatch_implied (x); +return ; + } +__smatch_implied (x); +} + +/* +* check-name: smatch bits 2 +* check-command: smatch sm_bits2.c +* +* check-output-start +sm_bits2.c:14 test() implied: x = '4-11' +sm_bits2.c:17 test() implied: x = '' +* check-output-end +*/
Signature · 1 line
2.32.0
Re: [PATCH 2/2] Fix handle_bit_test so that null set condition is taken care of
Quoted 5 lines · Harshvardhan Jha
The
handle_bit_testcondition wasn't setting a falserange_listfor the false state and hence the implied rl was coming out to be false. The false and true rls have been calculating usingrl_intersectionandrl_filtercommands.
Can you fix the commit message a bit. Assume that the person reading your patch understands Smatch basically but doesn't remember all the details. I like to give a little background. Here is my proposed commit message:
Imagine we have code like:
if ((1 << foo) & valid_bits) { The handle_bit_test() function is supposed to answer the question, "what does that mean about 'foo'"?
This patch fixes several bugs:
1) The range is off by one. ffsll() returns 1 for BIT(0) so we need to subtract 1 from what ffsll() returns.
2) This code returns early for impossible conditions but it is better to set "foo" to the empty state.
3) The false state was not set. We can use rl_filter() for that.
regards,
dan carpenter
Quoted 10 lines · Harshvardhan Jha
handle_AND_conditionandhandle_AND_opgave false outputs. This could be seen in the test case invalidation/sm_bits1.cthe expected output was0x1for possible and0x0for definitely set. However, in the previous state0x0was output for both possibly set and definitely set.Signed-off-by Harshvardhan Jha <[email protected]>--- smatch_expressions.c | 10 ++++++ smatch_extra.c | 72 +++++++++---------------------------------- validation/sm_bits1.c | 23 ++++++++++++++
We need the smatch_extra.h part as well.
regards,
dan carpenter