mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
The macros XSK_BTF_READ_xxx doesn't handle or report on errors if the input or field were wrong, which is not uncommon for macros. The bad behavior is that the macro continue to dereference the pointer even-when xsk_btf__read_xxx() functions returns an error. This often leads to crashing the application. Change macro to only dereference when no errors were reported. Side-note: The compiler warnings will detect if API user didn't init the 'dest' value with a default value. As the effect of the change is that the 'dest' value will not be touch, and thus contain the value before the macro was used. Example: warning: ‘mark’ may be used uninitialized in this function Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
55 lines
1.8 KiB
C
55 lines
1.8 KiB
C
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
|
|
|
#ifndef __LIB_XSK_EXTEND_H
|
|
#define __LIB_XSK_EXTEND_H
|
|
|
|
//#define LIBBPF_API ""
|
|
|
|
LIBBPF_API int xsk_umem__btf_id(void *umem_data);
|
|
|
|
struct xsk_btf_info;
|
|
|
|
struct xsk_btf_member {
|
|
__u32 offset;
|
|
__u32 size;
|
|
};
|
|
|
|
LIBBPF_API int xsk_btf__init_xdp_hint(struct btf *btf_obj,
|
|
const char *xdp_hints_name,
|
|
struct xsk_btf_info **xbi);
|
|
|
|
LIBBPF_API void xsk_btf__free_xdp_hint(struct xsk_btf_info *xbi);
|
|
|
|
LIBBPF_API __u32 xsk_btf__btf_type_id(struct xsk_btf_info *xbi);
|
|
|
|
/* Reading a member via a field string, require walking BTF members
|
|
* and string comparing (strcmp). To reduce overhead, the API will
|
|
* cache xsk_btf_member entries in a hashmap. The field string must
|
|
* be a read-only constant for this to work, as the hashmap cache use
|
|
* the field pointer as the lookup key.
|
|
*/
|
|
LIBBPF_API int xsk_btf__read_field(void **dest, size_t size,
|
|
const char *field,
|
|
struct xsk_btf_info *xbi, const void *addr);
|
|
|
|
LIBBPF_API int xsk_btf__read(void **dest, size_t size,
|
|
struct xsk_btf_member *entry,
|
|
struct xsk_btf_info *xbi, const void *addr);
|
|
|
|
LIBBPF_API bool xsk_btf__has_field(const char *field, struct xsk_btf_info *xbi);
|
|
LIBBPF_API bool xsk_btf__field_member(const char *field, struct xsk_btf_info *xbi,
|
|
struct xsk_btf_member *entry);
|
|
|
|
/* Notice: that field must NOT be a C-string as macro will stringify it */
|
|
#define XSK_BTF_READ_FIELD_INTO(dest, field, xbi, addr) ({ \
|
|
typeof(dest) *_d; \
|
|
int _err=xsk_btf__read_field((void **)&_d, sizeof(dest), #field, xbi, addr); \
|
|
if (!_err) dest = *_d; })
|
|
|
|
#define XSK_BTF_READ_INTO(dest, member, xbi, addr) ({ \
|
|
typeof(dest) *_d; \
|
|
int _err=xsk_btf__read((void **)&_d, sizeof(dest), member, xbi, addr); \
|
|
if (!_err) dest = *_d; })
|
|
|
|
#endif /* __LIB_XSK_EXTEND_H */
|