Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-2.0-only
2 : : /*
3 : : * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
4 : : */
5 : :
6 : : #include "fixup.h"
7 : :
8 : : #include <errno.h>
9 : : #include <stdlib.h>
10 : :
11 : : #include <bpfilter/dump.h>
12 : : #include <bpfilter/helper.h>
13 : : #include <bpfilter/set.h>
14 : :
15 : 21642 : int bf_fixup_new(struct bf_fixup **fixup, enum bf_fixup_type type,
16 : : size_t insn_offset, const union bf_fixup_attr *attr)
17 : : {
18 : : assert(fixup);
19 : :
20 : 21642 : *fixup = calloc(1, sizeof(struct bf_fixup));
21 [ + - ]: 21642 : if (!*fixup)
22 : : return -ENOMEM;
23 : :
24 : 21642 : (*fixup)->type = type;
25 : 21642 : (*fixup)->insn = insn_offset;
26 : :
27 [ + + ]: 21642 : if (attr)
28 : 275 : (*fixup)->attr = *attr;
29 : :
30 : : return 0;
31 : : }
32 : :
33 : 43284 : void bf_fixup_free(struct bf_fixup **fixup)
34 : : {
35 : : assert(fixup);
36 : :
37 [ + + ]: 43284 : if (!*fixup)
38 : : return;
39 : :
40 : : freep((void *)fixup);
41 : : }
42 : :
43 : : static const char *_bf_fixup_type_to_str(enum bf_fixup_type type)
44 : : {
45 : : static const char *str[] = {
46 : : [BF_FIXUP_TYPE_JMP_NEXT_RULE] = "BF_FIXUP_TYPE_JMP_NEXT_RULE",
47 : : [BF_FIXUP_TYPE_COUNTERS_MAP_FD] = "BF_FIXUP_TYPE_COUNTERS_MAP_FD",
48 : : [BF_FIXUP_TYPE_PRINTER_MAP_FD] = "BF_FIXUP_TYPE_PRINTER_MAP_FD",
49 : : [BF_FIXUP_TYPE_SET_MAP_FD] = "BF_FIXUP_TYPE_SET_MAP_FD",
50 : : [BF_FIXUP_ELFSTUB_CALL] = "BF_FIXUP_ELFSTUB_CALL",
51 : : };
52 : :
53 : : assert(0 <= type && type < _BF_FIXUP_TYPE_MAX);
54 : : static_assert_enum_mapping(str, _BF_FIXUP_TYPE_MAX);
55 : :
56 : 0 : return str[type];
57 : : }
58 : :
59 : 0 : void bf_fixup_dump(const struct bf_fixup *fixup, prefix_t *prefix)
60 : : {
61 : : assert(fixup);
62 : : assert(prefix);
63 : :
64 [ # # ]: 0 : DUMP(prefix, "struct bf_fixup at %p", fixup);
65 : :
66 : 0 : bf_dump_prefix_push(prefix);
67 : :
68 [ # # ]: 0 : DUMP(prefix, "type: %s", _bf_fixup_type_to_str(fixup->type));
69 [ # # ]: 0 : DUMP(prefix, "insn: %zu", fixup->insn);
70 : :
71 [ # # # ]: 0 : switch (fixup->type) {
72 : : case BF_FIXUP_TYPE_JMP_NEXT_RULE:
73 : : case BF_FIXUP_TYPE_COUNTERS_MAP_FD:
74 : : case BF_FIXUP_TYPE_PRINTER_MAP_FD:
75 : : // No specific value to dump
76 : : break;
77 : 0 : case BF_FIXUP_TYPE_SET_MAP_FD:
78 [ # # # # : 0 : DUMP(prefix, "set: %s",
# # ]
79 : : fixup->attr.set_ptr ?
80 : : (fixup->attr.set_ptr->name ?: "<anonymous>") :
81 : : "(null)");
82 : : break;
83 : 0 : default:
84 [ # # ]: 0 : DUMP(prefix, "unsupported bf_fixup_type: %d", fixup->type);
85 : : break;
86 : : };
87 : :
88 : 0 : bf_dump_prefix_pop(prefix);
89 : 0 : }
|