* [PATCH] Fix casting in-memory values of primitive types to const reference
[not found] <20240320160844.106-1-ssbssa.ref@yahoo.de>
@ 2024-03-20 16:08 ` Hannes Domani
2024-03-20 17:05 ` Tom Tromey
0 siblings, 1 reply; 4+ messages in thread
From: Hannes Domani @ 2024-03-20 16:08 UTC (permalink / raw)
To: gdb-patches
It's currently not possible to cast an in-memory value of a primitive
type to const reference:
```
(gdb) p Q.id
$1 = 42
(gdb) p (int&)Q.id
$2 = (int &) @0x22fd0c: 42
(gdb) p (const int&)Q.id
Attempt to take address of value not located in memory.
```
And if in a function call an argument needs the same kind of casting,
it also doesn't work:
```
(gdb) l f3
39 int f3(const int &i)
40 {
41 return i;
42 }
(gdb) p f3(Q.id)
Attempt to take address of value not located in memory.
```
It's because when the constness of the type changes in a call to
value_cast, a new not_lval value is allocated, which doesn't exist
in the target memory.
Fixed by ignoring const/volatile/restrict qualifications in
value_cast when comparing cast type to original type, so the new
value will point to the same location as the original value:
```
(gdb) p (int&)i
$2 = (int &) @0x39f72c: 1
(gdb) p (const int&)i
$3 = (const int &) @0x39f72c: 1
(gdb) p f3(Q.id)
$4 = 42
```
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19423
---
gdb/testsuite/gdb.cp/casts.exp | 3 +++
gdb/testsuite/gdb.cp/ref-params.cc | 6 ++++++
gdb/testsuite/gdb.cp/ref-params.exp | 1 +
gdb/valops.c | 3 ++-
4 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/gdb/testsuite/gdb.cp/casts.exp b/gdb/testsuite/gdb.cp/casts.exp
index ca82ab084b9..9f7638c8aee 100644
--- a/gdb/testsuite/gdb.cp/casts.exp
+++ b/gdb/testsuite/gdb.cp/casts.exp
@@ -180,6 +180,9 @@ gdb_test "print (unsigned long long) (LeftRight *) (Right *) &gd == gd_value" \
gdb_test "print (unsigned long long) (LeftRight *) (Right *) r_value == gd_value" \
" = true"
+gdb_test "print (const int &) gd.left" \
+ " = \\(const int \\&\\) @$nonzero_hex: 23"
+
gdb_test "print reinterpret_cast<LeftRight *>(l) == lr_l" " = true"
gdb_test "print reinterpret_cast<LeftRight *>(r) == lr_r" " = true"
gdb_test "print reinterpret_cast<Left *>(lr) == l_lr" " = true"
diff --git a/gdb/testsuite/gdb.cp/ref-params.cc b/gdb/testsuite/gdb.cp/ref-params.cc
index f038d71fe10..3ef28688607 100644
--- a/gdb/testsuite/gdb.cp/ref-params.cc
+++ b/gdb/testsuite/gdb.cp/ref-params.cc
@@ -36,6 +36,11 @@ int f2(Child& C)
return f1(C); /* Set breakpoint marker2 here. */
}
+int f3(const int &i)
+{
+ return i;
+}
+
struct OtherParent {
OtherParent (int other_id0) : other_id(other_id0) { }
int other_id;
@@ -64,6 +69,7 @@ int main(void)
f2(Q);
f2(QR);
+ f3(Q.id);
MultiChild MQ(53);
MultiChild& MQR = MQ;
diff --git a/gdb/testsuite/gdb.cp/ref-params.exp b/gdb/testsuite/gdb.cp/ref-params.exp
index 03bb8e62496..e5c28e6e2ad 100644
--- a/gdb/testsuite/gdb.cp/ref-params.exp
+++ b/gdb/testsuite/gdb.cp/ref-params.exp
@@ -62,3 +62,4 @@ gdb_test "print mf2(MQ)" ".* = 106"
gdb_test "print f1(MQR)" ".* = 53"
gdb_test "print mf1(MQR)" ".* = 106"
gdb_test "print mf2(MQR)" ".* = 106"
+gdb_test "print f3(Q.id)" ".* = 42"
diff --git a/gdb/valops.c b/gdb/valops.c
index 0a4e57672ce..14c12142112 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -411,7 +411,8 @@ value_cast (struct type *type, struct value *arg2)
In this case we want to preserve the LVAL of ARG2 as this allows the
resulting value to be used in more places. We do this by calling
VALUE_COPY if appropriate. */
- if (types_deeply_equal (arg2->type (), type))
+ if (types_deeply_equal (make_unqualified_type (arg2->type ()),
+ make_unqualified_type (type)))
{
/* If the types are exactly equal then we can avoid creating a new
value completely. */
--
2.35.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Fix casting in-memory values of primitive types to const reference
2024-03-20 16:08 ` [PATCH] Fix casting in-memory values of primitive types to const reference Hannes Domani
@ 2024-03-20 17:05 ` Tom Tromey
2024-03-20 17:25 ` Hannes Domani
0 siblings, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2024-03-20 17:05 UTC (permalink / raw)
To: Hannes Domani; +Cc: gdb-patches
>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
Hannes> It's currently not possible to cast an in-memory value of a primitive
Hannes> type to const reference:
Thank you for the patch.
I think it is ok. However I suspect there may be other bugs here.
Approved-By: Tom Tromey <tom@tromey.com>
Hannes> Fixed by ignoring const/volatile/restrict qualifications in
Hannes> value_cast when comparing cast type to original type, so the new
Hannes> value will point to the same location as the original value:
Hannes> - if (types_deeply_equal (arg2->type (), type))
Hannes> + if (types_deeply_equal (make_unqualified_type (arg2->type ()),
Hannes> + make_unqualified_type (type)))
This code seems slightly weird in that it isn't calling check_typedef.
So, I wonder what happens if typedefs are involved, for example if you
did:
typedef const int ci;
(gdb) print (ci&) Q.id
thanks,
Tom
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Fix casting in-memory values of primitive types to const reference
2024-03-20 17:05 ` Tom Tromey
@ 2024-03-20 17:25 ` Hannes Domani
2024-03-20 17:33 ` Tom Tromey
0 siblings, 1 reply; 4+ messages in thread
From: Hannes Domani @ 2024-03-20 17:25 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
Am Mittwoch, 20. März 2024 um 18:05:48 MEZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:
> >>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
>
> Hannes> It's currently not possible to cast an in-memory value of a primitive
> Hannes> type to const reference:
>
> Thank you for the patch.
>
> I think it is ok. However I suspect there may be other bugs here.
>
> Approved-By: Tom Tromey <tom@tromey.com>
Pushed, thanks.
> Hannes> Fixed by ignoring const/volatile/restrict qualifications in
> Hannes> value_cast when comparing cast type to original type, so the new
> Hannes> value will point to the same location as the original value:
>
> Hannes> - if (types_deeply_equal (arg2->type (), type))
> Hannes> + if (types_deeply_equal (make_unqualified_type (arg2->type ()),
> Hannes> + make_unqualified_type (type)))
>
> This code seems slightly weird in that it isn't calling check_typedef.
> So, I wonder what happens if typedefs are involved, for example if you
> did:
>
> typedef const int ci;
>
> (gdb) print (ci&) Q.id
This happens:
(gdb) p (ci&) Q.id
$1 = (const int &) @0x3cfb8c: 42
Because check_typedef is called when the reference is removed:
```
/* Check if we are casting struct reference to struct reference. */
if (TYPE_IS_REFERENCE (check_typedef (type)))
{
/* We dereference type; then we recurse and finally
we generate value of the given reference. Nothing wrong with
that. */
struct type *t1 = check_typedef (type);
struct type *dereftype = check_typedef (t1->target_type ());
struct value *val = value_cast (dereftype, arg2);
return value_ref (val, t1->code ());
}
```
Hannes
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Fix casting in-memory values of primitive types to const reference
2024-03-20 17:25 ` Hannes Domani
@ 2024-03-20 17:33 ` Tom Tromey
0 siblings, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2024-03-20 17:33 UTC (permalink / raw)
To: Hannes Domani; +Cc: Tom Tromey, gdb-patches
>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
Hannes> This happens:
Hannes> (gdb) p (ci&) Q.id
Hannes> $1 = (const int &) @0x3cfb8c: 42
Hannes> Because check_typedef is called when the reference is removed:
Thanks for looking.
Tom
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-03-20 17:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20240320160844.106-1-ssbssa.ref@yahoo.de>
2024-03-20 16:08 ` [PATCH] Fix casting in-memory values of primitive types to const reference Hannes Domani
2024-03-20 17:05 ` Tom Tromey
2024-03-20 17:25 ` Hannes Domani
2024-03-20 17:33 ` Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).