Skip to content

[lldb][NFCI] Simplify ProcessElfCore::GetAuxvData() #102263

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

Conversation

igorkudrin
Copy link
Collaborator

There is no need to clone the content and set extraction properties because m_auxv is already in the required form.

There is no need to create a new `DataExtractor` and clone the content,
because `m_auxv` already has the required form.
@llvmbot
Copy link
Member

llvmbot commented Aug 7, 2024

@llvm/pr-subscribers-lldb

Author: Igor Kudrin (igorkudrin)

Changes

There is no need to clone the content and set extraction properties because m_auxv is already in the required form.


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

1 Files Affected:

  • (modified) lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp (+4-4)
diff --git a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
index 30af9345999c41..e73e31ca78d19f 100644
--- a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
+++ b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
@@ -1077,10 +1077,10 @@ ArchSpec ProcessElfCore::GetArchitecture() {
 }
 
 DataExtractor ProcessElfCore::GetAuxvData() {
-  const uint8_t *start = m_auxv.GetDataStart();
-  size_t len = m_auxv.GetByteSize();
-  lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(start, len));
-  return DataExtractor(buffer, GetByteOrder(), GetAddressByteSize());
+  assert(m_auxv.GetByteSize() == 0 ||
+         (m_auxv.GetByteOrder() == GetByteOrder() &&
+          m_auxv.GetAddressByteSize() == GetAddressByteSize()));
+  return DataExtractor(m_auxv);
 }
 
 bool ProcessElfCore::GetProcessInfo(ProcessInstanceInfo &info) {

Comment on lines +1080 to +1082
assert(m_auxv.GetByteSize() == 0 ||
(m_auxv.GetByteOrder() == GetByteOrder() &&
m_auxv.GetAddressByteSize() == GetAddressByteSize()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this assert? Can't we just change this to:

const DataExtractor &ProcessElfCore::GetAuxvData() { return m_auxv; }

Copy link
Collaborator Author

@igorkudrin igorkudrin Aug 7, 2024

Choose a reason for hiding this comment

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

I added the assert to be on the safe side. It shows that the change is eligible, and if I overlooked some exotic execution path, it will trigger and point directly to the problem. If you believe it's excessive, I'll remove it.

The copy constructor for DataExtractor should be called explicitly, see the comment. And GetAuxvData() cannot be changed to return a reference to a data member because in some classes it returns a temporary object.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no need to copy the data extractor. It contains no modifiable state as the position curser is passed in as variables when acessing the data within. I would highly suggest changing to:

const DataExtractor &ProcessElfCore::GetAuxvData() { 
  // Feel free to add asserts here if you want them as long as the code functions correctly
  // even if the asserts get triggered.
  return m_auxv; 
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd also prefer to return a constant reference, but there are other implementations of this virtual function that can't do that, for example, ProcessGDBRemote::GetAuxvData().

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I didn't realize this was a virtual function from Process.h...

@igorkudrin igorkudrin merged commit 494eec0 into llvm:main Aug 13, 2024
9 checks passed
@igorkudrin igorkudrin deleted the simplify-processelfcore-getauxvdata branch August 13, 2024 17:55
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.

3 participants