diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp index fdc56e1c310eb..033638f680934 100644 --- a/lldb/source/Target/Process.cpp +++ b/lldb/source/Target/Process.cpp @@ -38,6 +38,7 @@ #include "lldb/Interpreter/CommandInterpreter.h" #include "lldb/Interpreter/OptionArgParser.h" #include "lldb/Interpreter/OptionValueProperties.h" +#include "lldb/Interpreter/OptionValueUInt64.h" #include "lldb/Symbol/Function.h" #include "lldb/Symbol/Symbol.h" #include "lldb/Target/ABI.h" @@ -173,6 +174,13 @@ ProcessProperties::ProcessProperties(lldb_private::Process *process) // Global process properties, set them up one time m_collection_sp = std::make_shared("process"); m_collection_sp->Initialize(g_process_properties_def); + // MemoryCache divides by the cache line size and holds it in a uint32_t, so + // reject a value it could not use. + OptionValueUInt64 *line_size = + m_collection_sp->GetPropertyAtIndexAsOptionValueUInt64( + ePropertyMemCacheLineSize); + line_size->SetMinimumValue(1); + line_size->SetMaximumValue(UINT32_MAX); m_collection_sp->AppendProperty( "thread", "Settings specific to threads.", true, Thread::GetGlobalProperties().GetValueProperties()); diff --git a/lldb/unittests/Target/MemoryTest.cpp b/lldb/unittests/Target/MemoryTest.cpp index 73f17ca4ce122..97402c4cb6e03 100644 --- a/lldb/unittests/Target/MemoryTest.cpp +++ b/lldb/unittests/Target/MemoryTest.cpp @@ -445,6 +445,45 @@ TEST_F(MemoryTest, TestReadStopsAtAnInvalidRange) { EXPECT_TRUE(inside_error.Fail()); } +TEST_F(MemoryTest, TestUnusableCacheLineSize) { + ArchSpec arch("arm64-apple-macosx"); + + Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch)); + + DebuggerSP debugger_sp = Debugger::CreateInstance(); + ASSERT_TRUE(debugger_sp); + + // A Process copies the global properties when it is constructed, so the + // setting must be in place before CreateProcess, and put back afterwards. + struct SettingGuard { + ~SettingGuard() { + Process::GetGlobalProperties().SetPropertyValue( + nullptr, eVarSetOperationClear, "memory-cache-line-size", ""); + } + } restore_setting; + + auto set_line_size = [](const char *setting) { + return Process::GetGlobalProperties().SetPropertyValue( + nullptr, eVarSetOperationAssign, "memory-cache-line-size", setting); + }; + + // A usable setting must take effect, or the checks below prove nothing. + ASSERT_TRUE(set_line_size("256").Success()); + TargetSP target_sp = CreateTarget(debugger_sp, arch); + DummyProcess *process = + static_cast(CreateProcess(target_sp).get()); + EXPECT_EQ(process->GetMemoryCacheLineSize(), 256u); + + for (const char *setting : {"0", "4294967296"}) { + SCOPED_TRACE(setting); + EXPECT_TRUE(set_line_size(setting).Fail()); + // Refused, so the last usable value is still in effect. + EXPECT_EQ(process->GetMemoryCacheLineSize(), 256u); + TargetSP later_target_sp = CreateTarget(debugger_sp, arch); + EXPECT_EQ(CreateProcess(later_target_sp)->GetMemoryCacheLineSize(), 256u); + } +} + TEST_F(MemoryTest, TestReadInteger) { ArchSpec arch("x86_64-apple-macosx-");