Skip to content

Commit 97ee4f7

Browse files
committed
Make ValueObject::Cast work for casts from smaller to larger structs in the cases where this currently can work. (llvm#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 b7ab938 commit 97ee4f7

File tree

3 files changed

+98
-18
lines changed

3 files changed

+98
-18
lines changed

lldb/source/Core/ValueObject.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,18 +2851,30 @@ ValueObjectSP ValueObject::DoCast(const CompilerType &compiler_type) {
28512851

28522852
ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
28532853
// Only allow casts if the original type is equal or larger than the cast
2854-
// type. We don't know how to fetch more data for all the ConstResult types,
2855-
// so we can't guarantee this will work:
2854+
// type, unless we know this is a load address. Getting the size wrong for
2855+
// a host side storage could leak lldb memory, so we absolutely want to
2856+
// prevent that. We may not always get the right value, for instance if we
2857+
// have an expression result value that's copied into a storage location in
2858+
// the target may not have copied enough memory. I'm not trying to fix that
2859+
// here, I'm just making Cast from a smaller to a larger possible in all the
2860+
// cases where that doesn't risk making a Value out of random lldb memory.
2861+
// You have to check the ValueObject's Value for the address types, since
2862+
// ValueObjects that use live addresses will tell you they fetch data from the
2863+
// live address, but once they are made, they actually don't.
2864+
// FIXME: Can we make ValueObject's with a live address fetch "more data" from
2865+
// the live address if it is still valid?
2866+
28562867
Status error;
28572868
CompilerType my_type = GetCompilerType();
28582869

28592870
ExecutionContextScope *exe_scope
28602871
= ExecutionContext(GetExecutionContextRef())
28612872
.GetBestExecutionContextScope();
2862-
if (compiler_type.GetByteSize(exe_scope)
2863-
<= GetCompilerType().GetByteSize(exe_scope)) {
2873+
if (compiler_type.GetByteSize(exe_scope)
2874+
<= GetCompilerType().GetByteSize(exe_scope)
2875+
|| m_value.GetValueType() == Value::ValueType::LoadAddress)
28642876
return DoCast(compiler_type);
2865-
}
2877+
28662878
error.SetErrorString("Can only cast to a type that is equal to or smaller "
28672879
"than the orignal type.");
28682880

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

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,73 @@ 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(bad_cast.GetError(),
155-
"Can only cast to a type that is equal to or smaller than the orignal type.")
156-
weird_cast = val_f.Cast(val_s.GetType())
157-
self.assertSuccess(weird_cast.GetError(),
158-
"Can cast from a larger to a smaller")
159-
self.assertEqual(weird_cast.GetChildMemberWithName("a").GetValueAsSigned(0), 33,
160-
"Got the right value")
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())
211+
self.assertSuccess(weird_cast.GetError(), "Can cast from a larger to a smaller")
212+
self.assertEqual(
213+
weird_cast.GetChildMemberWithName("a").GetValueAsSigned(0),
214+
33,
215+
"Got the right value",
216+
)
217+
>>>>>>> 3707c540d23a (Make ValueObject::Cast work for casts from smaller to larger structs in the cases where this currently can work. (#84588))
161218

162219
# Check that lldb.value implements truth testing.
163220
self.assertFalse(lldb.value(frame0.FindVariable("bogus")))

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

Lines changed: 14 additions & 3 deletions
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 };
47-
struct MyBiggerStruct f = { 33, 44, 55 };
55+
struct MyStruct s = {11, 22};
56+
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
const char **str_ptr = days_of_week;

0 commit comments

Comments
 (0)