Skip to content
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

Detect against invalid variant index for LibStdC++ std::variant data formatters #69253

Merged
merged 4 commits into from
Oct 18, 2023

Conversation

jeffreytan81
Copy link
Contributor

https://github.com/llvm/llvm-project/pull/68012/files added new data formatters for LibStdC++ std::variant.

However, this formatter can crash if std::variant's index field has invalid value (exceeds the number of template arguments).
This can happen if the current IP stops at a place std::variant is not initialized yet.

This patch fixes the crash by ensuring the index is a valid value.

@jeffreytan81 jeffreytan81 marked this pull request as ready for review October 16, 2023 21:27
@llvmbot llvmbot added the lldb label Oct 16, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 16, 2023

@llvm/pr-subscribers-lldb

Author: None (jeffreytan81)

Changes

https://github.com/llvm/llvm-project/pull/68012/files added new data formatters for LibStdC++ std::variant.

However, this formatter can crash if std::variant's index field has invalid value (exceeds the number of template arguments).
This can happen if the current IP stops at a place std::variant is not initialized yet.

This patch fixes the crash by ensuring the index is a valid value.


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

1 Files Affected:

  • (modified) lldb/examples/synthetic/gnu_libstdcpp.py (+5)
diff --git a/lldb/examples/synthetic/gnu_libstdcpp.py b/lldb/examples/synthetic/gnu_libstdcpp.py
index 29c926167fb440c..f778065aaca3771 100644
--- a/lldb/examples/synthetic/gnu_libstdcpp.py
+++ b/lldb/examples/synthetic/gnu_libstdcpp.py
@@ -914,6 +914,11 @@ def get_variant_npos_value(index_byte_size):
     if index == npos_value:
         return " No Value"
 
+    # Invalid index can happen when the variant is not initialized yet.
+    template_arg_count = data_obj.GetType().GetNumberOfTemplateArguments()
+    if index >= template_arg_count:
+        return " <Invalid>"
+
     active_type = data_obj.GetType().GetTemplateArgumentType(index)
     return f" Active Type = {active_type.GetDisplayTypeName()} "
 

Copy link
Collaborator

@clayborg clayborg left a comment

Choose a reason for hiding this comment

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

We should add a test. Should be easy to access the "_M_index" child and set its value to something invalid and then make sure the summary is "".

@jeffreytan81
Copy link
Contributor Author

We should add a test. Should be easy to access the "_M_index" child and set its value to something invalid and then make sure the summary is "".

Sure, I can do that.

template_arg_count = data_obj.GetType().GetNumberOfTemplateArguments()
if index >= template_arg_count:
return " <Invalid>"

active_type = data_obj.GetType().GetTemplateArgumentType(index)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this is what was crashing, we should modify the GetTemplateArgumentType() to not crash with an inalid index as part of this fix.

@jeffreytan81 jeffreytan81 merged commit 659a48f into llvm:main Oct 18, 2023
2 checks passed
@rupprecht
Copy link
Collaborator

This seems to cause TestTemplatePackArgs.py to fail, e.g. https://lab.llvm.org/buildbot/#/builders/68/builds/61819

  File "/home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/lang/cpp/class-template-parameter-pack/TestTemplatePackArgs.py", line 42, in test_template_argument_pack
    self.assertEqual(nested_template.GetTemplateArgumentType(2).GetName(), "bool")
AssertionError: '' != 'bool'

@DavidSpickett
Copy link
Collaborator

Same for AArch64 Ubuntu (https://lab.llvm.org/buildbot/#/builders/96/builds/47090) and Windows (https://lab.llvm.org/buildbot/#/builders/219/builds/6372) (32 bit ARM is out of action right now).

Same symptom as the above comment.

DavidSpickett added a commit that referenced this pull request Oct 18, 2023
…nt data formatters (#69253)"

This reverts commit 659a48f.

This caused another test to fail on Windows AArch64 and x86_64, and AArch64 Linux.
@DavidSpickett
Copy link
Collaborator

I've reverted it as the failure reason wasn't immediately obvious to me. Looks like it will reproduce quite easily.

jeffreytan81 added a commit that referenced this pull request Oct 24, 2023
…ant data formatters (#69614)

This is relanding of #69253.
`TestTemplatePackArgs.py` is passing now.

https://github.com/llvm/llvm-project/pull/68012/files added new data
formatters for LibStdC++ std::variant.

However, this formatter can crash if std::variant's index field has
invalid value (exceeds the number of template arguments).
This can happen if the current IP stops at a place std::variant is not
initialized yet.

This patch fixes the crash by ensuring the index is a valid value and
fix GetNthTemplateArgument() to make sure it is not crashing.

Co-authored-by: jeffreytan81 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants