-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] Fix crashes when passing VLA to va_arg #119563
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
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-codegen Author: 天音あめ (amane-ame) ChangesCloses #119360. Full diff: https://github.com/llvm/llvm-project/pull/119563.diff 1 Files Affected:
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
// But note that getTypeInfo returns 0 for a VLA.
if (auto *VAT = dyn_cast_or_null<VariableArrayType>(
getContext().getAsArrayType(Ty))) {
+ assert(Ty->isVariableArrayType());
+ EmitVariablyModifiedType(Ty);
QualType BaseEltTy;
SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);
|
|
Please add a testcase to clang/test/CodeGen/ . Put it in an existing file if there's already some related test. (See also https://llvm.org/docs/Contributing.html#how-to-submit-a-patch ) I don't think EmitAggregateCopy is the right place to call EmitVariablyModifiedType: we want to call EmitVariablyModifiedType exactly once for every VLA written in the source code. So the call should be located somewhere that's tightly related to the expression itself: CodeGenFunction::EmitVAArg, or something like that. While you're here, maybe look at emitting an undefined-behavior warning for this construct? A VLA is never compatible with a function argument: if you try to write an array in an function type, it gets promoted to a pointer. So this construct is guaranteed to produce broken results (which is why nobody has tripped over this before). |
Assuming the actual argument is a struct and the |
|
C standard rules for va_arg: "[...] if type is not compatible with the type of the actual next argument [...], the behavior is undefined [...]". A struct is never compatible with an array, so yes , it's undefined. (See 6.2.7 for what constitutes a "compatible type".) As a practical matter, breaking the "compatible type" rule can cause crashes or data corruption. Modern ABIs don't just pass everything on the stack. |
3184f4f to
d94f84a
Compare
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
shafik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello thank you for the fix.
This needs:
- a release note
- More details in the summary describing the cause of the bug and the proposed solution.
f47e96b to
ff56998
Compare
ff56998 to
46a4e44
Compare
|
Ping. |
|
cc @efriedma-quic @tbaederr. |
efriedma-quic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
0c20c75 to
da86d0a
Compare
|
@amane-ame Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Closes #119360.
This bug occurs when passing a VLA to
va_arg. Since the return value is inferred to be an array, it triggersScalarExprEmitter::VisitCastExpr, which converts it to a pointer and subsequently callsCodeGenFunction::EmitAggExpr. At this point, because the inferred type is anAggExprinstead of aScalarExpr,ScalarExprEmitter::VisitVAArgExpris not invoked, and as a result,CodeGenFunction::EmitVariablyModifiedTypeis also not called, leading to the size of the VLA not being retrieved.The solution is to move the call to
CodeGenFunction::EmitVariablyModifiedTypeintoCodeGenFunction::EmitVAArg, ensuring that the size of the VLA is correctly obtained regardless of whether the expression is anAggExpror aScalarExpr.