Skip to content
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

CodeGen: fix crash when using allocations in global initializer #181

Merged
merged 2 commits into from
Nov 17, 2023
Merged
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
41 changes: 26 additions & 15 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12640,17 +12640,22 @@ Value *CodeGenFunction::EmitCheerpBuiltinExpr(unsigned BuiltinID,
return Builder.CreateCall(Callee, {Vec});
}
else if (BuiltinID == Builtin::BImalloc) {
const FunctionDecl* FD=dyn_cast<FunctionDecl>(CurFuncDecl);
assert(FD);
ParentMap PM(FD->getBody());
const FunctionDecl* FD=dyn_cast_if_present<FunctionDecl>(CurFuncDecl);
const VarDecl* VD=dyn_cast_if_present<VarDecl>(CurCodeDecl);
// Malloc might be used as a global initializer, thus CurFuncDecl being null
assert(FD || VD);
// This const_cast below probably is completely safe. When taking a brief
// look at ParentMap, it doesn't seem to ever modify what's passed to it
ParentMap PM(FD ? FD->getBody() : const_cast<Expr*>(VD->getInit()));
const Stmt* parent=PM.getParent(E);
// We need an explicit cast after the call, void* can't be used
llvm::Type *Tys[] = { VoidPtrTy, VoidPtrTy };
const CastExpr* retCE=dyn_cast_or_null<CastExpr>(parent);
if (!retCE || retCE->getType()->isVoidPointerType())
{
if (asmjs) return 0;
CGM.getDiags().Report(E->getBeginLoc(), diag::err_cheerp_alloc_requires_cast);
if (!asmjs)
CGM.getDiags().Report(E->getBeginLoc(), diag::err_cheerp_alloc_requires_cast);
return 0;
}
else
{
Expand All @@ -12667,17 +12672,20 @@ Value *CodeGenFunction::EmitCheerpBuiltinExpr(unsigned BuiltinID,
return CB;
}
else if (BuiltinID == Builtin::BIcalloc) {
const FunctionDecl* FD=dyn_cast<FunctionDecl>(CurFuncDecl);
assert(FD);
ParentMap PM(FD->getBody());
const FunctionDecl* FD=dyn_cast_if_present<FunctionDecl>(CurFuncDecl);
const VarDecl* VD=dyn_cast_if_present<VarDecl>(CurCodeDecl);
assert(FD || VD);
// See malloc for note on const_cast
ParentMap PM(FD ? FD->getBody() : const_cast<Expr*>(VD->getInit()));
const Stmt* parent=PM.getParent(E);
// We need an explicit cast after the call, void* can't be used
llvm::Type *Tys[] = { VoidPtrTy , VoidPtrTy};
const CastExpr* retCE=dyn_cast_or_null<CastExpr>(parent);
if (!retCE || retCE->getType()->isVoidPointerType())
{
if (asmjs) return 0;
CGM.getDiags().Report(E->getBeginLoc(), diag::err_cheerp_alloc_requires_cast);
if (!asmjs)
CGM.getDiags().Report(E->getBeginLoc(), diag::err_cheerp_alloc_requires_cast);
return 0;
}
else
{
Expand Down Expand Up @@ -12714,16 +12722,19 @@ Value *CodeGenFunction::EmitCheerpBuiltinExpr(unsigned BuiltinID,
llvm::Type *Tys[] = { VoidPtrTy, ConvertType(reallocType) };
Ops[0]=EmitScalarExpr(existingMem);
// Some additional checks that can't be done in Sema
const FunctionDecl* FD=dyn_cast<FunctionDecl>(CurFuncDecl);
assert(FD);
ParentMap PM(FD->getBody());
const FunctionDecl* FD=dyn_cast_if_present<FunctionDecl>(CurFuncDecl);
const VarDecl* VD=dyn_cast_if_present<VarDecl>(CurCodeDecl);
assert(FD || VD);
// See malloc for note on const_cast
ParentMap PM(FD ? FD->getBody() : const_cast<Expr*>(VD->getInit()));
const Stmt* parent=PM.getParent(E);
// We need an explicit cast after the call, void* can't be used
const CastExpr* retCE=dyn_cast_or_null<CastExpr>(parent);
if (!retCE || retCE->getType()->isVoidPointerType())
{
if (asmjs) return 0;
CGM.getDiags().Report(E->getBeginLoc(), diag::err_cheerp_alloc_requires_cast);
if (!asmjs)
CGM.getDiags().Report(E->getBeginLoc(), diag::err_cheerp_alloc_requires_cast);
return 0;
}
else if(retCE->getType().getCanonicalType()!=reallocType.getCanonicalType())
{
Expand Down