[clang] [ubsan] Assert that each check only has one SanitizerKind (PR #122392)

Thurston Dang via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 9 16:07:58 PST 2025


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 <thurston at 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 =



More information about the cfe-commits mailing list