DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH] librte_acl: fix undefined behavior
@ 2019-07-30 21:39 Aaron Conole
  2019-07-31  8:16 ` Ananyev, Konstantin
  2019-07-31 15:43 ` [dpdk-dev] [PATCH v2] " Aaron Conole
  0 siblings, 2 replies; 6+ messages in thread
From: Aaron Conole @ 2019-07-30 21:39 UTC (permalink / raw)
  To: dev; +Cc: Konstantin Ananyev, Pablo de Lara Guarch

Left-shift of an integer constant is represented as 'int' type, but a left
shift of 1 by 31 bits in 'int' is undefined.  Use the U suffix to force
a representation as unsigned.

Caught while running with ubsan under gcc.

Fixes: dc276b5780c2 ("acl: new library")
Cc: Konstantin Ananyev <konstantin.ananyev@intel.com>
Signed-off-by: Aaron Conole <aconole@redhat.com>
---
I could have changed the sizeof(bits_t) * 8 in the bitset.bits as well during
the cleanup, but chose not to to keep the change minimal.

 lib/librte_acl/acl_bld.c | 6 +++---
 lib/librte_acl/acl_gen.c | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/librte_acl/acl_bld.c b/lib/librte_acl/acl_bld.c
index b82191f42..9d27c0a5a 100644
--- a/lib/librte_acl/acl_bld.c
+++ b/lib/librte_acl/acl_bld.c
@@ -320,7 +320,7 @@ acl_add_ptr_range(struct acl_build_context *context,
 	for (n = 0; n < UINT8_MAX + 1; n++)
 		if (n >= low && n <= high)
 			bitset.bits[n / (sizeof(bits_t) * 8)] |=
-				1 << (n % (sizeof(bits_t) * 8));
+				1U << (n % (sizeof(bits_t) * CHAR_BIT));
 
 	return acl_add_ptr(context, root, node, &bitset);
 }
@@ -343,7 +343,7 @@ acl_gen_mask(struct rte_acl_bitset *bitset, uint32_t value, uint32_t mask)
 		if ((n & mask) == value) {
 			range++;
 			bitset->bits[n / (sizeof(bits_t) * 8)] |=
-				1 << (n % (sizeof(bits_t) * 8));
+				1U << (n % (sizeof(bits_t) * CHAR_BIT));
 		}
 	}
 	return range;
@@ -972,7 +972,7 @@ build_trie(struct acl_build_context *context, struct rte_acl_build_rule *head,
 				sizeof(*end->mrt));
 
 		for (m = context->cfg.num_categories; 0 != m--; ) {
-			if (rule->f->data.category_mask & (1 << m)) {
+			if (rule->f->data.category_mask & (1U << m)) {
 				end->mrt->results[m] = rule->f->data.userdata;
 				end->mrt->priority[m] = rule->f->data.priority;
 			} else {
diff --git a/lib/librte_acl/acl_gen.c b/lib/librte_acl/acl_gen.c
index 35a0140b4..81dec3aa6 100644
--- a/lib/librte_acl/acl_gen.c
+++ b/lib/librte_acl/acl_gen.c
@@ -133,7 +133,7 @@ acl_node_fill_dfa(const struct rte_acl_node *node,
 		for (n = 0; n < RTE_ACL_DFA_SIZE; n++) {
 
 			if (bits->bits[n / (sizeof(bits_t) * CHAR_BIT)] &
-				(1 << (n % (sizeof(bits_t) * CHAR_BIT)))) {
+				(1U << (n % (sizeof(bits_t) * CHAR_BIT)))) {
 
 				dfa[n] = resolved ? child->node_index : x;
 				ranges += (last_bit == 0);
@@ -175,7 +175,7 @@ acl_count_sequential_groups(struct rte_acl_bitset *bits, int zero_one)
 	}
 	for (n = 0; n < QRANGE_MIN; n++) {
 		if (bits->bits[n / (sizeof(bits_t) * 8)] &
-				(1 << (n % (sizeof(bits_t) * 8)))) {
+				(1U << (n % (sizeof(bits_t) * 8)))) {
 			if (zero_one == 1 && last_bit != 1)
 				ranges++;
 			last_bit = 1;
-- 
2.21.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] librte_acl: fix undefined behavior
  2019-07-30 21:39 [dpdk-dev] [PATCH] librte_acl: fix undefined behavior Aaron Conole
@ 2019-07-31  8:16 ` Ananyev, Konstantin
  2019-07-31 13:06   ` Aaron Conole
  2019-07-31 15:43 ` [dpdk-dev] [PATCH v2] " Aaron Conole
  1 sibling, 1 reply; 6+ messages in thread
From: Ananyev, Konstantin @ 2019-07-31  8:16 UTC (permalink / raw)
  To: Aaron Conole, dev; +Cc: De Lara Guarch, Pablo

Hi Aaron,

> 
> Left-shift of an integer constant is represented as 'int' type, but a left
> shift of 1 by 31 bits in 'int' is undefined.  Use the U suffix to force
> a representation as unsigned.
> 
> Caught while running with ubsan under gcc.
> 
> Fixes: dc276b5780c2 ("acl: new library")
> Cc: Konstantin Ananyev <konstantin.ananyev@intel.com>
> Signed-off-by: Aaron Conole <aconole@redhat.com>
> ---
> I could have changed the sizeof(bits_t) * 8 in the bitset.bits as well during
> the cleanup, but chose not to to keep the change minimal.

But it seems that you did change it in some places:
 
> -				1 << (n % (sizeof(bits_t) * 8));
> +				1U << (n % (sizeof(bits_t) * CHAR_BIT));

While in others kept unchanged:
> -				(1 << (n % (sizeof(bits_t) * 8)))) {
> +				(1U << (n % (sizeof(bits_t) * 8)))) {

Was that intended?
Konstantin

> 
>  lib/librte_acl/acl_bld.c | 6 +++---
>  lib/librte_acl/acl_gen.c | 4 ++--
>  2 files changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/lib/librte_acl/acl_bld.c b/lib/librte_acl/acl_bld.c
> index b82191f42..9d27c0a5a 100644
> --- a/lib/librte_acl/acl_bld.c
> +++ b/lib/librte_acl/acl_bld.c
> @@ -320,7 +320,7 @@ acl_add_ptr_range(struct acl_build_context *context,
>  	for (n = 0; n < UINT8_MAX + 1; n++)
>  		if (n >= low && n <= high)
>  			bitset.bits[n / (sizeof(bits_t) * 8)] |=
> -				1 << (n % (sizeof(bits_t) * 8));
> +				1U << (n % (sizeof(bits_t) * CHAR_BIT));
> 
>  	return acl_add_ptr(context, root, node, &bitset);
>  }
> @@ -343,7 +343,7 @@ acl_gen_mask(struct rte_acl_bitset *bitset, uint32_t value, uint32_t mask)
>  		if ((n & mask) == value) {
>  			range++;
>  			bitset->bits[n / (sizeof(bits_t) * 8)] |=
> -				1 << (n % (sizeof(bits_t) * 8));
> +				1U << (n % (sizeof(bits_t) * CHAR_BIT));
>  		}
>  	}
>  	return range;
> @@ -972,7 +972,7 @@ build_trie(struct acl_build_context *context, struct rte_acl_build_rule *head,
>  				sizeof(*end->mrt));
> 
>  		for (m = context->cfg.num_categories; 0 != m--; ) {
> -			if (rule->f->data.category_mask & (1 << m)) {
> +			if (rule->f->data.category_mask & (1U << m)) {
>  				end->mrt->results[m] = rule->f->data.userdata;
>  				end->mrt->priority[m] = rule->f->data.priority;
>  			} else {
> diff --git a/lib/librte_acl/acl_gen.c b/lib/librte_acl/acl_gen.c
> index 35a0140b4..81dec3aa6 100644
> --- a/lib/librte_acl/acl_gen.c
> +++ b/lib/librte_acl/acl_gen.c
> @@ -133,7 +133,7 @@ acl_node_fill_dfa(const struct rte_acl_node *node,
>  		for (n = 0; n < RTE_ACL_DFA_SIZE; n++) {
> 
>  			if (bits->bits[n / (sizeof(bits_t) * CHAR_BIT)] &
> -				(1 << (n % (sizeof(bits_t) * CHAR_BIT)))) {
> +				(1U << (n % (sizeof(bits_t) * CHAR_BIT)))) {
> 
>  				dfa[n] = resolved ? child->node_index : x;
>  				ranges += (last_bit == 0);
> @@ -175,7 +175,7 @@ acl_count_sequential_groups(struct rte_acl_bitset *bits, int zero_one)
>  	}
>  	for (n = 0; n < QRANGE_MIN; n++) {
>  		if (bits->bits[n / (sizeof(bits_t) * 8)] &
> -				(1 << (n % (sizeof(bits_t) * 8)))) {
> +				(1U << (n % (sizeof(bits_t) * 8)))) {
>  			if (zero_one == 1 && last_bit != 1)
>  				ranges++;
>  			last_bit = 1;
> --



> 2.21.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] librte_acl: fix undefined behavior
  2019-07-31  8:16 ` Ananyev, Konstantin
@ 2019-07-31 13:06   ` Aaron Conole
  0 siblings, 0 replies; 6+ messages in thread
From: Aaron Conole @ 2019-07-31 13:06 UTC (permalink / raw)
  To: Ananyev, Konstantin; +Cc: dev, De Lara Guarch, Pablo

"Ananyev, Konstantin" <konstantin.ananyev@intel.com> writes:

> Hi Aaron,
>
>> 
>> Left-shift of an integer constant is represented as 'int' type, but a left
>> shift of 1 by 31 bits in 'int' is undefined.  Use the U suffix to force
>> a representation as unsigned.
>> 
>> Caught while running with ubsan under gcc.
>> 
>> Fixes: dc276b5780c2 ("acl: new library")
>> Cc: Konstantin Ananyev <konstantin.ananyev@intel.com>
>> Signed-off-by: Aaron Conole <aconole@redhat.com>
>> ---
>> I could have changed the sizeof(bits_t) * 8 in the bitset.bits as well during
>> the cleanup, but chose not to to keep the change minimal.
>
> But it seems that you did change it in some places:
>  
>> -				1 << (n % (sizeof(bits_t) * 8));
>> +				1U << (n % (sizeof(bits_t) * CHAR_BIT));
>
> While in others kept unchanged:
>> -				(1 << (n % (sizeof(bits_t) * 8)))) {
>> +				(1U << (n % (sizeof(bits_t) * 8)))) {
>
> Was that intended?

Oops - nope.  I'll respin.

> Konstantin
>
>> 
>>  lib/librte_acl/acl_bld.c | 6 +++---
>>  lib/librte_acl/acl_gen.c | 4 ++--
>>  2 files changed, 5 insertions(+), 5 deletions(-)
>> 
>> diff --git a/lib/librte_acl/acl_bld.c b/lib/librte_acl/acl_bld.c
>> index b82191f42..9d27c0a5a 100644
>> --- a/lib/librte_acl/acl_bld.c
>> +++ b/lib/librte_acl/acl_bld.c
>> @@ -320,7 +320,7 @@ acl_add_ptr_range(struct acl_build_context *context,
>>  	for (n = 0; n < UINT8_MAX + 1; n++)
>>  		if (n >= low && n <= high)
>>  			bitset.bits[n / (sizeof(bits_t) * 8)] |=
>> -				1 << (n % (sizeof(bits_t) * 8));
>> +				1U << (n % (sizeof(bits_t) * CHAR_BIT));
>> 
>>  	return acl_add_ptr(context, root, node, &bitset);
>>  }
>> @@ -343,7 +343,7 @@ acl_gen_mask(struct rte_acl_bitset *bitset, uint32_t value, uint32_t mask)
>>  		if ((n & mask) == value) {
>>  			range++;
>>  			bitset->bits[n / (sizeof(bits_t) * 8)] |=
>> -				1 << (n % (sizeof(bits_t) * 8));
>> +				1U << (n % (sizeof(bits_t) * CHAR_BIT));
>>  		}
>>  	}
>>  	return range;
>> @@ -972,7 +972,7 @@ build_trie(struct acl_build_context *context, struct rte_acl_build_rule *head,
>>  				sizeof(*end->mrt));
>> 
>>  		for (m = context->cfg.num_categories; 0 != m--; ) {
>> -			if (rule->f->data.category_mask & (1 << m)) {
>> +			if (rule->f->data.category_mask & (1U << m)) {
>>  				end->mrt->results[m] = rule->f->data.userdata;
>>  				end->mrt->priority[m] = rule->f->data.priority;
>>  			} else {
>> diff --git a/lib/librte_acl/acl_gen.c b/lib/librte_acl/acl_gen.c
>> index 35a0140b4..81dec3aa6 100644
>> --- a/lib/librte_acl/acl_gen.c
>> +++ b/lib/librte_acl/acl_gen.c
>> @@ -133,7 +133,7 @@ acl_node_fill_dfa(const struct rte_acl_node *node,
>>  		for (n = 0; n < RTE_ACL_DFA_SIZE; n++) {
>> 
>>  			if (bits->bits[n / (sizeof(bits_t) * CHAR_BIT)] &
>> -				(1 << (n % (sizeof(bits_t) * CHAR_BIT)))) {
>> +				(1U << (n % (sizeof(bits_t) * CHAR_BIT)))) {
>> 
>>  				dfa[n] = resolved ? child->node_index : x;
>>  				ranges += (last_bit == 0);
>> @@ -175,7 +175,7 @@ acl_count_sequential_groups(struct rte_acl_bitset *bits, int zero_one)
>>  	}
>>  	for (n = 0; n < QRANGE_MIN; n++) {
>>  		if (bits->bits[n / (sizeof(bits_t) * 8)] &
>> -				(1 << (n % (sizeof(bits_t) * 8)))) {
>> +				(1U << (n % (sizeof(bits_t) * 8)))) {
>>  			if (zero_one == 1 && last_bit != 1)
>>  				ranges++;
>>  			last_bit = 1;
>> --
>
>
>
>> 2.21.0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [dpdk-dev] [PATCH v2] librte_acl: fix undefined behavior
  2019-07-30 21:39 [dpdk-dev] [PATCH] librte_acl: fix undefined behavior Aaron Conole
  2019-07-31  8:16 ` Ananyev, Konstantin
@ 2019-07-31 15:43 ` Aaron Conole
  2019-08-01  8:14   ` Ananyev, Konstantin
  1 sibling, 1 reply; 6+ messages in thread
From: Aaron Conole @ 2019-07-31 15:43 UTC (permalink / raw)
  To: dev; +Cc: Konstantin Ananyev, Pablo de Lara Guarch

Left-shift of an integer constant is represented as 'int' type, but a left
shift of 1 by 31 bits in 'int' is undefined.  Use the U suffix to force
a representation as unsigned.

Caught while running with ubsan under gcc.

Fixes: dc276b5780c2 ("acl: new library")
Cc: Konstantin Ananyev <konstantin.ananyev@intel.com>
Signed-off-by: Aaron Conole <aconole@redhat.com>
---
v2: correct one other place where I could have used CHAR_BIT

I could have changed the sizeof(bits_t) * 8 in the bitset.bits as well during
the cleanup, but chose not to to keep the change only to the lines I needed.

 lib/librte_acl/acl_bld.c | 6 +++---
 lib/librte_acl/acl_gen.c | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/librte_acl/acl_bld.c b/lib/librte_acl/acl_bld.c
index b82191f42..9d27c0a5a 100644
--- a/lib/librte_acl/acl_bld.c
+++ b/lib/librte_acl/acl_bld.c
@@ -320,7 +320,7 @@ acl_add_ptr_range(struct acl_build_context *context,
 	for (n = 0; n < UINT8_MAX + 1; n++)
 		if (n >= low && n <= high)
 			bitset.bits[n / (sizeof(bits_t) * 8)] |=
-				1 << (n % (sizeof(bits_t) * 8));
+				1U << (n % (sizeof(bits_t) * CHAR_BIT));
 
 	return acl_add_ptr(context, root, node, &bitset);
 }
@@ -343,7 +343,7 @@ acl_gen_mask(struct rte_acl_bitset *bitset, uint32_t value, uint32_t mask)
 		if ((n & mask) == value) {
 			range++;
 			bitset->bits[n / (sizeof(bits_t) * 8)] |=
-				1 << (n % (sizeof(bits_t) * 8));
+				1U << (n % (sizeof(bits_t) * CHAR_BIT));
 		}
 	}
 	return range;
@@ -972,7 +972,7 @@ build_trie(struct acl_build_context *context, struct rte_acl_build_rule *head,
 				sizeof(*end->mrt));
 
 		for (m = context->cfg.num_categories; 0 != m--; ) {
-			if (rule->f->data.category_mask & (1 << m)) {
+			if (rule->f->data.category_mask & (1U << m)) {
 				end->mrt->results[m] = rule->f->data.userdata;
 				end->mrt->priority[m] = rule->f->data.priority;
 			} else {
diff --git a/lib/librte_acl/acl_gen.c b/lib/librte_acl/acl_gen.c
index 35a0140b4..81dec3aa6 100644
--- a/lib/librte_acl/acl_gen.c
+++ b/lib/librte_acl/acl_gen.c
@@ -133,7 +133,7 @@ acl_node_fill_dfa(const struct rte_acl_node *node,
 		for (n = 0; n < RTE_ACL_DFA_SIZE; n++) {
 
 			if (bits->bits[n / (sizeof(bits_t) * CHAR_BIT)] &
-				(1 << (n % (sizeof(bits_t) * CHAR_BIT)))) {
+				(1U << (n % (sizeof(bits_t) * CHAR_BIT)))) {
 
 				dfa[n] = resolved ? child->node_index : x;
 				ranges += (last_bit == 0);
@@ -175,7 +175,7 @@ acl_count_sequential_groups(struct rte_acl_bitset *bits, int zero_one)
 	}
 	for (n = 0; n < QRANGE_MIN; n++) {
 		if (bits->bits[n / (sizeof(bits_t) * 8)] &
-				(1 << (n % (sizeof(bits_t) * 8)))) {
+				(1U << (n % (sizeof(bits_t) * CHAR_BIT)))) {
 			if (zero_one == 1 && last_bit != 1)
 				ranges++;
 			last_bit = 1;
-- 
2.21.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH v2] librte_acl: fix undefined behavior
  2019-07-31 15:43 ` [dpdk-dev] [PATCH v2] " Aaron Conole
@ 2019-08-01  8:14   ` Ananyev, Konstantin
  2019-08-02 20:02     ` Thomas Monjalon
  0 siblings, 1 reply; 6+ messages in thread
From: Ananyev, Konstantin @ 2019-08-01  8:14 UTC (permalink / raw)
  To: Aaron Conole, dev; +Cc: De Lara Guarch, Pablo

> 
> Left-shift of an integer constant is represented as 'int' type, but a left
> shift of 1 by 31 bits in 'int' is undefined.  Use the U suffix to force
> a representation as unsigned.
> 
> Caught while running with ubsan under gcc.
> 
> Fixes: dc276b5780c2 ("acl: new library")
> Cc: Konstantin Ananyev <konstantin.ananyev@intel.com>
> Signed-off-by: Aaron Conole <aconole@redhat.com>
> ---

Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>

> 2.21.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH v2] librte_acl: fix undefined behavior
  2019-08-01  8:14   ` Ananyev, Konstantin
@ 2019-08-02 20:02     ` Thomas Monjalon
  0 siblings, 0 replies; 6+ messages in thread
From: Thomas Monjalon @ 2019-08-02 20:02 UTC (permalink / raw)
  To: Aaron Conole; +Cc: dev, Ananyev, Konstantin, De Lara Guarch, Pablo

01/08/2019 10:14, Ananyev, Konstantin:
> > 
> > Left-shift of an integer constant is represented as 'int' type, but a left
> > shift of 1 by 31 bits in 'int' is undefined.  Use the U suffix to force
> > a representation as unsigned.
> > 
> > Caught while running with ubsan under gcc.
> > 
> > Fixes: dc276b5780c2 ("acl: new library")
> > Cc: Konstantin Ananyev <konstantin.ananyev@intel.com>
> > Signed-off-by: Aaron Conole <aconole@redhat.com>
> 
> Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>

Applied, thanks



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-08-02 20:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-30 21:39 [dpdk-dev] [PATCH] librte_acl: fix undefined behavior Aaron Conole
2019-07-31  8:16 ` Ananyev, Konstantin
2019-07-31 13:06   ` Aaron Conole
2019-07-31 15:43 ` [dpdk-dev] [PATCH v2] " Aaron Conole
2019-08-01  8:14   ` Ananyev, Konstantin
2019-08-02 20:02     ` Thomas Monjalon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).