[clang] [CIR] Initial support for sanitizer attributes (PR #220625)
Sirui Mu via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 6 08:41:34 PDT 2026
================
@@ -522,6 +522,23 @@ void CIRGenFunction::startFunction(GlobalDecl gd, QualType returnType,
const auto *fd = dyn_cast_or_null<FunctionDecl>(d);
curFuncDecl = (d ? d->getNonClosureContext() : nullptr);
+ if (d) {
+ SanitizerMask noSanitizeMask;
+ for (const auto *attr : d->specific_attrs<NoSanitizeAttr>())
+ noSanitizeMask |= attr->getMask();
+ sanOpts.Mask &= ~noSanitizeMask;
+
+ assert(!cir::MissingFeatures::sanitizers());
+ }
+
+ llvm::SmallVector<cir::SanitizeKind> sanitizerKinds;
+ if (sanOpts.has(SanitizerKind::Address))
+ sanitizerKinds.push_back(cir::SanitizeKind::Address);
+ assert(!cir::MissingFeatures::sanitizers());
+ if (!sanitizerKinds.empty())
+ fn.setSanitizeAttr(
+ cir::SanitizeAttr::get(&getMLIRContext(), sanitizerKinds));
----------------
Lancern wrote:
The module-level attribute stands for two purposes.
1. Sanitizers are typically implemented as LLVM transformation passes in the LLVM pipeline. In the CIRGen path where compilation options are available, these passes are inserted into the pipeline by checking the clang compilation options. In other paths where the compilation options are not available (e.g. we may have a CIR-to-LLVM path that compiles a text representation of a CIR module into target code), these passes will be inserted into the pipeline by consuming the sanitize attribute on the module op.
2. Some LLVM sanitizer passes (namely, sanitize-bounds, sanitize-coverage, and sanitize-thread) identify target functions (i.e. functions that should be sanitized) via _negative_ attributes instead of _positive_ attributes. For example, the pass for bounds sanitizer would skip functions marked with `no_sanitize_bounds`. To generate such negative attributes, we would check the function-level sanitize attributes as well as the module-level sanitize attributes: if a function does not have to be bound-sanitized but the bounds sanitizer is enabled module-wise, we would attach the `no_sanitize_bounds` LLVM attribute to the lowered LLVM function.
https://github.com/llvm/llvm-project/pull/220625
More information about the cfe-commits
mailing list