Skip to content

[Clang][CodeGen] Respect -fwrapv-pointer when emitting struct GEPs #134269

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 1 commit into from
Apr 9, 2025

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Apr 3, 2025

This patch turns off inbounds/nuw flags for member accesses when -fwrapv-pointer is set.
Note: There are many calls to CGBuilderTy::CreateStructGEP, and most of them are safe. So I think it is probably overkill to handle this in CreateStructGEP.

Closes #132449.

It is required by #130734.

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

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-clang

Author: Yingwei Zheng (dtcxzyw)

Changes

This patch turns off inbounds/nuw flags for member accesses when -fwrapv-pointer is set.
Note: There are many calls to CGBuilderTy::CreateStructGEP, and most of them are safe. So I think it is probably overkill to handle this in CreateStructGEP.

Closes #132449.

It is required by #130734.


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

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGExpr.cpp (+9-2)
  • (modified) clang/test/CodeGen/pointer-overflow.c (+21)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 3d3a111f0514a..d32591ed896d0 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -4922,6 +4922,9 @@ static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
   unsigned idx =
     CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
 
+  if (CGF.getLangOpts().PointerOverflowDefined)
+    return CGF.Builder.CreateConstGEP2_32(base, 0, idx, field->getName());
+
   return CGF.Builder.CreateStructGEP(base, idx, field->getName());
 }
 
@@ -4979,9 +4982,13 @@ LValue CodeGenFunction::EmitLValueForField(LValue base,
     if (!UseVolatile) {
       if (!IsInPreservedAIRegion &&
           (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
-        if (Idx != 0)
+        if (Idx != 0) {
           // For structs, we GEP to the field that the record layout suggests.
-          Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
+          if (getLangOpts().PointerOverflowDefined)
+            Addr = Builder.CreateConstGEP2_32(Addr, 0, Idx, field->getName());
+          else
+            Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
+        }
       } else {
         llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateRecordType(
             getContext().getRecordType(rec), rec->getLocation());
diff --git a/clang/test/CodeGen/pointer-overflow.c b/clang/test/CodeGen/pointer-overflow.c
index 9c7821b841980..70e3c855bff90 100644
--- a/clang/test/CodeGen/pointer-overflow.c
+++ b/clang/test/CodeGen/pointer-overflow.c
@@ -10,3 +10,24 @@ void test(void) {
   // DEFAULT: getelementptr inbounds nuw i32, ptr
   // FWRAPV-POINTER: getelementptr i32, ptr
 }
+
+struct S {
+  int a;
+  int b;
+  int c: 10;
+  int d: 10;
+};
+
+int test_struct(struct S* s) {
+  // -fwrapv-pointer should turn off inbounds nuw for struct GEP's
+  return s->b;
+  // DEFAULT: getelementptr inbounds nuw %struct.S, ptr
+  // FWRAPV-POINTER: getelementptr %struct.S, ptr
+}
+
+int test_struct_bitfield(struct S* s) {
+  // -fwrapv-pointer should turn off inbounds nuw for struct GEP's
+  return s->d;
+  // DEFAULT: getelementptr inbounds nuw %struct.S, ptr
+  // FWRAPV-POINTER: getelementptr %struct.S, ptr
+}

@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-clang-codegen

Author: Yingwei Zheng (dtcxzyw)

Changes

This patch turns off inbounds/nuw flags for member accesses when -fwrapv-pointer is set.
Note: There are many calls to CGBuilderTy::CreateStructGEP, and most of them are safe. So I think it is probably overkill to handle this in CreateStructGEP.

Closes #132449.

It is required by #130734.


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

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGExpr.cpp (+9-2)
  • (modified) clang/test/CodeGen/pointer-overflow.c (+21)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 3d3a111f0514a..d32591ed896d0 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -4922,6 +4922,9 @@ static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
   unsigned idx =
     CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
 
+  if (CGF.getLangOpts().PointerOverflowDefined)
+    return CGF.Builder.CreateConstGEP2_32(base, 0, idx, field->getName());
+
   return CGF.Builder.CreateStructGEP(base, idx, field->getName());
 }
 
@@ -4979,9 +4982,13 @@ LValue CodeGenFunction::EmitLValueForField(LValue base,
     if (!UseVolatile) {
       if (!IsInPreservedAIRegion &&
           (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
-        if (Idx != 0)
+        if (Idx != 0) {
           // For structs, we GEP to the field that the record layout suggests.
-          Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
+          if (getLangOpts().PointerOverflowDefined)
+            Addr = Builder.CreateConstGEP2_32(Addr, 0, Idx, field->getName());
+          else
+            Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
+        }
       } else {
         llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateRecordType(
             getContext().getRecordType(rec), rec->getLocation());
diff --git a/clang/test/CodeGen/pointer-overflow.c b/clang/test/CodeGen/pointer-overflow.c
index 9c7821b841980..70e3c855bff90 100644
--- a/clang/test/CodeGen/pointer-overflow.c
+++ b/clang/test/CodeGen/pointer-overflow.c
@@ -10,3 +10,24 @@ void test(void) {
   // DEFAULT: getelementptr inbounds nuw i32, ptr
   // FWRAPV-POINTER: getelementptr i32, ptr
 }
+
+struct S {
+  int a;
+  int b;
+  int c: 10;
+  int d: 10;
+};
+
+int test_struct(struct S* s) {
+  // -fwrapv-pointer should turn off inbounds nuw for struct GEP's
+  return s->b;
+  // DEFAULT: getelementptr inbounds nuw %struct.S, ptr
+  // FWRAPV-POINTER: getelementptr %struct.S, ptr
+}
+
+int test_struct_bitfield(struct S* s) {
+  // -fwrapv-pointer should turn off inbounds nuw for struct GEP's
+  return s->d;
+  // DEFAULT: getelementptr inbounds nuw %struct.S, ptr
+  // FWRAPV-POINTER: getelementptr %struct.S, ptr
+}

@MaskRay
Copy link
Member

MaskRay commented Apr 3, 2025

Looks good to me, but please allow some time for other reviewers to weigh in.

@efriedma-quic
Copy link
Collaborator

most of them are safe

What's the distinguishing factor here? Do you consider it "safe" to mark inbounds if the pointer is immediately dereferenced? Or does the pointer have to refer to a known successful allocation? Or something else?

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Apr 4, 2025

most of them are safe

What's the distinguishing factor here? Do you consider it "safe" to mark inbounds if the pointer is immediately dereferenced? Or does the pointer have to refer to a known successful allocation? Or something else?

Other calls to CGBuilderTy::CreateStructGEP operate on some internal structures like valist/complex/kernel launch params/GPU printf args.

@nikic
Copy link
Contributor

nikic commented Apr 4, 2025

Usually we'd want to also insert -fsanitize=pointer-overflow instrumentation in places where we respect -fwrapv-pointer. I'm not entirely sure whether we want to do this here, as it will probably blow up the amount of inserted instrumentation for little benefit...

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Apr 4, 2025

Usually we'd want to also insert -fsanitize=pointer-overflow instrumentation in places where we respect -fwrapv-pointer. I'm not entirely sure whether we want to do this here, as it will probably blow up the amount of inserted instrumentation for little benefit...

A struct GEP is unlikely to overflow. But a null check should be useful.

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

This makes sense to me but this is not my area.

struct S {
int a;
int b;
int c: 10;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am curious why did you add bit-fields in specifically to this test? Are they particularly different wrt other types of fields like arrays, pointers, member pointers etc that we would expect them to break?

Copy link
Member Author

Choose a reason for hiding this comment

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

Bitfields are handled by a different code path:

Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Apr 9, 2025

@efriedma-quic Any more comments?

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 c048073 into llvm:main Apr 9, 2025
12 of 14 checks passed
@dtcxzyw dtcxzyw deleted the wrapv-pointer-struct-gep branch April 9, 2025 16:48
AllinLeeYL pushed a commit to AllinLeeYL/llvm-project that referenced this pull request Apr 10, 2025
…lvm#134269)

This patch turns off inbounds/nuw flags for member accesses when
`-fwrapv-pointer` is set.

Closes llvm#132449.

It is required by llvm#130734.
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…lvm#134269)

This patch turns off inbounds/nuw flags for member accesses when
`-fwrapv-pointer` is set.

Closes llvm#132449.

It is required by llvm#130734.
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 Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

-fwrapv-pointer doesn't affect member accesses
6 participants