diff mbox

[dpdk-dev,v3] test-pmd: Fix pointer aliasing error

Message ID 1417666564-19950-1-git-send-email-michael.qiu@intel.com (mailing list archive)
State Accepted, archived
Headers show

Commit Message

Michael Qiu Dec. 4, 2014, 4:16 a.m. UTC
app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
        does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here
        ...

The root cause is that, compile enable strict aliasing by default,
while in function rte_raw_cksum() try to convert 'const char *'
to 'const uint16_t *'.

This patch is one workaround fix.

Signed-off-by: Michael Qiu <michael.qiu@intel.com>
---
v3 --> v2:
	use uintptr_t instead of unsigned long to
	save pointer.

v2 --> v1:
	Workaround solution instead of shut off the
	gcc params.

 lib/librte_net/rte_ip.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Michael Qiu Dec. 5, 2014, 5:34 a.m. UTC | #1
Any comments about this version? a new workaround solution :)

Thanks,
Michael
On 12/4/2014 9:35 PM, Michael Qiu wrote:
> app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
> build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
>         does break strict-aliasing rules
> build/include/rte_ip.h:157: note: initialized from here
>         ...
>
> The root cause is that, compile enable strict aliasing by default,
> while in function rte_raw_cksum() try to convert 'const char *'
> to 'const uint16_t *'.
>
> This patch is one workaround fix.
>
> Signed-off-by: Michael Qiu <michael.qiu@intel.com>
> ---
> v3 --> v2:
> 	use uintptr_t instead of unsigned long to
> 	save pointer.
>
> v2 --> v1:
> 	Workaround solution instead of shut off the
> 	gcc params.
>
>  lib/librte_net/rte_ip.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/lib/librte_net/rte_ip.h b/lib/librte_net/rte_ip.h
> index 61e4457..cda3436 100644
> --- a/lib/librte_net/rte_ip.h
> +++ b/lib/librte_net/rte_ip.h
> @@ -154,7 +154,8 @@ struct ipv4_hdr {
>  static inline uint16_t
>  rte_raw_cksum(const char *buf, size_t len)
>  {
> -	const uint16_t *u16 = (const uint16_t *)buf;
> +	uintptr_t ptr = (uintptr_t)buf;
> +	const uint16_t *u16 = (const uint16_t *)ptr;
>  	uint32_t sum = 0;
>  
>  	while (len >= (sizeof(*u16) * 4)) {
Thomas Monjalon Dec. 5, 2014, 9:24 a.m. UTC | #2
2014-12-05 05:34, Qiu, Michael:
> Any comments about this version? a new workaround solution :)

Yes, one comment: I think it's ugly :)
These aliasing errors are not reliable so I think we can disable it (like
Linux does).
But in case you don't want to disable the warning, please add a comment to your
workaround to explain it is caused by GCC strict-aliasing check.

> > -	const uint16_t *u16 = (const uint16_t *)buf;
> > +	uintptr_t ptr = (uintptr_t)buf;
> > +	const uint16_t *u16 = (const uint16_t *)ptr;

Thanks
Michael Qiu Dec. 8, 2014, 1:28 a.m. UTC | #3
On 12/5/2014 5:26 PM, Thomas Monjalon wrote:
> 2014-12-05 05:34, Qiu, Michael:
>> Any comments about this version? a new workaround solution :)
> Yes, one comment: I think it's ugly :)
> These aliasing errors are not reliable so I think we can disable it (like
> Linux does).
> But in case you don't want to disable the warning, please add a comment to your
> workaround to explain it is caused by GCC strict-aliasing check.

Yes, really ugly ....

But lots of reviewer voted against with disable it as my first version
is disable.

I could not think out a better solution for all reviewers can accept.

I will add the comment with the thread of v3 patch.

Thanks,
Michael

>>> -	const uint16_t *u16 = (const uint16_t *)buf;
>>> +	uintptr_t ptr = (uintptr_t)buf;
>>> +	const uint16_t *u16 = (const uint16_t *)ptr;

> Thanks
Michael Qiu Dec. 8, 2014, 1:30 a.m. UTC | #4
On 12/4/2014 9:35 PM, Michael Qiu wrote:
> app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
> build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
>         does break strict-aliasing rules
> build/include/rte_ip.h:157: note: initialized from here
>         ...
>
> The root cause is that, compile enable strict aliasing by default,
> while in function rte_raw_cksum() try to convert 'const char *'
> to 'const uint16_t *'.
>
> This patch is one workaround fix.
>
> Signed-off-by: Michael Qiu <michael.qiu@intel.com>
> ---
> v3 --> v2:
> 	use uintptr_t instead of unsigned long to
> 	save pointer.
>
> v2 --> v1:
> 	Workaround solution instead of shut off the
> 	gcc params.
>
>  lib/librte_net/rte_ip.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/lib/librte_net/rte_ip.h b/lib/librte_net/rte_ip.h
> index 61e4457..cda3436 100644
> --- a/lib/librte_net/rte_ip.h
> +++ b/lib/librte_net/rte_ip.h
> @@ -154,7 +154,8 @@ struct ipv4_hdr {
>  static inline uint16_t
>  rte_raw_cksum(const char *buf, size_t len)
>  {
> -	const uint16_t *u16 = (const uint16_t *)buf;
> +	uintptr_t ptr = (uintptr_t)buf;
> +	const uint16_t *u16 = (const uint16_t *)ptr;
>  	uint32_t sum = 0;
>  
>  	while (len >= (sizeof(*u16) * 4)) {
This workaround is to solve the compile issue of GCC strict-aliasing(Two
different type pointers should not be point to the same memory address).

For GCC 4.4.7 it will definitely occurs if  flags "-fstrict-aliasing"
and "-Wall" used.

Thanks,
Michael
Michael Qiu Dec. 10, 2014, 3:44 a.m. UTC | #5
Hi Thomas,

What's going on with this patch?

I really do not have other better solution.

Thanks,
Michael
On 12/8/2014 9:30 AM, Qiu, Michael wrote:
> On 12/4/2014 9:35 PM, Michael Qiu wrote:
>> app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
>> build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
>>         does break strict-aliasing rules
>> build/include/rte_ip.h:157: note: initialized from here
>>         ...
>>
>> The root cause is that, compile enable strict aliasing by default,
>> while in function rte_raw_cksum() try to convert 'const char *'
>> to 'const uint16_t *'.
>>
>> This patch is one workaround fix.
>>
>> Signed-off-by: Michael Qiu <michael.qiu@intel.com>
>> ---
>> v3 --> v2:
>> 	use uintptr_t instead of unsigned long to
>> 	save pointer.
>>
>> v2 --> v1:
>> 	Workaround solution instead of shut off the
>> 	gcc params.
>>
>>  lib/librte_net/rte_ip.h | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/lib/librte_net/rte_ip.h b/lib/librte_net/rte_ip.h
>> index 61e4457..cda3436 100644
>> --- a/lib/librte_net/rte_ip.h
>> +++ b/lib/librte_net/rte_ip.h
>> @@ -154,7 +154,8 @@ struct ipv4_hdr {
>>  static inline uint16_t
>>  rte_raw_cksum(const char *buf, size_t len)
>>  {
>> -	const uint16_t *u16 = (const uint16_t *)buf;
>> +	uintptr_t ptr = (uintptr_t)buf;
>> +	const uint16_t *u16 = (const uint16_t *)ptr;
>>  	uint32_t sum = 0;
>>  
>>  	while (len >= (sizeof(*u16) * 4)) {
> This workaround is to solve the compile issue of GCC strict-aliasing(Two
> different type pointers should not be point to the same memory address).
>
> For GCC 4.4.7 it will definitely occurs if  flags "-fstrict-aliasing"
> and "-Wall" used.
>
> Thanks,
> Michael
>
Thomas Monjalon Dec. 11, 2014, 12:54 a.m. UTC | #6
> > app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
> > build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
> >         does break strict-aliasing rules
> > build/include/rte_ip.h:157: note: initialized from here
> >         ...
> >
> > The root cause is that, compile enable strict aliasing by default,
> > while in function rte_raw_cksum() try to convert 'const char *'
> > to 'const uint16_t *'.
> >
> > This patch is one workaround fix.
> >
> > Signed-off-by: Michael Qiu <michael.qiu@intel.com>
> > ---
> > v3 --> v2:
> > 	use uintptr_t instead of unsigned long to
> > 	save pointer.
> >
> > v2 --> v1:
> > 	Workaround solution instead of shut off the
> > 	gcc params.
> 
> This workaround is to solve the compile issue of GCC strict-aliasing(Two
> different type pointers should not be point to the same memory address).
> 
> For GCC 4.4.7 it will definitely occurs if  flags "-fstrict-aliasing"
> and "-Wall" used.

Acked-by: Thomas Monjalon <thomas.monjalon@6wind.com>> 

Applied with a comment in the code.

Thanks
Ravi Kerur Dec. 11, 2014, 5:51 p.m. UTC | #7
Thomas, Michael,

Wouldn't it cause unaligned memory access (new changes as well as the
previous code)? Wondering if get_unaligned/put_unaligned macros similar to
the ones used in kernel be ported to user-space?

Thanks,
Ravi

On Wed, Dec 10, 2014 at 4:54 PM, Thomas Monjalon <thomas.monjalon@6wind.com>
wrote:
>
> > > app/test-pmd/csumonly.c: In function 'get_psd_sum':
> > > build/include/rte_ip.h:161: error: dereferencing pointer 'u16'
> > >         does break strict-aliasing rules
> > > build/include/rte_ip.h:157: note: initialized from here
> > >         ...
> > >
> > > The root cause is that, compile enable strict aliasing by default,
> > > while in function rte_raw_cksum() try to convert 'const char *'
> > > to 'const uint16_t *'.
> > >
> > > This patch is one workaround fix.
> > >
> > > Signed-off-by: Michael Qiu <michael.qiu@intel.com>
> > > ---
> > > v3 --> v2:
> > >     use uintptr_t instead of unsigned long to
> > >     save pointer.
> > >
> > > v2 --> v1:
> > >     Workaround solution instead of shut off the
> > >     gcc params.
> >
> > This workaround is to solve the compile issue of GCC strict-aliasing(Two
> > different type pointers should not be point to the same memory address).
> >
> > For GCC 4.4.7 it will definitely occurs if  flags "-fstrict-aliasing"
> > and "-Wall" used.
>
> Acked-by: Thomas Monjalon <thomas.monjalon@6wind.com>>
>
> Applied with a comment in the code.
>
> Thanks
> --
> Thomas
>
Michael Qiu Dec. 12, 2014, 6:49 a.m. UTC | #8
On 2014/12/12 1:51, r k wrote:
Thomas, Michael,

Wouldn't it cause unaligned memory access (new changes as well as the previous code)? Wondering if get_unaligned/put_unaligned macros similar to the ones used in kernel be ported to user-space?


I think it will not, as all buf point to are struct udp_hdr/struct tcp_hdr/struct ipv6_psd_header/struct ipv4_psd_header, they are all aligned with uint16_t.

Thanks
Michael
Thanks,
Ravi

On Wed, Dec 10, 2014 at 4:54 PM, Thomas Monjalon <thomas.monjalon@6wind.com<mailto:thomas.monjalon@6wind.com>> wrote:
> > app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
> > build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
> >         does break strict-aliasing rules
> > build/include/rte_ip.h:157: note: initialized from here
> >         ...
> >
> > The root cause is that, compile enable strict aliasing by default,
> > while in function rte_raw_cksum() try to convert 'const char *'
> > to 'const uint16_t *'.
> >
> > This patch is one workaround fix.
> >
> > Signed-off-by: Michael Qiu <michael.qiu@intel.com<mailto:michael.qiu@intel.com>>
> > ---
> > v3 --> v2:
> >     use uintptr_t instead of unsigned long to
> >     save pointer.
> >
> > v2 --> v1:
> >     Workaround solution instead of shut off the
> >     gcc params.
>
> This workaround is to solve the compile issue of GCC strict-aliasing(Two
> different type pointers should not be point to the same memory address).
>
> For GCC 4.4.7 it will definitely occurs if  flags "-fstrict-aliasing"
> and "-Wall" used.

Acked-by: Thomas Monjalon <thomas.monjalon@6wind.com<mailto:thomas.monjalon@6wind.com>>>

Applied with a comment in the code.

Thanks
--
Thomas
diff mbox

Patch

diff --git a/lib/librte_net/rte_ip.h b/lib/librte_net/rte_ip.h
index 61e4457..cda3436 100644
--- a/lib/librte_net/rte_ip.h
+++ b/lib/librte_net/rte_ip.h
@@ -154,7 +154,8 @@  struct ipv4_hdr {
 static inline uint16_t
 rte_raw_cksum(const char *buf, size_t len)
 {
-	const uint16_t *u16 = (const uint16_t *)buf;
+	uintptr_t ptr = (uintptr_t)buf;
+	const uint16_t *u16 = (const uint16_t *)ptr;
 	uint32_t sum = 0;
 
 	while (len >= (sizeof(*u16) * 4)) {