facebook / facebook/folly

fbvector unaligned memory allocations lead to sigsegv

Open
#1,918 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
30.5k
Forks
5.9k
PR merge metrics
No merged PRs in 30d

Description

fbvector uses `malloc` instead of allocator methods for memory allocations if allocator type is `std::allocator` ([code](https://github.com/facebook/folly/blob/main/folly/FBVector.h#L126))

`malloc` usually aligns output data on 16 bytes boundary, but compiler may generate AVX instruction for initialization, such as `vmovaps` that requires alignment on a 64/128 byte boundary

Problem code example:
```c++
#include
#include
#include

struct alignas(128) aligned_struct {
aligned_struct() {
std::memset(padd, 0, sizeof(padd));
}

char padd[128];
};

int main() {
folly::fbvector> folly_vec(5); // segfault with -march option with AVX instructions, e.g. -march=icelake-server
std::vector> std_vec(5); // works, at least on C++20

return 0;
}
```

The main problem is that memory is not allocated using specified allocator methods under the hood that is not obvious

Contributor guide

Open the contributing guide

Research direction

Start at folly/FBVector.h around line 126 and reproduce the aligned_struct example with an AVX-enabled compiler. Trace the std::allocator allocation path, then verify that fbvector honors the allocator’s alignment requirements and no longer segfaults; the payload names no test file, so identify the relevant FBVector test before changing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.