folly icon indicating copy to clipboard operation
folly copied to clipboard

fbvector unaligned memory allocations lead to sigsegv

Open koomisov opened this issue 2 years ago • 1 comments
trafficstars

fbvector uses malloc instead of allocator methods for memory allocations if allocator type is std::allocator<T> (code)

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:

#include <cstring>
#include <vector>
#include <folly/FBVector.h>

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

    char padd[128];
};

int main() {
    folly::fbvector<aligned_struct, std::allocator<aligned_struct>> folly_vec(5); // segfault with -march option with AVX instructions, e.g. -march=icelake-server
    std::vector<aligned_struct, std::allocator<aligned_struct>> 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

koomisov avatar Jan 18 '23 10:01 koomisov

The issue you described is related to the use of the folly::fbvector container and the alignment of the data it stores. folly::fbvector is a variation of the standard std::vector container that is used in the Facebook codebase, and it uses malloc instead of allocator methods for memory allocations when the allocator type is std::allocator<T>.

The problem is that malloc usually aligns output data on a 16 byte boundary, but the compiler may generate AVX instructions for initialization, such as vmovaps, that require alignment on a 64/128 byte boundary. This can result in a segmentation fault when the -march option with AVX instructions is used, such as -march=icelake-server.

One solution to this issue would be to use a custom allocator that aligns memory using posix_memalign or aligned_alloc instead of malloc. This can be done by creating a custom allocator class that overloads the allocate method to use posix_memalign or aligned_alloc, and the deallocate method to use free.

Another solution would be to use the standard std::vector container instead of folly::fbvector, as it uses the specified allocator methods for memory allocations and does not have this alignment issue.

It's also important to note that the problem is not obvious because the memory is not allocated using specified allocator methods under the hood. It is a good practice to use aligned allocation for specific data types that have alignment requirement.

It's also good to check the project documentation, or reach out to the community, developers or maintainers for more information about how the container is implemented and if there are any known issues.

Kundanagrawalofficial avatar Jan 22 '23 11:01 Kundanagrawalofficial