Skip to content

Conversation

rapidsna
Copy link

@rapidsna rapidsna commented Jul 9, 2025

This should fix a miscompilation bug found during adoption. When a base is lvalue and is wrapped in OVE, the compiler created a temporary and incorrectly mapped the temporary's address as the lvalue instead of the actual address of the lvalue.

The solution is to emit lvalue directly instead of creating extra temporary.

rdar://146329029

This should fix a miscompilation bug found during adoption.
When a base is lvalue and is wrapped in OVE, the compiler
incorrectly creates a temporary and maps its address to the lvalue
instead of the actual address of the lvalue.

The solution is to emit lvalue directly instead of creating
extra temporary.

rdar://146329029
@rapidsna rapidsna added the clang:bounds-safety Issue relating to the experimental -fbounds-safety feature in Clang label Jul 9, 2025
@rapidsna
Copy link
Author

rapidsna commented Jul 9, 2025

@swift-ci test llvm

…ing in order to avoid non-determinism

The execution order of arguments is unspecified in C++. Directly
passing calls that emit code as arguments caused non-deterministic
order in the generated instructions and test failures in different
platforms.
@rapidsna
Copy link
Author

rapidsna commented Jul 9, 2025

@swift-ci test llvm

1 similar comment
@rapidsna
Copy link
Author

rapidsna commented Jul 9, 2025

@swift-ci test llvm

ElemBitCast);
: nullptr;

EmitWidePointerToDest(E->getType(), Ptr, Upper, Lower, ElemBitCast);
Copy link
Author

Choose a reason for hiding this comment

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

@patrykstefanski I suspect this was what caused the Linux code test failures where getelementptr instructions were emitted in a different order on Linux.

Choose a reason for hiding this comment

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

Nice find. After landing this we should try re-enabling the tests we disabled on Linux.

for (auto *OVE : MSE->opaquevalues()) {
if (CodeGenFunction::OpaqueValueMappingData::shouldBindAsLValue(OVE)) {
RValue PtrRV = EmitAnyExpr(OVE->getSourceExpr());
LValue LV = MakeAddrLValue(PtrRV.getAggregateAddress(), OVE->getType());

Choose a reason for hiding this comment

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

Which thing is the temporary here?

Copy link
Author

Choose a reason for hiding this comment

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

The EmitAnyExpr() function creates a temporary slot for aggregates.

Choose a reason for hiding this comment

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

Maybe worth explicitly mentioning that in the commit message for clarity.

@rapidsna
Copy link
Author

@swift-ci test llvm

@rapidsna
Copy link
Author

Test failures seem unrelated to this change

12:53:24 ********************
12:53:24 Failed Tests (7):
12:53:24 Clang :: BoundsSafety/Sema/attributes_in_template_decls_attr_only_mode.cpp
12:53:24 Clang :: BoundsSafety/Sema/complex-typespecs-with-bounds.c
12:53:24 Clang :: BoundsSafety/Sema/terminated-by-attr.c
12:53:24 Clang :: BoundsSafety/Sema/unsafe-late-const.c
12:53:24 Clang :: Frontend/macro_defined_type.cpp
12:53:24 Clang :: SemaCXX/attr-transparent-stepping-method.cpp
12:53:24 Clang :: SemaCXX/warn-thread-safety-parsing.cpp

@rapidsna rapidsna merged commit e7a3802 into next Jul 12, 2025
0 of 2 checks passed
@rapidsna rapidsna deleted the fix-ove-lvalue-codegen-2 branch July 12, 2025 00:07
rapidsna added a commit that referenced this pull request Jul 30, 2025
rapidsna added a commit that referenced this pull request Jul 30, 2025
rapidsna added a commit that referenced this pull request Jul 30, 2025
…11082)

* Revert "[BoundsSafety] Don't bind lvalues with temporary locations (#10969)"

This reverts commit e7a3802.

* Adjust some CodeGen tests to match `next`
rapidsna added a commit that referenced this pull request Jul 30, 2025
…1083)

* Revert "[BoundsSafety] Don't bind lvalues with temporary locations (#10969)"

This reverts commit e7a3802.

* Adjust some failing CodeGen tests
rapidsna added a commit that referenced this pull request Sep 24, 2025
… temporary locations (#11469)

- Solution from #10969:

This should fix a miscompilation bug found during adoption. When a base is lvalue and is wrapped in OVE, the compiler created a temporary and incorrectly mapped the temporary's address as the lvalue instead of the actual address of the lvalue.

The solution is to emit lvalue directly instead of creating extra temporary.

- Issue with the solution:

The issue #10969 got originally reverted due "error: cannot compile this l-value expression yet" occurred when __terminated_by_to_indexable is wrapped in an OVE.

- Fix for the new issue with __terminated_by_to_indexable:

The fix is to implement "TerminatedByToIndexableExprLValue" so the codegen can also emit as if it's an lvalue.

rdar://146329029
rapidsna added a commit that referenced this pull request Sep 26, 2025
… temporary locations (#11469)

- Solution from #10969:

This should fix a miscompilation bug found during adoption. When a base is lvalue and is wrapped in OVE, the compiler created a temporary and incorrectly mapped the temporary's address as the lvalue instead of the actual address of the lvalue.

The solution is to emit lvalue directly instead of creating extra temporary.

- Issue with the solution:

The issue #10969 got originally reverted due "error: cannot compile this l-value expression yet" occurred when __terminated_by_to_indexable is wrapped in an OVE.

- Fix for the new issue with __terminated_by_to_indexable:

The fix is to implement "TerminatedByToIndexableExprLValue" so the codegen can also emit as if it's an lvalue.

rdar://146329029
(cherry picked from commit 04da1af)
rapidsna added a commit that referenced this pull request Sep 27, 2025
… temporary locations (#11469) (#11504)

- Solution from #10969:

This should fix a miscompilation bug found during adoption. When a base is lvalue and is wrapped in OVE, the compiler created a temporary and incorrectly mapped the temporary's address as the lvalue instead of the actual address of the lvalue.

The solution is to emit lvalue directly instead of creating extra temporary.

- Issue with the solution:

The issue #10969 got originally reverted due "error: cannot compile this l-value expression yet" occurred when __terminated_by_to_indexable is wrapped in an OVE.

- Fix for the new issue with __terminated_by_to_indexable:

The fix is to implement "TerminatedByToIndexableExprLValue" so the codegen can also emit as if it's an lvalue.

rdar://146329029
(cherry picked from commit 04da1af)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:bounds-safety Issue relating to the experimental -fbounds-safety feature in Clang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants