Skip to content

Tests: verify that CRYPTOPP_ALIGN_DATA delivers the requested alignment - #1844

Open
bernardladenthin wants to merge 1 commit into
veracrypt:masterfrom
bernardladenthin:selftest-align-data
Open

Tests: verify that CRYPTOPP_ALIGN_DATA delivers the requested alignment#1844
bernardladenthin wants to merge 1 commit into
veracrypt:masterfrom
bernardladenthin:selftest-align-data

Conversation

@bernardladenthin

Copy link
Copy Markdown
Contributor

What

Adds an alignment check to both platform self-test suites:

  • src/Volume/EncryptionTest.cppEncryptionTest::TestAlignment(), called from TestAll()
  • src/Common/Tests.cTestBufferAlignment(), called from DoAutoTestAlgorithms()
  • src/Common/Volumes.hTC_IS_ALIGNED(address, alignment), next to the existing alignment constants

Why

Around 79 declarations across the code base rely on CRYPTOPP_ALIGN_DATA, and the SIMD backends issue aligned loads (_mm_load_si128 / _mm_store_si128, ~66 sites) against buffers declared with it. If the macro ever expands to nothing — it has an empty fallback branch for compilers that are neither MSVC nor GCC/Clang — those loads fault. In kernel mode that is a bugcheck.

Nothing verified this so far.

Design notes

Probe size. The probe buffers are deliberately smaller than the alignment under test. The x86-64 ABI already aligns objects of 16 bytes or more on its own, so a large probe would pass even with the macro removed, making the test vacuous. This was measured, not assumed:

with attribute,    64 bytes : addr % 16 = 0
without attribute, 64 bytes : addr % 16 = 0    <-- vacuous
without attribute,  4 bytes : addr % 16 = 1    <-- meaningful

Driver excluded. The check is skipped under TC_WINDOWS_DRIVER. A failing self-test in a boot-start driver reaches TC_BUG_CHECK (Ntdriver.c), which would leave a system-encrypted machine unbootable — too high a price for a check whose failure mode the SIMD code would surface anyway. The guard wraps both the call and the function definition, the latter because an unreferenced static function trips C4505 and the driver builds with TreatWarningAsError.

Verification

The test was confirmed to actually fail when the macro is broken. Neutering CRYPTOPP_ALIGN_DATA in a scratch build produces:

Error: TestFailed at VeraCrypt::EncryptionTest::TestAlignment:91

Unmodified, the suite passes. Additionally:

  • MSVC user-mode /W4 and kernel-mode /W4 /WX: clean, Tests.c warning-free
  • Linux build + veracrypt --text --test: pass

Limitation

Only the Linux side runs automatically — the repository has no Windows CI job. The Tests.c half covers the MSVC branch of the macro but executes only when Format/Mount/ExpandVolume/FormatDLL starts.

The macro expands to nothing for compilers that are neither MSVC nor
GCC/Clang, and the SIMD backends issue aligned loads against buffers declared
with it, so a silent expansion to nothing would fault at run time. Nothing
verified this so far.

Add TC_IS_ALIGNED next to the existing alignment constants and check both of
them from the platform self-test suites. The probe buffers stay below the
alignment under test because the ABI already aligns larger objects by itself,
which would make the check pass vacuously.

The check is skipped in the Windows driver: a failing self-test there reaches
TC_BUG_CHECK and would bugcheck the machine.
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.

1 participant