Skip to content

Avoid accessing unset optional, workaround for #100095 #100408

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 4 commits into from
Jul 26, 2024

Conversation

alexfh
Copy link
Contributor

@alexfh alexfh commented Jul 24, 2024

This patch avoids accessing an unset std::optional<>, which is a part of the manifestation of #100095. The other part is an assertion failure that is not addressed here. This is not a proper fix, but enables Clang to continue working with more libc++ runtime checks enabled (specifically, -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST, which checks access to unset optionals among other things). A proper fix is being discussed on #100095.

This is not a proper fix, but enables Clang to continue working with more libc++ runtime checks enabled.
@alexfh alexfh marked this pull request as ready for review July 24, 2024 16:41
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2024

@llvm/pr-subscribers-clang

Author: Alexander Kornienko (alexfh)

Changes

This is not a proper fix, but enables Clang to continue working with more libc++ runtime checks enabled.


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+3-1)
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index b7b857ebf804b..db7f233dcef73 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -951,9 +951,11 @@ class PackDeductionScope {
 
     // Skip over the pack elements that were expanded into separate arguments.
     // If we partially expanded, this is the number of partial arguments.
+    // FIXME: `&& FixedNumExpansions` is a workaround for UB described in
+    // https://github.com/llvm/llvm-project/issues/100095
     if (IsPartiallyExpanded)
       PackElements += NumPartialPackArgs;
-    else if (IsExpanded)
+    else if (IsExpanded && FixedNumExpansions)
       PackElements += *FixedNumExpansions;
 
     for (auto &Pack : Packs) {

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.

Can you please add a reference to #100095 in the summary so folks just reading the git log have more context w/o going to the commit itself.

I would like to see a more flushed out long-term plan for fixing this properly but I think it makes sense.

@alexfh
Copy link
Contributor Author

alexfh commented Jul 24, 2024

Can you please add a reference to #100095 in the summary so folks just reading the git log have more context w/o going to the commit itself.

I would like to see a more flushed out long-term plan for fixing this properly but I think it makes sense.

I've added more details to the commit message and another reference to #100095 (one was already in the short summary).

@mizvekov
Copy link
Contributor

I don't believe we currently have any code coverage at all for the line affected.

So with this change, which we don't fully understand, we could be breaking currently working code, and we could not even be fixing the known broken cases, as there could be further UB down the line.

I wouldn't object this patch, if we don't have other means of figuring this out soon.
But I'd like to try first getting a workaround implemented in libc++, I think that would be preferable.
If we don't get an answer about this soon, sure I'd say let's go ahead.

But in that case, I'd like to see at least a test added to this PR.

@alexfh
Copy link
Contributor Author

alexfh commented Jul 25, 2024

... I'd like to see at least a test added to this PR.

See #100556. The test would fail with assertions enabled (thus, marked XFAIL: asserts), and when compiled against hardened libc++ (e.g. libc++ built with -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST).

@ilya-biryukov
Copy link
Contributor

and when compiled against hardened libc++

Could we merge the test into this change? I think we could then land this as a temporary workaround indeed.
I'll keep looking into fixing this proper and will remove the XFAIL line when appropriate.

It seems that having this is strictly better than what we have today (adds real code where it fails, fails with an assertion when they are enabled, produces some deterministic, even if incorrect, result, rather than having a UB).

alexfh added 2 commits July 26, 2024 11:27
The test is set to XFAIL with assertions enabled.
@alexfh
Copy link
Contributor Author

alexfh commented Jul 26, 2024

Could we merge the test into this change? I think we could then land this as a temporary workaround indeed. I'll keep looking into fixing this proper and will remove the XFAIL line when appropriate.

I've added the test to the change and verified it:

  • fails without the SemaTemplateDeduction.cpp change, when compiled against libc++ with hardening enabled and with LLVM assertions disabled
  • passes with the SemaTemplateDeduction.cpp change, when compiled against libc++ with hardening enabled and with LLVM assertions disabled
  • XFAILs with the SemaTemplateDeduction.cpp change, when compiled against libc++ with hardening enabled and with LLVM assertions enabled.

Unfortunately, we don't have a buildbot with LLVM tests built against libc++ + hardening, but we're going to test it routinely internally at Google.

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

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

LGTM, since it now has a test that executes this code path and changes from UB to defined (even if undesired) behavior.

As mentioned earlier, I'm working on a proper fix for those cases

@alexfh alexfh merged commit 8322a30 into llvm:main Jul 26, 2024
7 checks passed
@alexfh alexfh deleted the clang-100095 branch July 26, 2024 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

5 participants