Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
C++ driver: strict aliasing warnings
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Pedro Larroy  
View profile  
 More options Apr 17 2012, 11:31 am
From: Pedro Larroy <pedro.larroy.li...@gmail.com>
Date: Tue, 17 Apr 2012 08:31:34 -0700 (PDT)
Local: Tues, Apr 17 2012 11:31 am
Subject: C++ driver: strict aliasing warnings

Hi

There are some strict aliasing warnings like this one:

mongo-cxx-driver/src/mongo/db/../util/optime.h:96:69: error: dereferencing
type-punned pointer will break strict-aliasing rules
[-Werror=strict-aliasing]

        int& dataAsInt() {
            return *((int *) _data);
        }

I see this is only used in QueryMessage.

Any reason not to return the data by value to avoid the warning and
possible aliasing?

Pedro.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andy Schwerin  
View profile  
 More options Apr 17 2012, 11:36 am
From: Andy Schwerin <schwe...@10gen.com>
Date: Tue, 17 Apr 2012 11:36:34 -0400
Local: Tues, Apr 17 2012 11:36 am
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

Are you referring to the dataAsInt() in struct MsgData?  It cannot return
by value, because its return value is indirectly used as an lvalue,
elsewhere.  I believe that making _data in MsgData a union with int and
char[4] members should make GCC aware of what's going on.  Can you file a
ticket against "Core Server" (SERVER) at jira.mongodb.org?

-Andy

On Tue, Apr 17, 2012 at 11:31 AM, Pedro Larroy <pedro.larroy.li...@gmail.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andy Schwerin  
View profile  
 More options Apr 17 2012, 11:39 am
From: Andy Schwerin <schwe...@10gen.com>
Date: Tue, 17 Apr 2012 11:39:14 -0400
Local: Tues, Apr 17 2012 11:39 am
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

Actually, I've filed said ticket, now.
https://jira.mongodb.org/browse/SERVER-5634.

-Andy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pedro Larroy  
View profile  
 More options Apr 17 2012, 11:57 am
From: Pedro Larroy <pedro.larroy.li...@gmail.com>
Date: Tue, 17 Apr 2012 08:57:53 -0700 (PDT)
Local: Tues, Apr 17 2012 11:57 am
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

The portable and safer alternative is doing something like this:

int asInt() {
        uint8_t* p = (uint8_t*)data;
        uint32_t res = 0;
        res = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0]);
        return static_cast<int>(res);

}

At -O2 and -O3 it gets optimized away and inlined, doesn't look like it
will be slower than the previous cast.

Pedro.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Glenn Maynard  
View profile  
 More options Apr 17 2012, 12:03 pm
From: Glenn Maynard <gl...@zewt.org>
Date: Tue, 17 Apr 2012 11:03:53 -0500
Local: Tues, Apr 17 2012 12:03 pm
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

On Tue, Apr 17, 2012 at 10:57 AM, Pedro Larroy <pedro.larroy.li...@gmail.com

> wrote:
> The portable and safer alternative is doing something like this:

> int asInt() {
>         uint8_t* p = (uint8_t*)data;
>         uint32_t res = 0;
>         res = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0]);
>         return static_cast<int>(res);
> }

> At -O2 and -O3 it gets optimized away and inlined, doesn't look like it
> will be slower than the previous cast.

This can't be used as an lvalue.

asInt() = 100

--
Glenn Maynard


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Per Ola Ingvarsson  
View profile  
 More options Apr 17 2012, 3:02 pm
From: Per Ola Ingvarsson <skrab...@gmail.com>
Date: Tue, 17 Apr 2012 21:02:11 +0200
Local: Tues, Apr 17 2012 3:02 pm
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

On Tue, Apr 17, 2012 at 5:39 PM, Andy Schwerin <schwe...@10gen.com> wrote:
> Actually, I've filed said ticket, now.
>  https://jira.mongodb.org/browse/SERVER-5634.

I'm starting to sound like a broken record, but this is actually also
solved in https://github.com/skrabban/mongo-nonx86 .

It would be nice if the maintainers actually took a serious look at
this, because you would get the solution to SERVER-1625, SERVER-1811
and now also SERVER-5634.

Apart from that you would get a nice uniform way of casting, for example:

-        int& dataAsInt() {
-            return *((int *) _data);
+        little<int>& dataAsInt() {
+            return little<int>::ref( _data );
         }

-            return *reinterpret_cast< const int* >( value() );
+            return little<int>::ref( value() );

-            return (reinterpret_cast < const PackedDouble* >(value ()))->d != 0
+            return little<double>::ref( value() ) != 0;

-            *((int*)data) = size;
+            little<int>::ref( data ) = size;

-        return (unsigned&) x[0];
+        return little<unsigned>::ref( x );

On x86/amd64 this will compile to the same as the original cast.

A lot of iterations have been made to make it as simple and uniform as possible.

I suggest you look at the master branch, but the v1.8 branch is the stable one.

/pi


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pedro Larroy  
View profile  
 More options Apr 18 2012, 6:12 am
From: Pedro Larroy <pedro.larroy.li...@gmail.com>
Date: Wed, 18 Apr 2012 03:12:50 -0700 (PDT)
Local: Wed, Apr 18 2012 6:12 am
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

Hi Skrabban.

Thanks for your remarks. I see your repository has not been updated for a
while, have you tried creating a pull request with updated contents?  If
I'm not mistaken the non-x86 changes are for release v1.8 only?

I will have a look at your solution with the little template so maybe I can
adapt it to the C++ driver I'm trying to use, since I'm using the one from
the master branch as it has fixes that prevents me from using an older one.

We treat all warnings as errors in our project and I wouldn't like to be
more liberal about it only for the mongo driver, anyway some warnings are
coming also from headers, such as the strict aliasing topic discussed here.
If it could actually produce a real bug by means of broken optimization
etc.  Could the problem with strict aliasing really produce undefined
behaviour?

Pedro.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Per Ola Ingvarsson  
View profile  
 More options Apr 19 2012, 2:46 pm
From: Per Ola Ingvarsson <skrab...@gmail.com>
Date: Thu, 19 Apr 2012 20:46:27 +0200
Local: Thurs, Apr 19 2012 2:46 pm
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings
On Wed, Apr 18, 2012 at 12:12 PM, Pedro Larroy

<pedro.larroy.li...@gmail.com> wrote:
> Hi Skrabban.

> Thanks for your remarks. I see your repository has not been updated for a
> while, have you tried creating a pull request with updated contents?  If I'm
> not mistaken the non-x86 changes are for release v1.8 only?

The master branch is currently also almost up to date, but there is
some issue in snappy.
Bson should be complete though.

> I will have a look at your solution with the little template so maybe I can
> adapt it to the C++ driver I'm trying to use, since I'm using the one from
> the master branch as it has fixes that prevents me from using an older one.
> We treat all warnings as errors in our project and I wouldn't like to be
> more liberal about it only for the mongo driver, anyway some warnings are
> coming also from headers, such as the strict aliasing topic discussed here.
> If it could actually produce a real bug by means of broken optimization etc.
>  Could the problem with strict aliasing really produce undefined behaviour?

Yes. You could get all sorts of results if you remove
-fno-strict-aliasing when the
project usually is built with that flag.

/pi


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pedro Larroy  
View profile  
 More options Apr 20 2012, 5:42 am
From: Pedro Larroy <pedro.larroy.li...@gmail.com>
Date: Fri, 20 Apr 2012 02:42:11 -0700 (PDT)
Local: Fri, Apr 20 2012 5:42 am
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

Hi

Good point, so -fno-strict-aliasing could do as a workaround. Another
option is using a union, and the more intrusive one is trying to port your
endian agnostic changes.

Pedro.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Per Ola Ingvarsson  
View profile  
 More options Apr 20 2012, 9:57 am
From: Per Ola Ingvarsson <skrab...@gmail.com>
Date: Fri, 20 Apr 2012 15:57:29 +0200
Local: Fri, Apr 20 2012 9:57 am
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings
On Fri, Apr 20, 2012 at 11:42 AM, Pedro Larroy

<pedro.larroy.li...@gmail.com> wrote:
> Hi

> Good point, so -fno-strict-aliasing could do as a workaround. Another option
> is using a union, and the more intrusive one is trying to port your endian
> agnostic changes.

Yes, but the union only solves this particular instance of the
warning. I think I saw more when I used -fstrict-aliasing.

/pi


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pedro Larroy  
View profile  
 More options Apr 20 2012, 1:19 pm
From: Pedro Larroy <pedro.larroy.li...@gmail.com>
Date: Fri, 20 Apr 2012 10:19:30 -0700 (PDT)
Local: Fri, Apr 20 2012 1:19 pm
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

Is there a recommended way to test the driver after changes to see that it
doesn't break it?

Pedro.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andy Schwerin  
View profile  
 More options Apr 20 2012, 1:29 pm
From: Andy Schwerin <schwe...@10gen.com>
Date: Fri, 20 Apr 2012 13:29:46 -0400
Local: Fri, Apr 20 2012 1:29 pm
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

On the master branch, you can run "scons clientBuild".  It will build
mongod, and several client programs, and perform some basic tests.

The mongo shell, mongod and mongos instances also use the C++ client to
communicate, so running the test suite (such as it is) can't hurt.  It can
take a while to run everything, but a not-too-slow smoke test is to run
"scons smoke smokeJs".  Might take about an hour.

-Andy

On Fri, Apr 20, 2012 at 1:19 PM, Pedro Larroy
<pedro.larroy.li...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pedro Larroy  
View profile  
 More options Apr 20 2012, 2:01 pm
From: Pedro Larroy <pedro.larroy.li...@gmail.com>
Date: Fri, 20 Apr 2012 11:01:25 -0700 (PDT)
Local: Fri, Apr 20 2012 2:01 pm
Subject: Re: [mongodb-dev] C++ driver: strict aliasing warnings

Thanks.

I'll test next week, have a nice weekend.

Pedro.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »