Skip to content

Commit

Permalink
clang: fix check for realloc pointer argument
Browse files Browse the repository at this point in the history
The previous check, in sema, would look at the section of realloc, not
the one of the calling function.
This would both miss detection of issue for genericjs uses, and falsely
flag asmjs uses with the upcoming commit intruducing #pragma cheerp
  • Loading branch information
yuri91 committed Oct 22, 2024
1 parent 7ef7bd1 commit 77f19d5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 24 deletions.
1 change: 0 additions & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -13372,7 +13372,6 @@ class Sema final {

bool CheckAArch64BuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
CallExpr *TheCall);
bool CheckCheerpBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
bool CheckBPFBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
bool CheckHexagonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
bool CheckHexagonBuiltinArgument(unsigned BuiltinID, CallExpr *TheCall);
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12738,11 +12738,12 @@ Value *CodeGenFunction::EmitCheerpBuiltinExpr(unsigned BuiltinID,
// There must be an incoming cast, void* are not directly accepted
const CastExpr* argCE=dyn_cast<CastExpr>(E->getArg(0));

if (asmjs && (!argCE || argCE->getSubExpr()->getType()->isVoidPointerType()))
if (!argCE || argCE->getSubExpr()->getType()->isVoidPointerType()) {
if (!asmjs)
CGM.getDiags().Report(E->getArg(0)->getBeginLoc(), diag::err_cheerp_memintrinsic_type_unknown);
return 0;
}

// This condition is verified in Sema
assert(argCE && !argCE->getSubExpr()->getType()->isVoidPointerType());
//TODO: realloc can be invoked with NULL, support that
const Expr* existingMem=argCE->getSubExpr();
// The type for the realloc is decided from the base type
Expand Down
20 changes: 0 additions & 20 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2692,13 +2692,6 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
}
}

bool asmjs = FDecl->hasAttr<AsmJSAttr>();
// Some builtins need special handling on generic Cheerp
if (!asmjs && Context.getTargetInfo().getTriple().getArch()==llvm::Triple::cheerp) {
if (CheckCheerpBuiltinFunctionCall(BuiltinID, TheCall))
return ExprError();
}

// Since the target specific builtins for each arch overlap, only check those
// of the arch we are compiling for.
if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) {
Expand Down Expand Up @@ -5484,19 +5477,6 @@ bool Sema::CheckX86BuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
return SemaBuiltinConstantArgRange(TheCall, i, l, u, /*RangeIsError*/ false);
}

bool Sema::CheckCheerpBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
if (BuiltinID == Builtin::BIrealloc) {
const CastExpr* argCE=dyn_cast<CastExpr>(TheCall->getArg(0));
if (!argCE || argCE->getSubExpr()->getType()->isVoidPointerType())
{
Diag(TheCall->getArg(0)->getBeginLoc(), diag::err_cheerp_memintrinsic_type_unknown);
return true;
}
// NOTE: It's not possible to analyze the return type here since we can't build the parent map
}
return false;
}

/// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
/// parameter with the FormatAttr's correct format_idx and firstDataArg.
/// Returns true when the format fits the function and the FormatStringInfo has
Expand Down

0 comments on commit 77f19d5

Please sign in to comment.