Skip to contentSkip to conversation

Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1

PATCH In review William Cohen 2 participants 3 messages Jul 10, 2018 – Jul 11, 2018 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1

Newer versions of GCC perform static analysis to determine whether string truncation is possible with functions such as snprintf and provide a warning if truncation could occur. The make for jvmti_agent.c uses the compiler option that treats any compiler warnings as compiler errors. For GCC-8.1.1 in Fedora 28 this causes the build to fail. The return value of the snprint is now checked to ensure snprintf produced a NULL-terminated string. If the string for the path is invalid, the code does attempt to use the string.

---
 tools/perf/jvmti/jvmti_agent.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/jvmti/jvmti_agent.c +5 -2
index 0c6d1002b524..30f14eafe4b3 100644
@@ -227,7 +227,7 @@ void *jvmti_open(void)
 {
 	char dump_path[PATH_MAX];
 	struct jitheader header;
-	int fd;
+	int retlen, fd;
 	FILE *fp;
 
 	init_arch_timestamp();
@@ -249,7 +249,10 @@ void *jvmti_open(void)
 	/*
 	 * jitdump file name
 	 */
-	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
+	retlen = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump",
+			  jit_path, getpid());
+	if (retlen <= 0 || ((int) sizeof(dump_path)) <= retlen)
+		return NULL;
 
 	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
 	if (fd == -1)
Signature · 1 line
2.17.1
#1 lore raw
Quoted 8 lines · William Cohen

Newer versions of GCC perform static analysis to determine whether string truncation is possible with functions such as snprintf and provide a warning if truncation could occur. The make for jvmti_agent.c uses the compiler option that treats any compiler warnings as compiler errors. For GCC-8.1.1 in Fedora 28 this causes the build to fail. The return value of the snprint is now checked to ensure snprintf produced a NULL-terminated string. If the string for the path is invalid, the code does attempt to use the string.

hi,
I posted fix for this recently:
https://lore.kernel.org/lkml/[email protected]/

it also covers the perf_regs.c, which was failing with gcc8 for me

should be pulled in soon

thanks,
jirka

Quoted 34 lines
---
 tools/perf/jvmti/jvmti_agent.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/jvmti/jvmti_agent.c +5 -2
index 0c6d1002b524..30f14eafe4b3 100644
@@ -227,7 +227,7 @@ void *jvmti_open(void)
 {
 	char dump_path[PATH_MAX];
 	struct jitheader header;
-	int fd;
+	int retlen, fd;
 	FILE *fp;
 
 	init_arch_timestamp();
@@ -249,7 +249,10 @@ void *jvmti_open(void)
 	/*
 	 * jitdump file name
 	 */
-	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
+	retlen = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump",
+			  jit_path, getpid());
+	if (retlen <= 0 || ((int) sizeof(dump_path)) <= retlen)
+		return NULL;
 
 	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
 	if (fd == -1)
Signature · 1 line
2.17.1
#2 lore raw
Quoted 14 lines · Jiri Olsa
Quoted 8 lines · William Cohen

Newer versions of GCC perform static analysis to determine whether string truncation is possible with functions such as snprintf and provide a warning if truncation could occur. The make for jvmti_agent.c uses the compiler option that treats any compiler warnings as compiler errors. For GCC-8.1.1 in Fedora 28 this causes the build to fail. The return value of the snprint is now checked to ensure snprintf produced a NULL-terminated string. If the string for the path is invalid, the code does attempt to use the string.

hi,
I posted fix for this recently:
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines

it also covers the perf_regs.c, which was failing with gcc8 for me

should be pulled in soon

thanks,
jirka

Hi Jirka,

Thanks. I hadn't seen the patch, and this failing to build for the past week has been bugging me. Glad to hear there is already a fix queued up.

-Will

Quoted 35 lines
Quoted 35 lines
---
 tools/perf/jvmti/jvmti_agent.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/jvmti/jvmti_agent.c +5 -2
index 0c6d1002b524..30f14eafe4b3 100644
@@ -227,7 +227,7 @@ void *jvmti_open(void)
 {
 	char dump_path[PATH_MAX];
 	struct jitheader header;
-	int fd;
+	int retlen, fd;
 	FILE *fp;
 
 	init_arch_timestamp();
@@ -249,7 +249,10 @@ void *jvmti_open(void)
 	/*
 	 * jitdump file name
 	 */
-	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
+	retlen = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump",
+			  jit_path, getpid());
+	if (retlen <= 0 || ((int) sizeof(dump_path)) <= retlen)
+		return NULL;
 
 	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
 	if (fd == -1)
Signature · 1 line
2.17.1