[clang] [Clang] Implement the 'counted_by' attribute (PR #76348)

Kees Cook via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 8 14:53:45 PST 2024


kees wrote:

Possibly due to bug #72032 , I can get this tree to crash using the latest `array-bounds.c` test from
https://github.com/kees/kernel-tools/tree/trunk/fortify

Specifically:

```
struct anon_struct {
        unsigned long flags;
        long count;
        int array[] __counted_by(count);
};

struct composite {
        unsigned stuff;
        struct annotated inner;
};

static struct composite * noinline alloc_composite(int index)
{
        struct composite *p;

        p = malloc(sizeof(*p) + index * sizeof(*p->inner.array));
        p->inner.count = index;

        return p;
}

       struct composite *c;
       c = alloc_composite(index);
       ... actions on c->inner.array ...
```

```
3.      array-bounds.c:363:1 <Spelling=array-bounds.c:363:6>: Generating code for declaration 'counted_by_seen_by_bdos'
4.      array-bounds.c:405:2 <Spelling=array-bounds.c:23:32>: LLVM IR generation of compound statement ('{}')
...
 #4 0x0000556574d5b858 clang::CodeGen::CodeGenTBAA::getAccessInfo(clang::QualType)
 #5 0x000055657489e25d clang::CodeGen::CodeGenModule::getTBAAAccessInfo(clang::QualType)
 #6 0x00005565748a9c20 clang::CodeGen::CodeGenModule::getNaturalTypeAlignment(clang::QualType, clang::CodeGen::LValueBaseInfo*, clang::CodeGen::TBAAAccessInfo*, bool)
 #7 0x00005565749fc4a2 EmitPointerWithAlignment(clang::Expr const*, clang::CodeGen::LValueBaseInfo*, clang::CodeGen::TBAAAccessInfo*, clang::CodeGen::KnownNonNull_t, clang::CodeGen::CodeGenFunction&) CGExpr.cpp:0:0
 #8 0x00005565749f94bd clang::CodeGen::CodeGenFunction::EmitCountedByFieldExpr(clang::Expr const*, clang::FieldDecl const*, clang::FieldDecl const*)
```



https://github.com/llvm/llvm-project/pull/76348


More information about the cfe-commits mailing list