Skip to content

Conversation

felipepiovezan
Copy link
Contributor

When evaluating any DWARF expression that ended in OP_deref and whose previous value on the dwarf stack -- the pointer address for the deref -- was a load address, we were treating the result itself as a pointer, calling Process:FixCodeAddress(result). This is wrong: there's no guarantee that the result is a pointer itself.

@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-lldb

Author: Felipe de Azevedo Piovezan (felipepiovezan)

Changes

When evaluating any DWARF expression that ended in OP_deref and whose previous value on the dwarf stack -- the pointer address for the deref -- was a load address, we were treating the result itself as a pointer, calling Process:FixCodeAddress(result). This is wrong: there's no guarantee that the result is a pointer itself.


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

3 Files Affected:

  • (modified) lldb/source/Expression/DWARFExpression.cpp (-2)
  • (modified) lldb/unittests/Expression/CMakeLists.txt (+4)
  • (modified) lldb/unittests/Expression/DWARFExpressionTest.cpp (+100)
diff --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp
index 5040351f4975b..4f9d6ebf27bf0 100644
--- a/lldb/source/Expression/DWARFExpression.cpp
+++ b/lldb/source/Expression/DWARFExpression.cpp
@@ -909,8 +909,6 @@ static llvm::Error Evaluate_DW_OP_deref(DWARFExpression::Stack &stack,
               " for DW_OP_deref",
               pointer_addr),
           error.takeError());
-    if (ABISP abi_sp = process->GetABI())
-      pointer_value = abi_sp->FixCodeAddress(pointer_value);
     stack.back().GetScalar() = pointer_value;
     stack.back().ClearContext();
   } break;
diff --git a/lldb/unittests/Expression/CMakeLists.txt b/lldb/unittests/Expression/CMakeLists.txt
index 4c58b3c5e3922..a22341d9155cb 100644
--- a/lldb/unittests/Expression/CMakeLists.txt
+++ b/lldb/unittests/Expression/CMakeLists.txt
@@ -6,12 +6,16 @@ add_lldb_unittest(ExpressionTests
   CppModuleConfigurationTest.cpp
   ExpressionTest.cpp
 
+  LINK_COMPONENTS
+    AArch64
+    Support
   LINK_LIBS
     lldbCore
     lldbPluginObjectFileELF
     lldbPluginObjectFileWasm
     lldbPluginSymbolVendorWasm
     lldbPluginPlatformLinux
+    lldbPluginABIAArch64
     lldbPluginExpressionParserClang
     lldbPluginTypeSystemClang
     lldbUtility
diff --git a/lldb/unittests/Expression/DWARFExpressionTest.cpp b/lldb/unittests/Expression/DWARFExpressionTest.cpp
index 5a5d3aba0e207..3ea232b70f863 100644
--- a/lldb/unittests/Expression/DWARFExpressionTest.cpp
+++ b/lldb/unittests/Expression/DWARFExpressionTest.cpp
@@ -6,6 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "lldb/Target/ABI.h"
+#include "Plugins/ABI/AArch64/ABISysV_arm64.h"
+#include "llvm/Support/TargetSelect.h"
 #include "lldb/Expression/DWARFExpression.h"
 #include "Plugins/ObjectFile/wasm/ObjectFileWasm.h"
 #include "Plugins/Platform/Linux/PlatformLinux.h"
@@ -190,15 +193,39 @@ class DWARFExpressionMockProcessTest : public ::testing::Test {
   void SetUp() override {
     FileSystem::Initialize();
     HostInfo::Initialize();
+    LLVMInitializeAArch64TargetInfo();
+    LLVMInitializeAArch64TargetMC();
     platform_linux::PlatformLinux::Initialize();
+    ABISysV_arm64::Initialize();
   }
   void TearDown() override {
     platform_linux::PlatformLinux::Terminate();
     HostInfo::Terminate();
     FileSystem::Terminate();
+    ABISysV_arm64::Terminate();
   }
 };
 
+struct PlatformTargetDebugger {
+  lldb::PlatformSP platform_sp;
+  lldb::TargetSP target_sp;
+  lldb::DebuggerSP debugger_sp;
+};
+
+/// A helper function to create <Platform, Target, Debugger> objects with the
+/// "aarch64-pc-linux" ArchSpec.
+static PlatformTargetDebugger CreateTarget() {
+  ArchSpec arch("aarch64-pc-linux");
+  Platform::SetHostPlatform(
+      platform_linux::PlatformLinux::CreateInstance(true, &arch));
+  lldb::PlatformSP platform_sp;
+  lldb::TargetSP target_sp;
+  lldb::DebuggerSP debugger_sp = Debugger::CreateInstance();
+  debugger_sp->GetTargetList().CreateTarget(
+      *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
+  return PlatformTargetDebugger{platform_sp, target_sp, debugger_sp};
+}
+
 // NB: This class doesn't use the override keyword to avoid
 // -Winconsistent-missing-override warnings from the compiler. The
 // inconsistency comes from the overriding definitions in the MOCK_*** macros.
@@ -1135,3 +1162,76 @@ TEST_F(DWARFExpressionMockProcessTest, DW_OP_piece_file_addr) {
   ASSERT_EQ(result->GetValueType(), Value::ValueType::HostAddress);
   ASSERT_THAT(result->GetBuffer().GetData(), ElementsAre(0x11, 0x22));
 }
+
+/// A Process whose `ReadMemory` override queries a DenseMap.
+struct MockProcessWithMemRead : Process {
+  using addr_t = lldb::addr_t;
+
+  llvm::DenseMap<addr_t, addr_t> memory_map;
+
+  MockProcessWithMemRead(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
+                         llvm::DenseMap<addr_t, addr_t> &&memory_map)
+      : Process(target_sp, listener_sp), memory_map(memory_map) {}
+  size_t DoReadMemory(addr_t vm_addr, void *buf, size_t size,
+                      Status &error) override {
+    assert(memory_map.contains(vm_addr));
+    assert(size == sizeof(addr_t));
+    *reinterpret_cast<addr_t *>(buf) = memory_map[vm_addr];
+    return sizeof(addr_t);
+  }
+  size_t ReadMemory(addr_t addr, void *buf, size_t size,
+                    Status &status) override {
+    return DoReadMemory(addr, buf, size, status);
+  }
+  bool CanDebug(lldb::TargetSP, bool) override { return true; }
+  Status DoDestroy() override { return Status(); }
+  llvm::StringRef GetPluginName() override { return ""; }
+  void RefreshStateAfterStop() override {}
+  bool DoUpdateThreadList(ThreadList &, ThreadList &) override { return false; }
+};
+
+/// Sets the value of register x22 to "42".
+/// Creates a process whose memory address 42 contains the value
+///   memory[42] = ((0xffULL) << 56) | 0xabcdef;
+/// The expression DW_OP_breg22, 0, DW_OP_deref should produce that same value,
+/// without clearing the top byte 0xff.
+TEST_F(DWARFExpressionMockProcessTest, DW_op_deref_no_ptr_fixing) {
+  llvm::DenseMap<lldb::addr_t, lldb::addr_t> memory;
+  constexpr lldb::addr_t expected_value = ((0xffULL) << 56) | 0xabcdefULL;
+  memory[42] = expected_value;
+
+  PlatformTargetDebugger test_setup = CreateTarget();
+  lldb::ProcessSP process_sp = std::make_shared<MockProcessWithMemRead>(
+      test_setup.target_sp, Listener::MakeListener("dummy"), std::move(memory));
+  auto thread = std::make_shared<MockThread>(*process_sp);
+  lldb::RegisterContextSP reg_ctx_sp =
+      std::make_shared<MockRegisterContext>(*thread, RegisterValue(42ull));
+  thread->SetRegisterContext(reg_ctx_sp);
+  process_sp->GetThreadList().AddThread(thread);
+
+  auto evaluate_expr = [&](auto &expr_data) {
+    DataExtractor extractor(expr_data, sizeof(expr_data),
+                            lldb::eByteOrderLittle,
+                            /*addr_size*/ 8);
+    DWARFExpression expr(extractor);
+
+    ExecutionContext exe_ctx(process_sp);
+    llvm::Expected<Value> result = DWARFExpression::Evaluate(
+        &exe_ctx, reg_ctx_sp.get(), /*module_sp*/ nullptr, extractor,
+        /*unit*/ nullptr, lldb::eRegisterKindLLDB,
+        /*initial_value_ptr=*/nullptr,
+        /*object_address_ptr=*/nullptr);
+    return result;
+  };
+
+  uint8_t expr_reg[] = {DW_OP_breg22, 0};
+  llvm::Expected<Value> result_reg = evaluate_expr(expr_reg);
+  ASSERT_THAT_EXPECTED(result_reg, llvm::Succeeded());
+  ASSERT_EQ(result_reg->GetValueType(), Value::ValueType::LoadAddress);
+  ASSERT_EQ(result_reg->GetScalar().ULongLong(), 42ull);
+
+  uint8_t expr_deref[] = {DW_OP_breg22, 0, DW_OP_deref};
+  llvm::Expected<Value> result_deref = evaluate_expr(expr_deref);
+  ASSERT_THAT_EXPECTED(result_deref, llvm::Succeeded());
+  ASSERT_EQ(result_deref->GetScalar().ULongLong(), expected_value);
+}

Copy link

github-actions bot commented Sep 17, 2025

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

When evaluating any DWARF expression that ended in OP_deref and whose
previous value on the dwarf stack -- the pointer address for the deref
-- was a load address, we were treating the result itself as a pointer,
calling Process:FixCodeAddress(result). This is wrong: there's no
guarantee that the result is a pointer itself.
Copy link
Collaborator

@jasonmolenda jasonmolenda left a comment

Choose a reason for hiding this comment

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

Nice fix, good job on coming up with a test for it too, that's a lot of setup for this.

@felipepiovezan felipepiovezan merged commit 1d2007b into llvm:main Sep 17, 2025
9 checks passed
@felipepiovezan felipepiovezan deleted the felipe/op_deref_bugdix branch September 17, 2025 22:46
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 17, 2025

LLVM Buildbot has detected a new failure on builder cross-project-tests-sie-ubuntu-dwarf5 running on doug-worker-1b while building lldb at step 4 "cmake-configure".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/163/builds/26651

Here is the relevant piece of the build log for the reference
Step 4 (cmake-configure) failure: cmake (failure)
...
-- Enable Lua scripting support in LLDB: FALSE
-- Found Python3: /usr/bin/python3.10 (found version "3.10.12") found components: Interpreter Development Development.Module Development.Embed 
-- Found PythonAndSwig: /usr/lib/x86_64-linux-gnu/libpython3.10.so  
-- Enable Python scripting support in LLDB: TRUE
-- Could NOT find LibXml2 (missing: LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR) (Required is at least version "2.8")
-- Enable Libxml 2 support in LLDB: FALSE
-- Enable libfbsdvmcore support in LLDB: 0
-- Performing Test CXX_SUPPORTS_STRINGOP_TRUNCATION
-- Performing Test CXX_SUPPORTS_STRINGOP_TRUNCATION - Success
-- LLDB version: 22.0.0git
-- Looking for ppoll
-- Looking for ppoll - found
-- Looking for ptsname_r
-- Looking for ptsname_r - found
-- Looking for accept4
-- Looking for accept4 - found
-- Looking for termios.h
-- Looking for termios.h - found
-- Looking for include files sys/types.h, sys/event.h
-- Looking for include files sys/types.h, sys/event.h - not found
-- Looking for process_vm_readv
-- Looking for process_vm_readv - found
-- Looking for __NR_process_vm_readv
-- Looking for __NR_process_vm_readv - found
-- Looking for compression_encode_buffer in compression
-- Looking for compression_encode_buffer in compression - not found
-- SWIG version 4.0.2 uses `-py3` flag.
-- Skipping FreeBSDKernel plugin due to missing libfbsdvmcore
-- Symbols (liblldb): exporting all symbols from the lldb namespace
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
-- Found make: /usr/bin/gmake
-- Performing Test CXX_SUPPORTS_DOCUMENTATION
-- Performing Test CXX_SUPPORTS_DOCUMENTATION - Failed
-- Performing Test CXX_SUPPORTS_NO_DOCUMENTATION_DEPRECATED_SYNC
-- Performing Test CXX_SUPPORTS_NO_DOCUMENTATION_DEPRECATED_SYNC - Success
CMake Error at cmake/modules/LLVM-Config.cmake:271 (message):
  Library 'AArch64' is a direct reference to a target library for an omitted
  target.
Call Stack (most recent call first):
  cmake/modules/LLVM-Config.cmake:105 (llvm_map_components_to_libnames)
  cmake/modules/LLVM-Config.cmake:98 (explicit_llvm_config)
  cmake/modules/AddLLVM.cmake:1131 (llvm_config)
  cmake/modules/AddLLVM.cmake:1769 (add_llvm_executable)
  /home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/lldb/unittests/CMakeLists.txt:26 (add_unittest)
  /home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/lldb/unittests/Expression/CMakeLists.txt:1 (add_lldb_unittest)


-- Configuring incomplete, errors occurred!
See also "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/build/CMakeFiles/CMakeOutput.log".
See also "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/build/CMakeFiles/CMakeError.log".

@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 17, 2025

LLVM Buildbot has detected a new failure on builder cross-project-tests-sie-ubuntu running on doug-worker-1a while building lldb at step 4 "cmake-configure".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/181/builds/28097

Here is the relevant piece of the build log for the reference
Step 4 (cmake-configure) failure: cmake (failure)
...
-- Enable Lua scripting support in LLDB: FALSE
-- Found Python3: /usr/bin/python3.8 (found version "3.8.10") found components: Interpreter Development Development.Module Development.Embed 
-- Found PythonAndSwig: /usr/lib/x86_64-linux-gnu/libpython3.8.so  
-- Enable Python scripting support in LLDB: TRUE
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found suitable version "2.9.10", minimum required is "2.8") 
-- Enable Libxml 2 support in LLDB: TRUE
-- Enable libfbsdvmcore support in LLDB: 0
-- Performing Test CXX_SUPPORTS_STRINGOP_TRUNCATION
-- Performing Test CXX_SUPPORTS_STRINGOP_TRUNCATION - Success
-- LLDB version: 22.0.0git
-- Looking for ppoll
-- Looking for ppoll - found
-- Looking for ptsname_r
-- Looking for ptsname_r - found
-- Looking for accept4
-- Looking for accept4 - found
-- Looking for termios.h
-- Looking for termios.h - found
-- Looking for include files sys/types.h, sys/event.h
-- Looking for include files sys/types.h, sys/event.h - not found
-- Looking for process_vm_readv
-- Looking for process_vm_readv - found
-- Looking for __NR_process_vm_readv
-- Looking for __NR_process_vm_readv - found
-- Looking for compression_encode_buffer in compression
-- Looking for compression_encode_buffer in compression - not found
-- SWIG version 4.0.1 uses `-py3` flag.
-- Skipping FreeBSDKernel plugin due to missing libfbsdvmcore
-- Symbols (liblldb): exporting all symbols from the lldb namespace
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
-- Found make: /usr/bin/make
-- Performing Test CXX_SUPPORTS_DOCUMENTATION
-- Performing Test CXX_SUPPORTS_DOCUMENTATION - Failed
-- Performing Test CXX_SUPPORTS_NO_DOCUMENTATION_DEPRECATED_SYNC
-- Performing Test CXX_SUPPORTS_NO_DOCUMENTATION_DEPRECATED_SYNC - Success
CMake Error at cmake/modules/LLVM-Config.cmake:271 (message):
  Library 'AArch64' is a direct reference to a target library for an omitted
  target.
Call Stack (most recent call first):
  cmake/modules/LLVM-Config.cmake:105 (llvm_map_components_to_libnames)
  cmake/modules/LLVM-Config.cmake:98 (explicit_llvm_config)
  cmake/modules/AddLLVM.cmake:1131 (llvm_config)
  cmake/modules/AddLLVM.cmake:1769 (add_llvm_executable)
  /home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/lldb/unittests/CMakeLists.txt:26 (add_unittest)
  /home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/llvm-project/lldb/unittests/Expression/CMakeLists.txt:1 (add_lldb_unittest)


-- Configuring incomplete, errors occurred!
See also "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/build/CMakeFiles/CMakeOutput.log".
See also "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu/build/CMakeFiles/CMakeError.log".

@felipepiovezan
Copy link
Contributor Author

CMake Error at cmake/modules/LLVM-Config.cmake:271 (message):
  Library 'AArch64' is a direct reference to a target library for an omitted
  target.

Seems like a real problem. Will revert while I debug.

felipepiovezan added a commit to felipepiovezan/llvm-project that referenced this pull request Sep 17, 2025
…lts (llvm#159460)""

The original had an issue on "AArch-less" bots.
Fixed it with some ifdefs around the presence of the AArch ABI plugin.

This reverts commit 1a4685d.
felipepiovezan added a commit that referenced this pull request Sep 18, 2025
#159482)

…lts (#159460)""

The original had an issue on "AArch-less" bots.
Fixed it with some ifdefs around the presence of the AArch ABI plugin.

This reverts commit 1a4685d.
felipepiovezan added a commit to felipepiovezan/llvm-project that referenced this pull request Sep 18, 2025
…lts (llvm#159460)""

The original had an issue on "AArch-less" bots.
Fixed it with some ifdefs around the presence of the AArch ABI plugin.

This reverts commit 1a4685d.

(cherry picked from commit 40eb976)
felipepiovezan added a commit to felipepiovezan/llvm-project that referenced this pull request Sep 18, 2025
…lts (llvm#159460)""

The original had an issue on "AArch-less" bots.
Fixed it with some ifdefs around the presence of the AArch ABI plugin.

Note for the cherry-pick: the test was removed as the related test file
in this branch is too old.

This reverts commit 1a4685d.

Cherry-picked from 40eb976.
kimsh02 pushed a commit to kimsh02/llvm-project that referenced this pull request Sep 19, 2025
When evaluating any DWARF expression that ended in OP_deref and whose
previous value on the dwarf stack -- the pointer address for the deref
-- was a load address, we were treating the result itself as a pointer,
calling Process:FixCodeAddress(result). This is wrong: there's no
guarantee that the result is a pointer itself.
kimsh02 pushed a commit to kimsh02/llvm-project that referenced this pull request Sep 19, 2025
kimsh02 pushed a commit to kimsh02/llvm-project that referenced this pull request Sep 19, 2025
llvm#159482)

…lts (llvm#159460)""

The original had an issue on "AArch-less" bots.
Fixed it with some ifdefs around the presence of the AArch ABI plugin.

This reverts commit 1a4685d.
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