Skip to content

Commit d6fcaef

Browse files
authored
[LLDB][Value] Require type size when reading a scalar (#153386)
When reading a value as a scalar, the type size is required. It's returned as a `std::optional`. This optional isn't checked for scalar values, where it is unconditionally accessed. This came up in the [Shell/Process/Windows/msstl_smoke.cpp](https://github.com/llvm/llvm-project/blob/4e10b62442e9edf1769b98406b0559f515d9791f/lldb/test/Shell/Process/Windows/msstl_smoke.cpp) test. There, LLDB breaks at the function entry, so all locals aren't initialized yet. Most values will contain garbage. The [`std::list` synthetic provider](https://github.com/llvm/llvm-project/blob/4e10b62442e9edf1769b98406b0559f515d9791f/lldb/source/Plugins/Language/CPlusPlus/GenericList.cpp#L517) tries to read the value using `GetData`. However, in [`ValueObject::GetData`](https://github.com/llvm/llvm-project/blob/4e10b62442e9edf1769b98406b0559f515d9791f/lldb/source/ValueObject/ValueObject.cpp#L766), [`ValueObjectChild::UpdateValue`](https://github.com/llvm/llvm-project/blob/88c993fbc5b87030b082aeb99d4db94cc885ed1d/lldb/source/ValueObject/ValueObjectChild.cpp#L102) fails because the parent already failed to read its data, so `m_value` won't have a compiler type, thus the size can't be read.
1 parent 17dbb92 commit d6fcaef

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lldb/source/Core/Value.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
347347
else
348348
data.SetAddressByteSize(sizeof(void *));
349349

350+
if (!type_size)
351+
return Status::FromErrorString("type does not have a size");
352+
350353
uint32_t result_byte_size = *type_size;
351354
if (m_value.GetData(data, result_byte_size))
352355
return error; // Success;

lldb/unittests/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_lldb_unittest(LLDBCoreTests
1515
SourceManagerTest.cpp
1616
TelemetryTest.cpp
1717
UniqueCStringMapTest.cpp
18+
Value.cpp
1819

1920
LINK_COMPONENTS
2021
Support

lldb/unittests/Core/Value.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lldb/Core/Value.h"
10+
#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
11+
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12+
#include "TestingSupport/SubsystemRAII.h"
13+
#include "TestingSupport/Symbol/ClangTestUtils.h"
14+
15+
#include "lldb/Utility/DataExtractor.h"
16+
17+
#include "gtest/gtest.h"
18+
19+
using namespace lldb_private;
20+
using namespace lldb_private::clang_utils;
21+
22+
TEST(ValueTest, GetValueAsData) {
23+
SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX> subsystems;
24+
auto holder = std::make_unique<clang_utils::TypeSystemClangHolder>("test");
25+
auto *clang = holder->GetAST();
26+
27+
Value v(Scalar(42));
28+
DataExtractor extractor;
29+
30+
// no compiler type
31+
Status status = v.GetValueAsData(nullptr, extractor, nullptr);
32+
ASSERT_TRUE(status.Fail());
33+
34+
// with compiler type
35+
v.SetCompilerType(clang->GetBasicType(lldb::BasicType::eBasicTypeChar));
36+
37+
status = v.GetValueAsData(nullptr, extractor, nullptr);
38+
ASSERT_TRUE(status.Success());
39+
}

0 commit comments

Comments
 (0)