bpf: add support for checking structures outside union bpf_attr

struct bpf_prog_info and bpf_map_info need essentially the same handling
as union bpf_attr.

* gen_bpf_attr_check.sh: Derive type_name from $struct if it doesn't
start with "BPF_", derive TYPE_NAME from type_name, use them in code
generation.
* m4/gen_bpf_attr_m4.sh: Rewrite parsing/generation code into awk,
add support for structures outside union bpf_attr.

Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
This commit is contained in:
Eugene Syromyatnikov 2018-05-21 01:24:08 +02:00 committed by Dmitry V. Levin
parent dae6367f18
commit 9228e78d4f
2 changed files with 36 additions and 31 deletions

View File

@ -38,7 +38,13 @@ cat <<EOF
# define SoM(type_, member_) (sizeof(((type_ *)0)->member_))
EOF
for struct in $(sed -n 's/^struct \(BPF_[^[:space:]]\+_struct\) .*/\1/p' < "$input"); do
for struct in $(sed -n 's/^struct \([^[:space:]]\+_struct\) .*/\1/p' < "$input"); do
case "$struct" in
BPF_*) type_name='union bpf_attr' ;;
*) type_name="struct ${struct%_struct}" ;;
esac
TYPE_NAME="$(printf %s "$type_name" |tr '[:lower:] ' '[:upper:]_')"
enum="$(sed -n 's/^struct '"$struct"' \/\* \([^[:space:]]\+\) \*\/ {.*/\1/p' < "$input")"
ENUM="$(printf %s "$enum" |tr '[:lower:]' '[:upper:]')"
enum="$enum${enum:+.}"
@ -49,12 +55,12 @@ for struct in $(sed -n 's/^struct \(BPF_[^[:space:]]\+_struct\) .*/\1/p' < "$inp
FIELD="$(printf %s "$field" |tr '[:lower:]' '[:upper:]')"
cat <<EOF
# ifdef HAVE_UNION_BPF_ATTR_$ENUM$FIELD
static_assert(SoM(struct $struct, $field) == SoM(union bpf_attr, $enum$field),
# ifdef HAVE_${TYPE_NAME}_$ENUM$FIELD
static_assert(SoM(struct $struct, $field) == SoM($type_name, $enum$field),
"$struct.$field size mismatch");
static_assert(offsetof(struct $struct, $field) == offsetof(union bpf_attr, $enum$field),
static_assert(offsetof(struct $struct, $field) == offsetof($type_name, $enum$field),
"$struct.$field offset mismatch");
# endif /* HAVE_UNION_BPF_ATTR_$ENUM$FIELD */
# endif /* HAVE_${TYPE_NAME}_$ENUM$FIELD */
EOF
done
cat <<EOF

View File

@ -34,37 +34,36 @@ AC_DEFUN([st_BPF_ATTR], [dnl
AC_CHECK_MEMBERS(m4_normalize([
EOF
fetch_structs()
{
local name="${1:-}"
local name_re=
[ -z "$name" ] ||
name_re='\/\* '"$name"' \*\/ '
gawk -e '
/^struct ([^[:space:]]+)_struct([[:space:]]+\/\* ([^[:space:]]+) \*\/)?[[:space:]]+{/ {
match($0, /^struct ([^[:space:]]+)_struct([[:space:]]+\/\* ([^[:space:]]+) \*\/)?[[:space:]]+{/, a)
sed -n '/^struct BPF_[^[:space:]]\+_struct '"$name_re"'{/,/^};/p' < "$input"
struct_name = a[1]
subtype_name = a[3]
if (struct_name ~ /^BPF_/)
prefix = "union bpf_attr"
else
prefix = "struct " struct_name
if (subtype_name != "")
prefix = prefix "." subtype_name
in_struct = 1
next
}
filter_entries()
{
local name="${1:-}"
local subtype=
[ -z "$name" ] ||
subtype=".$name"
local search='^[[:space:]]\+[^][;]*[[:space:]]\([^][[:space:];]\+\)\(\[[^;]*\]\)\?;$'
local replacement='\t\tunion bpf_attr'"$subtype"'.\1,'
sed -n "s/$search/$replacement/p" |
sort -u
/^}( ATTRIBUTE_ALIGNED\(.*\))?;/ {
in_struct = 0
next
}
# nameless structures in union bpf_attr
fetch_structs |
filter_entries
# named structures in union bpf_attr
for name in $(sed -n 's/^struct BPF_[^[:space:]]\+_struct \/\* \([^[:space:]]\+\) \*\/ {.*/\1/p' < "$input"); do
fetch_structs "$name" |
filter_entries "$name"
done
(in_struct == 1) {
if (match($0, /^[[:space:]]+[^;\[\]]+[[:space:]]+([^[:space:]\[\];]+)(\[[^;]*\])?;$/, a)) {
print "\t\t" prefix "." a[1] ","
}
}
' < "$input" | sort -u
cat <<'EOF'
union bpf_attr.dummy