Skip to content

Commit 55b4de5

Browse files
qmonnetAlexei Starovoitov
authored andcommitted
bpftool: Remove asserts from JIT disassembler
The JIT disassembler in bpftool is the only components (with the JSON writer) using asserts to check the return values of functions. But it does not do so in a consistent way, and diasm_print_insn() returns no value, although sometimes the operation failed. Remove the asserts, and instead check the return values, print messages on errors, and propagate the error to the caller from prog.c. Remove the inclusion of assert.h from jit_disasm.c, and also from map.c where it is unused. Signed-off-by: Quentin Monnet <[email protected]> Tested-by: Niklas Söderlund <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b3d84af commit 55b4de5

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

tools/bpf/bpftool/jit_disasm.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <stdarg.h>
1919
#include <stdint.h>
2020
#include <stdlib.h>
21-
#include <assert.h>
2221
#include <unistd.h>
2322
#include <string.h>
2423
#include <bfd.h>
@@ -31,14 +30,18 @@
3130
#include "json_writer.h"
3231
#include "main.h"
3332

34-
static void get_exec_path(char *tpath, size_t size)
33+
static int get_exec_path(char *tpath, size_t size)
3534
{
3635
const char *path = "/proc/self/exe";
3736
ssize_t len;
3837

3938
len = readlink(path, tpath, size - 1);
40-
assert(len > 0);
39+
if (len <= 0)
40+
return -1;
41+
4142
tpath[len] = 0;
43+
44+
return 0;
4245
}
4346

4447
static int oper_count;
@@ -99,30 +102,39 @@ static int fprintf_json_styled(void *out,
99102
return r;
100103
}
101104

102-
void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
103-
const char *arch, const char *disassembler_options,
104-
const struct btf *btf,
105-
const struct bpf_prog_linfo *prog_linfo,
106-
__u64 func_ksym, unsigned int func_idx,
107-
bool linum)
105+
int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
106+
const char *arch, const char *disassembler_options,
107+
const struct btf *btf,
108+
const struct bpf_prog_linfo *prog_linfo,
109+
__u64 func_ksym, unsigned int func_idx,
110+
bool linum)
108111
{
109112
const struct bpf_line_info *linfo = NULL;
110113
disassembler_ftype disassemble;
114+
int count, i, pc = 0, err = -1;
111115
struct disassemble_info info;
112116
unsigned int nr_skip = 0;
113-
int count, i, pc = 0;
114117
char tpath[PATH_MAX];
115118
bfd *bfdf;
116119

117120
if (!len)
118-
return;
121+
return -1;
119122

120123
memset(tpath, 0, sizeof(tpath));
121-
get_exec_path(tpath, sizeof(tpath));
124+
if (get_exec_path(tpath, sizeof(tpath))) {
125+
p_err("failed to create disasembler (get_exec_path)");
126+
return -1;
127+
}
122128

123129
bfdf = bfd_openr(tpath, NULL);
124-
assert(bfdf);
125-
assert(bfd_check_format(bfdf, bfd_object));
130+
if (!bfdf) {
131+
p_err("failed to create disassembler (bfd_openr)");
132+
return -1;
133+
}
134+
if (!bfd_check_format(bfdf, bfd_object)) {
135+
p_err("failed to create disassembler (bfd_check_format)");
136+
goto exit_close;
137+
}
126138

127139
if (json_output)
128140
init_disassemble_info_compat(&info, stdout,
@@ -141,7 +153,7 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
141153
bfdf->arch_info = inf;
142154
} else {
143155
p_err("No libbfd support for %s", arch);
144-
return;
156+
goto exit_close;
145157
}
146158
}
147159

@@ -162,7 +174,10 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
162174
#else
163175
disassemble = disassembler(bfdf);
164176
#endif
165-
assert(disassemble);
177+
if (!disassemble) {
178+
p_err("failed to create disassembler");
179+
goto exit_close;
180+
}
166181

167182
if (json_output)
168183
jsonw_start_array(json_wtr);
@@ -226,7 +241,11 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
226241
if (json_output)
227242
jsonw_end_array(json_wtr);
228243

244+
err = 0;
245+
246+
exit_close:
229247
bfd_close(bfdf);
248+
return err;
230249
}
231250

232251
int disasm_init(void)

tools/bpf/bpftool/main.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,23 @@ int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
173173

174174
struct bpf_prog_linfo;
175175
#ifdef HAVE_LIBBFD_SUPPORT
176-
void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
177-
const char *arch, const char *disassembler_options,
178-
const struct btf *btf,
179-
const struct bpf_prog_linfo *prog_linfo,
180-
__u64 func_ksym, unsigned int func_idx,
181-
bool linum);
176+
int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
177+
const char *arch, const char *disassembler_options,
178+
const struct btf *btf,
179+
const struct bpf_prog_linfo *prog_linfo,
180+
__u64 func_ksym, unsigned int func_idx,
181+
bool linum);
182182
int disasm_init(void);
183183
#else
184184
static inline
185-
void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
186-
const char *arch, const char *disassembler_options,
187-
const struct btf *btf,
188-
const struct bpf_prog_linfo *prog_linfo,
189-
__u64 func_ksym, unsigned int func_idx,
190-
bool linum)
185+
int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
186+
const char *arch, const char *disassembler_options,
187+
const struct btf *btf,
188+
const struct bpf_prog_linfo *prog_linfo,
189+
__u64 func_ksym, unsigned int func_idx,
190+
bool linum)
191191
{
192+
return 0;
192193
}
193194
static inline int disasm_init(void)
194195
{

tools/bpf/bpftool/map.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
22
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
33

4-
#include <assert.h>
54
#include <errno.h>
65
#include <fcntl.h>
76
#include <linux/err.h>

tools/bpf/bpftool/prog.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,11 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
822822
printf("%s:\n", sym_name);
823823
}
824824

825-
disasm_print_insn(img, lens[i], opcodes,
826-
name, disasm_opt, btf,
827-
prog_linfo, ksyms[i], i,
828-
linum);
825+
if (disasm_print_insn(img, lens[i], opcodes,
826+
name, disasm_opt, btf,
827+
prog_linfo, ksyms[i], i,
828+
linum))
829+
goto exit_free;
829830

830831
img += lens[i];
831832

@@ -838,8 +839,10 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
838839
if (json_output)
839840
jsonw_end_array(json_wtr);
840841
} else {
841-
disasm_print_insn(buf, member_len, opcodes, name,
842-
disasm_opt, btf, NULL, 0, 0, false);
842+
if (disasm_print_insn(buf, member_len, opcodes, name,
843+
disasm_opt, btf, NULL, 0, 0,
844+
false))
845+
goto exit_free;
843846
}
844847
} else if (visual) {
845848
if (json_output)

0 commit comments

Comments
 (0)