Skip to content

Commit 648ee8d

Browse files
authored
Reducing Warnings - Misaligned Address of Over-Aligned Types (#1072)
* Enforce strict C99 conformance for debug-compile-aws * Fix C99 standard conformance detection in link-sample-program scripts * Add BSON_ALIGNOF * Redefine BSON_ALIGN_OF_PTR in terms of BSON_ALIGNOF * Add bson_aligned_alloc and bson_aligned_alloc0 * Add BSON_ALIGNED_ALLOC macro to avoid type repetition * Add mongoc_array_aligned_init * Fix alignment of allocated objects * Add documentation for pool_node layout * Add -fno-sanitize-recover=alignment to debug-compile-ubsan * Add debug-compile-ubsan-with-extra-alignment
1 parent c7a0498 commit 648ee8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+569
-170
lines changed

.evergreen/config.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ tasks:
11801180
script: |-
11811181
set -o errexit
11821182
export CC="clang-3.8"
1183-
export CFLAGS="-fno-omit-frame-pointer"
1183+
export CFLAGS="-fno-omit-frame-pointer -fno-sanitize-recover=alignment"
11841184
export CHECK_LOG="ON"
11851185
export DEBUG="ON"
11861186
export EXTRA_CONFIGURE_FLAGS="-DENABLE_EXTRA_ALIGNMENT=OFF"
@@ -1191,6 +1191,29 @@ tasks:
11911191
export ZSTD="OFF"
11921192
CC='${CC}' MARCH='${MARCH}' sh .evergreen/compile.sh
11931193
- func: upload build
1194+
- name: debug-compile-ubsan-with-extra-alignment
1195+
tags:
1196+
- special
1197+
commands:
1198+
- command: shell.exec
1199+
type: test
1200+
params:
1201+
working_dir: mongoc
1202+
shell: bash
1203+
script: |-
1204+
set -o errexit
1205+
export CC="clang-3.8"
1206+
export CFLAGS="-fno-omit-frame-pointer -fno-sanitize-recover=alignment"
1207+
export CHECK_LOG="ON"
1208+
export DEBUG="ON"
1209+
export EXTRA_CONFIGURE_FLAGS="-DENABLE_EXTRA_ALIGNMENT=ON"
1210+
export PATH="/usr/lib/llvm-3.8/bin:$PATH"
1211+
export SANITIZE="undefined"
1212+
export SNAPPY="OFF"
1213+
export ZLIB="BUNDLED"
1214+
export ZSTD="OFF"
1215+
CC='${CC}' MARCH='${MARCH}' sh .evergreen/compile.sh
1216+
- func: upload build
11941217
- name: debug-compile-scan-build
11951218
tags:
11961219
- clang
@@ -17189,7 +17212,7 @@ tasks:
1718917212
# Compile mongoc-ping. Disable unnecessary dependencies since mongoc-ping is copied to a remote Ubuntu 18.04 ECS cluster for testing, which may not have all dependent libraries.
1719017213
. .evergreen/find-cmake.sh
1719117214
export CC='${CC}'
17192-
$CMAKE -DENABLE_SASL=OFF -DENABLE_SNAPPY=OFF -DENABLE_ZSTD=OFF -DENABLE_CLIENT_SIDE_ENCRYPTION=OFF .
17215+
$CMAKE -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DENABLE_SASL=OFF -DENABLE_SNAPPY=OFF -DENABLE_ZSTD=OFF -DENABLE_CLIENT_SIDE_ENCRYPTION=OFF .
1719317216
$CMAKE --build . --target mongoc-ping
1719417217
- func: upload build
1719517218
- name: test-aws-openssl-regular-latest
@@ -24239,6 +24262,7 @@ buildvariants:
2423924262
- debug-compile-scan-build
2424024263
- debug-compile-asan-clang
2424124264
- debug-compile-ubsan
24265+
- debug-compile-ubsan-with-extra-alignment
2424224266
- release-compile
2424324267
- debug-compile-nosasl-nossl
2424424268
- debug-compile-no-align

.evergreen/link-sample-program-bson.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ $TAR xf ../../mongoc.tar.gz -C . --strip-components=1
4848

4949
if [ "$LINK_STATIC" ]; then
5050
# Our CMake system builds shared and static by default.
51-
$CMAKE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DENABLE_TESTS=OFF -DENABLE_BSON=ON .
51+
$CMAKE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DENABLE_TESTS=OFF -DENABLE_BSON=ON .
5252
$CMAKE --build .
5353
$CMAKE --build . --target install
5454
else
55-
$CMAKE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DENABLE_TESTS=OFF -DENABLE_BSON=ON -DENABLE_STATIC=OFF .
55+
$CMAKE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DENABLE_TESTS=OFF -DENABLE_BSON=ON -DENABLE_STATIC=OFF .
5656
$CMAKE --build .
5757
$CMAKE --build . --target install
5858

.evergreen/link-sample-program-mingw-bson.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cd %BUILD_DIR%
2727
%TAR% xf ..\..\mongoc.tar.gz -C . --strip-components=1
2828

2929
rem Build libmongoc, with flags that the downstream R driver mongolite uses
30-
%CMAKE% -G "MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=%CMAKE_MAKE_PROGRAM% -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DCMAKE_PREFIX_PATH=%INSTALL_DIR%\lib\cmake -DENABLE_BSON=ON -DENABLE_STATIC=ON .
30+
%CMAKE% -G "MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=%CMAKE_MAKE_PROGRAM% -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DCMAKE_CFLAGS="-pedantic" -DCMAKE_PREFIX_PATH=%INSTALL_DIR%\lib\cmake -DENABLE_BSON=ON -DENABLE_STATIC=ON .
3131
%CMAKE% --build .
3232
if errorlevel 1 (
3333
exit /B 1

.evergreen/link-sample-program-mingw.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cd %BUILD_DIR%
2727
%TAR% xf ..\..\mongoc.tar.gz -C . --strip-components=1
2828

2929
rem Build libmongoc, with flags that the downstream R driver mongolite uses
30-
%CMAKE% -G "MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=%CMAKE_MAKE_PROGRAM% -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DCMAKE_PREFIX_PATH=%INSTALL_DIR%\lib\cmake %CMAKE_FLAGS% -DENABLE_BSON=ON .
30+
%CMAKE% -G "MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=%CMAKE_MAKE_PROGRAM% -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DCMAKE_CFLAGS="-pedantic" -DCMAKE_PREFIX_PATH=%INSTALL_DIR%\lib\cmake %CMAKE_FLAGS% -DENABLE_BSON=ON .
3131
%CMAKE% --build .
3232
if errorlevel 1 (
3333
exit /B 1

.evergreen/link-sample-program-msvc-bson.cmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ cd %BUILD_DIR%
2727
%TAR% xf ..\..\mongoc.tar.gz -C . --strip-components=1
2828

2929
if "%LINK_STATIC%"=="1" (
30-
%CMAKE% -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DENABLE_TESTS=OFF -DENABLE_BSON=ON .
30+
%CMAKE% -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DENABLE_TESTS=OFF -DENABLE_BSON=ON .
3131
) else (
32-
%CMAKE% -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DENABLE_TESTS=OFF -DENABLE_BSON=ON -DENABLE_STATIC=OFF .
32+
%CMAKE% -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DENABLE_TESTS=OFF -DENABLE_BSON=ON -DENABLE_STATIC=OFF .
3333
)
3434

3535
%CMAKE% --build . --target ALL_BUILD --config "Debug" -- /m

.evergreen/link-sample-program-msvc.cmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ if "%ENABLE_SNAPPY%"=="1" (
4545
cd %BUILD_DIR%
4646
rem Build libmongoc
4747
if "%ENABLE_SSL%"=="1" (
48-
%CMAKE% -G "Visual Studio 15 2017 Win64" -DCMAKE_PREFIX_PATH=%INSTALL_DIR%\lib\cmake -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DENABLE_BSON=ON -DENABLE_SSL=WINDOWS %ENABLE_SNAPPY_OPTION% .
48+
%CMAKE% -G "Visual Studio 15 2017 Win64" -DCMAKE_PREFIX_PATH=%INSTALL_DIR%\lib\cmake -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DENABLE_BSON=ON -DENABLE_SSL=WINDOWS %ENABLE_SNAPPY_OPTION% .
4949
) else (
50-
%CMAKE% -G "Visual Studio 15 2017 Win64" -DCMAKE_PREFIX_PATH=%INSTALL_DIR%\lib\cmake -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DENABLE_BSON=ON -DENABLE_SSL=OFF %ENABLE_SNAPPY_OPTION% .
50+
%CMAKE% -G "Visual Studio 15 2017 Win64" -DCMAKE_PREFIX_PATH=%INSTALL_DIR%\lib\cmake -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DENABLE_BSON=ON -DENABLE_SSL=OFF %ENABLE_SNAPPY_OPTION% .
5151
)
5252

5353
%CMAKE% --build . --target ALL_BUILD --config "Debug" -- /m

.evergreen/link-sample-program.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ else
8181
fi
8282

8383

84-
$CMAKE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DCMAKE_PREFIX_PATH=$INSTALL_DIR/lib/cmake $SSL_CMAKE_OPTION $SNAPPY_CMAKE_OPTION $STATIC_CMAKE_OPTION -DENABLE_BSON=ON -DENABLE_ZSTD=$ZSTD .
84+
$CMAKE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DCMAKE_PREFIX_PATH=$INSTALL_DIR/lib/cmake -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF $SSL_CMAKE_OPTION $SNAPPY_CMAKE_OPTION $STATIC_CMAKE_OPTION -DENABLE_BSON=ON -DENABLE_ZSTD=$ZSTD .
8585
$CMAKE --build .
8686
$CMAKE --build . --target install
8787

build/evergreen_config_lib/tasks.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,19 @@ def __init__(self, task_name, suffix_commands, orchestration=True, **kwargs):
241241
SpecialTask('debug-compile-ubsan',
242242
compression='zlib',
243243
CC='clang-3.8',
244-
CFLAGS='-fno-omit-frame-pointer',
244+
CFLAGS='-fno-omit-frame-pointer -fno-sanitize-recover=alignment',
245245
CHECK_LOG='ON',
246246
sanitize=['undefined'],
247247
EXTRA_CONFIGURE_FLAGS="-DENABLE_EXTRA_ALIGNMENT=OFF",
248248
PATH='/usr/lib/llvm-3.8/bin:$PATH'),
249+
SpecialTask('debug-compile-ubsan-with-extra-alignment',
250+
compression='zlib',
251+
CC='clang-3.8',
252+
CFLAGS='-fno-omit-frame-pointer -fno-sanitize-recover=alignment',
253+
CHECK_LOG='ON',
254+
sanitize=['undefined'],
255+
EXTRA_CONFIGURE_FLAGS="-DENABLE_EXTRA_ALIGNMENT=ON",
256+
PATH='/usr/lib/llvm-3.8/bin:$PATH'),
249257
SpecialTask('debug-compile-scan-build',
250258
tags=['clang', 'debug-compile', 'scan-build'],
251259
continue_on_err=True,
@@ -975,7 +983,7 @@ def _check_allowed(self):
975983
# Compile mongoc-ping. Disable unnecessary dependencies since mongoc-ping is copied to a remote Ubuntu 18.04 ECS cluster for testing, which may not have all dependent libraries.
976984
. .evergreen/find-cmake.sh
977985
export CC='${CC}'
978-
$CMAKE -DENABLE_SASL=OFF -DENABLE_SNAPPY=OFF -DENABLE_ZSTD=OFF -DENABLE_CLIENT_SIDE_ENCRYPTION=OFF .
986+
$CMAKE -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=ON -DCMAKE_C_EXTENSIONS=OFF -DENABLE_SASL=OFF -DENABLE_SNAPPY=OFF -DENABLE_ZSTD=OFF -DENABLE_CLIENT_SIDE_ENCRYPTION=OFF .
979987
$CMAKE --build . --target mongoc-ping
980988
'''), func('upload build')])
981989

build/evergreen_config_lib/variants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def days(n):
183183
'debug-compile-scan-build',
184184
'debug-compile-asan-clang',
185185
'debug-compile-ubsan',
186+
'debug-compile-ubsan-with-extra-alignment',
186187
'release-compile',
187188
'debug-compile-nosasl-nossl',
188189
'debug-compile-no-align',
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
:man_page: bson_aligned_alloc
2+
3+
bson_aligned_alloc()
4+
====================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void *
12+
bson_aligned_alloc (size_t alignment, size_t num_bytes);
13+
14+
Parameters
15+
----------
16+
17+
* ``alignment``: The alignment of the allocated bytes of memory. Must be a power of 2 and a multiple of ``sizeof (void *)``.
18+
* ``num_bytes``: The number of bytes to allocate. Must be a multiple of ``alignment``.
19+
20+
Description
21+
-----------
22+
23+
This is a portable ``aligned_alloc()`` wrapper.
24+
25+
In general, this function will return an allocation at least ``sizeof(void*)`` bytes or bigger with an alignment of at least ``alignment``.
26+
27+
If there was a failure to allocate ``num_bytes`` bytes aligned to ``alignment``, the process will be aborted.
28+
29+
.. warning::
30+
31+
This function will abort on failure to allocate memory.
32+
33+
Returns
34+
-------
35+
36+
A pointer to a memory region which *HAS NOT* been zeroed.

0 commit comments

Comments
 (0)