From 5da3fa21a4d849a8f889ce0429dc2630999325b9 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:05:31 -0600 Subject: [PATCH 01/15] Add UBSan coverage with extra alignment --- .../config_generator/components/sanitizers.py | 3 +- .evergreen/generated_configs/tasks.yml | 970 +++++++++++++++++- 2 files changed, 954 insertions(+), 19 deletions(-) diff --git a/.evergreen/config_generator/components/sanitizers.py b/.evergreen/config_generator/components/sanitizers.py index 5afa5a7e6f..b06c6c7998 100644 --- a/.evergreen/config_generator/components/sanitizers.py +++ b/.evergreen/config_generator/components/sanitizers.py @@ -22,8 +22,7 @@ # pylint: disable=line-too-long # fmt: off MATRIX = [ - ('rhel80', ['asan', ], ['shared', 'static'], [False, True], ['4.0', '8.0', 'latest'], ['single', 'replica', 'sharded']), - ('rhel80', [ 'ubsan'], [ 'static'], [False ], ['4.0', '8.0', 'latest'], ['single', 'replica', 'sharded']), + ('rhel80', ['asan', 'ubsan'], ['shared', 'static'], [False, True], ['4.0', '8.0', 'latest'], ['single', 'replica', 'sharded']), ] # fmt: on # pylint: enable=line-too-long diff --git a/.evergreen/generated_configs/tasks.yml b/.evergreen/generated_configs/tasks.yml index 067b980a93..bb3b5b0749 100644 --- a/.evergreen/generated_configs/tasks.yml +++ b/.evergreen/generated_configs/tasks.yml @@ -15947,9 +15947,728 @@ tasks: example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=address -fno-omit-frame-pointer example_projects_ldflags: -fsanitize=address + - name: sanitizers-ubsan-rhel80-clang-shared-4.0-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, "4.0", replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: "4.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-4.0-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, "4.0", sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: "4.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-4.0-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, "4.0", single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + mongodb_version: "4.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-8.0-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, "8.0", replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: "8.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-8.0-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, "8.0", sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: "8.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-8.0-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, "8.0", single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + mongodb_version: "8.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-4.0-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, "4.0", replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: "4.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-4.0-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, "4.0", sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: "4.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-4.0-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, "4.0", single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + mongodb_version: "4.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-8.0-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, "8.0", replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: "8.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-8.0-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, "8.0", sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: "8.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-8.0-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, "8.0", single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + mongodb_version: "8.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-latest-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, latest, replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: latest + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-latest-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, latest, sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: latest + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-extra_alignment-latest-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, extra_alignment, latest, single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + mongodb_version: latest + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-latest-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, latest, replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: latest + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-latest-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, latest, sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: latest + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-shared-latest-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, shared, latest, single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - func: setup + - func: start_mongod + vars: + mongodb_version: latest + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined - name: sanitizers-ubsan-rhel80-clang-static-4.0-replica run_on: rhel80-large - tags: [sanitizers, ubsan, rhel80, clang, static, "4.0", replica] + tags: [sanitizers, ubsan, rhel80, clang, static, "4.0", replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: "4.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-4.0-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, "4.0", sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: "4.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-4.0-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, "4.0", single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + mongodb_version: "4.0" + - func: install_c_driver + vars: + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + ENABLE_TESTS: "ON" + RUN_DISTCHECK: 1 + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-8.0-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", replica] commands: - command: expansions.update params: @@ -15962,7 +16681,7 @@ tasks: - func: start_mongod vars: TOPOLOGY: replica_set - mongodb_version: "4.0" + mongodb_version: "8.0" - func: install_c_driver vars: SKIP_INSTALL_LIBMONGOCRYPT: 1 @@ -15982,9 +16701,9 @@ tasks: example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined - - name: sanitizers-ubsan-rhel80-clang-static-4.0-sharded + - name: sanitizers-ubsan-rhel80-clang-static-8.0-sharded run_on: rhel80-large - tags: [sanitizers, ubsan, rhel80, clang, static, "4.0", sharded] + tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", sharded] commands: - command: expansions.update params: @@ -15997,7 +16716,7 @@ tasks: - func: start_mongod vars: TOPOLOGY: sharded_cluster - mongodb_version: "4.0" + mongodb_version: "8.0" - func: install_c_driver vars: SKIP_INSTALL_LIBMONGOCRYPT: 1 @@ -16017,9 +16736,9 @@ tasks: example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined - - name: sanitizers-ubsan-rhel80-clang-static-4.0-single + - name: sanitizers-ubsan-rhel80-clang-static-8.0-single run_on: rhel80-large - tags: [sanitizers, ubsan, rhel80, clang, static, "4.0", single] + tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", single] commands: - command: expansions.update params: @@ -16031,7 +16750,7 @@ tasks: - func: setup - func: start_mongod vars: - mongodb_version: "4.0" + mongodb_version: "8.0" - func: install_c_driver vars: SKIP_INSTALL_LIBMONGOCRYPT: 1 @@ -16051,9 +16770,116 @@ tasks: example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined - - name: sanitizers-ubsan-rhel80-clang-static-8.0-replica + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-4.0-replica run_on: rhel80-large - tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", replica] + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "4.0", replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: "4.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-4.0-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "4.0", sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: "4.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-4.0-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "4.0", single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + mongodb_version: "4.0" + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-8.0-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "8.0", replica] commands: - command: expansions.update params: @@ -16069,12 +16895,13 @@ tasks: mongodb_version: "8.0" - func: install_c_driver vars: + BSON_EXTRA_ALIGNMENT: 1 SKIP_INSTALL_LIBMONGOCRYPT: 1 - func: install-uv - func: compile vars: + BSON_EXTRA_ALIGNMENT: 1 ENABLE_TESTS: "ON" - RUN_DISTCHECK: 1 USE_SANITIZER_UBSAN: "ON" - func: fetch-det - func: run_kms_servers @@ -16086,9 +16913,9 @@ tasks: example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined - - name: sanitizers-ubsan-rhel80-clang-static-8.0-sharded + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-8.0-sharded run_on: rhel80-large - tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", sharded] + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "8.0", sharded] commands: - command: expansions.update params: @@ -16104,12 +16931,13 @@ tasks: mongodb_version: "8.0" - func: install_c_driver vars: + BSON_EXTRA_ALIGNMENT: 1 SKIP_INSTALL_LIBMONGOCRYPT: 1 - func: install-uv - func: compile vars: + BSON_EXTRA_ALIGNMENT: 1 ENABLE_TESTS: "ON" - RUN_DISTCHECK: 1 USE_SANITIZER_UBSAN: "ON" - func: fetch-det - func: run_kms_servers @@ -16121,9 +16949,9 @@ tasks: example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined - - name: sanitizers-ubsan-rhel80-clang-static-8.0-single + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-8.0-single run_on: rhel80-large - tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", single] + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "8.0", single] commands: - command: expansions.update params: @@ -16138,12 +16966,120 @@ tasks: mongodb_version: "8.0" - func: install_c_driver vars: + BSON_EXTRA_ALIGNMENT: 1 SKIP_INSTALL_LIBMONGOCRYPT: 1 - func: install-uv - func: compile vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: single + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-latest-replica + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, latest, replica] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: replica_set + mongodb_version: latest + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: replica + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-latest-sharded + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, latest, sharded] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + TOPOLOGY: sharded_cluster + mongodb_version: latest + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 + ENABLE_TESTS: "ON" + USE_SANITIZER_UBSAN: "ON" + - func: fetch-det + - func: run_kms_servers + - func: test + vars: + MONGOCXX_TEST_TOPOLOGY: sharded + TEST_WITH_UBSAN: "ON" + example_projects_cc: clang + example_projects_cxx: clang++ + example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-latest-single + run_on: rhel80-large + tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, latest, single] + commands: + - command: expansions.update + params: + updates: + - { key: build_type, value: Debug } + - { key: cc_compiler, value: /opt/mongodbtoolchain/v4/bin/clang } + - { key: cxx_compiler, value: /opt/mongodbtoolchain/v4/bin/clang++ } + - { key: USE_STATIC_LIBS, value: "1" } + - func: setup + - func: start_mongod + vars: + mongodb_version: latest + - func: install_c_driver + vars: + BSON_EXTRA_ALIGNMENT: 1 + SKIP_INSTALL_LIBMONGOCRYPT: 1 + - func: install-uv + - func: compile + vars: + BSON_EXTRA_ALIGNMENT: 1 ENABLE_TESTS: "ON" - RUN_DISTCHECK: 1 USE_SANITIZER_UBSAN: "ON" - func: fetch-det - func: run_kms_servers From 8352fb7e33bf354b073dce13091a62b67ad24d1d Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:05:31 -0600 Subject: [PATCH 02/15] Add bsoncxx::aligned_object --- src/bsoncxx/lib/CMakeLists.txt | 1 + .../bsoncxx/private/aligned_storage.hh | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh diff --git a/src/bsoncxx/lib/CMakeLists.txt b/src/bsoncxx/lib/CMakeLists.txt index 4103e8c825..f7eae596d0 100644 --- a/src/bsoncxx/lib/CMakeLists.txt +++ b/src/bsoncxx/lib/CMakeLists.txt @@ -86,6 +86,7 @@ set_dist_list(src_bsoncxx_lib_DIST bsoncxx/v_noabi/bsoncxx/config/private/config.hh.in bsoncxx/v_noabi/bsoncxx/config/private/postlude.hh bsoncxx/v_noabi/bsoncxx/config/private/prelude.hh + bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh bsoncxx/v_noabi/bsoncxx/private/b64_ntop.hh bsoncxx/v_noabi/bsoncxx/private/helpers.hh bsoncxx/v_noabi/bsoncxx/private/make_unique.hh diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh new file mode 100644 index 0000000000..f95be4f8c5 --- /dev/null +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh @@ -0,0 +1,52 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include + +namespace bsoncxx { + +// Workaround lack of aligned allocation support prior to C++17. +// +// Unlike `std::aligned_storage` and `alignas`, this type does NOT propagate alignment requirements to the parent +// object. This permits correct behavior despite use with regular (alignment-unaware) allocation functions. +// +// `alignof(T)` must be explicitly provided by the user for correct behavior, as template parameters do not necessarily +// inherit attributes such as alignment, e.g. `std::size_t alignment = alignof(T)` does not work as expected. +template +class aligned_storage { + // Sanity check. + static_assert(size >= alignment, "sizeof(T) >= alignof(T) must be true"); + + private: + unsigned char _storage[2u * size]; + + public: + void* get() BSONCXX_PRIVATE_IF_GNU_LIKE(__attribute__((assume_aligned(alignment)))) + { + void* ptr = _storage; + std::size_t space = sizeof(_storage); + return std::align(alignment, size, ptr, space); + } + + void const* get() const BSONCXX_PRIVATE_IF_GNU_LIKE(__attribute__((assume_aligned(alignment)))) + { + return const_cast(this)->get(); + } +}; + +} // namespace bsoncxx From aa28f840e2f22be91818d4537fc03c15ea056080 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:05:31 -0600 Subject: [PATCH 03/15] Avoid leaking extended alignment requirements to parent objects --- .../bsoncxx/v_noabi/bsoncxx/builder/core.cpp | 38 +++++++++++++------ .../mongocxx/private/client_session.hh | 12 ++++-- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp index 7cc4931c59..ae84236388 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -44,8 +45,8 @@ void bson_free_deleter(std::uint8_t* ptr) { // class managed_bson_t { public: - managed_bson_t() { - bson_init(&bson); + managed_bson_t() : bson_ptr(new(bson_storage.get()) bson_t) { + bson_init(bson_ptr); } managed_bson_t(managed_bson_t&&) = delete; @@ -55,15 +56,17 @@ class managed_bson_t { managed_bson_t& operator=(managed_bson_t const&) = delete; ~managed_bson_t() { - bson_destroy(&bson); + bson_destroy(bson_ptr); + bson_ptr->~bson_t(); } bson_t* get() { - return &bson; + return bson_ptr; } private: - bson_t bson; + bsoncxx::aligned_storage bson_storage; + bson_t* bson_ptr; }; } // namespace @@ -116,7 +119,7 @@ class core::impl { if (_stack.empty()) { return _root.get(); } else { - return &_stack.back().bson; + return _stack.back().bson_ptr; } } @@ -202,14 +205,24 @@ class core::impl { private: struct frame { + ~frame() { + bson_ptr->~bson_t(); + } + + frame(frame&&) = delete; + frame& operator=(frame&&) = delete; + + frame(frame const&) = delete; + frame& operator=(frame const&) = delete; + frame(bson_t* parent, char const* key, std::int32_t len, bool is_array) - : n(0), is_array(is_array), parent(parent) { + : n(0), is_array(is_array), bson_ptr(new(bson_storage.get()) bson_t), parent(parent) { if (is_array) { - if (!bson_append_array_begin(parent, key, len, &bson)) { + if (!bson_append_array_begin(parent, key, len, bson_ptr)) { throw bsoncxx::v_noabi::exception{error_code::k_cannot_begin_appending_array}; } } else { - if (!bson_append_document_begin(parent, key, len, &bson)) { + if (!bson_append_document_begin(parent, key, len, bson_ptr)) { throw bsoncxx::v_noabi::exception{error_code::k_cannot_begin_appending_document}; } } @@ -217,11 +230,11 @@ class core::impl { void close() { if (is_array) { - if (!bson_append_array_end(parent, &bson)) { + if (!bson_append_array_end(parent, bson_ptr)) { throw bsoncxx::v_noabi::exception{error_code::k_cannot_end_appending_array}; } } else { - if (!bson_append_document_end(parent, &bson)) { + if (!bson_append_document_end(parent, bson_ptr)) { throw bsoncxx::v_noabi::exception{error_code::k_cannot_end_appending_document}; } } @@ -229,7 +242,8 @@ class core::impl { std::size_t n; bool is_array; - bson_t bson; + bsoncxx::aligned_storage bson_storage; + bson_t* bson_ptr; bson_t* parent; }; diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh index b3e2803aa8..503f663830 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh @@ -18,6 +18,7 @@ #include +#include #include #include @@ -73,7 +74,11 @@ bool with_transaction_cpp_cb(mongoc_client_session_t*, void* ctx, bson_t** reply class client_session::impl { public: impl(mongocxx::v_noabi::client const* client, options::client_session const& session_options) - : _client(client), _options(session_options), _session_t(nullptr, nullptr) { + : _client(client), + _options(session_options), + _session_t(nullptr, nullptr), + _empty_cluster_time_storage(), // -Wuninitialized + _empty_cluster_time_ptr(new(_empty_cluster_time_storage.get()) bson_t BSON_INITIALIZER) { // Create a mongoc_session_opts_t from session_options. std::unique_ptr opt_t{ libmongoc::session_opts_new(), libmongoc::session_opts_destroy}; @@ -123,7 +128,7 @@ class client_session::impl { return bsoncxx::helpers::view_from_bson_t(ct); } - return bsoncxx::helpers::view_from_bson_t(&_empty_cluster_time); + return bsoncxx::helpers::view_from_bson_t(_empty_cluster_time_ptr); } bsoncxx::v_noabi::types::b_timestamp operation_time() const noexcept { @@ -236,7 +241,8 @@ class client_session::impl { unique_session _session_t; - bson_t _empty_cluster_time = BSON_INITIALIZER; + bsoncxx::aligned_storage _empty_cluster_time_storage; + bson_t const* _empty_cluster_time_ptr; // Just a long-lasting empty bson_t. Destruction is not required. }; } // namespace v_noabi From d946a2b06a1c4ff3b284e2e4d384330a0fa6f8ba Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:05:32 -0600 Subject: [PATCH 04/15] Replace std::aligned_storage with alignas + sizeof --- src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh | 2 +- src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh index 173edaf6d2..01aa92afaa 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh @@ -94,7 +94,7 @@ class stack { } private: - typename std::aligned_storage::type _object_memory[size]; + alignas(T) unsigned char _object_memory[size * sizeof(T)]; std::list _buckets; diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp index 233a11699a..fbecf2ee66 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp @@ -74,7 +74,7 @@ void user_log_handler( // A region of memory that acts as a sentintel value indicating that an instance object is being // destroyed. We only care about the address of this object, never its contents. -typename std::aligned_storage::type sentinel; +alignas(instance) unsigned char sentinel[sizeof(instance)]; std::atomic current_instance{nullptr}; static_assert(std::is_standard_layout::value, "Must be standard layout"); From 13c3bf239dab133acf4f5c85729e5dcfa3693a4e Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:05:32 -0600 Subject: [PATCH 05/15] Address -Walign-mismatch warnings for bson_t* returned by test_util::mock --- .../v_noabi/mongocxx/test_util/mock.hh | 71 +++++++++++++++---- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh index e505aa9d28..21b8d41900 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh @@ -14,6 +14,8 @@ #pragma once +#include + #include #include #include @@ -33,11 +35,61 @@ namespace mongocxx { namespace test_util { +template +struct mock_invoke_fn { + template + static R invoke(Self self, Args... args) { + auto crtp = static_cast(self); + auto instance = crtp->active_instance(); + if (instance) { + while (!instance->_callbacks.empty()) { + if (instance->_callbacks.top()._conditional(args...)) { + return instance->_callbacks.top()._callback(args...); + } + instance->_callbacks.pop(); + } + } + + return crtp->_func(args...); + } +}; + +template +struct mock_invoke; + +template +struct mock_invoke { + R operator()(Args... args) { + return mock_invoke_fn::invoke(this, args...); + } +}; + +BSONCXX_PRIVATE_WARNINGS_PUSH(); +BSONCXX_PRIVATE_WARNINGS_DISABLE(GNU("-Wignored-attributes")); + +template +struct mock_invoke { + bson_t const* operator()(Args... args) BSONCXX_PRIVATE_IF_GNU_LIKE(__attribute__((assume_aligned(alignof(bson_t))))) + { + return mock_invoke_fn::invoke(this, args...); + } +}; + +template +struct mock_invoke { + bson_t* operator()(Args... args) BSONCXX_PRIVATE_IF_GNU_LIKE(__attribute__((assume_aligned(alignof(bson_t))))) + { + return mock_invoke_fn::invoke(this, args...); + } +}; + +BSONCXX_PRIVATE_WARNINGS_POP(); + template class mock; template -class mock { +class mock : public mock_invoke, R(Args...)> { public: using underlying_ptr = R (*)(Args...); using callback = std::function; @@ -45,6 +97,7 @@ class mock { class rule { friend mock; + friend mock_invoke_fn, R>; public: rule(callback callback) : _callback(std::move(callback)) { @@ -70,6 +123,7 @@ class mock { class instance { friend mock; + friend mock_invoke_fn, R>; public: instance(instance const&) = delete; @@ -128,26 +182,13 @@ class mock { }; friend instance; + friend mock_invoke_fn, R>; mock(underlying_ptr func) : _func(std::move(func)) {} mock(mock&&) = delete; mock(mock const&) = delete; mock& operator=(mock const&) = delete; - R operator()(Args... args) { - auto instance = active_instance(); - if (instance) { - while (!instance->_callbacks.empty()) { - if (instance->_callbacks.top()._conditional(args...)) { - return instance->_callbacks.top()._callback(args...); - } - instance->_callbacks.pop(); - } - } - - return _func(args...); - } - std::unique_ptr create_instance() { std::unique_ptr mock_instance(new instance(this)); active_instance(mock_instance.get()); From f86ebb1b87a3a8e1312d9413c3cf54201cf3d38b Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:05:32 -0600 Subject: [PATCH 06/15] Address -Walign-mismatch warnings for bson_t* passed by stack::emplace_back --- .../bsoncxx/v_noabi/bsoncxx/private/stack.hh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh index 01aa92afaa..5b1920621d 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh @@ -80,6 +80,24 @@ class stack { new (_get_ptr()) T(std::forward(args)...); } + // Avoid -Walign-mismatch warnings due to the `bson_t*` argument losing + // its alignment attribute when specified as a template parameter. + BSONCXX_PRIVATE_WARNINGS_PUSH(); + BSONCXX_PRIVATE_WARNINGS_DISABLE(GNU("-Wignored-attributes")); + template < + typename... Args, + typename std::enable_if::value>::type* = nullptr> + void emplace_back(bson_t* bson, Args&&... args) { + if (_is_empty) { + _is_empty = false; + } else { + _inc(); + } + + new (_get_ptr()) T(bson, std::forward(args)...); + } + BSONCXX_PRIVATE_WARNINGS_POP(); + void pop_back() { _get_ptr()->close(); _dec(); From 321e6c16f0c9e3855e9582e50f385472f65a9372 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:05:33 -0600 Subject: [PATCH 07/15] Add -static-libsan when compiling UBSan tasks with static libs --- .../config_generator/components/sanitizers.py | 2 +- .evergreen/generated_configs/tasks.yml | 36 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/.evergreen/config_generator/components/sanitizers.py b/.evergreen/config_generator/components/sanitizers.py index b06c6c7998..1f1057cbef 100644 --- a/.evergreen/config_generator/components/sanitizers.py +++ b/.evergreen/config_generator/components/sanitizers.py @@ -100,7 +100,7 @@ def tasks(): test_vars |= { 'TEST_WITH_UBSAN': 'ON', 'example_projects_cxxflags': '-fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer', - 'example_projects_ldflags': '-fsanitize=undefined -fno-sanitize-recover=undefined', + 'example_projects_ldflags': '-fsanitize=undefined -fno-sanitize-recover=undefined' + (' -static-libsan' if link_type == 'static' else ''), } commands += [ diff --git a/.evergreen/generated_configs/tasks.yml b/.evergreen/generated_configs/tasks.yml index bb3b5b0749..34c63de528 100644 --- a/.evergreen/generated_configs/tasks.yml +++ b/.evergreen/generated_configs/tasks.yml @@ -16596,7 +16596,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-4.0-sharded run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, "4.0", sharded] @@ -16631,7 +16631,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-4.0-single run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, "4.0", single] @@ -16665,7 +16665,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-8.0-replica run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", replica] @@ -16700,7 +16700,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-8.0-sharded run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", sharded] @@ -16735,7 +16735,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-8.0-single run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, "8.0", single] @@ -16769,7 +16769,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-4.0-replica run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "4.0", replica] @@ -16805,7 +16805,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-4.0-sharded run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "4.0", sharded] @@ -16841,7 +16841,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-4.0-single run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "4.0", single] @@ -16876,7 +16876,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-8.0-replica run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "8.0", replica] @@ -16912,7 +16912,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-8.0-sharded run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "8.0", sharded] @@ -16948,7 +16948,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-8.0-single run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, "8.0", single] @@ -16983,7 +16983,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-latest-replica run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, latest, replica] @@ -17019,7 +17019,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-latest-sharded run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, latest, sharded] @@ -17055,7 +17055,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-extra_alignment-latest-single run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, extra_alignment, latest, single] @@ -17090,7 +17090,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-latest-replica run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, latest, replica] @@ -17125,7 +17125,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-latest-sharded run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, latest, sharded] @@ -17160,7 +17160,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: sanitizers-ubsan-rhel80-clang-static-latest-single run_on: rhel80-large tags: [sanitizers, ubsan, rhel80, clang, static, latest, single] @@ -17194,7 +17194,7 @@ tasks: example_projects_cc: clang example_projects_cxx: clang++ example_projects_cxxflags: -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer - example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined + example_projects_ldflags: -fsanitize=undefined -fno-sanitize-recover=undefined -static-libsan - name: scan-build-rhel80-std11-default run_on: rhel80-large tags: [scan-build, rhel80, std11] From 0b82b0f07f16a5211352d875fffacdd30bf7b962 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:28:00 -0600 Subject: [PATCH 08/15] Remove unnecessary assume_aligned attributes for specializations --- .../lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh index 21b8d41900..b382199310 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/test_util/mock.hh @@ -69,16 +69,14 @@ BSONCXX_PRIVATE_WARNINGS_DISABLE(GNU("-Wignored-attributes")); template struct mock_invoke { - bson_t const* operator()(Args... args) BSONCXX_PRIVATE_IF_GNU_LIKE(__attribute__((assume_aligned(alignof(bson_t))))) - { + bson_t const* operator()(Args... args) { return mock_invoke_fn::invoke(this, args...); } }; template struct mock_invoke { - bson_t* operator()(Args... args) BSONCXX_PRIVATE_IF_GNU_LIKE(__attribute__((assume_aligned(alignof(bson_t))))) - { + bson_t* operator()(Args... args) { return mock_invoke_fn::invoke(this, args...); } }; From 1db333b3d44ca7b15d5c1a1807b1948481aeae11 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:32:26 -0600 Subject: [PATCH 09/15] Remove unnecessary SFINAE on bson_t* parameter --- src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh index 5b1920621d..01c0b0dbe5 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/stack.hh @@ -80,13 +80,7 @@ class stack { new (_get_ptr()) T(std::forward(args)...); } - // Avoid -Walign-mismatch warnings due to the `bson_t*` argument losing - // its alignment attribute when specified as a template parameter. - BSONCXX_PRIVATE_WARNINGS_PUSH(); - BSONCXX_PRIVATE_WARNINGS_DISABLE(GNU("-Wignored-attributes")); - template < - typename... Args, - typename std::enable_if::value>::type* = nullptr> + template void emplace_back(bson_t* bson, Args&&... args) { if (_is_empty) { _is_empty = false; @@ -96,7 +90,6 @@ class stack { new (_get_ptr()) T(bson, std::forward(args)...); } - BSONCXX_PRIVATE_WARNINGS_POP(); void pop_back() { _get_ptr()->close(); From 60d212f9e28cadf4aaa8f96e36b5f8e32e013559 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 09:32:46 -0600 Subject: [PATCH 10/15] Use default member initialization for simplicity --- .../v_noabi/mongocxx/private/client_session.hh | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh index 503f663830..be1ae85291 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh @@ -74,11 +74,7 @@ bool with_transaction_cpp_cb(mongoc_client_session_t*, void* ctx, bson_t** reply class client_session::impl { public: impl(mongocxx::v_noabi::client const* client, options::client_session const& session_options) - : _client(client), - _options(session_options), - _session_t(nullptr, nullptr), - _empty_cluster_time_storage(), // -Wuninitialized - _empty_cluster_time_ptr(new(_empty_cluster_time_storage.get()) bson_t BSON_INITIALIZER) { + : _client(client), _options(session_options), _session_t(nullptr, nullptr) { // Create a mongoc_session_opts_t from session_options. std::unique_ptr opt_t{ libmongoc::session_opts_new(), libmongoc::session_opts_destroy}; @@ -241,8 +237,9 @@ class client_session::impl { unique_session _session_t; - bsoncxx::aligned_storage _empty_cluster_time_storage; - bson_t const* _empty_cluster_time_ptr; // Just a long-lasting empty bson_t. Destruction is not required. + // Just a long-lasting empty bson_t. Destruction is not required. + bsoncxx::aligned_storage _empty_cluster_time_storage; // -Wuninitialized + bson_t const* _empty_cluster_time_ptr = new (_empty_cluster_time_storage.get()) bson_t BSON_INITIALIZER; }; } // namespace v_noabi From da7af7e0dab93a6a01f0128606456f4f4e867728 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 10:07:48 -0600 Subject: [PATCH 11/15] Remove stray comment --- .../lib/mongocxx/v_noabi/mongocxx/private/client_session.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh index be1ae85291..8750c53f9d 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/client_session.hh @@ -238,7 +238,7 @@ class client_session::impl { unique_session _session_t; // Just a long-lasting empty bson_t. Destruction is not required. - bsoncxx::aligned_storage _empty_cluster_time_storage; // -Wuninitialized + bsoncxx::aligned_storage _empty_cluster_time_storage; bson_t const* _empty_cluster_time_ptr = new (_empty_cluster_time_storage.get()) bson_t BSON_INITIALIZER; }; From 16380cb409725b7e4937288b89c3a9ab23858a0a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 10:12:25 -0600 Subject: [PATCH 12/15] Take advantage of trivial destructibility of bson_t --- .../lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp index ae84236388..97397edfad 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp @@ -45,7 +45,7 @@ void bson_free_deleter(std::uint8_t* ptr) { // class managed_bson_t { public: - managed_bson_t() : bson_ptr(new(bson_storage.get()) bson_t) { + managed_bson_t() { bson_init(bson_ptr); } @@ -57,7 +57,6 @@ class managed_bson_t { ~managed_bson_t() { bson_destroy(bson_ptr); - bson_ptr->~bson_t(); } bson_t* get() { @@ -66,7 +65,7 @@ class managed_bson_t { private: bsoncxx::aligned_storage bson_storage; - bson_t* bson_ptr; + bson_t* bson_ptr = new (bson_storage.get()) bson_t; }; } // namespace @@ -205,9 +204,7 @@ class core::impl { private: struct frame { - ~frame() { - bson_ptr->~bson_t(); - } + ~frame() = default; frame(frame&&) = delete; frame& operator=(frame&&) = delete; @@ -216,7 +213,7 @@ class core::impl { frame& operator=(frame const&) = delete; frame(bson_t* parent, char const* key, std::int32_t len, bool is_array) - : n(0), is_array(is_array), bson_ptr(new(bson_storage.get()) bson_t), parent(parent) { + : n(0), is_array(is_array), parent(parent) { if (is_array) { if (!bson_append_array_begin(parent, key, len, bson_ptr)) { throw bsoncxx::v_noabi::exception{error_code::k_cannot_begin_appending_array}; @@ -243,7 +240,7 @@ class core::impl { std::size_t n; bool is_array; bsoncxx::aligned_storage bson_storage; - bson_t* bson_ptr; + bson_t* bson_ptr = new (bson_storage.get()) bson_t; bson_t* parent; }; From 94cf1bdb83d7a019316bbdedc44df13b0ebb9449 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 11:06:18 -0600 Subject: [PATCH 13/15] Reserve `size + alignment` instead of `2 * size` --- .../lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh index f95be4f8c5..af662911e3 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/private/aligned_storage.hh @@ -33,7 +33,8 @@ class aligned_storage { static_assert(size >= alignment, "sizeof(T) >= alignof(T) must be true"); private: - unsigned char _storage[2u * size]; + // We only need at most `alignment` additional bytes to guarantee enough space for alignment. + unsigned char _storage[size + alignment]; public: void* get() BSONCXX_PRIVATE_IF_GNU_LIKE(__attribute__((assume_aligned(alignment)))) From b6271f6ba3143d898c1a0430ce37aea0858ecd2c Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 14:43:24 -0600 Subject: [PATCH 14/15] Add missing include directive for bson.hh --- src/mongocxx/lib/mongocxx/private/mock.hh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mongocxx/lib/mongocxx/private/mock.hh b/src/mongocxx/lib/mongocxx/private/mock.hh index 98fa254c52..f9494c7086 100644 --- a/src/mongocxx/lib/mongocxx/private/mock.hh +++ b/src/mongocxx/lib/mongocxx/private/mock.hh @@ -30,6 +30,8 @@ #include +#include + namespace mongocxx { namespace test_util { From 4b5976c265f3da2ce4c26974883c5c87eab8582c Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Feb 2025 14:45:04 -0600 Subject: [PATCH 15/15] Add additional missing include directives --- src/bsoncxx/lib/bsoncxx/private/aligned_storage.hh | 1 + src/bsoncxx/lib/bsoncxx/private/stack.hh | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/bsoncxx/lib/bsoncxx/private/aligned_storage.hh b/src/bsoncxx/lib/bsoncxx/private/aligned_storage.hh index af662911e3..fcb84f8a17 100644 --- a/src/bsoncxx/lib/bsoncxx/private/aligned_storage.hh +++ b/src/bsoncxx/lib/bsoncxx/private/aligned_storage.hh @@ -16,6 +16,7 @@ #include +#include #include namespace bsoncxx { diff --git a/src/bsoncxx/lib/bsoncxx/private/stack.hh b/src/bsoncxx/lib/bsoncxx/private/stack.hh index b7f9c7ad4d..caf08c27ba 100644 --- a/src/bsoncxx/lib/bsoncxx/private/stack.hh +++ b/src/bsoncxx/lib/bsoncxx/private/stack.hh @@ -18,6 +18,8 @@ #include #include +#include + namespace bsoncxx { // Note: This stack is only intended for use with the 'frame' type in