Skip to content

[clang][bytecode] Use in Expr::tryEvaluateStrLen() #149677

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 2 commits into from
Jul 20, 2025
Merged
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
37 changes: 37 additions & 0 deletions clang/lib/AST/ByteCode/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,43 @@ bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
}

bool Context::evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result) {
assert(Stk.empty());
Compiler<EvalEmitter> C(*this, *P, Parent, Stk);

auto PtrRes = C.interpretAsPointer(E, [&](const Pointer &Ptr) {
const Descriptor *FieldDesc = Ptr.getFieldDesc();
if (!FieldDesc->isPrimitiveArray())
return false;

unsigned N = Ptr.getNumElems();
if (Ptr.elemSize() == 1) {
Result = strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), N);
return Result != N;
}

PrimType ElemT = FieldDesc->getPrimType();
Result = 0;
for (unsigned I = Ptr.getIndex(); I != N; ++I) {
INT_TYPE_SWITCH(ElemT, {
auto Elem = Ptr.atIndex(I).deref<T>();
if (Elem.isZero())
return true;
++Result;
});
}
// We didn't find a 0 byte.
return false;
});

if (PtrRes.isInvalid()) {
C.cleanup();
Stk.clear();
return false;
}
return true;
}

const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }

static PrimType integralTypeToPrimTypeS(unsigned BitWidth) {
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ByteCode/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class Context final {
bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
const Expr *PtrExpr, std::string &Result);

/// Evalute \param E and if it can be evaluated to a string literal,
/// run strlen() on it.
bool evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result);

/// Returns the AST context.
ASTContext &getASTContext() const { return Ctx; }
/// Returns the language options.
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18184,6 +18184,10 @@ bool Expr::EvaluateCharRangeAsString(APValue &Result,
bool Expr::tryEvaluateStrLen(uint64_t &Result, ASTContext &Ctx) const {
Expr::EvalStatus Status;
EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold);

if (Info.EnableNewConstInterp)
return Info.Ctx.getInterpContext().evaluateStrlen(Info, this, Result);

return EvaluateBuiltinStrLen(this, Result, Info);
}

Expand Down
5 changes: 5 additions & 0 deletions clang/test/Sema/warn-fortify-source.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify
// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS

// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS -fexperimental-new-constant-interpreter

typedef unsigned long size_t;

#ifdef __cplusplus
Expand Down
Loading