https://github.com/thurstond created 
https://github.com/llvm/llvm-project/pull/122392

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.

>From 5e27cb11fa4d55996441df07255fb3de4c6664ce Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurs...@google.com>
Date: Fri, 10 Jan 2025 00:05:04 +0000
Subject: [PATCH] [ubsan] Assert that each check only has one SanitizerKind

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.
---
 clang/lib/CodeGen/CGExpr.cpp | 2 ++
 1 file changed, 2 insertions(+)

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 =

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to