Skip to content
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

[ubsan] Assert that each check only has one SanitizerKind #122392

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thurstond
Copy link
Contributor

@thurstond thurstond commented Jan 10, 2025

The Checked parameter of CodeGenFunction::EmitCheck is of type ArrayRef<std::pair<llvm::Value *, SanitizerMask>>. In the general case, SanitizerMask is used to denote that zero or more sanitizers are enabled, but I believe that EmitCheck assumes there is exactly one sanitizer enabled per SanitizerMask (e.g., SanitizeTrap.has(Checked[i].second) is called, whereby .has checks that there is only one sanitizer enabled). This patch adds an assertion for this invariant.

This is not intended to change the functionality of UBSan, but will make it easier for maintainers to reason about and extend the EmitCheck function.

The `Checked` parameter of `CodeGenFunction::EmitCheck` is of type `ArrayRef<std::pair<llvm::Value *, SanitizerMask>>`. In the general case, SanitizerMask can denote that zero or more sanitizers are enabled, but I believe (from tests and inspecting the code) that `EmitCheck` assumes exactly one sanitizer enabled per SanitizerMask. This patch adds an assertion for this invariant.

This is not intended to change the functionality of the code, but will
make it easier for maintainers to reason about and extend the
`EmitCheck` function.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen labels Jan 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 10, 2025

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Thurston Dang (thurstond)

Changes

The Checked parameter of CodeGenFunction::EmitCheck is of type ArrayRef&lt;std::pair&lt;llvm::Value *, SanitizerMask&gt;&gt;. In the general case, SanitizerMask can denote that zero or more sanitizers are enabled, but I believe (from tests and inspecting the code) that EmitCheck assumes exactly one sanitizer enabled per SanitizerMask. This patch adds an assertion for this invariant.

This is not intended to change the functionality of UBSan, but will make it easier for maintainers to reason about and extend the EmitCheck function.


Full diff: https://github.com/llvm/llvm-project/pull/122392.diff

1 Files Affected:

  • (modified) clang/lib/CodeGen/CGExpr.cpp (+2)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 1bad7a722da07a..792fe05025e393 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -3603,6 +3603,8 @@ void CodeGenFunction::EmitCheck(
   llvm::Value *TrapCond = nullptr;
   bool NoMerge = false;
   for (int i = 0, n = Checked.size(); i < n; ++i) {
+    assert(Checked[i].second.isPowerOf2());
+
     llvm::Value *Check = Checked[i].first;
     // -fsanitize-trap= overrides -fsanitize-recover=.
     llvm::Value *&Cond =

@@ -3603,6 +3603,8 @@ void CodeGenFunction::EmitCheck(
llvm::Value *TrapCond = nullptr;
bool NoMerge = false;
for (int i = 0, n = Checked.size(); i < n; ++i) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe instead switch SanitizerMask -> SanitizerOrdinal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants