public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection
@ 2024-05-02 12:55 vries at gcc dot gnu.org
  2024-05-02 12:56 ` [Bug exp/31693] " vries at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: vries at gcc dot gnu.org @ 2024-05-02 12:55 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

            Bug ID: 31693
           Summary: [gdb/exp] cast not handled correctly by indirection
           Product: gdb
           Version: HEAD
            Status: NEW
          Severity: normal
          Priority: P2
         Component: exp
          Assignee: unassigned at sourceware dot org
          Reporter: vries at gcc dot gnu.org
  Target Milestone: ---

I was reviewing this patch (
https://sourceware.org/pipermail/gdb-patches/2024-May/208737.html ) and came
across this description:
...
+           # On Linux, using -g3, which causes macro information to
+           # be included in the debuginfo, errno might be defined as
+           # follows:
+           #
+           #   #define errno (*__errno_location ())
+           #
+           # So, when we do "ptype errno", due to macro expansion,
+           # this ends up being "ptype (*__errno_location ())".  So
+           # the call to __errno_location (or something similar on
+           # other OSes) is the call mentioned in the error message.
+           #
+           # For the test "print (int) errno", we're casting the
+           # result of the expression, which includes both the call
+           # along with a dereferencing operation.
+           #
+           # This will sometimes produce the right answer, but it's
+           # also just as likely to fail.  E.g.  on x86_64, if the
+           # address being returned as a 32-bit int is the same as
+           # that which would have been returned as a 64-bit pointer,
+           # then the test might pass.  Otherwise, it will almost
+           # certainly fail, which is why we XFAIL it here.  But do
+           # expect to see the occasional XPASS for this case.
...

I tried to reproduce this (in a fedora rawhide container with the
check-errno-macros exec) and got:
...
(gdb) p errno
'__errno_location' has unknown return type; cast the call to its declared
return type
(gdb) ptype errno
'__errno_location' has unknown return type; cast the call to its declared
return type
(gdb) p __errno_location
$9 = {<text variable, no debug info>} 0x7ffff7d10540 <__errno_location>
(gdb) ptype __errno_location
type = <unknown return type> ()
(gdb) p __errno_location ()
'__errno_location' has unknown return type; cast the call to its declared
return type
(gdb) p *__errno_location ()
'__errno_location' has unknown return type; cast the call to its declared
return type
(gdb) p (*__errno_location ())
'__errno_location' has unknown return type; cast the call to its declared
return type
(gdb) p (int)(*__errno_location ())
Cannot access memory at address 0xfffffffff7ce36c8
(gdb) p (int)(*(int *)__errno_location ())
$10 = 42
(gdb) p /x &(int)*__errno_location ()
$11 = 0xfffffffff7ce36c8
(gdb) p /x &(int)*(int *)__errno_location ()
$12 = 0x7ffff7ce36c8
(gdb) p /x &(int)*(int)__errno_location ()
$3 = 0xfffffffff7ce36c8
...

We known that __errno_location has an unknown return type.  We ask for it to be
cast before using it.  But then when using it nested in a expression we cast it
to int.

Probably there's a bug in this code expop.h at unop_ind_base_operation:
...
  value *evaluate (struct type *expect_type,
                   struct expression *exp,
                   enum noside noside) override
  {
    if (expect_type != nullptr && expect_type->code () == TYPE_CODE_PTR)
      expect_type = check_typedef (expect_type)->target_type ();
    value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
    return eval_op_ind (expect_type, exp, noside, val);
  }
...

We enter with expect_type "int", we should we execute the call with expect_type
"int*", but instead we do so with "int".

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
@ 2024-05-02 12:56 ` vries at gcc dot gnu.org
  2024-05-02 13:35 ` vries at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: vries at gcc dot gnu.org @ 2024-05-02 12:56 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

Tom de Vries <vries at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kevinb at redhat dot com

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
  2024-05-02 12:56 ` [Bug exp/31693] " vries at gcc dot gnu.org
@ 2024-05-02 13:35 ` vries at gcc dot gnu.org
  2024-05-02 14:32 ` vries at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: vries at gcc dot gnu.org @ 2024-05-02 13:35 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

--- Comment #1 from Tom de Vries <vries at gcc dot gnu.org> ---
This seems to fix it:
...
diff --git a/gdb/expop.h b/gdb/expop.h
index b81e228c07e..043142f2f53 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -1513,9 +1513,10 @@ class unop_ind_base_operation
                   struct expression *exp,
                   enum noside noside) override
   {
-    if (expect_type != nullptr && expect_type->code () == TYPE_CODE_PTR)
-      expect_type = check_typedef (expect_type)->target_type ();
-    value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
+    struct type *expect_type_2 = expect_type;
+    if (expect_type != nullptr)
+      expect_type_2 = lookup_pointer_type (expect_type);
+    value *val = std::get<0> (m_storage)->evaluate (expect_type_2, exp,
noside);
     return eval_op_ind (expect_type, exp, noside, val);
   }

...

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
  2024-05-02 12:56 ` [Bug exp/31693] " vries at gcc dot gnu.org
  2024-05-02 13:35 ` vries at gcc dot gnu.org
@ 2024-05-02 14:32 ` vries at gcc dot gnu.org
  2024-05-02 15:34 ` kevinb at redhat dot com
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: vries at gcc dot gnu.org @ 2024-05-02 14:32 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

--- Comment #2 from Tom de Vries <vries at gcc dot gnu.org> ---
To take it out of errno context, a simple reproducer.

Test-case:
...
$ cat test.c
char a = 'a';

char *
a_loc (void)
{
  return &a;
}

int
main (void)
{
  return 0;
}
...

Compile, no debug info:
...
$ gcc test.c
...

System gdb (13.2), not fixed:
...
$ gdb -q -batch a.out -ex start -ex "p (char)*a_loc ()"
Temporary breakpoint 1 at 0x4004b6

Temporary breakpoint 1, 0x00000000004004b6 in main ()
Cannot access memory at address 0x10
...

Fixed gdb (trunk):
...
$ gdb -q -batch a.out -ex start -ex "p (char)*a_loc ()"
Temporary breakpoint 1 at 0x4004b6

Temporary breakpoint 1, 0x00000000004004b6 in main ()
$1 = 97 'a'
....

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-05-02 14:32 ` vries at gcc dot gnu.org
@ 2024-05-02 15:34 ` kevinb at redhat dot com
  2024-05-02 15:49 ` vries at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: kevinb at redhat dot com @ 2024-05-02 15:34 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

--- Comment #3 from Kevin Buettner <kevinb at redhat dot com> ---
The proposed fix in Comment 1 looks reasonable to me.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-05-02 15:34 ` kevinb at redhat dot com
@ 2024-05-02 15:49 ` vries at gcc dot gnu.org
  2024-05-02 15:49 ` tromey at sourceware dot org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: vries at gcc dot gnu.org @ 2024-05-02 15:49 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

--- Comment #4 from Tom de Vries <vries at gcc dot gnu.org> ---
https://sourceware.org/pipermail/gdb-patches/2024-May/208750.html

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-05-02 15:49 ` vries at gcc dot gnu.org
@ 2024-05-02 15:49 ` tromey at sourceware dot org
  2024-05-02 15:54 ` vries at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: tromey at sourceware dot org @ 2024-05-02 15:49 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

--- Comment #5 from Tom Tromey <tromey at sourceware dot org> ---
The patch is probably fine, but the intent of the message
(IMO) is to say that the call itself should have the
cast, like:

print *(char*)a_loc()

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2024-05-02 15:49 ` tromey at sourceware dot org
@ 2024-05-02 15:54 ` vries at gcc dot gnu.org
  2024-05-02 15:57 ` vries at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: vries at gcc dot gnu.org @ 2024-05-02 15:54 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

--- Comment #6 from Tom de Vries <vries at gcc dot gnu.org> ---
(In reply to Tom Tromey from comment #5)
> The patch is probably fine, but the intent of the message
> (IMO) is to say that the call itself should have the
> cast, like:
> 
> print *(char*)a_loc()

Agreed.  This is more about making errno work out of the box, when errno is a
macro defined as *(__errno_location ()) and there's no debug info for
__errno_location.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2024-05-02 15:54 ` vries at gcc dot gnu.org
@ 2024-05-02 15:57 ` vries at gcc dot gnu.org
  2024-05-03  7:37 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: vries at gcc dot gnu.org @ 2024-05-02 15:57 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

--- Comment #7 from Tom de Vries <vries at gcc dot gnu.org> ---
(In reply to Tom de Vries from comment #6)
> (In reply to Tom Tromey from comment #5)
> > The patch is probably fine, but the intent of the message
> > (IMO) is to say that the call itself should have the
> > cast, like:
> > 
> > print *(char*)a_loc()
> 
> Agreed.  This is more about making errno work out of the box

Well, I mean (int)errno.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2024-05-02 15:57 ` vries at gcc dot gnu.org
@ 2024-05-03  7:37 ` cvs-commit at gcc dot gnu.org
  2024-05-03  7:38 ` vries at gcc dot gnu.org
  2024-05-04  4:22 ` sam at gentoo dot org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-03  7:37 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

--- Comment #8 from Sourceware Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ed8fd0a342f6e832fee1a3fabc3e494977780dcf

commit ed8fd0a342f6e832fee1a3fabc3e494977780dcf
Author: Tom de Vries <tdevries@suse.de>
Date:   Fri May 3 09:37:19 2024 +0200

    [gdb/exp] Fix cast handling for indirection

    Consider a test-case compiled without debug info, containing:
    ...
    char a = 'a';

    char *
    a_loc (void)
    {
      return &a;
    }
    ...

    We get:
    ...
    (gdb) p (char)*a_loc ()
    Cannot access memory at address 0x10
    ...

    There's a bug in unop_ind_base_operation::evaluate that evaluates
    "(char)*a_loc ()" the same as:
    ...
    (gdb) p (char)*(char)a_loc ()
    Cannot access memory at address 0x10
    ...

    Fix this by instead evaluating it the same as:
    ...
    (gdb) p (char)*(char *)a_loc ()
    $1 = 97 'a'
    ...

    Tested on x86_64-linux.

    PR exp/31693
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31693

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2024-05-03  7:37 ` cvs-commit at gcc dot gnu.org
@ 2024-05-03  7:38 ` vries at gcc dot gnu.org
  2024-05-04  4:22 ` sam at gentoo dot org
  10 siblings, 0 replies; 12+ messages in thread
From: vries at gcc dot gnu.org @ 2024-05-03  7:38 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

Tom de Vries <vries at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED
   Target Milestone|---                         |15.1

--- Comment #9 from Tom de Vries <vries at gcc dot gnu.org> ---
Fixed.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug exp/31693] [gdb/exp] cast not handled correctly by indirection
  2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2024-05-03  7:38 ` vries at gcc dot gnu.org
@ 2024-05-04  4:22 ` sam at gentoo dot org
  10 siblings, 0 replies; 12+ messages in thread
From: sam at gentoo dot org @ 2024-05-04  4:22 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=31693

Sam James <sam at gentoo dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sam at gentoo dot org

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-05-04  4:22 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-02 12:55 [Bug exp/31693] New: [gdb/exp] cast not handled correctly by indirection vries at gcc dot gnu.org
2024-05-02 12:56 ` [Bug exp/31693] " vries at gcc dot gnu.org
2024-05-02 13:35 ` vries at gcc dot gnu.org
2024-05-02 14:32 ` vries at gcc dot gnu.org
2024-05-02 15:34 ` kevinb at redhat dot com
2024-05-02 15:49 ` vries at gcc dot gnu.org
2024-05-02 15:49 ` tromey at sourceware dot org
2024-05-02 15:54 ` vries at gcc dot gnu.org
2024-05-02 15:57 ` vries at gcc dot gnu.org
2024-05-03  7:37 ` cvs-commit at gcc dot gnu.org
2024-05-03  7:38 ` vries at gcc dot gnu.org
2024-05-04  4:22 ` sam at gentoo dot org

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).