Alignment requirement when load a flatbuffer

1,050 views
Skip to first unread message

Mehdi AMINI

unread,
Nov 2, 2017, 7:36:59 PM11/2/17
to FlatBuffers
Hi,

I'm looking into the alignment requirement for the beginning of a buffer in memory (that will be populated with a flatbuffer received from the network for instance).
I read past discussion about using this API to find the alignment requirement at serialization time: https://google.github.io/flatbuffers/classflatbuffers_1_1_flat_buffer_builder.html#aa1ebce1f3f46832946a95952af1e9c2b ; or this API that allows to force the padding within the buffer itself to ensure alignment of a member (for nested FB I guess): https://github.com/google/flatbuffers/commit/07da3fc216c62b18eb13a8bcb9afa95d7c325418

However I couldn't find what requirement exists on the buffer itself?

Thanks,

-- 
Mehdi

Wouter van Oortmerssen

unread,
Nov 6, 2017, 1:14:22 PM11/6/17
to Mehdi AMINI, FlatBuffers
Yes, sadly there is no way to recover the minimum alignment from just a buffer. Usually that is not a problem, as pretty much all allocators align to at least 8 bytes (often to 16, only 32-bit Windows is still at 8 afaik), and the largest built-in types are at most 8.

But yes, if you use force_align on some structs, and you can't guarantee your platform's allocator will minimally align to that too, you'll probably need to use an alignment function to pad your allocations.

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

Mehdi AMINI

unread,
Nov 6, 2017, 9:23:29 PM11/6/17
to Wouter van Oortmerssen, FlatBuffers
Thanks for the confirmation! 

Maybe one way to carry the requirement in the API may be to take a `const uint64_t *buffer` instead of a `char *`? I wonder if UBSAN wouldn't be able to catch any misalignment in such case.

The pattern I encountered in code-review was a struct that contained a bunch of fields and then an unaligned `char buffer[NBYTES];` (we added an `__attribute__((aligned(8)))` on this field).
The author wasn't convinced by what I was asking for, which is why I asked here. At least there will be a trace in the mailing-list archive now :)

Best,

-- 
Mehdi

Wouter van Oortmerssen

unread,
Nov 9, 2017, 11:43:44 AM11/9/17
to Mehdi AMINI, FlatBuffers
Except we generally do not have a 64bit alignment requirement: if you don't use and long/double types in your schema, the minimum alignment will be 4.

And yes, if the buffer isn't the first thing in the allocation then that is potentially problematic, good idea to enforce the alignment.

Reply all
Reply to author
Forward
0 new messages