I found another reason this can happen: If you compile with an `-march` 
newer than the target CPU.

For example, if I compile a minimal protobuf example 
using SerializeToArray() using `-march=haswell` and execute it on a Sandy 
Bridge CPU, I get:

[libprotobuf FATAL google/protobuf/message_lite.cc:71] CHECK failed: 
(bytes_produced_by_serialization) == (byte_size_before_serialization): Byte 
size calculation and serialization were inconsistent.  This may indicate a 
bug in protocol buffers or it may be caused by concurrent modification of 
myproject.proto.MyMessage.

This is very surprising, because usually using a too new march only results 
at most in an obvious `illegal instruction` error, not an incorrect result 
at runtime.

I have investigated why this is the case:

I bisected the feature flags enabled by `-march=haswell` and found that 
`-mno-lzcnt` fixes the issue (so `-march=haswell -mno-lzcnt` works fine on 
Sandy Bridge for my example code).

Here is why:

* 
https://nextmovesoftware.com/blog/2017/06/06/the-perils-of-using-__lzcnt-with-msvc/
* 
https://stackoverflow.com/questions/25683690/confusion-about-bsr-and-lzcnt/43443701#43443701

> instead of getting an ‘illegal instruction’ result when you run it, Intel 
[...] decided to reuse or repurpose existing opcodes so that CPUs without 
‘lzcnt’ instead did a ‘bsr’ (bit scan reverse). This was why (a) the 
results were different/wrong, and (b) why a value of 0 gave gibberish (the 
docs for ‘bsr’ say the results are undefined in that case).

I have confirmed that the code generated by `-march=haswell` contains 
`lzcnt` instructions.

Hope this helps anybody!

Niklas

On Monday, November 16, 2015 at 1:05:19 AM UTC+1, snail thum wrote:
>
> I have same problem now, and there is no uninitialized boolean attr
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to