Bug 30651 - GDB crash on _BitInt debuginfo
Summary: GDB crash on _BitInt debuginfo
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: 12.1
: P2 normal
Target Milestone: 14.1
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-18 17:33 UTC by Jakub Jelinek
Modified: 2023-07-21 20:21 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
bitint-39a.s (6.32 KB, text/plain)
2023-07-18 17:33 UTC, Jakub Jelinek
Details
bitint-39a.c (369 bytes, text/x-csrc)
2023-07-18 17:33 UTC, Jakub Jelinek
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Jelinek 2023-07-18 17:33:20 UTC
Created attachment 14984 [details]
bitint-39a.s

gdb 12.1 (haven't tried more recent) crashes with
../../gdb/dwarf2/attribute.h:90: internal-error: as_unsigned: Assertion `form_is_unsigned ()' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
when trying to debug following testcase:
int v;

__attribute__((noinline, noclone))
void bar (void)
{
  v++;
}

__attribute__((noinline, noclone))
signed _BitInt (19) t111_1mul (unsigned _BitInt (19) x, signed _BitInt (19) y)
{
  signed _BitInt (19) r;
  if (__builtin_mul_overflow (x, y, &r))
    bar ();
  return r;
}

__attribute__((noinline, noclone))
signed _BitInt (19) t111_2mul (signed _BitInt (19) y)
{
  unsigned _BitInt (19) x = (2);
  signed _BitInt (19) r;
  if (__builtin_mul_overflow (x, y, &r))
    bar ();
  return r;
}

__attribute__((noinline, noclone))
signed _BitInt (19) t111_3mul (signed _BitInt (19) y)
{
  signed _BitInt (19) r;
  if (__builtin_mul_overflow ((unsigned _BitInt (19)) (2), y, &r))
    bar ();
  return r;
}

__attribute__((noinline, noclone))
signed _BitInt (19) t111_4mul (unsigned _BitInt (19) x)
{
  signed _BitInt (19) y = (-262143wb - 1 / 2);
  signed _BitInt (19) r;
  if (__builtin_mul_overflow (x, y, &r))
    bar ();
  return r;
}

__attribute__((noinline, noclone))
signed _BitInt (19) t111_5mul (unsigned _BitInt (19) x)
{
  signed _BitInt (19) r;
  if (__builtin_mul_overflow
      (x, (signed _BitInt (19)) (-262143wb - 1 / 2), &r))
    bar ();
  return r;
}

__attribute__((noinline, noclone))
void t111mul (void)
{
  unsigned _BitInt (19) x = (2);
  signed _BitInt (19) y = (-262143wb - 1 / 2);
  signed _BitInt (19) r1, r2;
  v = 0;
  if (t111_1mul (x, y) != (signed _BitInt (19)) (-262143wb - 1)
      || t111_2mul (y) != (signed _BitInt (19)) (-262143wb - 1)
      || t111_3mul (y) != (signed _BitInt (19)) (-262143wb - 1)
      || t111_4mul (x) != (signed _BitInt (19)) (-262143wb - 1)
      || t111_5mul (x) != (signed _BitInt (19)) (-262143wb - 1))
    __builtin_abort ();
  if (__builtin_mul_overflow (x, y, &r1))
    bar ();
  if (r1 != (signed _BitInt (19)) (-262143wb - 1))
    __builtin_abort ();
  if (__builtin_mul_overflow
      ((unsigned _BitInt (19)) (2),
       (signed _BitInt (19)) (-262143wb - 1 / 2), &r2))
    bar ();
  if (r2 != (signed _BitInt (19)) (-262143wb - 1) || v != 7 * 0)
    __builtin_abort ();
}

int
main ()
{
  t111mul ();
  return 0;
}

with GCC trunk patched with https://gcc.gnu.org/bugzilla/attachment.cgi?id=55572&action=diff
compiled with -g -std=gnu2x.
The DW_TAG_base_type for _BitInt uses
        .byte   0x4     # uleb128 0x4; (abbrev code)
        .byte   0x24    # uleb128 0x24; (TAG: DW_TAG_base_type)
        .byte   0       # DW_children_no
        .byte   0xb     # uleb128 0xb; (DW_AT_byte_size)
        .byte   0x21    # uleb128 0x21; (DW_FORM_implicit_const)
        .byte   0x4     # sleb128 4
        .byte   0x3e    # uleb128 0x3e; (DW_AT_encoding)
        .byte   0xb     # uleb128 0xb; (DW_FORM_data1)
        .byte   0xd     # uleb128 0xd; (DW_AT_bit_size)
        .byte   0x21    # uleb128 0x21; (DW_FORM_implicit_const)
        .byte   0x13    # sleb128 19
        .byte   0x3     # uleb128 0x3; (DW_AT_name)
        .byte   0xe     # uleb128 0xe; (DW_FORM_strp)
        .byte   0
        .byte   0
but I don't see anything wrong on that, DW_FORM_implicit_const always uses sleb128
encoding of the constant and all that matters is whether that constant is signed or unsigned, not that it uses sleb128 encoding.
The crash is after
run
up
Comment 1 Jakub Jelinek 2023-07-18 17:33:46 UTC
Created attachment 14985 [details]
bitint-39a.c
Comment 2 Tom Tromey 2023-07-18 21:35:44 UTC
I have a fix, just have to write a test case.
Comment 3 Tom Tromey 2023-07-20 21:31:08 UTC
There should probably be a separate bug for implementing
this stuff in gdb's parser / expression evaluator.
Comment 4 Jakub Jelinek 2023-07-20 21:57:02 UTC
Yeah.  I've filed https://dwarfstd.org/issues/230529.1.html proposal for DWARF6, but it hasn't been discussed yet.  And, while GDB can print some _BitInt values just fine (seems if they are multiple of 64 bits, it gives up on others.  Right now when debugging stuff I'm just printing the limbs using x/<N>gx &var when GDB can't handle it.
Comment 6 Tom Tromey 2023-07-20 22:12:54 UTC
(In reply to Jakub Jelinek from comment #4)
> Yeah.  I've filed https://dwarfstd.org/issues/230529.1.html proposal for
> DWARF6, but it hasn't been discussed yet.  And, while GDB can print some
> _BitInt values just fine (seems if they are multiple of 64 bits, it gives up
> on others.  Right now when debugging stuff I'm just printing the limbs using
> x/<N>gx &var when GDB can't handle it.

It looks like there are new suffixes too?  And the semantics of these aren't
identical to existing int types?  And I guess new ABI... probably will end
up wanting several gdb bugs to implement it all.
Comment 7 Sourceware Commits 2023-07-21 20:20:40 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

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

commit 4d051f3a4e2839225f7de0ea783e2fdca1c7717e
Author: Tom Tromey <tom@tromey.com>
Date:   Thu Jul 20 15:48:46 2023 -0600

    Fix crash with DW_FORM_implicit_const
    
    Jakub pointed out that using DW_FORM_implicit_const with
    DW_AT_bit_size would cause gdb to crash.  This happened because
    DW_FORM_implicit_const is not an "unsigned" form, causing as_unsigned
    to assert.  This patch fixes the problem.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30651
    Approved-By: Andrew Burgess <aburgess@redhat.com>
Comment 8 Tom Tromey 2023-07-21 20:21:30 UTC
Fixed.