[llvm-branch-commits] [clang] [Clang] Introduce -fsanitize=alloc-token (PR #156839)

Marco Elver via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Sep 29 08:06:29 PDT 2025


================
@@ -0,0 +1,45 @@
+// RUN: %clang_cc1    -fsanitize=alloc-token -falloc-token-max=2147483647 -triple x86_64-linux-gnu -x c -emit-llvm %s -o - | FileCheck %s
----------------
melver wrote:

Ok, I'm adding alloc-token-lower.c:
```
+++ b/clang/test/CodeGen/alloc-token-lower.c
@@ -0,0 +1,58 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6
+//
+// Test optimization pipelines do not interfere with AllocToken lowering, and we
+// pass on function attributes correctly.
+//
+// RUN: %clang_cc1     -fsanitize=alloc-token -triple x86_64-linux-gnu -emit-llvm %s -o - | FileCheck --check-prefix=CHECK-O0 %s
+// RUN: %clang_cc1 -O1 -fsanitize=alloc-token -triple x86_64-linux-gnu -emit-llvm %s -o - | FileCheck --check-prefix=CHECK-O1 %s
+// RUN: %clang_cc1 -O2 -fsanitize=alloc-token -triple x86_64-linux-gnu -emit-llvm %s -o - | FileCheck --check-prefix=CHECK-O2 %s
+
+typedef __typeof(sizeof(int)) size_t;
+
+void *malloc(size_t size);
+
+// CHECK-O0-LABEL: define dso_local ptr @test_malloc(
+// CHECK-O0-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-O0-NEXT:  [[ENTRY:.*:]]
+// CHECK-O0-NEXT:    [[TMP0:%.*]] = call ptr @__alloc_token_malloc(i64 noundef 4, i64 0) #[[ATTR3:[0-9]+]]
+// CHECK-O0-NEXT:    ret ptr [[TMP0]]
+//
+// CHECK-O1-LABEL: define dso_local noalias noundef ptr @test_malloc(
+// CHECK-O1-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// CHECK-O1-NEXT:  [[ENTRY:.*:]]
+// CHECK-O1-NEXT:    [[TMP0:%.*]] = tail call dereferenceable_or_null(4) ptr @__alloc_token_malloc(i64 noundef 4, i64 0) #[[ATTR3:[0-9]+]]
+// CHECK-O1-NEXT:    ret ptr [[TMP0]]
+//
+// CHECK-O2-LABEL: define dso_local noalias noundef ptr @test_malloc(
+// CHECK-O2-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// CHECK-O2-NEXT:  [[ENTRY:.*:]]
+// CHECK-O2-NEXT:    [[TMP0:%.*]] = tail call dereferenceable_or_null(4) ptr @__alloc_token_malloc(i64 noundef 4, i64 0) #[[ATTR3:[0-9]+]]
+// CHECK-O2-NEXT:    ret ptr [[TMP0]]
+//
+void *test_malloc() {
+  return malloc(sizeof(int));
+}
+
+// CHECK-O0-LABEL: define dso_local ptr @no_sanitize_malloc(
+// CHECK-O0-SAME: i64 noundef [[SIZE:%.*]]) #[[ATTR2:[0-9]+]] {
+// CHECK-O0-NEXT:  [[ENTRY:.*:]]
+// CHECK-O0-NEXT:    [[SIZE_ADDR:%.*]] = alloca i64, align 8
+// CHECK-O0-NEXT:    store i64 [[SIZE]], ptr [[SIZE_ADDR]], align 8
+// CHECK-O0-NEXT:    [[CALL:%.*]] = call ptr @malloc(i64 noundef 4) #[[ATTR3]]
+// CHECK-O0-NEXT:    ret ptr [[CALL]]
+//
+// CHECK-O1-LABEL: define dso_local noalias noundef ptr @no_sanitize_malloc(
+// CHECK-O1-SAME: i64 noundef [[SIZE:%.*]]) local_unnamed_addr #[[ATTR2:[0-9]+]] {
+// CHECK-O1-NEXT:  [[ENTRY:.*:]]
+// CHECK-O1-NEXT:    [[CALL:%.*]] = tail call dereferenceable_or_null(4) ptr @malloc(i64 noundef 4) #[[ATTR3]]
+// CHECK-O1-NEXT:    ret ptr [[CALL]]
+//
+// CHECK-O2-LABEL: define dso_local noalias noundef ptr @no_sanitize_malloc(
+// CHECK-O2-SAME: i64 noundef [[SIZE:%.*]]) local_unnamed_addr #[[ATTR2:[0-9]+]] {
+// CHECK-O2-NEXT:  [[ENTRY:.*:]]
+// CHECK-O2-NEXT:    [[CALL:%.*]] = tail call dereferenceable_or_null(4) ptr @malloc(i64 noundef 4) #[[ATTR3]]
+// CHECK-O2-NEXT:    ret ptr [[CALL]]
+//
+void *no_sanitize_malloc(size_t size) __attribute__((no_sanitize("alloc-token"))) {
+  return malloc(sizeof(int));
+}
```

https://github.com/llvm/llvm-project/pull/156839


More information about the llvm-branch-commits mailing list