Skip to content

build: add -Wa,-mbranches-within-32B-boundaries compiler flag #5267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Apr 28, 2025

Conversation

johubertj
Copy link
Contributor

@johubertj johubertj commented Apr 22, 2025

Description of changes:

Add support for the -Wa,-mbranches-within-32B-boundaries compiler flag to reduce conditional branch alignment penalties on modern x86 microarchitectures. This flag was recommended to be added to s2n-tls by researchers (Annika Wilde, Samira Briongos, Claudio Soriente, and Ghassan Karame) from Ruhr University Bochum.

This PR introduces:

  • A feature probe S2N_BRANCH_ALIGN to detect whether the current compiler supports the flag.
  • Conditional application of the flag based on the probe result and platform.
  • Skips applying the flag for fuzz builds to avoid Clang-related build failures.

Call-outs:

This change aligns with Intel's guidance to mitigate conditional branch alignment issues on modern x86 microarchitectures. See: Intel Mitigations for Jump Conditional Code Erratum (INTEL-841076)

Testing:

  1. Performance Benchmarking
    Benchmarks were run locally on an x86_64 machine using:

cargo bench --bench handshake

Tested 5 handshake types, running each 3 times. First, I commented out the line that adds the compiler flag and recompiled. Then, I re-enabled the flag, recompiled, and ran the benchmarks again for comparison.

  • handshake-server-auth
  • handshake-mTLS
  • handshake-resumption
  • handshake-secp256r1

Result: No performance regression (based on execution time) was observed with the flag enabled vs disabled. Performance remained within normal variance range.

  1. Compiler Compatibility
    To safely apply this flag:
  • Added a feature probe that attempts to compile a minimal source file using the flag.
  • If compilation succeeds, the flag is applied.
  • The flag is not applied during fuzz builds (which use Clang), even if the probe passes.
  1. Verified Compiler Flag Application (CMake + build.rs)

To confirm that the -Wa,-mbranches-within-32B-boundaries flag was applied on the Rust side (build.rs), I added CC_ENABLE_DEBUG_OUTPUT=1 and -vv to the asan-unit-tests CI job. This exposed the final compiler command in the logs.

Link to CI output showing flag

To confirm the flag is applied on the CMake side, I updated the build command in buildspec_mem.yml to use verbose mode (--verbose --clean-first). -Wa,-mbranches-within-32B-boundaries is on line 1326 of the following CodeBuild job:

s2nGeneralBatch:8a50d464-55dd-46b4-ab16-6a9ca1f8346c (line 1326)

@github-actions github-actions bot added the s2n-core team label Apr 22, 2025
@johubertj johubertj marked this pull request as ready for review April 23, 2025 00:04
@johubertj johubertj requested a review from dougch as a code owner April 24, 2025 22:37
@johubertj johubertj requested review from goatgoose and lrstewart and removed request for dougch April 24, 2025 23:59
@johubertj johubertj requested a review from lrstewart April 25, 2025 22:03
@johubertj johubertj enabled auto-merge April 28, 2025 17:38
@johubertj johubertj disabled auto-merge April 28, 2025 17:38
@johubertj johubertj added this pull request to the merge queue Apr 28, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 28, 2025
@johubertj johubertj added this pull request to the merge queue Apr 28, 2025
Merged via the queue into aws:main with commit 1c5798b Apr 28, 2025
46 of 47 checks passed
@johubertj johubertj deleted the ci/add-timing-flag branch April 28, 2025 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants