-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
__builtin_dynamic_object_size() fails to return correct size depending on depth of flexible array #110385
Comments
@llvm/issue-subscribers-clang-frontend Author: Kees Cook (kees)
As seen in the Linux kernel:
we're having erroneous size reporting from
cc @isanbard @bwendling |
I found a fix. Haven't run the tests on it yet, but it compiles the kernel. Will open a PR tomorrow:
|
Fixes llvm#110385 Fix counted_by attribute for cases where the flexible array member is accessed through struct pointer inside another struct: struct variable { int a; int b; int length; short array[] __attribute__((counted_by(length))); }; struct bucket { int a; struct variable *growable; int b; }; __builtin_dynamic_object_size(p->growable->array, 0); This commit makes sure that if the StructBase is both a MemberExpr and a pointer, it is treated as a pointer. Otherwise clang will generate to code to access the address of p->growable intead of loading the value of p->growable->length.
Fixes llvm#110385 Fix counted_by attribute for cases where the flexible array member is accessed through struct pointer inside another struct: struct variable { int a; int b; int length; short array[] __attribute__((counted_by(length))); }; struct bucket { int a; struct variable *growable; int b; }; __builtin_dynamic_object_size(p->growable->array, 0); This commit makes sure that if the StructBase is both a MemberExpr and a pointer, it is treated as a pointer. Otherwise clang will generate to code to access the address of p->growable intead of loading the value of p->growable->length.
@Cydox, I have a fix that I'm planning on submitting first. |
@llvm/issue-subscribers-bug Author: Kees Cook (kees)
As seen in the Linux kernel:
we're having erroneous size reporting from https://godbolt.org/z/qohGd5xh1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct variable {
int a;
int b;
int length;
short array[] __attribute__((counted_by(length)));
};
struct bucket {
int a;
struct variable *growable;
int b;
};
int main(int argc, char *argv[])
{
struct bucket *p;
struct variable *v;
p = malloc(sizeof(*p));
v = malloc(sizeof(*p->growable) + sizeof(*p->growable->array) * 32);
v->length = 32;
printf("%zu\n", __builtin_dynamic_object_size(v->array, 1));
p->growable = v;
printf("%zu\n", __builtin_dynamic_object_size(p->growable->array, 1));
return 0;
}
GCC shows 64 64, but Clang shows 64 0. cc @isanbard @bwendling |
See #110487 for a proposed fix. (One liner even!) |
Fixes llvm#110385 Fix counted_by attribute for cases where the flexible array member is accessed through struct pointer inside another struct: struct variable { int a; int b; int length; short array[] __attribute__((counted_by(length))); }; struct bucket { int a; struct variable *growable; int b; }; __builtin_dynamic_object_size(p->growable->array, 0); This commit makes sure that if the StructBase is both a MemberExpr and a pointer, it is treated as a pointer. Otherwise clang will generate to code to access the address of p->growable intead of loading the value of p->growable->length.
@llvm/issue-subscribers-clang-codegen Author: Kees Cook (kees)
As seen in the Linux kernel:
we're having erroneous size reporting from https://godbolt.org/z/qohGd5xh1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct variable {
int a;
int b;
int length;
short array[] __attribute__((counted_by(length)));
};
struct bucket {
int a;
struct variable *growable;
int b;
};
int main(int argc, char *argv[])
{
struct bucket *p;
struct variable *v;
p = malloc(sizeof(*p));
v = malloc(sizeof(*p->growable) + sizeof(*p->growable->array) * 32);
v->length = 32;
printf("%zu\n", __builtin_dynamic_object_size(v->array, 1));
p->growable = v;
printf("%zu\n", __builtin_dynamic_object_size(p->growable->array, 1));
return 0;
}
GCC shows 64 64, but Clang shows 64 0. cc @isanbard @bwendling |
Fix counted_by attribute for cases where the flexible array member is accessed through struct pointer inside another struct: ``` struct variable { int a; int b; int length; short array[] __attribute__((counted_by(length))); }; struct bucket { int a; struct variable *growable; int b; }; ``` __builtin_dynamic_object_size(p->growable->array, 0); This commit makes sure that if the StructBase is both a MemberExpr and a pointer, it is treated as a pointer. Otherwise clang will generate to code to access the address of p->growable intead of loading the value of p->growable->length. Fixes llvm#110385
Fix counted_by attribute for cases where the flexible array member is accessed through struct pointer inside another struct: ``` struct variable { int a; int b; int length; short array[] __attribute__((counted_by(length))); }; struct bucket { int a; struct variable *growable; int b; }; ``` __builtin_dynamic_object_size(p->growable->array, 0); This commit makes sure that if the StructBase is both a MemberExpr and a pointer, it is treated as a pointer. Otherwise clang will generate to code to access the address of p->growable intead of loading the value of p->growable->length. Fixes llvm#110385
Fix counted_by attribute for cases where the flexible array member is accessed through struct pointer inside another struct: ``` struct variable { int a; int b; int length; short array[] __attribute__((counted_by(length))); }; struct bucket { int a; struct variable *growable; int b; }; ``` __builtin_dynamic_object_size(p->growable->array, 0); This commit makes sure that if the StructBase is both a MemberExpr and a pointer, it is treated as a pointer. Otherwise clang will generate to code to access the address of p->growable intead of loading the value of p->growable->length. Fixes llvm#110385
As seen in the Linux kernel:
we're having erroneous size reporting from
__builtin_dynamic_object_size()
, where the depth of dereference for the flexible array causes a 0 size report:cc @isanbard @bwendling
The text was updated successfully, but these errors were encountered: