[CDRIVER-2810] misaligned address Created: 30/Aug/18 Updated: 06/Sep/18 Resolved: 06/Sep/18 |
|
| Status: | Closed |
| Project: | C Driver |
| Component/s: | libbson, libmongoc |
| Affects Version/s: | 1.12.0 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Adrian Imboden | Assignee: | A. Jesse Jiryu Davis |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Ubuntu 18.04 libasan build |
||
| Attachments: |
|
||||||||||||||||
| Issue Links: |
|
||||||||||||||||
| Description |
|
I updated the mongo-cxx-driver, mongo-c-driver and libbson (which is now part of mongo-c-driver) to the newest versions. The mongo-c-driver is now version 1.12 (changeset a690091bae086f267791bd2227400f2035de99e8). At our company, we use a self built toolchain:
We test our software with many sanitizers. UBSan is one of them. Since the update, I get the following error:
hundreds more will come after that.
It seems that the memory from bson_malloc0 is not properly aligned for the use case it is being used in this case. |
| Comments |
| Comment by A. Jesse Jiryu Davis [ 03/Sep/18 ] | ||||||||||||||||||||||||||||||||||||||
|
Interesting, thanks for sharing that code. So the problem is that malloc guarantees only 16-byte aligned memory. "The address of a block returned by malloc or realloc in GNU systems is always a multiple of eight (or sixteen on 64-bit systems)." https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html libbson and libmongoc made an unfortunate decision a few years ago, we declare a few structs like bson_t as 128-byte aligned. Structs that include a bson_t, such as a mongoc_read_prefs_t, must also be 128-byte aligned as a side-effect. The intent was to enforce efficient use of the CPU cache, but it causes problems like this one. We'll remove the alignment specifier when we make our next ABI-breaking release, libmongoc 2.0: CDRIVER-2813. Meanwhile, it's best to override the default: execute CMake with -DENABLE_EXTRA_ALIGNMENT=OFF and rebuild the driver. | ||||||||||||||||||||||||||||||||||||||
| Comment by Adrian Imboden [ 03/Sep/18 ] | ||||||||||||||||||||||||||||||||||||||
|
Hi
As you said, malloc and calloc should already return memory suitable for any alignment.
https://en.cppreference.com/w/c/memory/malloc states, that the memory returned is "... allocated memory block that is suitably aligned for any object type with fundamental alignment", which according to the doc is normally https://en.cppreference.com/w/c/memory/aligned_alloc, which is most of the time 4 or 8 bytes depending on the platform. The warning states, that a 128 byte alignment is expected for the struct mongoc_read_prefs_t (and the others mentioned in the output). I did not check how it gets to a 128 byte alignment, but if I add this code at the beginning of my program, no warnings occur (this example is only C11 compatible, but I think the alignment can be forced by hand as well of course):
The 128 bytes are arbitrary and should be calculated using the given type or the num_bytes.
I hope that helps to find out more about the problem. | ||||||||||||||||||||||||||||||||||||||
| Comment by A. Jesse Jiryu Davis [ 01/Sep/18 ] | ||||||||||||||||||||||||||||||||||||||
|
Hi, bson_malloc0 uses calloc by default. C89 says, "The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object." I have three questions to help us diagnose the issue. 1. Does your custom toolchain alter memory allocation from clang's defaults? 2. What happens if you recompile the driver, running CMake with "-DENABLE_EXTRA_ALIGNMENT=OFF"? 3. What happens if you replace our calloc call with malloc, using code something like this at the very beginning of your program?:
|