Skip to content

Commit 3fd7d43

Browse files
Reland "Revert "[lldb] Fix OP_deref evaluation for large integer results (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)
1 parent e4c052c commit 3fd7d43

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,6 @@ static llvm::Error Evaluate_DW_OP_deref(DWARFExpression::Stack &stack,
993993
" for DW_OP_deref",
994994
pointer_addr),
995995
error.takeError());
996-
if (ABISP abi_sp = process->GetABI())
997-
pointer_value = abi_sp->FixCodeAddress(pointer_value);
998996
stack.back().GetScalar() = pointer_value;
999997
stack.back().ClearContext();
1000998
} break;

lldb/unittests/Expression/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
if ("AArch64" IN_LIST LLVM_TARGETS_TO_BUILD)
2+
set(LINK_COMPONENTS_AARCH AArch64)
3+
set(LINK_LIBS_AARCH lldbPluginABIAArch64)
4+
endif()
5+
16
add_lldb_unittest(ExpressionTests
27
ClangParserTest.cpp
38
ClangExpressionDeclMapTest.cpp
@@ -6,6 +11,9 @@ add_lldb_unittest(ExpressionTests
611
CppModuleConfigurationTest.cpp
712
ExpressionTest.cpp
813

14+
LINK_COMPONENTS
15+
Support
16+
${LINK_COMPONENTS_AARCH}
917
LINK_LIBS
1018
lldbCore
1119
lldbPluginObjectFileELF
@@ -18,4 +26,9 @@ add_lldb_unittest(ExpressionTests
1826
lldbUtilityHelpers
1927
lldbSymbolHelpers
2028
LLVMTestingSupport
29+
${LINK_LIBS_AARCH}
2130
)
31+
32+
if ("AArch64" IN_LIST LLVM_TARGETS_TO_BUILD)
33+
target_compile_definitions(ExpressionTests PRIVATE ARCH_AARCH64)
34+
endif()

lldb/unittests/Expression/DWARFExpressionTest.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/Expression/DWARFExpression.h"
10+
#ifdef ARCH_AARCH64
11+
#include "Plugins/ABI/AArch64/ABISysV_arm64.h"
12+
#endif
1013
#include "Plugins/ObjectFile/wasm/ObjectFileWasm.h"
1114
#include "Plugins/Platform/Linux/PlatformLinux.h"
1215
#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
@@ -21,10 +24,12 @@
2124
#include "lldb/Core/dwarf.h"
2225
#include "lldb/Host/HostInfo.h"
2326
#include "lldb/Symbol/ObjectFile.h"
27+
#include "lldb/Target/ABI.h"
2428
#include "lldb/Target/RegisterContext.h"
2529
#include "lldb/Utility/RegisterValue.h"
2630
#include "lldb/Utility/StreamString.h"
2731
#include "llvm/ADT/StringExtras.h"
32+
#include "llvm/Support/TargetSelect.h"
2833
#include "llvm/Testing/Support/Error.h"
2934
#include "gtest/gtest.h"
3035

@@ -199,6 +204,26 @@ class DWARFExpressionMockProcessTest : public ::testing::Test {
199204
}
200205
};
201206

207+
struct PlatformTargetDebugger {
208+
lldb::PlatformSP platform_sp;
209+
lldb::TargetSP target_sp;
210+
lldb::DebuggerSP debugger_sp;
211+
};
212+
213+
/// A helper function to create <Platform, Target, Debugger> objects with the
214+
/// "aarch64-pc-linux" ArchSpec.
215+
static PlatformTargetDebugger CreateTarget() {
216+
ArchSpec arch("aarch64-pc-linux");
217+
Platform::SetHostPlatform(
218+
platform_linux::PlatformLinux::CreateInstance(true, &arch));
219+
lldb::PlatformSP platform_sp;
220+
lldb::TargetSP target_sp;
221+
lldb::DebuggerSP debugger_sp = Debugger::CreateInstance();
222+
debugger_sp->GetTargetList().CreateTarget(
223+
*debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
224+
return PlatformTargetDebugger{platform_sp, target_sp, debugger_sp};
225+
}
226+
202227
// NB: This class doesn't use the override keyword to avoid
203228
// -Winconsistent-missing-override warnings from the compiler. The
204229
// inconsistency comes from the overriding definitions in the MOCK_*** macros.
@@ -1114,3 +1139,96 @@ TEST_F(DWARFExpressionMockProcessTest, DW_OP_piece_file_addr) {
11141139
ASSERT_EQ(result->GetValueType(), Value::ValueType::HostAddress);
11151140
ASSERT_THAT(result->GetBuffer().GetData(), ElementsAre(0x11, 0x22));
11161141
}
1142+
1143+
/// A Process whose `ReadMemory` override queries a DenseMap.
1144+
struct MockProcessWithMemRead : Process {
1145+
using addr_t = lldb::addr_t;
1146+
1147+
llvm::DenseMap<addr_t, addr_t> memory_map;
1148+
1149+
MockProcessWithMemRead(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
1150+
llvm::DenseMap<addr_t, addr_t> &&memory_map)
1151+
: Process(target_sp, listener_sp), memory_map(memory_map) {}
1152+
size_t DoReadMemory(addr_t vm_addr, void *buf, size_t size,
1153+
Status &error) override {
1154+
assert(memory_map.contains(vm_addr));
1155+
assert(size == sizeof(addr_t));
1156+
*reinterpret_cast<addr_t *>(buf) = memory_map[vm_addr];
1157+
return sizeof(addr_t);
1158+
}
1159+
size_t ReadMemory(addr_t addr, void *buf, size_t size,
1160+
Status &status) override {
1161+
return DoReadMemory(addr, buf, size, status);
1162+
}
1163+
bool CanDebug(lldb::TargetSP, bool) override { return true; }
1164+
Status DoDestroy() override { return Status(); }
1165+
llvm::StringRef GetPluginName() override { return ""; }
1166+
void RefreshStateAfterStop() override {}
1167+
bool DoUpdateThreadList(ThreadList &, ThreadList &) override { return false; }
1168+
};
1169+
1170+
class DWARFExpressionMockProcessTestWithAArch
1171+
: public DWARFExpressionMockProcessTest {
1172+
public:
1173+
void SetUp() override {
1174+
DWARFExpressionMockProcessTest::SetUp();
1175+
#ifdef ARCH_AARCH64
1176+
LLVMInitializeAArch64TargetInfo();
1177+
LLVMInitializeAArch64TargetMC();
1178+
ABISysV_arm64::Initialize();
1179+
#endif
1180+
}
1181+
void TearDown() override {
1182+
DWARFExpressionMockProcessTest::TearDown();
1183+
#ifdef ARCH_AARCH64
1184+
ABISysV_arm64::Terminate();
1185+
#endif
1186+
}
1187+
};
1188+
1189+
/// Sets the value of register x22 to "42".
1190+
/// Creates a process whose memory address 42 contains the value
1191+
/// memory[42] = ((0xffULL) << 56) | 0xabcdef;
1192+
/// The expression DW_OP_breg22, 0, DW_OP_deref should produce that same value,
1193+
/// without clearing the top byte 0xff.
1194+
TEST_F(DWARFExpressionMockProcessTestWithAArch, DW_op_deref_no_ptr_fixing) {
1195+
llvm::DenseMap<lldb::addr_t, lldb::addr_t> memory;
1196+
constexpr lldb::addr_t expected_value = ((0xffULL) << 56) | 0xabcdefULL;
1197+
constexpr lldb::addr_t addr = 42;
1198+
memory[addr] = expected_value;
1199+
1200+
PlatformTargetDebugger test_setup = CreateTarget();
1201+
lldb::ProcessSP process_sp = std::make_shared<MockProcessWithMemRead>(
1202+
test_setup.target_sp, Listener::MakeListener("dummy"), std::move(memory));
1203+
auto thread = std::make_shared<MockThread>(*process_sp);
1204+
lldb::RegisterContextSP reg_ctx_sp =
1205+
std::make_shared<MockRegisterContext>(*thread, RegisterValue(addr));
1206+
thread->SetRegisterContext(reg_ctx_sp);
1207+
process_sp->GetThreadList().AddThread(thread);
1208+
1209+
auto evaluate_expr = [&](auto &expr_data) {
1210+
DataExtractor extractor(expr_data, sizeof(expr_data),
1211+
lldb::eByteOrderLittle,
1212+
/*addr_size*/ 8);
1213+
DWARFExpression expr(extractor);
1214+
1215+
ExecutionContext exe_ctx(process_sp);
1216+
llvm::Expected<Value> result = DWARFExpression::Evaluate(
1217+
&exe_ctx, reg_ctx_sp.get(), /*module_sp*/ nullptr, extractor,
1218+
/*unit*/ nullptr, lldb::eRegisterKindLLDB,
1219+
/*initial_value_ptr=*/nullptr,
1220+
/*object_address_ptr=*/nullptr);
1221+
return result;
1222+
};
1223+
1224+
uint8_t expr_reg[] = {DW_OP_breg22, 0};
1225+
llvm::Expected<Value> result_reg = evaluate_expr(expr_reg);
1226+
ASSERT_THAT_EXPECTED(result_reg, llvm::Succeeded());
1227+
ASSERT_EQ(result_reg->GetValueType(), Value::ValueType::LoadAddress);
1228+
ASSERT_EQ(result_reg->GetScalar().ULongLong(), addr);
1229+
1230+
uint8_t expr_deref[] = {DW_OP_breg22, 0, DW_OP_deref};
1231+
llvm::Expected<Value> result_deref = evaluate_expr(expr_deref);
1232+
ASSERT_THAT_EXPECTED(result_deref, llvm::Succeeded());
1233+
ASSERT_EQ(result_deref->GetScalar().ULongLong(), expected_value);
1234+
}

0 commit comments

Comments
 (0)