[gdb/python] Make gdb.UnwindInfo.add_saved_register more robust
Checks
Context |
Check |
Description |
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 |
success
|
Testing passed
|
linaro-tcwg-bot/tcwg_gdb_build--master-arm |
success
|
Testing passed
|
linaro-tcwg-bot/tcwg_gdb_check--master-arm |
success
|
Testing passed
|
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 |
success
|
Testing passed
|
Commit Message
On arm-linux, until commit bbb12eb9c84 ("gdb/arm: Remove tpidruro register
from non-FreeBSD target descriptions") I ran into:
...
FAIL: gdb.base/inline-frame-cycle-unwind.exp: cycle at level 5: \
backtrace when the unwind is broken at frame 5
...
What happens is the following:
- the TestUnwinder from inline-frame-cycle-unwind.py calls
gdb.UnwindInfo.add_saved_register with reg == tpidruro and value
"<unavailable>",
- pyuw_sniffer calls value->contents ().data () to access the value of the
register, which throws an UNAVAILABLE_ERROR,
- this causes the TestUnwinder unwinder to fail, after which another unwinder
succeeds and returns the correct frame, and
- the test-case fails because it's counting on the TestUnwinder to succeed and
return an incorrect frame.
Fix this by checking for !value::entirely_available as well as
valued::optimized_out in unwind_infopy_add_saved_register.
Tested on x86_64-linux and arm-linux.
PR python/31437
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31437
---
gdb/python/py-unwind.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
base-commit: a6a3b67fa9052bba81ed91a38569c11ecb95baf1
Comments
On 3/2/24 13:36, Tom de Vries wrote:
> On arm-linux, until commit bbb12eb9c84 ("gdb/arm: Remove tpidruro register
> from non-FreeBSD target descriptions") I ran into:
> ...
> FAIL: gdb.base/inline-frame-cycle-unwind.exp: cycle at level 5: \
> backtrace when the unwind is broken at frame 5
> ...
>
> What happens is the following:
> - the TestUnwinder from inline-frame-cycle-unwind.py calls
> gdb.UnwindInfo.add_saved_register with reg == tpidruro and value
> "<unavailable>",
> - pyuw_sniffer calls value->contents ().data () to access the value of the
> register, which throws an UNAVAILABLE_ERROR,
> - this causes the TestUnwinder unwinder to fail, after which another unwinder
> succeeds and returns the correct frame, and
> - the test-case fails because it's counting on the TestUnwinder to succeed and
> return an incorrect frame.
>
> Fix this by checking for !value::entirely_available as well as
> valued::optimized_out in unwind_infopy_add_saved_register.
>
> Tested on x86_64-linux and arm-linux.
>
Ping.
Thanks,
- Tom
> PR python/31437
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31437
> ---
> gdb/python/py-unwind.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
> index 56f925bc57f..1c1289f7e7d 100644
> --- a/gdb/python/py-unwind.c
> +++ b/gdb/python/py-unwind.c
> @@ -362,6 +362,18 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args, PyObject *kw)
> return nullptr;
> }
>
> + if (value->optimized_out () || !value->entirely_available ())
> + {
> + /* If we allow this value to be registered here, pyuw_sniffer is going
> + to run into an exception when trying to access its contents.
> + Throwing an exception here just puts a burden on the user to
> + implement the same checks on the user side. We could return False
> + here and True otherwise, but again that might require changes in user
> + code. So, handle this with minimal impact for the user, while
> + improving robustness: silently ignore the register/value pair. */
> + Py_RETURN_NONE;
> + }
> +
> gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value);
> bool found = false;
> for (saved_reg ® : *unwind_info->saved_regs)
>
> base-commit: a6a3b67fa9052bba81ed91a38569c11ecb95baf1
On 3/19/24 10:34, Tom de Vries wrote:
> On 3/2/24 13:36, Tom de Vries wrote:
>> On arm-linux, until commit bbb12eb9c84 ("gdb/arm: Remove tpidruro
>> register
>> from non-FreeBSD target descriptions") I ran into:
>> ...
>> FAIL: gdb.base/inline-frame-cycle-unwind.exp: cycle at level 5: \
>> backtrace when the unwind is broken at frame 5
>> ...
>>
>> What happens is the following:
>> - the TestUnwinder from inline-frame-cycle-unwind.py calls
>> gdb.UnwindInfo.add_saved_register with reg == tpidruro and value
>> "<unavailable>",
>> - pyuw_sniffer calls value->contents ().data () to access the value of
>> the
>> register, which throws an UNAVAILABLE_ERROR,
>> - this causes the TestUnwinder unwinder to fail, after which another
>> unwinder
>> succeeds and returns the correct frame, and
>> - the test-case fails because it's counting on the TestUnwinder to
>> succeed and
>> return an incorrect frame.
>>
>> Fix this by checking for !value::entirely_available as well as
>> valued::optimized_out in unwind_infopy_add_saved_register.
>>
>> Tested on x86_64-linux and arm-linux.
>>
>
> Ping.
>
Ping once more.
Also found that this fixes the gdb.base/inline-frame-cycle-unwind.exp
failure on ppc64le reported in PR30548.
Thanks,
- Tom
>> PR python/31437
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31437
>> ---
>> gdb/python/py-unwind.c | 12 ++++++++++++
>> 1 file changed, 12 insertions(+)
>>
>> diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
>> index 56f925bc57f..1c1289f7e7d 100644
>> --- a/gdb/python/py-unwind.c
>> +++ b/gdb/python/py-unwind.c
>> @@ -362,6 +362,18 @@ unwind_infopy_add_saved_register (PyObject *self,
>> PyObject *args, PyObject *kw)
>> return nullptr;
>> }
>> + if (value->optimized_out () || !value->entirely_available ())
>> + {
>> + /* If we allow this value to be registered here, pyuw_sniffer
>> is going
>> + to run into an exception when trying to access its contents.
>> + Throwing an exception here just puts a burden on the user to
>> + implement the same checks on the user side. We could return False
>> + here and True otherwise, but again that might require changes in
>> user
>> + code. So, handle this with minimal impact for the user, while
>> + improving robustness: silently ignore the register/value pair. */
>> + Py_RETURN_NONE;
>> + }
>> +
>> gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value);
>> bool found = false;
>> for (saved_reg ® : *unwind_info->saved_regs)
>>
>> base-commit: a6a3b67fa9052bba81ed91a38569c11ecb95baf1
>
Tom de Vries <tdevries@suse.de> writes:
> On arm-linux, until commit bbb12eb9c84 ("gdb/arm: Remove tpidruro register
> from non-FreeBSD target descriptions") I ran into:
> ...
> FAIL: gdb.base/inline-frame-cycle-unwind.exp: cycle at level 5: \
> backtrace when the unwind is broken at frame 5
> ...
>
> What happens is the following:
> - the TestUnwinder from inline-frame-cycle-unwind.py calls
> gdb.UnwindInfo.add_saved_register with reg == tpidruro and value
> "<unavailable>",
> - pyuw_sniffer calls value->contents ().data () to access the value of the
> register, which throws an UNAVAILABLE_ERROR,
> - this causes the TestUnwinder unwinder to fail, after which another unwinder
> succeeds and returns the correct frame, and
> - the test-case fails because it's counting on the TestUnwinder to succeed and
> return an incorrect frame.
>
> Fix this by checking for !value::entirely_available as well as
> valued::optimized_out in unwind_infopy_add_saved_register.
>
> Tested on x86_64-linux and arm-linux.
>
> PR python/31437
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31437
> ---
> gdb/python/py-unwind.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
> index 56f925bc57f..1c1289f7e7d 100644
> --- a/gdb/python/py-unwind.c
> +++ b/gdb/python/py-unwind.c
> @@ -362,6 +362,18 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args, PyObject *kw)
> return nullptr;
> }
>
> + if (value->optimized_out () || !value->entirely_available ())
> + {
> + /* If we allow this value to be registered here, pyuw_sniffer is going
> + to run into an exception when trying to access its contents.
> + Throwing an exception here just puts a burden on the user to
> + implement the same checks on the user side. We could return False
> + here and True otherwise, but again that might require changes in user
> + code. So, handle this with minimal impact for the user, while
> + improving robustness: silently ignore the register/value pair. */
> + Py_RETURN_NONE;
> + }
Thanks for fixing this.
I agree with this approach. Registers that aren't saved will be
reported as optimized out anyway, which seems good enough -- though
having them report as unavailable might be better, but that's not a
problem for this patch.
I think returning True/False would be OK, I don't think users are
likely to be depending on this function returning None. But that said,
I don't see any immediate need to change the return type; if a user
really wants to know then they can check for unavailable/optimized-out
values prior to saving the register value.
Approved-By: Andrew Burgess <aburgess@redhat.com>
Thanks,
Andrew
> +
> gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value);
> bool found = false;
> for (saved_reg ® : *unwind_info->saved_regs)
>
> base-commit: a6a3b67fa9052bba81ed91a38569c11ecb95baf1
> --
> 2.35.3
>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
Tom> @@ -362,6 +362,18 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args, PyObject *kw)
Tom> return nullptr;
Tom> }
Tom> + if (value->optimized_out () || !value->entirely_available ())
Tom> + {
Both of these seem to have cases where they can throw an exception.
In the Python layer, gdb code that can throw normally has to be wrapped
in a try/catch, as throwing across the Python boundary will cause crashes.
I don't know whether exceptions can actually be provoked here or not.
Tom
On 5/8/24 17:52, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
>
> Tom> @@ -362,6 +362,18 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args, PyObject *kw)
> Tom> return nullptr;
> Tom> }
>
> Tom> + if (value->optimized_out () || !value->entirely_available ())
> Tom> + {
>
> Both of these seem to have cases where they can throw an exception.
> In the Python layer, gdb code that can throw normally has to be wrapped
> in a try/catch, as throwing across the Python boundary will cause crashes.
>
> I don't know whether exceptions can actually be provoked here or not.
Hi Tom,
thanks for the review.
Submitted fix here (
https://sourceware.org/pipermail/gdb-patches/2024-May/209032.html ).
Thanks,
- Tom
@@ -362,6 +362,18 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args, PyObject *kw)
return nullptr;
}
+ if (value->optimized_out () || !value->entirely_available ())
+ {
+ /* If we allow this value to be registered here, pyuw_sniffer is going
+ to run into an exception when trying to access its contents.
+ Throwing an exception here just puts a burden on the user to
+ implement the same checks on the user side. We could return False
+ here and True otherwise, but again that might require changes in user
+ code. So, handle this with minimal impact for the user, while
+ improving robustness: silently ignore the register/value pair. */
+ Py_RETURN_NONE;
+ }
+
gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value);
bool found = false;
for (saved_reg ® : *unwind_info->saved_regs)