Skip to content

[Clang][CodeGen] Enable pointer overflow check for GCC workaround #137849

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

Merged
merged 4 commits into from
May 9, 2025

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Apr 29, 2025

Do not suppress the pointer overflow check for the (i8*) nullptr + N idiom.

Related issue: #137833

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Apr 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Yingwei Zheng (dtcxzyw)

Changes

Do not set inbounds on GEP when the pointer operand is a constant null.

Note: This patch is fragile :( For a more complex case (int*)0 + offset1 + offset2, we still set inbounds for the second addition.

Related issue: #137833


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

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprScalar.cpp (+2-1)
  • (modified) clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c (+12)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 8dbbcdaef25d8..d214d2af52563 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4238,7 +4238,8 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,
   else
     elemTy = CGF.ConvertTypeForMem(elementType);
 
-  if (CGF.getLangOpts().PointerOverflowDefined)
+  if (CGF.getLangOpts().PointerOverflowDefined ||
+      CGF.isUnderlyingBasePointerConstantNull(pointerOperand))
     return CGF.Builder.CreateGEP(elemTy, pointer, index, "add.ptr");
 
   return CGF.EmitCheckedInBoundsGEP(
diff --git a/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c b/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c
index 63b6db2c2adeb..c5ae3f8bcc368 100644
--- a/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c
+++ b/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c
@@ -431,6 +431,18 @@ char *void_ptr(void *base, unsigned long offset) {
   return base + offset;
 }
 
+int *constant_null_add(long offset) {
+  // CHECK: define{{.*}} ptr @constant_null_add(i64 noundef %[[OFFSET:.*]])
+  // CHECK-NEXT: [[ENTRY:.*]]:
+  // CHECK-NEXT:   %[[OFFSET_ADDR:.*]] = alloca i64, align 8
+  // CHECK-NEXT:   store i64 %[[OFFSET]], ptr %[[OFFSET_ADDR]], align 8
+  // CHECK-NEXT:   %[[OFFSET_RELOADED:.*]] = load i64, ptr %[[OFFSET_ADDR]], align 8
+  // CHECK-NEXT:   %[[ADD_PTR:.*]] = getelementptr i32, ptr null, i64 %[[OFFSET_RELOADED]]
+  // CHECK-NEXT:   ret ptr %[[ADD_PTR]]
+#line 1800
+  return (int *)0 + offset;
+}
+
 #ifdef __cplusplus
 }
 #endif

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

While we should omit inbounds, I think it would be good to not suppress the sanitizer. If the sanitizer doesn't warn about it, then things will never get better :(

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Apr 30, 2025

While we should omit inbounds, I think it would be good to not suppress the sanitizer. If the sanitizer doesn't warn about it, then things will never get better :(

Fixed.

@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label May 4, 2025
@dtcxzyw dtcxzyw changed the title [Clang][CodeGen] Check isUnderlyingBasePointerConstantNull in emitPointerArithmetic [Clang][CodeGen] Check isUnderlyingBasePointerConstantNull in isNullPointerArithmeticExtension May 4, 2025
@dtcxzyw dtcxzyw changed the title [Clang][CodeGen] Check isUnderlyingBasePointerConstantNull in isNullPointerArithmeticExtension [Clang][CodeGen] Enable pointer overflow check for GCC workaround May 6, 2025
Copy link

github-actions bot commented May 8, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

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

LGTM

@dtcxzyw dtcxzyw merged commit d2b012e into llvm:main May 9, 2025
11 checks passed
@dtcxzyw dtcxzyw deleted the fix-137833-1 branch May 9, 2025 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants