Skip to contentSkip to conversation

libbpf: Fix cast away const qualifiers in btf.h

PATCH In review Jiri Olsa 4 participants 11 messages Sep 6, 2019 – Sep 7, 2019

Subsystems (4)
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

cover libbpf: Fix cast away const qualifiers in btf.h

hi,
when including btf.h in bpftrace, I'm getting -Wcast-qual warnings like:

bpf/btf.h: In function ‘btf_var_secinfo* btf_var_secinfos(const btf_type*)’:
bpf/btf.h:302:41: warning: cast from type ‘const btf_type*’ to type ‘btf_var_secinfo*’ casts away qualifiers [-Wcast-qual]

    302 |  return (struct btf_var_secinfo *)(t + 1);
        |                                         ^

I changed the btf.h header to comply with -Wcast-qual checks and used const cast away casting in libbpf objects, where it's all related to deduplication code, so I believe loosing const is fine there.

thanks,
jirka

---
Jiri Olsa (7):
libbpf: Use const cast for btf_int_* functions
libbpf: Return const btf_array from btf_array inline function
libbpf: Return const btf_enum from btf_enum inline function
libbpf: Return const btf_member from btf_members inline function
libbpf: Return const btf_param from btf_params inline function
libbpf: Return const btf_var from btf_var inline function
libbpf: Return const struct btf_var_secinfo from btf_var_secinfos inline function

 tools/lib/bpf/btf.c    | 21 +++++++++++----------
 tools/lib/bpf/btf.h    | 30 +++++++++++++++---------------
 tools/lib/bpf/libbpf.c |  2 +-
 3 files changed, 27 insertions(+), 26 deletions(-)
author reviewer
#1 lore raw

PATCH 1/7 libbpf: Use const cast for btf_int_* functions

I'm getting following errors when compiling with -Wcast-qual:

bpf/btf.h: In function ‘__u8 btf_int_offset(const btf_type*)’:
bpf/btf.h:244:40: warning: cast from type ‘const btf_type*’ to type ‘__u32*’ {aka ‘unsigned int*’} casts away qualifiers [-Wcast-qual]

    244 |  return BTF_INT_OFFSET(*(__u32 *)(t + 1));
        |                                        ^

The argument is const so the cast to following __u32 pointer should be const as well.

---
 tools/lib/bpf/btf.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.h +3 -3
index 9cb44b4fbf60..952e3496467d 100644
@@ -236,17 +236,17 @@ static inline bool btf_is_datasec(const struct btf_type *t)
 
 static inline __u8 btf_int_encoding(const struct btf_type *t)
 {
-	return BTF_INT_ENCODING(*(__u32 *)(t + 1));
+	return BTF_INT_ENCODING(*(const __u32 *)(t + 1));
 }
 
 static inline __u8 btf_int_offset(const struct btf_type *t)
 {
-	return BTF_INT_OFFSET(*(__u32 *)(t + 1));
+	return BTF_INT_OFFSET(*(const __u32 *)(t + 1));
 }
 
 static inline __u8 btf_int_bits(const struct btf_type *t)
 {
-	return BTF_INT_BITS(*(__u32 *)(t + 1));
+	return BTF_INT_BITS(*(const __u32 *)(t + 1));
 }
 
 static inline struct btf_array *btf_array(const struct btf_type *t)
Signature · 1 line
2.21.0
author reviewer
#2 lore raw

PATCH 2/7 libbpf: Return const btf_array from btf_array inline function

In reply to Jiri Olsa

I'm getting following errors when compiling with -Wcast-qual:

bpf/btf.h: In function ‘btf_array* btf_array(const btf_type*)’:
bpf/btf.h:254:35: warning: cast from type ‘const btf_type*’ to type ‘btf_array*’ casts away qualifiers [-Wcast-qual]

    254 |  return (struct btf_array *)(t + 1);
        |                                   ^

The argument is const so the cast to following struct btf_array pointer should be const as well. Casting the const away in btf.c calls where it's correctly used without const in deduplication code.

---
 tools/lib/bpf/btf.c | 4 ++--
 tools/lib/bpf/btf.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
tools/lib/bpf/btf.c +2 -2
index 1aa189a9112a..d6327bcc713a 100644
@@ -2587,7 +2587,7 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
 		break;
 
 	case BTF_KIND_ARRAY: {
-		struct btf_array *info = btf_array(t);
+		struct btf_array *info = (struct btf_array *) btf_array(t);
 
 		ref_type_id = btf_dedup_ref_type(d, info->type);
 		if (ref_type_id < 0)
@@ -2782,7 +2782,7 @@ static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
 		break;
 
 	case BTF_KIND_ARRAY: {
-		struct btf_array *arr_info = btf_array(t);
+		struct btf_array *arr_info = (struct btf_array *) btf_array(t);
 
 		r = btf_dedup_remap_type_id(d, arr_info->type);
 		if (r < 0)
tools/lib/bpf/btf.h +2 -2
index 952e3496467d..6bbf5772aa61 100644
@@ -249,9 +249,9 @@ static inline __u8 btf_int_bits(const struct btf_type *t)
 	return BTF_INT_BITS(*(const __u32 *)(t + 1));
 }
 
-static inline struct btf_array *btf_array(const struct btf_type *t)
+static inline const struct btf_array *btf_array(const struct btf_type *t)
 {
-	return (struct btf_array *)(t + 1);
+	return (const struct btf_array *)(t + 1);
 }
 
 static inline struct btf_enum *btf_enum(const struct btf_type *t)
Signature · 1 line
2.21.0
author reviewer
#3 lore raw

PATCH 3/7 libbpf: Return const btf_enum from btf_enum inline function

In reply to Jiri Olsa

I'm getting following errors when compiling with -Wcast-qual:

bpf/btf.h: In function ‘btf_enum* btf_enum(const btf_type*)’:
bpf/btf.h:259:34: warning: cast from type ‘const btf_type*’ to type ‘btf_enum*’ casts away qualifier

    259 |  return (struct btf_enum *)(t + 1);
        |                                  ^

The argument is const so the cast to following struct btf_enum pointer should be const as well. Casting the const away in btf.c call where it's correctly used without const in deduplication code.

---
 tools/lib/bpf/btf.c | 2 +-
 tools/lib/bpf/btf.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.c +1 -1
index d6327bcc713a..5a39e9506760 100644
@@ -1475,7 +1475,7 @@ static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
 			break;
 		}
 		case BTF_KIND_ENUM: {
-			struct btf_enum *m = btf_enum(t);
+			struct btf_enum *m = (struct btf_enum *) btf_enum(t);
 			__u16 vlen = btf_vlen(t);
 
 			for (j = 0; j < vlen; j++) {
tools/lib/bpf/btf.h +2 -2
index 6bbf5772aa61..3b3216fa348f 100644
@@ -254,9 +254,9 @@ static inline const struct btf_array *btf_array(const struct btf_type *t)
 	return (const struct btf_array *)(t + 1);
 }
 
-static inline struct btf_enum *btf_enum(const struct btf_type *t)
+static inline const struct btf_enum *btf_enum(const struct btf_type *t)
 {
-	return (struct btf_enum *)(t + 1);
+	return (const struct btf_enum *)(t + 1);
 }
 
 static inline struct btf_member *btf_members(const struct btf_type *t)
Signature · 1 line
2.21.0
author reviewer
#4 lore raw

PATCH 4/7 libbpf: Return const btf_member from btf_members inline function

In reply to Jiri Olsa

I'm getting following errors when compiling with -Wcast-qual:

bpf/btf.h: In function ‘btf_member* btf_members(const btf_type*)’:
bpf/btf.h:264:36: warning: cast from type ‘const btf_type*’ to type ‘btf_member*’ casts away qualifiers [-Wcast-qual]

    264 |  return (struct btf_member *)(t + 1);
        |                                    ^

The argument is const so the cast to following struct btf_member pointer should be const as well. Casting the const away in btf.c call where it's correctly used without const in deduplication code.

---
 tools/lib/bpf/btf.c    | 4 ++--
 tools/lib/bpf/btf.h    | 4 ++--
 tools/lib/bpf/libbpf.c | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
tools/lib/bpf/btf.c +2 -2
index 5a39e9506760..560d1ae33675 100644
@@ -1463,7 +1463,7 @@ static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
 		switch (btf_kind(t)) {
 		case BTF_KIND_STRUCT:
 		case BTF_KIND_UNION: {
-			struct btf_member *m = btf_members(t);
+			struct btf_member *m = (struct btf_member *) btf_members(t);
 			__u16 vlen = btf_vlen(t);
 
 			for (j = 0; j < vlen; j++) {
@@ -2797,7 +2797,7 @@ static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
 
 	case BTF_KIND_STRUCT:
 	case BTF_KIND_UNION: {
-		struct btf_member *member = btf_members(t);
+		struct btf_member *member = (struct btf_member *) btf_members(t);
 		__u16 vlen = btf_vlen(t);
 
 		for (i = 0; i < vlen; i++) {
tools/lib/bpf/btf.h +2 -2
index 3b3216fa348f..cd1bd018ba8b 100644
@@ -259,9 +259,9 @@ static inline const struct btf_enum *btf_enum(const struct btf_type *t)
 	return (const struct btf_enum *)(t + 1);
 }
 
-static inline struct btf_member *btf_members(const struct btf_type *t)
+static inline const struct btf_member *btf_members(const struct btf_type *t)
 {
-	return (struct btf_member *)(t + 1);
+	return (const struct btf_member *)(t + 1);
 }
 
 /* Get bit offset of a member with specified index. */
tools/lib/bpf/libbpf.c +1 -1
index 2233f919dd88..7d3d6284dd2e 100644
@@ -1389,7 +1389,7 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj)
 		} else if (!has_datasec && btf_is_datasec(t)) {
 			/* replace DATASEC with STRUCT */
 			const struct btf_var_secinfo *v = btf_var_secinfos(t);
-			struct btf_member *m = btf_members(t);
+			struct btf_member *m = (struct btf_member *) btf_members(t);
 			struct btf_type *vt;
 			char *name;
 
Signature · 1 line
2.21.0
author reviewer
#5 lore raw

PATCH 5/7 libbpf: Return const btf_param from btf_params inline function

In reply to Jiri Olsa

I'm getting following errors when compiling with -Wcast-qual:

bpf/btf.h: In function ‘btf_param* btf_params(const btf_type*)’:
bpf/btf.h:291:35: warning: cast from type ‘const btf_type*’ to type ‘btf_param*’ casts away qualifiers [-Wcast-qual]

    291 |  return (struct btf_param *)(t + 1);
        |                                   ^

The argument is const so the cast to following struct btf_param pointer should be const as well. Casting the const away in btf.c call where it's correctly used without const in deduplication code.

---
 tools/lib/bpf/btf.c | 6 +++---
 tools/lib/bpf/btf.h | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)
tools/lib/bpf/btf.c +3 -3
index 560d1ae33675..b5121c79fd9f 100644
@@ -1487,7 +1487,7 @@ static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
 			break;
 		}
 		case BTF_KIND_FUNC_PROTO: {
-			struct btf_param *m = btf_params(t);
+			struct btf_param *m = (struct btf_param *) btf_params(t);
 			__u16 vlen = btf_vlen(t);
 
 			for (j = 0; j < vlen; j++) {
@@ -2622,7 +2622,7 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
 		t->type = ref_type_id;
 
 		vlen = btf_vlen(t);
-		param = btf_params(t);
+		param = (struct btf_param *) btf_params(t);
 		for (i = 0; i < vlen; i++) {
 			ref_type_id = btf_dedup_ref_type(d, param->type);
 			if (ref_type_id < 0)
@@ -2811,7 +2811,7 @@ static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
 	}
 
 	case BTF_KIND_FUNC_PROTO: {
-		struct btf_param *param = btf_params(t);
+		struct btf_param *param = (struct btf_param *) btf_params(t);
 		__u16 vlen = btf_vlen(t);
 
 		r = btf_dedup_remap_type_id(d, t->type);
tools/lib/bpf/btf.h +2 -2
index cd1bd018ba8b..2817cf7ce2ee 100644
@@ -286,9 +286,9 @@ static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
 	return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
 }
 
-static inline struct btf_param *btf_params(const struct btf_type *t)
+static inline const struct btf_param *btf_params(const struct btf_type *t)
 {
-	return (struct btf_param *)(t + 1);
+	return (const struct btf_param *)(t + 1);
 }
 
 static inline struct btf_var *btf_var(const struct btf_type *t)
Signature · 1 line
2.21.0
author reviewer
#6 lore raw

PATCH 6/7 libbpf: Return const btf_var from btf_var inline function

In reply to Jiri Olsa

I'm getting following errors when compiling with -Wcast-qual:

bpf/btf.h: In function ‘btf_var* btf_var(const btf_type*)’:
bpf/btf.h:296:33: warning: cast from type ‘const btf_type*’ to type ‘btf_var*’ casts away qualifiers [-Wcast-qual]

    296 |  return (struct btf_var *)(t + 1);
        |                                 ^

The argument is const so the cast to following struct btf_var pointer should be const as well.

---
 tools/lib/bpf/btf.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/btf.h +2 -2
index 2817cf7ce2ee..480dbe780fa7 100644
@@ -291,9 +291,9 @@ static inline const struct btf_param *btf_params(const struct btf_type *t)
 	return (const struct btf_param *)(t + 1);
 }
 
-static inline struct btf_var *btf_var(const struct btf_type *t)
+static inline const struct btf_var *btf_var(const struct btf_type *t)
 {
-	return (struct btf_var *)(t + 1);
+	return (const struct btf_var *)(t + 1);
 }
 
 static inline struct btf_var_secinfo *
Signature · 1 line
2.21.0
author reviewer
#7 lore raw

PATCH 7/7 libbpf: Return const struct btf_var_secinfo from btf_var_secinfos inline function

In reply to Jiri Olsa

I'm getting following errors when compiling with -Wcast-qual:

bpf/btf.h: In function ‘btf_var_secinfo* btf_var_secinfos(const btf_type*)’:
bpf/btf.h:302:41: warning: cast from type ‘const btf_type*’ to type ‘btf_var_secinfo*’ casts away qualifiers [-Wcast-qual]

    302 |  return (struct btf_var_secinfo *)(t + 1);
        |                                         ^

The argument is const so the cast to following struct btf_var_secinfo pointer should be const as well. Casting the const away in btf.c call where it's correctly used without const in deduplication code.

---
 tools/lib/bpf/btf.c | 5 +++--
 tools/lib/bpf/btf.h | 4 ++--
 2 files changed, 5 insertions(+), 4 deletions(-)
tools/lib/bpf/btf.c +3 -2
index b5121c79fd9f..32527622792d 100644
@@ -526,7 +526,8 @@ static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
 
 	t->size = size;
 
-	for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
+	for (i = 0, vsi = (struct btf_var_secinfo *) btf_var_secinfos(t);
+	     i < vars; i++, vsi++) {
 		t_var = btf__type_by_id(btf, vsi->type);
 		var = btf_var(t_var);
 
@@ -2830,7 +2831,7 @@ static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
 	}
 
 	case BTF_KIND_DATASEC: {
-		struct btf_var_secinfo *var = btf_var_secinfos(t);
+		struct btf_var_secinfo *var = (struct btf_var_secinfo *) btf_var_secinfos(t);
 		__u16 vlen = btf_vlen(t);
 
 		for (i = 0; i < vlen; i++) {
tools/lib/bpf/btf.h +2 -2
index 480dbe780fa7..ecccde0988b1 100644
@@ -296,10 +296,10 @@ static inline const struct btf_var *btf_var(const struct btf_type *t)
 	return (const struct btf_var *)(t + 1);
 }
 
-static inline struct btf_var_secinfo *
+static inline const struct btf_var_secinfo *
 btf_var_secinfos(const struct btf_type *t)
 {
-	return (struct btf_var_secinfo *)(t + 1);
+	return (const struct btf_var_secinfo *)(t + 1);
 }
 
 #ifdef __cplusplus
Signature · 1 line
2.21.0
#8 lore raw
In reply to Jiri Olsa
Quoted 11 lines · Jiri Olsa

hi,
when including btf.h in bpftrace, I'm getting -Wcast-qual warnings like:

bpf/btf.h: In function ‘btf_var_secinfo* btf_var_secinfos(const btf_type*)’:
bpf/btf.h:302:41: warning: cast from type ‘const btf_type*’ to type ‘btf_var_secinfo*’ casts away qualifiers [-Wcast-qual]

     302 |  return (struct btf_var_secinfo *)(t + 1);
         |                                         ^

I changed the btf.h header to comply with -Wcast-qual checks and used const cast away casting in libbpf objects, where it's

Hey Jiri,

We made all those helper funcs return non-const structs intentionally to improve their usability and avoid all those casts that you added back.

Also, those helpers are now part of public API, so we can't just change them to const, as it can break existing users easily.

If there is a need to run with -Wcast-qual, we should probably disable those checks where appropriate in libbpf code.

So this will be a NACK from me, sorry.

Quoted 22 lines

all related to deduplication code, so I believe loosing const is fine there.

thanks,
jirka

---
Jiri Olsa (7):
libbpf: Use const cast for btf_int_* functions
libbpf: Return const btf_array from btf_array inline function
libbpf: Return const btf_enum from btf_enum inline function
libbpf: Return const btf_member from btf_members inline function
libbpf: Return const btf_param from btf_params inline function
libbpf: Return const btf_var from btf_var inline function
libbpf: Return const struct btf_var_secinfo from btf_var_secinfos inline function

  tools/lib/bpf/btf.c    | 21 +++++++++++----------
  tools/lib/bpf/btf.h    | 30 +++++++++++++++---------------
  tools/lib/bpf/libbpf.c |  2 +-
  3 files changed, 27 insertions(+), 26 deletions(-)
#9 lore raw
Quoted 19 lines · Andrii Nakryiko
Quoted 11 lines · Jiri Olsa

hi,
when including btf.h in bpftrace, I'm getting -Wcast-qual warnings like:

bpf/btf.h: In function ‘btf_var_secinfo* btf_var_secinfos(const btf_type*)’:
bpf/btf.h:302:41: warning: cast from type ‘const btf_type*’ to type ‘btf_var_secinfo*’ casts away qualifiers [-Wcast-qual]

     302 |  return (struct btf_var_secinfo *)(t + 1);
         |                                         ^

I changed the btf.h header to comply with -Wcast-qual checks and used const cast away casting in libbpf objects, where it's

Hey Jiri,

We made all those helper funcs return non-const structs intentionally to improve their usability and avoid all those casts that you added back.

Quoted 7 lines

Also, those helpers are now part of public API, so we can't just change them to const, as it can break existing users easily.

If there is a need to run with -Wcast-qual, we should probably disable those checks where appropriate in libbpf code.

So this will be a NACK from me, sorry.

Same opinion. This gcc warning is bogus.

#10 lore raw
In reply to Andrii Nakryiko
Quoted 18 lines · Andrii Nakryiko
Quoted 11 lines · Jiri Olsa

hi,
when including btf.h in bpftrace, I'm getting -Wcast-qual warnings like:

bpf/btf.h: In function ‘btf_var_secinfo* btf_var_secinfos(const btf_type*)’:
bpf/btf.h:302:41: warning: cast from type ‘const btf_type*’ to type ‘btf_var_secinfo*’ casts away qualifiers [-Wcast-qual]

     302 |  return (struct btf_var_secinfo *)(t + 1);
         |                                         ^

I changed the btf.h header to comply with -Wcast-qual checks and used const cast away casting in libbpf objects, where it's

Hey Jiri,

We made all those helper funcs return non-const structs intentionally to improve their usability and avoid all those casts that you added back.

Quoted 7 lines

Also, those helpers are now part of public API, so we can't just change them to const, as it can break existing users easily.

If there is a need to run with -Wcast-qual, we should probably disable those checks where appropriate in libbpf code.

So this will be a NACK from me, sorry.

ok, I'll disable disable it in bpftrace code then

thanks,
jirka

Quoted 24 lines
Quoted 23 lines

all related to deduplication code, so I believe loosing const is fine there.

thanks,
jirka

---
Jiri Olsa (7):
libbpf: Use const cast for btf_int_* functions
libbpf: Return const btf_array from btf_array inline function
libbpf: Return const btf_enum from btf_enum inline function
libbpf: Return const btf_member from btf_members inline function
libbpf: Return const btf_param from btf_params inline function
libbpf: Return const btf_var from btf_var inline function
libbpf: Return const struct btf_var_secinfo from btf_var_secinfos inline function

  tools/lib/bpf/btf.c    | 21 +++++++++++----------
  tools/lib/bpf/btf.h    | 30 +++++++++++++++---------------
  tools/lib/bpf/libbpf.c |  2 +-
  3 files changed, 27 insertions(+), 26 deletions(-)