Skip to content
Open
Changes from 2 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
15 changes: 15 additions & 0 deletions clang/lib/CodeGen/CGExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,21 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
bool isVolatile) {
assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");

if (SanOpts.hasOneOf(SanitizerKind::Null | SanitizerKind::Alignment)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend omitting this check and leaving sanitizePerformTypeCheck to do its work.

Address SrcAddr = Src.getAddress();
Address DestAddr = Dest.getAddress();

// Check source pointer for null and alignment violations
EmitTypeCheck(TCK_Load, SourceLocation(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the scope should be expanded to other cases covered by EmitCheckedLValue.

Copy link
Author

@vasu-the-sharma vasu-the-sharma Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion @hubert-reinterpretcast
I've reviewed the two EmitCheckedLValue usage sites in CGExprAgg.cpp:

Line 802 (VisitCastExpr): Uses EmitCheckedLValue with TCK_Load for dynamic_cast operations
Line 1313 (VisitBinAssign): Uses EmitCheckedLValue with TCK_Store, then calls EmitCopy which delegates to EmitAggregateCopy

Both cases are already covered:
EmitCheckedLValue performs type checking on the LValue expression itself
My changes to EmitAggregateCopy add sanitizer checks at the actual copy operation (the memcpy call)

These checks are complementary rather than redundant:
EmitCheckedLValue: Validates the expression evaluation produces a valid LValue
EmitAggregateCopy: Validates the source and destination pointers during the memory copy operation

The EmitAggregateCopy checks catch cases where pointers might become invalid between LValue emission and the actual copy (like array indexing or pointer arithmetic).
Do you see other specific cases in EmitCheckedLValue usage that would benefit from additional instrumentation?

SrcAddr.emitRawPointer(*this), Ty, SrcAddr.getAlignment(),
SanitizerSet());

// Check destination pointer for null and alignment violations
EmitTypeCheck(TCK_Store, SourceLocation(),
DestAddr.emitRawPointer(*this), Ty, DestAddr.getAlignment(),
SanitizerSet());
}

Address DestPtr = Dest.getAddress();
Address SrcPtr = Src.getAddress();

Expand Down
Loading