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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions clang/test/CodeGen/ubsan-emit-bounds-check-crash-msp.c
Original file line number Diff line number Diff line change
@@ -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}}
}
7 changes: 7 additions & 0 deletions clang/test/CodeGen/ubsan-emit-bounds-check-crash-x86.c
Original file line number Diff line number Diff line change
@@ -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;
}
Loading