Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Eduard Zingerman and committed by
Alexei Starovoitov
21a56fc5 c52a1e6e

+14 -3
+14 -3
tools/testing/selftests/bpf/jit_disasm_helpers.c
··· 16 16 */ 17 17 #define MAX_LOCAL_LABELS 32 18 18 19 + /* Local labels are encoded as 'L42', this requires 4 bytes of storage: 20 + * 3 characters + zero byte 21 + */ 22 + #define LOCAL_LABEL_LEN 4 23 + 19 24 static bool llvm_initialized; 20 25 21 26 struct local_labels { ··· 28 23 __u32 prog_len; 29 24 __u32 cnt; 30 25 __u32 pcs[MAX_LOCAL_LABELS]; 31 - char names[MAX_LOCAL_LABELS][4]; 26 + char names[MAX_LOCAL_LABELS][LOCAL_LABEL_LEN]; 32 27 }; 33 28 34 29 static const char *lookup_symbol(void *data, uint64_t ref_value, uint64_t *ref_type, ··· 123 118 } 124 119 qsort(labels.pcs, labels.cnt, sizeof(*labels.pcs), cmp_u32); 125 120 for (i = 0; i < labels.cnt; ++i) 126 - /* use (i % 100) to avoid format truncation warning */ 127 - snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i % 100); 121 + /* gcc is unable to infer upper bound for labels.cnt and assumes 122 + * it to be U32_MAX. U32_MAX takes 10 decimal digits. 123 + * snprintf below prints into labels.names[*], 124 + * which has space only for two digits and a letter. 125 + * To avoid truncation warning use (i % MAX_LOCAL_LABELS), 126 + * which informs gcc about printed value upper bound. 127 + */ 128 + snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i % MAX_LOCAL_LABELS); 128 129 129 130 /* now print with labels */ 130 131 labels.print_phase = true;