public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Is this a bug for __builtin_dynamic_object_size?
@ 2023-08-14 23:12 Qing Zhao
  2023-08-15 10:57 ` Siddhesh Poyarekar
  0 siblings, 1 reply; 3+ messages in thread
From: Qing Zhao @ 2023-08-14 23:12 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: jakub Jelinek, Qing Zhao via Gcc-patches

Hi, Sid,

For the following testing case:

#include <stdio.h>

#define noinline __attribute__((__noinline__))

static void noinline alloc_buf_more (int index)
{
  struct annotated {
    long foo;
    char b;
    char array[index];
    long c;
  } q, *p;

  p = &q;

  printf("the__bdos of p->array whole max is %d \n", __builtin_dynamic_object_size(p->array, 0)); 
  printf("the__bdos of p->array sub max is %d \n", __builtin_dynamic_object_size(p->array, 1));      
  printf("the__bdos of p->array whole min is %d \n", __builtin_dynamic_object_size(p->array, 2)); 
  printf("the__bdos of p->array sub min is %d \n", __builtin_dynamic_object_size(p->array, 3)); 

  return;
}

int main ()
{
  alloc_buf_more (10);
  return 0;
}

If I compile it with the latest upstream gcc and run it:

/home/opc/Install/latest-d/bin/gcc -O t.c
the__bdos of p->array whole max is 23 
the__bdos of p->array sub max is 23 
the__bdos of p->array whole min is 23 
the__bdos of p->array sub min is 23 

In which__builtin_dynamic_object_size(p->array, 0) and __builtin_dynamic_object_size(p->array, 1) return the same size, this seems wrong to me. 

There is one line in tree-object-size.cc might relate to this bug: (in the routine “addr_object_size”)

 603           if (! TYPE_SIZE_UNIT (TREE_TYPE (var))
 604               || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var)))
 605               || (pt_var_size && TREE_CODE (pt_var_size) == INTEGER_CST
 606                   && tree_int_cst_lt (pt_var_size,
 607                                       TYPE_SIZE_UNIT (TREE_TYPE (var)))))
 608             var = pt_var;

I suspect that the above line 604 “ ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var)))” relates to this bug, since the TYPESIZE of the VLA “array” is not a unsigned HOST_WIDE_INT, but we still can use its TYPESIZE for dynamic_object_size?

What do you think?

Thanks.

Qing

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

end of thread, other threads:[~2023-08-15 14:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-14 23:12 Is this a bug for __builtin_dynamic_object_size? Qing Zhao
2023-08-15 10:57 ` Siddhesh Poyarekar
2023-08-15 14:10   ` Qing Zhao

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