[PATCH v1 14/48] perf jvmti: Silence -Wshorten-64-to-32 warnings

From: Ian Rogers
Date: Tue Apr 01 2025 - 14:28:31 EST


The clang warning -Wshorten-64-to-32 can be useful to catch
inadvertent truncation. In some instances this truncation can lead to
changing the sign of a result, for example, truncation to return an
int to fit a sort routine. Silence the warning by making the implicit
truncation explicit.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/jvmti/jvmti_agent.c | 6 +++---
tools/perf/util/genelf.c | 2 +-
tools/perf/util/jitdump.c | 24 +++++++++++++-----------
3 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/tools/perf/jvmti/jvmti_agent.c b/tools/perf/jvmti/jvmti_agent.c
index 526dcaf9f079..2e2be9d8a118 100644
--- a/tools/perf/jvmti/jvmti_agent.c
+++ b/tools/perf/jvmti/jvmti_agent.c
@@ -377,7 +377,7 @@ jvmti_write_code(void *agent, char const *sym,
sym_len = strlen(sym) + 1;

rec.p.id = JIT_CODE_LOAD;
- rec.p.total_size = sizeof(rec) + sym_len;
+ rec.p.total_size = (uint32_t)(sizeof(rec) + sym_len);
rec.p.timestamp = perf_get_timestamp();

rec.code_size = size;
@@ -400,7 +400,7 @@ jvmti_write_code(void *agent, char const *sym,
*/
rec.code_index = code_generation++;

- ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
+ ret = (int)fwrite_unlocked(&rec, sizeof(rec), 1, fp);
fwrite_unlocked(sym, sym_len, 1, fp);

if (code)
@@ -454,7 +454,7 @@ jvmti_write_debug_info(void *agent, uint64_t code,
*/
size += nr_lines * sizeof(struct debug_entry);
size += flen;
- rec.p.total_size = size;
+ rec.p.total_size = (uint32_t)size;

/*
* If JVM is multi-threaded, multiple concurrent calls to agent
diff --git a/tools/perf/util/genelf.c b/tools/perf/util/genelf.c
index cdce7f173d00..6da8f3c53f7d 100644
--- a/tools/perf/util/genelf.c
+++ b/tools/perf/util/genelf.c
@@ -252,7 +252,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
Elf_Shdr *shdr;
uint64_t eh_frame_base_offset;
char *strsym = NULL;
- int symlen;
+ size_t symlen;
int retval = -1;

if (elf_version(EV_CURRENT) == EV_NONE) {
diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c
index 624964f01b5f..874ced77d0a4 100644
--- a/tools/perf/util/jitdump.c
+++ b/tools/perf/util/jitdump.c
@@ -160,7 +160,7 @@ jit_open(struct jit_buf_desc *jd, const char *name)
*/
flockfile(jd->in);

- ret = fread(buf, sizeof(header), 1, jd->in);
+ ret = (int)fread(buf, sizeof(header), 1, jd->in);
if (ret != 1)
goto error;

@@ -226,7 +226,7 @@ jit_open(struct jit_buf_desc *jd, const char *name)
bsz = bs;
buf = n;
/* read extra we do not know about */
- ret = fread(buf, bs - bsz, 1, jd->in);
+ ret = (int)fread(buf, bs - bsz, 1, jd->in);
if (ret != 1)
goto error;
}
@@ -274,7 +274,7 @@ jit_get_next_entry(struct jit_buf_desc *jd)
/*
* file is still locked at this point
*/
- ret = fread(prefix, sizeof(*prefix), 1, jd->in);
+ ret = (int)fread(prefix, sizeof(*prefix), 1, jd->in);
if (ret != 1)
return NULL;

@@ -304,7 +304,7 @@ jit_get_next_entry(struct jit_buf_desc *jd)

addr = ((void *)jd->buf) + sizeof(*prefix);

- ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
+ ret = (int)fread(addr, bs - sizeof(*prefix), 1, jd->in);
if (ret != 1)
return NULL;

@@ -398,7 +398,7 @@ static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
return timestamp;

tc.time_shift = time_conv->time_shift;
- tc.time_mult = time_conv->time_mult;
+ tc.time_mult = (unsigned int)time_conv->time_mult;
tc.time_zero = time_conv->time_zero;

/*
@@ -443,8 +443,8 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
nspid = jr->load.pid;
pid = jr_entry_pid(jd, jr);
tid = jr_entry_tid(jd, jr);
- csize = jr->load.code_size;
- usize = jd->unwinding_mapped_size;
+ csize = (int)jr->load.code_size;
+ usize = (int)jd->unwinding_mapped_size;
addr = jr->load.code_addr;
sym = (void *)((unsigned long)jr + sizeof(jr->load));
code = (unsigned long)jr + jr->load.p.total_size - csize;
@@ -465,8 +465,10 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)

size = PERF_ALIGN(size, sizeof(u64));
uaddr = (uintptr_t)code;
- ret = jit_emit_elf(jd, filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
- jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
+ ret = jit_emit_elf(jd, filename, sym, addr, (const void *)uaddr, csize, jd->debug_data,
+ (int)jd->nr_debug_entries, jd->unwinding_data,
+ (uint32_t)jd->eh_frame_hdr_size,
+ (uint32_t)jd->unwinding_size);

if (jd->debug_data && jd->nr_debug_entries) {
zfree(&jd->debug_data);
@@ -559,8 +561,8 @@ static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)

nspid = jr->load.pid;
pid = jr_entry_pid(jd, jr);
- tid = jr_entry_tid(jd, jr);
- usize = jd->unwinding_mapped_size;
+ tid = (int)jr_entry_tid(jd, jr);
+ usize = (int)jd->unwinding_mapped_size;
idr_size = jd->machine->id_hdr_size;

/*
--
2.49.0.504.g3bcea36a83-goog