Commit fc88b3abe23fe3d4cb2e2b1f3f7036e7f68889fe

Authored by Gustavo Romero
Committed by Greg Kroah-Hartman
1 parent ef8d2a5d99

perf tools: Fix undefined symbol scnprintf in libperf-jvmti.so

[ Upstream commit 6ac2226229d931153331a93d90655a3de05b9290 ]

Currently jvmti agent can not be used because function scnprintf is not
present in the agent libperf-jvmti.so. As a result the JVM when using
such agent to record JITed code profiling information will fail on
looking up scnprintf:

  java: symbol lookup error: lib/libperf-jvmti.so: undefined symbol: scnprintf

This commit fixes that by reverting to the use of snprintf, that can be
looked up, instead of scnprintf, adding a proper check for the returned
value in order to print a better error message when the jitdump file
pathname is too long. Checking the returned value also helps to comply
with some recent gcc versions, like gcc8, which will fail due to
truncated writing checks related to the -Werror=format-truncation= flag.

Signed-off-by: Gustavo Romero <gromero@linux.vnet.ibm.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
LPU-Reference: 1541117601-18937-2-git-send-email-gromero@linux.vnet.ibm.com
Link: https://lkml.kernel.org/n/tip-mvpxxxy7wnzaj74cq75muw3f@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>

Showing 1 changed file with 38 additions and 11 deletions Side-by-side Diff

tools/perf/jvmti/jvmti_agent.c
... ... @@ -125,7 +125,7 @@
125 125 }
126 126  
127 127 static int
128   -debug_cache_init(void)
  128 +create_jit_cache_dir(void)
129 129 {
130 130 char str[32];
131 131 char *base, *p;
... ... @@ -144,8 +144,13 @@
144 144  
145 145 strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
146 146  
147   - snprintf(jit_path, PATH_MAX - 1, "%s/.debug/", base);
148   -
  147 + ret = snprintf(jit_path, PATH_MAX, "%s/.debug/", base);
  148 + if (ret >= PATH_MAX) {
  149 + warnx("jvmti: cannot generate jit cache dir because %s/.debug/"
  150 + " is too long, please check the cwd, JITDUMPDIR, and"
  151 + " HOME variables", base);
  152 + return -1;
  153 + }
149 154 ret = mkdir(jit_path, 0755);
150 155 if (ret == -1) {
151 156 if (errno != EEXIST) {
152 157  
153 158  
154 159  
... ... @@ -154,20 +159,32 @@
154 159 }
155 160 }
156 161  
157   - snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit", base);
  162 + ret = snprintf(jit_path, PATH_MAX, "%s/.debug/jit", base);
  163 + if (ret >= PATH_MAX) {
  164 + warnx("jvmti: cannot generate jit cache dir because"
  165 + " %s/.debug/jit is too long, please check the cwd,"
  166 + " JITDUMPDIR, and HOME variables", base);
  167 + return -1;
  168 + }
158 169 ret = mkdir(jit_path, 0755);
159 170 if (ret == -1) {
160 171 if (errno != EEXIST) {
161   - warn("cannot create jit cache dir %s", jit_path);
  172 + warn("jvmti: cannot create jit cache dir %s", jit_path);
162 173 return -1;
163 174 }
164 175 }
165 176  
166   - snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit/%s.XXXXXXXX", base, str);
167   -
  177 + ret = snprintf(jit_path, PATH_MAX, "%s/.debug/jit/%s.XXXXXXXX", base, str);
  178 + if (ret >= PATH_MAX) {
  179 + warnx("jvmti: cannot generate jit cache dir because"
  180 + " %s/.debug/jit/%s.XXXXXXXX is too long, please check"
  181 + " the cwd, JITDUMPDIR, and HOME variables",
  182 + base, str);
  183 + return -1;
  184 + }
168 185 p = mkdtemp(jit_path);
169 186 if (p != jit_path) {
170   - warn("cannot create jit cache dir %s", jit_path);
  187 + warn("jvmti: cannot create jit cache dir %s", jit_path);
171 188 return -1;
172 189 }
173 190  
... ... @@ -228,7 +245,7 @@
228 245 {
229 246 char dump_path[PATH_MAX];
230 247 struct jitheader header;
231   - int fd;
  248 + int fd, ret;
232 249 FILE *fp;
233 250  
234 251 init_arch_timestamp();
235 252  
... ... @@ -245,12 +262,22 @@
245 262  
246 263 memset(&header, 0, sizeof(header));
247 264  
248   - debug_cache_init();
  265 + /*
  266 + * jitdump file dir
  267 + */
  268 + if (create_jit_cache_dir() < 0)
  269 + return NULL;
249 270  
250 271 /*
251 272 * jitdump file name
252 273 */
253   - scnprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
  274 + ret = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
  275 + if (ret >= PATH_MAX) {
  276 + warnx("jvmti: cannot generate jitdump file full path because"
  277 + " %s/jit-%i.dump is too long, please check the cwd,"
  278 + " JITDUMPDIR, and HOME variables", jit_path, getpid());
  279 + return NULL;
  280 + }
254 281  
255 282 fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
256 283 if (fd == -1)