aboutsummaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/jit_disasm_helpers.c
diff options
context:
space:
mode:
authorEduard Zingerman <eddyz87@gmail.com>2024-08-23 01:06:44 -0700
committerAlexei Starovoitov <ast@kernel.org>2024-08-23 07:29:03 -0700
commit21a56fc503faae7589167fcd2771ab6dce36ab39 (patch)
tree0288f89c2540dda329dbd0297e215e6f5dd2af25 /tools/testing/selftests/bpf/jit_disasm_helpers.c
parentc52a1e6eb74ffe4f217e9b53ed75440a45b08c4c (diff)
selftests/bpf: #define LOCAL_LABEL_LEN for jit_disasm_helpers.c
Extract local label length as a #define directive and elaborate why 'i % MAX_LOCAL_LABELS' expression is needed for local labels array initialization. Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20240823080644.263943-4-eddyz87@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/testing/selftests/bpf/jit_disasm_helpers.c')
-rw-r--r--tools/testing/selftests/bpf/jit_disasm_helpers.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/tools/testing/selftests/bpf/jit_disasm_helpers.c b/tools/testing/selftests/bpf/jit_disasm_helpers.c
index 1b0f1fd267c0..febd6b12e372 100644
--- a/tools/testing/selftests/bpf/jit_disasm_helpers.c
+++ b/tools/testing/selftests/bpf/jit_disasm_helpers.c
@@ -16,6 +16,11 @@
*/
#define MAX_LOCAL_LABELS 32
+/* Local labels are encoded as 'L42', this requires 4 bytes of storage:
+ * 3 characters + zero byte
+ */
+#define LOCAL_LABEL_LEN 4
+
static bool llvm_initialized;
struct local_labels {
@@ -23,7 +28,7 @@ struct local_labels {
__u32 prog_len;
__u32 cnt;
__u32 pcs[MAX_LOCAL_LABELS];
- char names[MAX_LOCAL_LABELS][4];
+ char names[MAX_LOCAL_LABELS][LOCAL_LABEL_LEN];
};
static const char *lookup_symbol(void *data, uint64_t ref_value, uint64_t *ref_type,
@@ -118,8 +123,14 @@ static int disasm_one_func(FILE *text_out, uint8_t *image, __u32 len)
}
qsort(labels.pcs, labels.cnt, sizeof(*labels.pcs), cmp_u32);
for (i = 0; i < labels.cnt; ++i)
- /* use (i % 100) to avoid format truncation warning */
- snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i % 100);
+ /* gcc is unable to infer upper bound for labels.cnt and assumes
+ * it to be U32_MAX. U32_MAX takes 10 decimal digits.
+ * snprintf below prints into labels.names[*],
+ * which has space only for two digits and a letter.
+ * To avoid truncation warning use (i % MAX_LOCAL_LABELS),
+ * which informs gcc about printed value upper bound.
+ */
+ snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i % MAX_LOCAL_LABELS);
/* now print with labels */
labels.print_phase = true;