Repository navigation
fix: Read unaligned buffers safely in ArrowArrayView getters - #1011
Conversation
The ArrowArrayView getters read values, offsets and binary views through typed pointers (e.g., data.as_int32[i]). Buffers that arrive through the C Data interface do not have to be aligned, and for those each of these reads is a misaligned load that UBSan reports. This adds memcpy-based ArrowBufferViewGet*Unsafe() helpers for the remaining fixed-width types next to the 32- and 64-bit ones from apache#946 and uses them in ArrowArrayViewGetIntUnsafe(), ArrowArrayViewGetUIntUnsafe(), ArrowArrayViewGetDoubleUnsafe(), ArrowArrayViewGetStringUnsafe(), ArrowArrayViewGetBytesUnsafe(), ArrowArrayViewListChildOffset() and ArrowArrayViewUnionChildOffset(). Binary views are copied before their fields are read. The helpers index the buffer as an array of byte arrays instead of adding i * sizeof(T) to a byte pointer. With the multiplication, clang stopped inlining ArrowArrayViewGetIntUnsafe() and ArrowArrayViewGetDoubleUnsafe() in a file with more than one call site. Part of apache#950.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1011 +/- ##
==========================================
+ Coverage 78.48% 82.09% +3.60%
==========================================
Files 106 100 -6
Lines 17245 14702 -2543
Branches 2065 2068 +3
==========================================
- Hits 13535 12069 -1466
+ Misses 2446 1371 -1075
+ Partials 1264 1262 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
paleolimbot
left a comment
There was a problem hiding this comment.
Thank you!
I had Codex run a few benchmarks to make sure this doesn't negatively impact performance, and it largely doesn't!
It did pick up one regression that I think is worth fixing here if you're up for it: for string and binary view reads, the new version results in more loads than strictly required for inline values which can be up to 30% slower. The suggested fix was to copy the size bytes separately and check the length before materializing the full 16 byte view.
We can also merge and do that in a follow up 🙂
|
Thanks, done in 9aa4717: it now copies the 4-byte size first and reads |
Part of #950. The ArrowArrayView getters in
inline_array.hread values, offsets and binary views through typed pointers, which is a misaligned load when a buffer from the C Data interface isn’t aligned. This addsArrowBufferViewGet*Unsafe()helpers for the remaining fixed-width types next to the two from #946, uses them in the integer, double, string, bytes, list child offset and union child offset getters, and copies binary views before reading their fields.New tests read from buffers misaligned by 1 to 7 bytes in sliced arrays. On main UBSan reports 56 misaligned loads on 43 lines; with this change it reports none, and ctest passes under ASan and UBSan with clang and gcc.
The helpers index the buffer as an array of byte arrays because with
i * sizeof(T)clang stopped inliningArrowArrayViewGetIntUnsafe()andArrowArrayViewGetDoubleUnsafe()in files with more than one call site. With this form clang generates the same code as on main, and the array benchmarks are unchanged. The remaining sites (array.c, the IPC decoder, the C++ helpers) can follow separately.