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 : 23634 : 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 : 23634 : *fixup = calloc(1, sizeof(struct bf_fixup));
21 [ + - ]: 23634 : if (!*fixup)
22 : : return -ENOMEM;
23 : :
24 : 23634 : (*fixup)->type = type;
25 : 23634 : (*fixup)->insn = insn_offset;
26 : :
27 [ + + ]: 23634 : if (attr)
28 : 331 : (*fixup)->attr = *attr;
29 : :
30 : : return 0;
31 : : }
32 : :
33 : 47268 : void bf_fixup_free(struct bf_fixup **fixup)
34 : : {
35 : : assert(fixup);
36 : :
37 [ + + ]: 47268 : if (!*fixup)
38 : : return;
39 : :
40 : 23634 : BF_FREEP(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_LOG_MAP_FD] = "BF_FIXUP_TYPE_LOG_MAP_FD",
50 : : [BF_FIXUP_TYPE_STATE_MAP_FD] = "BF_FIXUP_TYPE_STATE_MAP_FD",
51 : : [BF_FIXUP_TYPE_SET_MAP_FD] = "BF_FIXUP_TYPE_SET_MAP_FD",
52 : : [BF_FIXUP_ELFSTUB_CALL] = "BF_FIXUP_ELFSTUB_CALL",
53 : : };
54 : :
55 : : assert(0 <= type && type < _BF_FIXUP_TYPE_MAX);
56 : : static_assert_enum_mapping(str, _BF_FIXUP_TYPE_MAX);
57 : :
58 : 0 : return str[type];
59 : : }
60 : :
61 : 0 : void bf_fixup_dump(const struct bf_fixup *fixup, prefix_t *prefix)
62 : : {
63 : : assert(fixup);
64 : : assert(prefix);
65 : :
66 [ # # ]: 0 : DUMP(prefix, "struct bf_fixup at %p", fixup);
67 : :
68 : 0 : bf_dump_prefix_push(prefix);
69 : :
70 [ # # ]: 0 : DUMP(prefix, "type: %s", _bf_fixup_type_to_str(fixup->type));
71 [ # # ]: 0 : DUMP(prefix, "insn: %zu", fixup->insn);
72 : :
73 [ # # # # ]: 0 : switch (fixup->type) {
74 : : case BF_FIXUP_TYPE_JMP_NEXT_RULE:
75 : : case BF_FIXUP_TYPE_COUNTERS_MAP_FD:
76 : : case BF_FIXUP_TYPE_PRINTER_MAP_FD:
77 : : case BF_FIXUP_TYPE_LOG_MAP_FD:
78 : : case BF_FIXUP_TYPE_STATE_MAP_FD:
79 : : // No specific value to dump
80 : : break;
81 : 0 : case BF_FIXUP_TYPE_SET_MAP_FD:
82 [ # # # # : 0 : DUMP(prefix, "set: %s",
# # ]
83 : : fixup->attr.set_ptr ?
84 : : (fixup->attr.set_ptr->name ?: "<anonymous>") :
85 : : "(null)");
86 : : break;
87 : 0 : case BF_FIXUP_ELFSTUB_CALL:
88 [ # # ]: 0 : DUMP(prefix, "elfstub_id: %d", fixup->attr.elfstub_id);
89 : : break;
90 : 0 : default:
91 [ # # ]: 0 : DUMP(prefix, "unsupported bf_fixup_type: %d", fixup->type);
92 : : break;
93 : : };
94 : :
95 : 0 : bf_dump_prefix_pop(prefix);
96 : 0 : }
|