llvm-capstone/clang/lib/Sema
Bill Wendling 9a954c6935 [Clang] Implement the 'counted_by' attribute
The 'counted_by' attribute is used on flexible array members. The
argument for the attribute is the name of the field member in the same
structure holding the count of elements in the flexible array. This
information can be used to improve the results of the array bound sanitizer
and the '__builtin_dynamic_object_size' builtin.

This example specifies the that the flexible array member 'array' has the
number of elements allocated for it in 'count':

  struct bar;
  struct foo {
    size_t count;
     /* ... */
    struct bar *array[] __attribute__((counted_by(count)));
  };

This establishes a relationship between 'array' and 'count', specifically
that 'p->array' must have *at least* 'p->count' number of elements available.
It's the user's responsibility to ensure that this relationship is maintained
through changes to the structure.

In the following, the allocated array erroneously has fewer elements than
what's specified by 'p->count'. This would result in an out-of-bounds access not
not being detected:

  struct foo *p;

  void foo_alloc(size_t count) {
    p = malloc(MAX(sizeof(struct foo),
                   offsetof(struct foo, array[0]) + count *
                       sizeof(struct bar *)));
    p->count = count + 42;
  }

The next example updates 'p->count', breaking the relationship requirement that
'p->array' must have at least 'p->count' number of elements available:

  struct foo *p;

  void foo_alloc(size_t count) {
    p = malloc(MAX(sizeof(struct foo),
                   offsetof(struct foo, array[0]) + count *
                       sizeof(struct bar *)));
    p->count = count + 42;
  }

  void use_foo(int index) {
    p->count += 42;
    p->array[index] = 0; /* The sanitizer cannot properly check this access */
  }

Reviewed By: nickdesaulniers, aaron.ballman

Differential Revision: https://reviews.llvm.org/D148381
2023-10-04 18:26:15 -07:00
..
AnalysisBasedWarnings.cpp Revert "[clang analysis][thread-safety] Handle return-by-reference...… (#67795) 2023-09-29 14:13:53 +02:00
CMakeLists.txt cmake: add missing dependencies on ClangDriverOptions tablegen 2023-08-04 10:27:19 -07:00
CodeCompleteConsumer.cpp Reland "[clang-repl] support code completion at a REPL." 2023-08-28 20:09:03 +00:00
CoroutineStmtBuilder.h
DeclSpec.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
DelayedDiagnostic.cpp
HLSLExternalSemaSource.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
IdentifierResolver.cpp [NFC][clang] Fix static analyzer concerns 2023-08-03 01:52:26 -07:00
JumpDiagnostics.cpp [Clang] Diagnose jumps into statement expressions 2023-07-21 15:08:51 +02:00
MultiplexExternalSemaSource.cpp PR60985: Fix merging of lambda closure types across modules. 2023-03-30 14:22:40 -07:00
OpenCLBuiltins.td clang/OpenCL: Add inline implementations of sqrt in builtin header 2023-09-12 23:23:00 +03:00
ParsedAttr.cpp Recommit "Implement [[msvc::no_unique_address]] (#65675)" (#67199) 2023-09-28 14:29:32 -07:00
Scope.cpp [Clang] Implement Change scope of lambda trailing-return-type 2023-03-02 10:04:16 +01:00
ScopeInfo.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
Sema.cpp [NFC] [C++20] [Modules] Rename NamedModuleHasInit to NamedModuleHasInit 2023-09-29 21:49:10 +08:00
SemaAccess.cpp [NFC][Clang][Coverity] Fix Static Code Analysis Concerns with copy without assign 2023-05-18 18:14:07 -07:00
SemaAttr.cpp [clang] Type safety tweak for AttributeCommonInfo::Form 2023-04-13 10:14:49 +01:00
SemaAvailability.cpp [clang/cxx-interop] Teach clang to ignore availability errors that come from CF_OPTIONS 2023-08-07 09:56:25 -04:00
SemaCast.cpp [Sema] Make C++ functional-style cast warn about dropped qualifiers (-Wcast-qual) 2023-09-05 12:48:38 +02:00
SemaChecking.cpp [Sema] Use underlying type of scoped enum for -Wformat diagnostics (#67378) 2023-10-02 11:32:54 -07:00
SemaCodeComplete.cpp [clangd][CodeComplete] Improve FunctionCanBeCall 2023-09-28 21:42:02 +08:00
SemaConcept.cpp [clang][Sema] Fix a bug when instantiating a lambda with requires clause (#65193) 2023-10-04 10:19:35 +08:00
SemaConsumer.cpp
SemaCoroutine.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaCUDA.cpp [HIP][Clang][Sema] Add Sema support for hipstdpar 2023-10-03 13:29:12 +01:00
SemaCXXScopeSpec.cpp [Sema] Remove unused isNonTypeNestedNameSpecifier 2023-06-13 00:24:40 -07:00
SemaDecl.cpp [Clang] Implement the 'counted_by' attribute 2023-10-04 18:26:15 -07:00
SemaDeclAttr.cpp [Clang] Implement the 'counted_by' attribute 2023-10-04 18:26:15 -07:00
SemaDeclCXX.cpp [Clang][Sema] Fix display of characters on static assertion failure 2023-10-04 14:09:06 +09:00
SemaDeclObjC.cpp [modules] Allow parsing a duplicate Obj-C interface if a previous one comes from a hidden [sub]module. 2023-01-20 10:18:18 -06:00
SemaExceptionSpec.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaExpr.cpp [clang] Preserve UDL nodes in RemoveNestedImmediateInvocation (#66641) 2023-10-04 04:45:46 -05:00
SemaExprCXX.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaExprMember.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaExprObjC.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaFixItUtils.cpp Do not suggest taking the address of a const pointer to get void* 2022-11-23 18:43:06 +00:00
SemaHLSL.cpp [HLSL] Support cbuffer/tbuffer for hlsl. 2022-09-21 10:07:43 -07:00
SemaInit.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaLambda.cpp [clang][Sema] Fix a bug when instantiating a lambda with requires clause (#65193) 2023-10-04 10:19:35 +08:00
SemaLookup.cpp [clang] NFCI: Use FileEntryRef in suggestPathToFileForDiagnostics() 2023-09-09 20:29:04 -07:00
SemaModule.cpp Reapply "[clang] NFCI: Adopt SourceManager::getFileEntryRefForID()" 2023-09-08 19:04:01 -07:00
SemaObjCProperty.cpp Remove private rdar links. NFC 2023-07-28 16:42:31 -07:00
SemaOpenMP.cpp Revert "[OpenMP] Introduce the initial support for OpenMP kernel language (#66844)" 2023-09-29 15:35:10 -05:00
SemaOverload.cpp [clang] Choose non-templated ctor as deduction guide unambiguously (#66487) 2023-10-04 09:11:43 -07:00
SemaPseudoObject.cpp [clang] Use range-based for loops (NFC) 2023-09-04 00:31:37 -07:00
SemaRISCVVectorLookup.cpp [RISCV] Add feature checks for vector crypto C intrinsics 2023-08-30 21:03:08 -07:00
SemaStmt.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaStmtAsm.cpp [HIP][Clang][Sema] Add Sema support for hipstdpar 2023-10-03 13:29:12 +01:00
SemaStmtAttr.cpp [clang] Add Parse and Sema support for RegularKeyword attributes 2023-05-31 10:43:10 +01:00
SemaSYCL.cpp [SYCL][NFC] Remove dead code 2023-05-08 13:08:23 -07:00
SemaTemplate.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaTemplateDeduction.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
SemaTemplateInstantiate.cpp [Clang] Fix crash when ill-formed code is treated as a deduction guide (#67373) 2023-10-02 08:08:39 -07:00
SemaTemplateInstantiateDecl.cpp [clang] Choose non-templated ctor as deduction guide unambiguously (#66487) 2023-10-04 09:11:43 -07:00
SemaTemplateVariadic.cpp [SemaCXX]use CorrectDelayedTyposInExpr in ActOnCXXFoldExpr only when Diag 2023-03-15 01:08:41 +08:00
SemaType.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
TreeTransform.h Revert "[OpenMP] Introduce the initial support for OpenMP kernel language (#66844)" 2023-09-29 15:35:10 -05:00
TypeLocBuilder.cpp [clang] Implement sugared substitution changes to infrastructure 2022-10-27 06:18:07 +02:00
TypeLocBuilder.h [NFC][clang] Fix static analyzer concerns 2023-07-26 08:40:40 -07:00
UsedDeclVisitor.h Implement CWG2631 2023-01-08 10:35:26 +01:00