From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 394E93858D3C; Wed, 22 Feb 2023 21:26:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 394E93858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677101219; bh=mGoCUSsNvQCFggIqKMRV3vT51mDRxDhy+EMSUo4RLJo=; h=From:To:Subject:Date:From; b=vC/p5h17MhYtDLgXIaVhCQ709kcDiGad/fTpI64QuKQZY4tlqS5c8dl2ukUGNDC7K BSHvRgeXPz8BmzQsF5b/Kak+q13DwL+4/3QL4pvC+lGEoorNhWD6q4dYCYAeboTY8p 1BDH9NxhPe+rv52TTZz3dtJGULSAc4OW+e2drIsg= From: "kees at outflux dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/108896] New: provide "element_count" attribute to give more context to __builtin_dynamic_object_size() and -fsanitize=bounds Date: Wed, 22 Feb 2023 21:26:58 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: kees at outflux dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108896 Bug ID: 108896 Summary: provide "element_count" attribute to give more context to __builtin_dynamic_object_size() and -fsanitize=3Dbounds Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: kees at outflux dot net Target Milestone: --- Frequently a structure containing a flexible array member will also contain= a member where the count of array elements is stored. For example: struct foo { ... unsigned int count; ... int data[]; }; struct foo *allocate_foo(unsigned int how_many) { struct foo *p; p =3D malloc(sizeof(*p) + how_many * sizeof(*byte_array)); p->count =3D how_many; return p; } While __builtin_dynamic_object_size(p->data, 1) will know the size within "allocate_foo" due to malloc's __alloc_size hinting, this information is immediately lost on return. However, the information _is_ still available in p->count, but the compiler has no way to know about it. Please provide a struct member attribute "element_count" that can be used to associate the size of a flexible array to another struct member. For exampl= e: struct foo { ... unsigned int count; ... int data[] __attribute__((__element_count__(count))); }; Now any later examination of the size of "data" can be calculated. For exam= ple, this equality will hold true: __builtin_dynamic_object_size(p->data) =3D=3D p->count * sizeof(*p->dat= a) and -fsanitize-bounds can examine this as well, to trap: p->data[index] =3D ...; /* traps when index < 0, or index >=3D p->count= */=