Skip to content

Commit 21a56fc

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent c52a1e6 commit 21a56fc

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

tools/testing/selftests/bpf/jit_disasm_helpers.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616
*/
1717
#define MAX_LOCAL_LABELS 32
1818

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+
1924
static bool llvm_initialized;
2025

2126
struct local_labels {
2227
bool print_phase;
2328
__u32 prog_len;
2429
__u32 cnt;
2530
__u32 pcs[MAX_LOCAL_LABELS];
26-
char names[MAX_LOCAL_LABELS][4];
31+
char names[MAX_LOCAL_LABELS][LOCAL_LABEL_LEN];
2732
};
2833

2934
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)
118123
}
119124
qsort(labels.pcs, labels.cnt, sizeof(*labels.pcs), cmp_u32);
120125
for (i = 0; i < labels.cnt; ++i)
121-
/* use (i % 100) to avoid format truncation warning */
122-
snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i % 100);
126+
/* gcc is unable to infer upper bound for labels.cnt and assumes
127+
* it to be U32_MAX. U32_MAX takes 10 decimal digits.
128+
* snprintf below prints into labels.names[*],
129+
* which has space only for two digits and a letter.
130+
* To avoid truncation warning use (i % MAX_LOCAL_LABELS),
131+
* which informs gcc about printed value upper bound.
132+
*/
133+
snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i % MAX_LOCAL_LABELS);
123134

124135
/* now print with labels */
125136
labels.print_phase = true;

0 commit comments

Comments
 (0)