danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
726 stars 164 forks source link

constructor call on misaligned address #441

Closed chakaz closed 1 year ago

chakaz commented 1 year ago

Hi there! Our unit test show some runtime errors while using jsoncons with pmr:

/home/shahar/dragonfly/build-dbg/third_party/libs/jsoncons/include/jsoncons/detail/heap_string.hpp:134:36: runtime error: constructor call on misaligned addre
ss 0x55d61bc6204e for type 'struct heap_string_type', which requires 8 byte alignment
0x55d61bc6204e: note: pointer points here
 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00
             ^ 
/home/shahar/dragonfly/build-dbg/third_party/libs/jsoncons/include/jsoncons/detail/heap_string.hpp:64:86: runtime error: member access within misaligned addre
ss 0x55d61bc6204e for type 'struct heap_string', which requires 8 byte alignment
0x55d61bc6204e: note: pointer points here
 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00
             ^ 
/home/shahar/dragonfly/build-dbg/third_party/libs/jsoncons/include/jsoncons/detail/heap_string.hpp:64:86: runtime error: constructor call on misaligned addres
s 0x55d61bc6204e for type 'struct heap_string_base', which requires 8 byte alignment
0x55d61bc6204e: note: pointer points here
 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00
             ^ 

Specifically such prints are printed by this line: https://github.com/dragonflydb/dragonfly/blob/3b0bd212f46f84e8eda3a02fe99de24f319c9725/src/core/json_test.cc#L127C1-L127C1

The prints are coming from here: https://github.com/danielaparker/jsoncons/blob/a414677d1333abd110e78c5eec950589956eee54/include/jsoncons/detail/heap_string.hpp#L134C1-L134C1

I think that the issue is actually within jsoncons rather than in our code, and the reason is that the allocation done internally in jsoncons is in byte (byte_alloc.allocate(mem_size);), while later we try to construct a struct in that memory (new(storage)heap_string_type(extra, byte_alloc);), but it's not guaranteed to be aligned (and indeed in my setup it is not)

chakaz commented 1 year ago

Hi @danielaparker - just wondering if you also saw this behavior?

danielaparker commented 1 year ago

@chakaz - apologies for the late response. This hasn't come up before, possibly because many implementations of allocate, like new, provide maximally aligned storage. But we'll need to address this.

danielaparker commented 1 year ago

This should be addressed on master

chakaz commented 1 year ago

Thanks @danielaparker!