]> granicus.if.org Git - strace/blob - gen_bpf_attr_check.sh
tests: fix format warnings on x32
[strace] / gen_bpf_attr_check.sh
1 #!/bin/sh -efu
2 # Copyright (c) 2018 Dmitry V. Levin <ldv@altlinux.org>
3 # Copyright (c) 2018-2019 The strace developers.
4 # All rights reserved.
5 #
6 # SPDX-License-Identifier: LGPL-2.1-or-later
7
8 [ "x${D:-0}" != x1 ] || set -x
9
10 input="$1"
11 shift
12
13 cat <<EOF
14 /* Generated by $0 from $input; do not edit. */
15 #include "defs.h"
16 #ifdef HAVE_LINUX_BPF_H
17 # include <linux/bpf.h>
18 # include "bpf_attr.h"
19 # include "static_assert.h"
20
21 # define SoM(type_, member_) (sizeof(((type_ *)0)->member_))
22 EOF
23
24 for struct in $(sed -n 's/^struct \([^[:space:]]\+_struct\) .*/\1/p' < "$input"); do
25         case "$struct" in
26                 BPF_*) type_name='union bpf_attr' ;;
27                 *) type_name="struct ${struct%_struct}" ;;
28         esac
29         TYPE_NAME="$(printf %s "$type_name" |tr '[:lower:] ' '[:upper:]_')"
30
31         enum="$(sed -n 's/^struct '"$struct"' \/\* \([^[:space:]]\+\) \*\/ {.*/\1/p' < "$input")"
32         ENUM="$(printf %s "$enum" |tr '[:lower:]' '[:upper:]')"
33         enum="$enum${enum:+.}"
34         ENUM="$ENUM${ENUM:+_}"
35         sed -n '/^struct '"$struct"' [^{]*{/,/^};$/p' < "$input" |
36         sed -n 's/^[[:space:]]\+[^][;:]*[[:space:]]\([^][[:space:];:]\+\)\(\[[^;:]*\]\)\?;$/\1/p' |
37         while read field; do
38                 FIELD="$(printf %s "$field" |tr '[:lower:]' '[:upper:]')"
39                 cat <<EOF
40
41 # ifdef HAVE_${TYPE_NAME}_$ENUM$FIELD
42         static_assert(SoM(struct $struct, $field) == SoM($type_name, $enum$field),
43                       "$struct.$field size mismatch");
44         static_assert(offsetof(struct $struct, $field) == offsetof($type_name, $enum$field),
45                       "$struct.$field offset mismatch");
46 # endif /* HAVE_${TYPE_NAME}_$ENUM$FIELD */
47 EOF
48         done
49                 cat <<EOF
50
51 static_assert(${struct}_size == expected_${struct}_size,
52               "${struct}_size mismatch");
53 EOF
54
55 done
56
57 cat <<'EOF'
58
59 #endif /* HAVE_LINUX_BPF_H */
60 EOF