Skip to content

Commit 3707c54

Browse files
authored
Make ValueObject::Cast work for casts from smaller to larger structs in the cases where this currently can work. (#84588)
The ValueObjectConstResult classes that back expression result variables play a complicated game with where the data for their values is stored. They try to make it appear as though they are still tied to the memory in the target into which their value was written when the expression is run, but they also keep a copy in the Host which they use after the value is made (expression results are "history values" so that's how we make sure they have "the value at the time of the expression".) However, that means that if you ask them to cast themselves to a value bigger than their original size, they don't have a way to get more memory for that purpose. The same thing is true of ValueObjects backed by DataExtractors, the data extractors don't know how to get more data than they were made with in general. The only place where we actually ask ValueObjects to sample outside their captured bounds is when you do ValueObject::Cast from one structure type to a bigger structure type. In https://reviews.llvm.org/D153657 I handled this by just disallowing casts from one structure value to a larger one. My reasoning at the time was that the use case for this was to support discriminator based C inheritance schemes, and you can't directly cast values in C, only pointers, so this was not a natural way to handle those types. It seemed logical that since you would have had to start with pointers in the implementation, that's how you would write your lldb introspection code as well. Famous last words... Turns out there are some heavy users of the SB API's who were relying on this working, and this is a behavior change, so this patch makes this work in the cases where it used to work before, while still disallowing the cases we don't know how to support. Note that if you had done this Cast operation before with either expression results or value objects from data extractors, lldb would not have returned the correct results, so the cases this patch outlaws are ones that actually produce invalid results. So nobody should be using Cast in these cases, or if they were, this patch will point out the bug they hadn't yet noticed.
1 parent 0f0f0ff commit 3707c54

File tree

3 files changed

+89
-14
lines changed

3 files changed

+89
-14
lines changed

lldb/source/Core/ValueObject.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -2744,18 +2744,30 @@ ValueObjectSP ValueObject::DoCast(const CompilerType &compiler_type) {
27442744

27452745
ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
27462746
// Only allow casts if the original type is equal or larger than the cast
2747-
// type. We don't know how to fetch more data for all the ConstResult types,
2748-
// so we can't guarantee this will work:
2747+
// type, unless we know this is a load address. Getting the size wrong for
2748+
// a host side storage could leak lldb memory, so we absolutely want to
2749+
// prevent that. We may not always get the right value, for instance if we
2750+
// have an expression result value that's copied into a storage location in
2751+
// the target may not have copied enough memory. I'm not trying to fix that
2752+
// here, I'm just making Cast from a smaller to a larger possible in all the
2753+
// cases where that doesn't risk making a Value out of random lldb memory.
2754+
// You have to check the ValueObject's Value for the address types, since
2755+
// ValueObjects that use live addresses will tell you they fetch data from the
2756+
// live address, but once they are made, they actually don't.
2757+
// FIXME: Can we make ValueObject's with a live address fetch "more data" from
2758+
// the live address if it is still valid?
2759+
27492760
Status error;
27502761
CompilerType my_type = GetCompilerType();
27512762

27522763
ExecutionContextScope *exe_scope
27532764
= ExecutionContext(GetExecutionContextRef())
27542765
.GetBestExecutionContextScope();
27552766
if (compiler_type.GetByteSize(exe_scope)
2756-
<= GetCompilerType().GetByteSize(exe_scope)) {
2767+
<= GetCompilerType().GetByteSize(exe_scope)
2768+
|| m_value.GetValueType() == Value::ValueType::LoadAddress)
27572769
return DoCast(compiler_type);
2758-
}
2770+
27592771
error.SetErrorString("Can only cast to a type that is equal to or smaller "
27602772
"than the orignal type.");
27612773

lldb/test/API/python_api/value/TestValueAPI.py

+60-8
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,66 @@ def test(self):
148148

149149
# Test some other cases of the Cast API. We allow casts from one struct type
150150
# to another, which is a little weird, but we don't support casting from a
151-
# smaller type to a larger as we often wouldn't know how to get the extra data:
152-
val_f = target.EvaluateExpression("f")
153-
bad_cast = val_s.Cast(val_f.GetType())
154-
self.assertFailure(
155-
bad_cast.GetError(),
156-
"Can only cast to a type that is equal to or smaller than the orignal type.",
157-
)
158-
weird_cast = val_f.Cast(val_s.GetType())
151+
# smaller type to a larger when the underlying data is not in the inferior,
152+
# since then we have no way to fetch the out-of-bounds values.
153+
# For an expression that references a variable, or a FindVariable result,
154+
# or an SBValue made from an address and a type, we can get back to the target,
155+
# so those will work. Make sure they do and get the right extra values as well.
156+
157+
# We're casting everything to the type of "f", so get that first:
158+
f_var = frame0.FindVariable("f")
159+
self.assertSuccess(f_var.error, "Got f")
160+
bigger_type = f_var.GetType()
161+
162+
# First try a value that we got from FindVariable
163+
container = frame0.FindVariable("my_container")
164+
self.assertSuccess(container.error, "Found my_container")
165+
fv_small = container.GetValueForExpressionPath(".data.small")
166+
self.assertSuccess(fv_small.error, "Found small in my_container")
167+
fv_cast = fv_small.Cast(bigger_type)
168+
self.assertSuccess(fv_cast.error, "Can cast up from FindVariable")
169+
child_checks = [
170+
ValueCheck(name="a", value="33", type="int"),
171+
ValueCheck(name="b", value="44", type="int"),
172+
ValueCheck(name="c", value="55", type="int"),
173+
]
174+
cast_check = ValueCheck(type=bigger_type.name, children=child_checks)
175+
176+
# Now try one we made with expr. This one should fail, because expr
177+
# stores the "canonical value" in host memory, and doesn't know how
178+
# to augment that from the live address.
179+
expr_cont = frame0.EvaluateExpression("my_container")
180+
self.assertSuccess(expr_cont.error, "Got my_container by expr")
181+
expr_small = expr_cont.GetValueForExpressionPath(".data.small")
182+
self.assertSuccess(expr_small.error, "Got small by expr")
183+
expr_cast = expr_small.Cast(bigger_type)
184+
self.assertFailure(expr_cast.error, msg="Cannot cast expr result")
185+
186+
# Now try one we made with CreateValueFromAddress. That will succeed
187+
# because this directly tracks the inferior memory.
188+
small_addr = fv_small.addr
189+
self.assertTrue(small_addr.IsValid())
190+
small_type = fv_small.GetType()
191+
vfa_small = target.CreateValueFromAddress(
192+
"small_from_addr", small_addr, small_type
193+
)
194+
self.assertSuccess(vfa_small.error, "Made small from address")
195+
vfa_cast = vfa_small.Cast(bigger_type)
196+
self.assertSuccess(vfa_cast.error, "Made a cast from vfa_small")
197+
cast_check.check_value(self, vfa_cast, "Cast of ValueFromAddress succeeds")
198+
199+
# Next try ValueObject created from data. They should fail as there's no
200+
# way to grow the data:
201+
data_small = target.CreateValueFromData(
202+
"small_from_data", fv_small.data, fv_small.type
203+
)
204+
self.assertSuccess(data_small.error, "Made a valid object from data")
205+
data_cast = data_small.Cast(bigger_type)
206+
self.assertFailure(data_cast.error, msg="Cannot cast data backed SBValue")
207+
208+
# Now check casting from a larger type to a smaller, we can always do this,
209+
# so just test one case:
210+
weird_cast = f_var.Cast(val_s.GetType())
159211
self.assertSuccess(weird_cast.GetError(), "Can cast from a larger to a smaller")
160212
self.assertEqual(
161213
weird_cast.GetChildMemberWithName("a").GetValueAsSigned(0),

lldb/test/API/python_api/value/main.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const char *weekdays[5] = { "Monday",
2222
const char **g_table[2] = { days_of_week, weekdays };
2323

2424
typedef int MyInt;
25-
25+
2626
struct MyStruct
2727
{
2828
int a;
@@ -36,15 +36,26 @@ struct MyBiggerStruct
3636
int c;
3737
};
3838

39+
struct Container
40+
{
41+
int discriminator;
42+
union Data {
43+
struct MyStruct small;
44+
struct MyBiggerStruct big;
45+
} data;
46+
};
47+
3948
int main (int argc, char const *argv[])
4049
{
4150
uint32_t uinthex = 0xE0A35F10;
4251
int32_t sinthex = 0xE0A35F10;
4352

4453
int i;
4554
MyInt a = 12345;
46-
struct MyStruct s = { 11, 22 };
55+
struct MyStruct s = {11, 22};
4756
struct MyBiggerStruct f = { 33, 44, 55 };
57+
struct Container my_container;
58+
my_container.data.big = f;
4859
int *my_int_ptr = &g_my_int;
4960
printf("my_int_ptr points to location %p\n", my_int_ptr);
5061
int *fixed_int_ptr = (int*)(void*)0xAA;

0 commit comments

Comments
 (0)