Skip to content

[24603] Add opt-in strict bool normalization#319

Open
mirkomorati wants to merge 3 commits into
eProsima:masterfrom
mirkomorati:feat-strict-bool
Open

[24603] Add opt-in strict bool normalization#319
mirkomorati wants to merge 3 commits into
eProsima:masterfrom
mirkomorati:feat-strict-bool

Conversation

@mirkomorati

@mirkomorati mirkomorati commented Jun 7, 2026

Copy link
Copy Markdown

Description

In release builds, the compiler optimizes the if (bool_t) { write 1 } else { write 0 } pattern in all the bool serialization paths.

This PR introduces a STRICT_BOOL CMake option (off by default), the relative FASTCDR_STRICT_BOOL compiler definition and a new launder_bool() helper function that uses memcpy and a compiler barrier to force normalization of boolean values to 0/1.
A dedicated test file BoolStrictTest.cpp has also been included that tests the "truthy" cases.

Since this is an opt-in, we can backport the new feature to 2.3.x, which is the most used branch.
@Mergifyio backport 2.3.x

Fixes #315

Contributor Checklist

  • Commit messages follow the project guidelines.
  • The code follows the style guidelines of this project.
  • Tests that thoroughly check the new feature have been added/Regression tests checking the bug and its fix have been added; the added tests pass locally
  • Any new/modified methods have been properly documented using Doxygen.
  • Changes are backport compatible: they do NOT break ABI nor change library core behavior.
  • Changes are API compatible.
  • New feature has been added to the versions.md file (if applicable).
  • Applicable backports have been included in the description.

Reviewer Checklist

  • The PR has a milestone assigned.
  • The title and description correctly express the PR's purpose.
  • Check contributor checklist is correct.
  • Check CI results: changes do not issue any warning.
  • Check CI results: CI pass and failing tests are unrelated with the changes.

Signed-off-by: Mirko Morati <mirkomorati@gmail.com>

@MiguelCompany MiguelCompany left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this well-crafted PR.

Please take the following suggestions into consideration.

Comment thread src/cpp/helpers/memory_helpers.cpp
Comment thread src/cpp/helpers/memory_helpers.hpp Outdated
Comment thread src/cpp/Cdr.cpp Outdated
Comment thread src/cpp/Cdr.cpp Outdated
Comment thread src/cpp/FastCdr.cpp Outdated
@MiguelCompany MiguelCompany changed the title Add opt-in strict bool normalization [24603] Add opt-in strict bool normalization Jun 8, 2026
Signed-off-by: Mirko Morati <mirkomorati@gmail.com>
Comment thread src/cpp/Cdr.cpp
@MiguelCompany MiguelCompany added this to the v2.4.0 milestone Jun 12, 2026
Signed-off-by: Mirko Morati <mirkomorati@gmail.com>

@MiguelCompany MiguelCompany left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the great work @mirkomorati

Now, for the final touches:

  • Rebase on top of master
  • Add a single line * Add STRICT_BOOL CMake option to enforce canonical bool serialization to versions.md

It would also be nice if you could add a way of testing the new feature in the CI job. If you don't have experience with GitHub Actions, or you'd prefer we take care of it, we'll do.

@mirkomorati

Copy link
Copy Markdown
Author

Thank you for the review!

As you guessed I have little to no experience directly with github actions, but I hope the idea is correct:

  • Expand the fastcdr_test matrix jobs to test with both builds
  • Upload both test results

The add_labels step I think should be called once, so I added a check there.

Would something like this work? Or were you thinking about exposing a variable input.test-strict-bool to control it as an opt-in from the other CI jobs?

diff --git a/.github/workflows/reusable-ci.yml b/.github/workflows/reusable-ci.yml
index e0ab3b5..5ae1c4d 100644
--- a/.github/workflows/reusable-ci.yml
+++ b/.github/workflows/reusable-ci.yml
@@ -68,10 +68,13 @@ jobs:
       matrix:
         cmake-build-type:
           - 'RelWithDebInfo'
+        strict-bool:
+          - false
+          - true
 
     steps:
     - name: Add ci-pending label if PR
-      if: ${{ github.event_name == 'pull_request' && inputs.add-label == true}}
+      if: ${{ github.event_name == 'pull_request' && inputs.add-label == true && matrix.strict-bool == false }}
       uses: eProsima/eProsima-CI/external/add_labels@v0
       with:
         labels: ci-pending
@@ -122,7 +125,7 @@ jobs:
         colcon_meta_file: ${{ github.workspace }}/src/fastcdr/.github/workflows/config/build.meta
         colcon_build_args: ${{ inputs.colcon-args }}
         colcon_build_args_default: --event-handlers=console_direct+
-        cmake_args: ${{ inputs.cmake-args }}
+        cmake_args: ${{ inputs.cmake-args }} ${{ matrix.strict-bool == true && '-DSTRICT_BOOL=ON' || '' }}
         cmake_args_default: ${{ env.colcon-build-default-cmake-args }} ${{ env.toolset }}
         cmake_build_type: ${{ matrix.cmake-build-type }}
         workspace: ${{ github.workspace }}
@@ -137,7 +140,7 @@ jobs:
         ctest_args: ${{ inputs.ctest-args }}
         packages_names: fastcdr
         workspace: ${{ github.workspace }}
-        test_report_artifact: ${{ inputs.label }}
+        test_report_artifact: ${{ inputs.label }}${{ matrix.strict-bool == true && '-strict-bool' || '' }}
 
     - name: Fast CDR Test summary
       uses: eProsima/eProsima-CI/multiplatform/junit_summary@v0
@@ -153,5 +156,5 @@ jobs:
       if: always()
       uses: eProsima/eProsima-CI/external/upload-artifact@v0
       with:
-        name: test-results-${{ inputs.label }}
+        name: test-results-${{ inputs.label }}${{ matrix.strict-bool == true && '-strict-bool' || '' }}
         path: log/latest_test/fastcdr

@MiguelCompany

Copy link
Copy Markdown
Member

@mirkomorati Looks like the thing I would do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler optimization removes normalization for Cdr::serialize(bool)

2 participants