Skip to content

Conversation

@amane-ame
Copy link
Contributor

@amane-ame amane-ame commented Dec 11, 2024

Closes #119360.

This bug occurs when passing a VLA to va_arg. Since the return value is inferred to be an array, it triggers ScalarExprEmitter::VisitCastExpr, which converts it to a pointer and subsequently calls CodeGenFunction::EmitAggExpr. At this point, because the inferred type is an AggExpr instead of a ScalarExpr, ScalarExprEmitter::VisitVAArgExpr is not invoked, and as a result, CodeGenFunction::EmitVariablyModifiedType is also not called, leading to the size of the VLA not being retrieved.
The solution is to move the call to CodeGenFunction::EmitVariablyModifiedType into CodeGenFunction::EmitVAArg, ensuring that the size of the VLA is correctly obtained regardless of whether the expression is an AggExpr or a ScalarExpr.

@github-actions
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Dec 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 11, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: 天音あめ (amane-ame)

Changes

Closes #119360.


Full diff: https://github.com/llvm/llvm-project/pull/119563.diff

1 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprAgg.cpp (+2)
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);

@efriedma-quic efriedma-quic self-requested a review December 11, 2024 20:49
@efriedma-quic
Copy link
Collaborator

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).

@mo7sen
Copy link

mo7sen commented Dec 11, 2024

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 va_arg(args, uint8_t[sizeof(struct)]) is just to get the struct bytes, would that still be considered undefined behaviour?

@efriedma-quic
Copy link
Collaborator

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.

@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Dec 12, 2024
@github-actions
Copy link

github-actions bot commented Dec 16, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@shafik shafik left a 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:

  1. a release note
  2. More details in the summary describing the cause of the bug and the proposed solution.

@amane-ame
Copy link
Contributor Author

Ping.

@amane-ame
Copy link
Contributor Author

cc @efriedma-quic @tbaederr.
Could anyone please review this?

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

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

LGTM

@AaronBallman AaronBallman merged commit ca5fd06 into llvm:main Jan 7, 2025
9 checks passed
@github-actions
Copy link

github-actions bot commented Jan 7, 2025

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clang crashes when casting array returned by va_arg to pointer

6 participants