Qing Zhao bddb7b2528 Extend "counted_by" attribute to pointer fields of structures. Convert a pointer reference with counted_by attribute to .ACCESS_WITH_SIZE. Fix PR120929.
For example:

struct PP {
  size_t count2;
  char other1;
  char *array2 __attribute__ ((counted_by (count2)));
  int other2;
} *pp;

specifies that the "array2" is an array that is pointed by the
pointer field, and its number of elements is given by the field
"count2" in the same structure.

In order to fix PR120929, we agreed on the following solution:

for a pointer field with counted_by attribute:

struct S {
  int n;
  int *p __attribute__((counted_by(n)));
} *f;

when generating call to .ACCESS_WITH_SIZE for f->p, instead of generating
 *.ACCESS_WITH_SIZE (&f->p, &f->n,...)

We should generate
 .ACCESS_WITH_SIZE (f->p, &f->n,...)

i.e.,
the return type and the type of the first argument of the call is the
   original pointer type in this version.

However, this code generation might bring undefined behavior into the
applicaiton if the call to .ACCESS_WITH_SIZE is generated for a pointer
field reference when this refernece is written to.

For example:

f->p = malloc (size);

***** the IL for the above is:

  tmp1 = f->p;
  tmp2 = &f->n;
  tmp3 = .ACCESS_WITH_SIZE (tmp1, tmp2, ...);
  tmp4 = malloc (size);
  tmp3 = tmp4;

In the above, in order to generate a call to .ACCESS_WITH_SIZE for the pointer
reference f->p,  the new GIMPLE tmp1 = f->p is necessary to pass the value of
the pointer f->p to the call to .ACCESS_WITH_SIZE. However, this new GIMPLE is
the one that brings UB into the application since the value of f->p is not
initialized yet when it is assigned to "tmp1".

the above IL will be expanded to the following when .ACCESS_WITH_SIZE is
expanded to its first argument:

  tmp1 = f->p;
  tmp2 = &f->n;
  tmp3 = tmp1;
  tmp4 = malloc (size);
  tmp3 = tmp4;

the final optimized IL will be:

  tmp3 = f->p;
  tmp3 = malloc (size);;

As a result, the f->p will NOT be set correctly to the pointer
returned by malloc (size).

Due to this potential issue, We will need to selectively generate the call to
.ACCESS_WITH_SIZE for f->p according to whether it's a read or a write.

We will only generate call to .ACCESS_WITH_SIZE for f->p when it's a read in
C FE.

gcc/c-family/ChangeLog:

	* c-attribs.cc (handle_counted_by_attribute): Accept counted_by
	attribute for pointer fields.

gcc/c/ChangeLog:

	* c-decl.cc (verify_counted_by_attribute): Change the 2nd argument
	to a vector of fields with counted_by attribute. Verify all fields
	in this vector.
	(finish_struct): Collect all the fields with counted_by attribute
	to a vector and pass this vector to verify_counted_by_attribute.
	* c-tree.h (handle_counted_by_for_component_ref): New prototype of
	handle_counted_by_form_component_ref.
	* c-parser.cc (c_parser_postfix_expression): Call the new prototype
	of handle_counted_by_for_component_ref.
	* c-typeck.cc (default_function_array_read_conversion): Only generate
	call to .ACCESS_WITH_SIZE for a pointer field when it's a read.
	(convert_lvalue_to_rvalue): Likewise.
	(default_conversion): Likewise.
	(handle_counted_by_p): New routine.
	(check_counted_by_attribute): New routine.
	(build_counted_by_ref): Handle pointers with counted_by.
	(build_access_with_size_for_counted_by): Handle pointers with counted_by.
	(handle_counted_by_for_component_ref): Add one more argument.
	(build_component_ref): Call the new prototype of
	handle_counted_by_for_component_ref.

gcc/ChangeLog:

	* doc/extend.texi: Extend counted_by attribute to pointer fields in
	structures. Add one more requirement to pointers with counted_by
	attribute.

gcc/testsuite/ChangeLog:

	* gcc.dg/flex-array-counted-by.c: Update test.
	* gcc.dg/pointer-counted-by-1.c: New test.
	* gcc.dg/pointer-counted-by-2.c: New test.
	* gcc.dg/pointer-counted-by-3.c: New test.
	* gcc.dg/pointer-counted-by-8.c: New test.
	* gcc.dg/pointer-counted-by-9.c: New test.
	* gcc.dg/pointer-counted-by.c: New test.
2025-08-15 15:27:41 +00:00
2025-06-03 00:18:06 +00:00
2025-08-11 00:16:52 +00:00
2025-08-08 00:18:50 +00:00
2025-06-23 00:16:33 +00:00
2025-07-10 00:20:18 +00:00
2025-01-02 11:59:57 +01:00
2025-08-01 00:20:20 +00:00
2025-04-11 00:17:32 +00:00
2025-07-26 00:19:51 +00:00
2025-08-11 00:16:52 +00:00
2025-01-02 11:59:57 +01:00
2024-10-26 00:19:39 +00:00
2025-08-14 00:20:55 +00:00
2025-08-14 00:20:55 +00:00
2025-08-07 00:20:46 +00:00
2025-07-24 00:20:56 +00:00
2025-08-07 00:20:46 +00:00
2025-08-06 00:21:26 +00:00
2025-08-14 00:20:55 +00:00
2025-05-10 00:17:59 +00:00
2025-01-02 11:59:57 +01:00
2025-07-02 00:19:39 +00:00
2025-04-10 00:18:06 +00:00
2025-01-02 11:59:57 +01:00
2025-08-11 00:16:52 +00:00
2025-05-10 00:17:59 +00:00
2025-03-07 00:17:19 +00:00
2025-07-05 00:19:48 +00:00
2025-08-01 00:20:20 +00:00
2025-08-15 00:17:03 +00:00
2024-11-19 12:27:33 +01:00

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.
Description
No description provided
Readme 2.7 GiB
Languages
C++ 30.7%
C 30.1%
Ada 14.4%
D 6.1%
Go 5.7%
Other 12.5%