Skip to content

Make ValueObject::Cast work for casts from smaller to larger structs in the cases where this currently can work. #84588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lldb/source/Core/ValueObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2744,18 +2744,30 @@ ValueObjectSP ValueObject::DoCast(const CompilerType &compiler_type) {

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

Status error;
CompilerType my_type = GetCompilerType();

ExecutionContextScope *exe_scope
= ExecutionContext(GetExecutionContextRef())
.GetBestExecutionContextScope();
if (compiler_type.GetByteSize(exe_scope)
<= GetCompilerType().GetByteSize(exe_scope)) {
<= GetCompilerType().GetByteSize(exe_scope)
|| m_value.GetValueType() == Value::ValueType::LoadAddress)
return DoCast(compiler_type);
}

error.SetErrorString("Can only cast to a type that is equal to or smaller "
"than the orignal type.");

Expand Down
68 changes: 60 additions & 8 deletions lldb/test/API/python_api/value/TestValueAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,66 @@ def test(self):

# Test some other cases of the Cast API. We allow casts from one struct type
# to another, which is a little weird, but we don't support casting from a
# smaller type to a larger as we often wouldn't know how to get the extra data:
val_f = target.EvaluateExpression("f")
bad_cast = val_s.Cast(val_f.GetType())
self.assertFailure(
bad_cast.GetError(),
"Can only cast to a type that is equal to or smaller than the orignal type.",
)
weird_cast = val_f.Cast(val_s.GetType())
# smaller type to a larger when the underlying data is not in the inferior,
# since then we have no way to fetch the out-of-bounds values.
# For an expression that references a variable, or a FindVariable result,
# or an SBValue made from an address and a type, we can get back to the target,
# so those will work. Make sure they do and get the right extra values as well.

# We're casting everything to the type of "f", so get that first:
f_var = frame0.FindVariable("f")
self.assertSuccess(f_var.error, "Got f")
bigger_type = f_var.GetType()

# First try a value that we got from FindVariable
container = frame0.FindVariable("my_container")
self.assertSuccess(container.error, "Found my_container")
fv_small = container.GetValueForExpressionPath(".data.small")
self.assertSuccess(fv_small.error, "Found small in my_container")
fv_cast = fv_small.Cast(bigger_type)
self.assertSuccess(fv_cast.error, "Can cast up from FindVariable")
child_checks = [
ValueCheck(name="a", value="33", type="int"),
ValueCheck(name="b", value="44", type="int"),
ValueCheck(name="c", value="55", type="int"),
]
cast_check = ValueCheck(type=bigger_type.name, children=child_checks)

# Now try one we made with expr. This one should fail, because expr
# stores the "canonical value" in host memory, and doesn't know how
# to augment that from the live address.
expr_cont = frame0.EvaluateExpression("my_container")
self.assertSuccess(expr_cont.error, "Got my_container by expr")
expr_small = expr_cont.GetValueForExpressionPath(".data.small")
self.assertSuccess(expr_small.error, "Got small by expr")
expr_cast = expr_small.Cast(bigger_type)
self.assertFailure(expr_cast.error, msg="Cannot cast expr result")

# Now try one we made with CreateValueFromAddress. That will succeed
# because this directly tracks the inferior memory.
small_addr = fv_small.addr
self.assertTrue(small_addr.IsValid())
small_type = fv_small.GetType()
vfa_small = target.CreateValueFromAddress(
"small_from_addr", small_addr, small_type
)
self.assertSuccess(vfa_small.error, "Made small from address")
vfa_cast = vfa_small.Cast(bigger_type)
self.assertSuccess(vfa_cast.error, "Made a cast from vfa_small")
cast_check.check_value(self, vfa_cast, "Cast of ValueFromAddress succeeds")

# Next try ValueObject created from data. They should fail as there's no
# way to grow the data:
data_small = target.CreateValueFromData(
"small_from_data", fv_small.data, fv_small.type
)
self.assertSuccess(data_small.error, "Made a valid object from data")
data_cast = data_small.Cast(bigger_type)
self.assertFailure(data_cast.error, msg="Cannot cast data backed SBValue")

# Now check casting from a larger type to a smaller, we can always do this,
# so just test one case:
weird_cast = f_var.Cast(val_s.GetType())
self.assertSuccess(weird_cast.GetError(), "Can cast from a larger to a smaller")
self.assertEqual(
weird_cast.GetChildMemberWithName("a").GetValueAsSigned(0),
Expand Down
15 changes: 13 additions & 2 deletions lldb/test/API/python_api/value/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const char *weekdays[5] = { "Monday",
const char **g_table[2] = { days_of_week, weekdays };

typedef int MyInt;

struct MyStruct
{
int a;
Expand All @@ -36,15 +36,26 @@ struct MyBiggerStruct
int c;
};

struct Container
{
int discriminator;
union Data {
struct MyStruct small;
struct MyBiggerStruct big;
} data;
};

int main (int argc, char const *argv[])
{
uint32_t uinthex = 0xE0A35F10;
int32_t sinthex = 0xE0A35F10;

int i;
MyInt a = 12345;
struct MyStruct s = { 11, 22 };
struct MyStruct s = {11, 22};
struct MyBiggerStruct f = { 33, 44, 55 };
struct Container my_container;
my_container.data.big = f;
int *my_int_ptr = &g_my_int;
printf("my_int_ptr points to location %p\n", my_int_ptr);
int *fixed_int_ptr = (int*)(void*)0xAA;
Expand Down