Skip to content

[codegen] Fix crash in codegen caused by pointer calculation overflow #115791

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

Closed
wants to merge 1 commit into from

Conversation

vabridgers
Copy link
Contributor

@vabridgers vabridgers commented Nov 12, 2024

Fixes #48168

This crash was found by internal randomized testing. Under certain conditions, the front end does not detect a possible overflow address calculations until codegen. This change emits a warning instead of allowing the compiler to crash.

clang: <root>/clang/lib/CodeGen/CGExprScalar.cpp:5834: llvm::Value* clang::CodeGen::CodeGenFunction::EmitCheckedInBoundsGEP(
    llvm::Type*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, bool, bool, clang::SourceLocation, const llvm::Twine&):
    Assertion `(!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) || EvaluatedGEP.OffsetOverflows == Builder.getFalse())
    && "If the offset got constant-folded, we don't expect that there was an " "overflow."' failed.

0.      Program arguments: clang -c --target=x86_64-- -fsanitize=undefined ubsan-emit-bounds-check-crash-x86.c
1.      <eof> parser at end of file
2.      ubsan-emit-bounds-check-crash-x86.c:4:5: LLVM IR generation of declaration 'main'
3.      ubsan-emit-bounds-check-crash-x86.c:4:5: Generating code for declaration 'main'
...
 #9 <addr> clang::CodeGen::CodeGenFunction::EmitCheckedInBoundsGEP(clang::CodeGen::Address,
             llvm::ArrayRef<llvm::Value*>, llvm::Type*, bool, bool, clang::SourceLocation,
             clang::CharUnits, llvm::Twine const&)
             llvm::ArrayRef<llvm::Value*>, clang::QualType, bool, bool, clang::SourceLocation,
             clang::QualType*, clang::Expr const*, llvm::Twine const&) CGExpr.cpp:0:0
             bool)

Fixes llvm#48168

Under certain conditions, the front end does not detect a possible
overflow address calculations until codegen. This change emits
a warning instead of allowing the compiler to crash.

clang: <root>/clang/lib/CodeGen/CGExprScalar.cpp:5834: llvm::Value* clang::CodeGen::CodeGenFunction::EmitCheckedInBoundsGEP(
    llvm::Type*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, bool, bool, clang::SourceLocation, const llvm::Twine&):
    Assertion `(!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) || EvaluatedGEP.OffsetOverflows == Builder.getFalse())
    && "If the offset got constant-folded, we don't expect that there was an " "overflow."' failed.

0.      Program arguments: clang -c --target=x86_64-- -fsanitize=undefined ubsan-emit-bounds-check-crash-x86.c
1.      <eof> parser at end of file
2.      ubsan-emit-bounds-check-crash-x86.c:4:5: LLVM IR generation of declaration 'main'
3.      ubsan-emit-bounds-check-crash-x86.c:4:5: Generating code for declaration 'main'
...
 llvm#9 <addr> clang::CodeGen::CodeGenFunction::EmitCheckedInBoundsGEP(clang::CodeGen::Address,
             llvm::ArrayRef<llvm::Value*>, llvm::Type*, bool, bool, clang::SourceLocation,
             clang::CharUnits, llvm::Twine const&)
             llvm::ArrayRef<llvm::Value*>, clang::QualType, bool, bool, clang::SourceLocation,
             clang::QualType*, clang::Expr const*, llvm::Twine const&) CGExpr.cpp:0:0
             bool)
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Nov 12, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 12, 2024

@llvm/pr-subscribers-clang

Author: None (vabridgers)

Changes

Fixes #48168

Under certain conditions, the front end does not detect a possible overflow address calculations until codegen. This change emits a warning instead of allowing the compiler to crash.

clang: &lt;root&gt;/clang/lib/CodeGen/CGExprScalar.cpp:5834: llvm::Value* clang::CodeGen::CodeGenFunction::EmitCheckedInBoundsGEP(
    llvm::Type*, llvm::Value*, llvm::ArrayRef&lt;llvm::Value*&gt;, bool, bool, clang::SourceLocation, const llvm::Twine&amp;):
    Assertion `(!isa&lt;llvm::Constant&gt;(EvaluatedGEP.TotalOffset) || EvaluatedGEP.OffsetOverflows == Builder.getFalse())
    &amp;&amp; "If the offset got constant-folded, we don't expect that there was an " "overflow."' failed.

0.      Program arguments: clang -c --target=x86_64-- -fsanitize=undefined ubsan-emit-bounds-check-crash-x86.c
1.      &lt;eof&gt; parser at end of file
2.      ubsan-emit-bounds-check-crash-x86.c:4:5: LLVM IR generation of declaration 'main'
3.      ubsan-emit-bounds-check-crash-x86.c:4:5: Generating code for declaration 'main'
...
 #<!-- -->9 &lt;addr&gt; clang::CodeGen::CodeGenFunction::EmitCheckedInBoundsGEP(clang::CodeGen::Address,
             llvm::ArrayRef&lt;llvm::Value*&gt;, llvm::Type*, bool, bool, clang::SourceLocation,
             clang::CharUnits, llvm::Twine const&amp;)
             llvm::ArrayRef&lt;llvm::Value*&gt;, clang::QualType, bool, bool, clang::SourceLocation,
             clang::QualType*, clang::Expr const*, llvm::Twine const&amp;) CGExpr.cpp:0:0
             bool)

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

3 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprScalar.cpp (+9-4)
  • (added) clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c (+7)
  • (added) clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c (+7)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 287d911e10ba58..5ed865820a9151 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -5831,10 +5831,15 @@ CodeGenFunction::EmitCheckedInBoundsGEP(llvm::Type *ElemTy, Value *Ptr,
   GEPOffsetAndOverflow EvaluatedGEP =
       EmitGEPOffsetInBytes(Ptr, GEPVal, getLLVMContext(), CGM, Builder);
 
-  assert((!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) ||
-          EvaluatedGEP.OffsetOverflows == Builder.getFalse()) &&
-         "If the offset got constant-folded, we don't expect that there was an "
-         "overflow.");
+  if (!(!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) ||
+        EvaluatedGEP.OffsetOverflows == Builder.getFalse())) {
+    DiagnosticsEngine &Diags = CGM.getDiags();
+    unsigned DiagID = Diags.getCustomDiagID(
+        DiagnosticsEngine::Error, "Expression caused pointer calculation "
+                                  "overflow during code generation");
+    Diags.Report(Loc, DiagID);
+    return GEPVal;
+  }
 
   auto *Zero = llvm::ConstantInt::getNullValue(IntPtrTy);
 
diff --git a/clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c b/clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c
new file mode 100644
index 00000000000000..b4da2f2b7ee720
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c
@@ -0,0 +1,7 @@
+// REQUIRES: msp430-registered-target
+// RUN: %clang -c -fsanitize=undefined -Wno-tentative-definition-array -Wno-return-type -Wno-unused-value -Wno-array-bounds -Xclang -verify --target=msp430-- %s
+int a;
+_Complex double b[1][1];
+void c(void) {
+  b[a][8920]; // expected-error {{Expression caused pointer calculation overflow during code generation}}
+}
diff --git a/clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c b/clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c
new file mode 100644
index 00000000000000..63bda82c14ae20
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c
@@ -0,0 +1,7 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang -c -Wno-tentative-definition-array -Wno-return-type -Wno-unused-value -Wno-array-bounds -Xclang -verify --target=x86_64-- -fsanitize=undefined %s 
+int **a[];
+int main() {
+  (*a)[3300220222222200000]; // expected-error {{Expression caused pointer calculation overflow during code generation}}
+  return 0;
+}

@llvmbot
Copy link
Member

llvmbot commented Nov 12, 2024

@llvm/pr-subscribers-clang-codegen

Author: None (vabridgers)

Changes

Fixes #48168

Under certain conditions, the front end does not detect a possible overflow address calculations until codegen. This change emits a warning instead of allowing the compiler to crash.

clang: &lt;root&gt;/clang/lib/CodeGen/CGExprScalar.cpp:5834: llvm::Value* clang::CodeGen::CodeGenFunction::EmitCheckedInBoundsGEP(
    llvm::Type*, llvm::Value*, llvm::ArrayRef&lt;llvm::Value*&gt;, bool, bool, clang::SourceLocation, const llvm::Twine&amp;):
    Assertion `(!isa&lt;llvm::Constant&gt;(EvaluatedGEP.TotalOffset) || EvaluatedGEP.OffsetOverflows == Builder.getFalse())
    &amp;&amp; "If the offset got constant-folded, we don't expect that there was an " "overflow."' failed.

0.      Program arguments: clang -c --target=x86_64-- -fsanitize=undefined ubsan-emit-bounds-check-crash-x86.c
1.      &lt;eof&gt; parser at end of file
2.      ubsan-emit-bounds-check-crash-x86.c:4:5: LLVM IR generation of declaration 'main'
3.      ubsan-emit-bounds-check-crash-x86.c:4:5: Generating code for declaration 'main'
...
 #<!-- -->9 &lt;addr&gt; clang::CodeGen::CodeGenFunction::EmitCheckedInBoundsGEP(clang::CodeGen::Address,
             llvm::ArrayRef&lt;llvm::Value*&gt;, llvm::Type*, bool, bool, clang::SourceLocation,
             clang::CharUnits, llvm::Twine const&amp;)
             llvm::ArrayRef&lt;llvm::Value*&gt;, clang::QualType, bool, bool, clang::SourceLocation,
             clang::QualType*, clang::Expr const*, llvm::Twine const&amp;) CGExpr.cpp:0:0
             bool)

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

3 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprScalar.cpp (+9-4)
  • (added) clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c (+7)
  • (added) clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c (+7)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 287d911e10ba58..5ed865820a9151 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -5831,10 +5831,15 @@ CodeGenFunction::EmitCheckedInBoundsGEP(llvm::Type *ElemTy, Value *Ptr,
   GEPOffsetAndOverflow EvaluatedGEP =
       EmitGEPOffsetInBytes(Ptr, GEPVal, getLLVMContext(), CGM, Builder);
 
-  assert((!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) ||
-          EvaluatedGEP.OffsetOverflows == Builder.getFalse()) &&
-         "If the offset got constant-folded, we don't expect that there was an "
-         "overflow.");
+  if (!(!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) ||
+        EvaluatedGEP.OffsetOverflows == Builder.getFalse())) {
+    DiagnosticsEngine &Diags = CGM.getDiags();
+    unsigned DiagID = Diags.getCustomDiagID(
+        DiagnosticsEngine::Error, "Expression caused pointer calculation "
+                                  "overflow during code generation");
+    Diags.Report(Loc, DiagID);
+    return GEPVal;
+  }
 
   auto *Zero = llvm::ConstantInt::getNullValue(IntPtrTy);
 
diff --git a/clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c b/clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c
new file mode 100644
index 00000000000000..b4da2f2b7ee720
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c
@@ -0,0 +1,7 @@
+// REQUIRES: msp430-registered-target
+// RUN: %clang -c -fsanitize=undefined -Wno-tentative-definition-array -Wno-return-type -Wno-unused-value -Wno-array-bounds -Xclang -verify --target=msp430-- %s
+int a;
+_Complex double b[1][1];
+void c(void) {
+  b[a][8920]; // expected-error {{Expression caused pointer calculation overflow during code generation}}
+}
diff --git a/clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c b/clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c
new file mode 100644
index 00000000000000..63bda82c14ae20
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c
@@ -0,0 +1,7 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang -c -Wno-tentative-definition-array -Wno-return-type -Wno-unused-value -Wno-array-bounds -Xclang -verify --target=x86_64-- -fsanitize=undefined %s 
+int **a[];
+int main() {
+  (*a)[3300220222222200000]; // expected-error {{Expression caused pointer calculation overflow during code generation}}
+  return 0;
+}

@vabridgers
Copy link
Contributor Author

vabridgers commented Nov 12, 2024

I know the diagnostic could probably be better, just trying to get the code review party started. This is not ideal, but better than a crash. Thanks.

@vabridgers vabridgers requested a review from bogner November 12, 2024 10:30
@vabridgers vabridgers changed the title [codegen] Fix crash in codegan caused by pointer calculation overflow [codegen] Fix crash in codegen caused by pointer calculation overflow Nov 12, 2024
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.

CodeGen should not produce errors for overflows which do not occur at compile-time according to the language semantics. This is happening at runtime, so we need to just eat the error; we can emit an unconditional call to the ubsan error handler. We are not allowed to reject valid programs.

Ideally Sema should produce a warning so users aren't caught off-guard.

@vabridgers
Copy link
Contributor Author

@efriedma-quic , thanks for the comments. I'll explore a more appropriate solution.

@vabridgers
Copy link
Contributor Author

Closing this review since this approach is not appropriate.

@vabridgers vabridgers closed this Nov 12, 2024
@vabridgers vabridgers deleted the sanitizer-codegen-crash branch November 12, 2024 23:25
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
3 participants