Fri, 03 Apr 2020 14:14:26 +0100
8076475: Misuses of strncpy/strncat
Summary: Various small fixes around strncpy and strncat
Reviewed-by: andrew
1 /*
2 * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "code/codeCache.hpp"
28 #include "compiler/disassembler.hpp"
29 #include "gc_interface/collectedHeap.hpp"
30 #include "memory/cardTableModRefBS.hpp"
31 #include "runtime/fprofiler.hpp"
32 #include "runtime/handles.inline.hpp"
33 #include "runtime/stubCodeGenerator.hpp"
34 #include "runtime/stubRoutines.hpp"
35 #ifdef TARGET_ARCH_x86
36 # include "depChecker_x86.hpp"
37 #endif
38 #ifdef TARGET_ARCH_sparc
39 # include "depChecker_sparc.hpp"
40 #endif
41 #ifdef TARGET_ARCH_zero
42 # include "depChecker_zero.hpp"
43 #endif
44 #ifdef TARGET_ARCH_arm
45 # include "depChecker_arm.hpp"
46 #endif
47 #ifdef TARGET_ARCH_ppc
48 # include "depChecker_ppc.hpp"
49 #endif
50 #ifdef SHARK
51 #include "shark/sharkEntry.hpp"
52 #endif
54 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
56 void* Disassembler::_library = NULL;
57 bool Disassembler::_tried_to_load_library = false;
59 // This routine is in the shared library:
60 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL;
61 Disassembler::decode_func Disassembler::_decode_instructions = NULL;
63 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH;
64 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";
65 static const char decode_instructions_name[] = "decode_instructions";
66 static bool use_new_version = true;
67 #define COMMENT_COLUMN 40 LP64_ONLY(+8) /*could be an option*/
68 #define BYTES_COMMENT ";..." /* funky byte display comment */
70 bool Disassembler::load_library() {
71 if (_decode_instructions_virtual != NULL || _decode_instructions != NULL) {
72 // Already succeeded.
73 return true;
74 }
75 if (_tried_to_load_library) {
76 // Do not try twice.
77 // To force retry in debugger: assign _tried_to_load_library=0
78 return false;
79 }
80 // Try to load it.
81 char ebuf[1024];
82 char buf[JVM_MAXPATHLEN];
83 os::jvm_path(buf, sizeof(buf));
84 int jvm_offset = -1;
85 int lib_offset = -1;
86 {
87 // Match "jvm[^/]*" in jvm_path.
88 const char* base = buf;
89 const char* p = strrchr(buf, '/');
90 if (p != NULL) lib_offset = p - base + 1;
91 p = strstr(p ? p : base, "jvm");
92 if (p != NULL) jvm_offset = p - base;
93 }
94 // Find the disassembler shared library.
95 // Search for several paths derived from libjvm, in this order:
96 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so (for compatibility)
97 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
98 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
99 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
100 if (jvm_offset >= 0) {
101 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so
102 strcpy(&buf[jvm_offset], hsdis_library_name);
103 strcat(&buf[jvm_offset], os::dll_file_extension());
104 _library = os::dll_load(buf, ebuf, sizeof ebuf);
105 if (_library == NULL) {
106 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
107 strcpy(&buf[lib_offset], hsdis_library_name);
108 strcat(&buf[lib_offset], os::dll_file_extension());
109 _library = os::dll_load(buf, ebuf, sizeof ebuf);
110 }
111 if (_library == NULL) {
112 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
113 buf[lib_offset - 1] = '\0';
114 const char* p = strrchr(buf, '/');
115 if (p != NULL) {
116 lib_offset = p - buf + 1;
117 strcpy(&buf[lib_offset], hsdis_library_name);
118 strcat(&buf[lib_offset], os::dll_file_extension());
119 _library = os::dll_load(buf, ebuf, sizeof ebuf);
120 }
121 }
122 }
123 if (_library == NULL) {
124 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
125 strcpy(&buf[0], hsdis_library_name);
126 strcat(&buf[0], os::dll_file_extension());
127 _library = os::dll_load(buf, ebuf, sizeof ebuf);
128 }
129 if (_library != NULL) {
130 _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual,
131 os::dll_lookup(_library, decode_instructions_virtual_name));
132 }
133 if (_decode_instructions_virtual == NULL) {
134 // could not spot in new version, try old version
135 _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func,
136 os::dll_lookup(_library, decode_instructions_name));
137 use_new_version = false;
138 } else {
139 use_new_version = true;
140 }
141 _tried_to_load_library = true;
142 if (_decode_instructions_virtual == NULL && _decode_instructions == NULL) {
143 tty->print_cr("Could not load %s; %s; %s", buf,
144 ((_library != NULL)
145 ? "entry point is missing"
146 : (WizardMode || PrintMiscellaneous)
147 ? (const char*)ebuf
148 : "library not loadable"),
149 "PrintAssembly is disabled");
150 return false;
151 }
153 // Success.
154 tty->print_cr("Loaded disassembler from %s", buf);
155 return true;
156 }
159 class decode_env {
160 private:
161 nmethod* _nm;
162 CodeBlob* _code;
163 CodeStrings _strings;
164 outputStream* _output;
165 address _start, _end;
167 char _option_buf[512];
168 char _print_raw;
169 bool _print_pc;
170 bool _print_bytes;
171 address _cur_insn;
172 int _total_ticks;
173 int _bytes_per_line; // arch-specific formatting option
175 static bool match(const char* event, const char* tag) {
176 size_t taglen = strlen(tag);
177 if (strncmp(event, tag, taglen) != 0)
178 return false;
179 char delim = event[taglen];
180 return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
181 }
183 void collect_options(const char* p) {
184 if (p == NULL || p[0] == '\0') return;
185 size_t opt_so_far = strlen(_option_buf);
186 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return;
187 char* fillp = &_option_buf[opt_so_far];
188 if (opt_so_far > 0) *fillp++ = ',';
189 strcat(fillp, p);
190 // replace white space by commas:
191 char* q = fillp;
192 while ((q = strpbrk(q, " \t\n")) != NULL)
193 *q++ = ',';
194 // Note that multiple PrintAssemblyOptions flags accumulate with \n,
195 // which we want to be changed to a comma...
196 }
198 void print_insn_labels();
199 void print_insn_bytes(address pc0, address pc);
200 void print_address(address value);
202 public:
203 decode_env(CodeBlob* code, outputStream* output, CodeStrings c = CodeStrings());
205 address decode_instructions(address start, address end);
207 void start_insn(address pc) {
208 _cur_insn = pc;
209 output()->bol();
210 print_insn_labels();
211 }
213 void end_insn(address pc) {
214 address pc0 = cur_insn();
215 outputStream* st = output();
216 if (_print_bytes && pc > pc0)
217 print_insn_bytes(pc0, pc);
218 if (_nm != NULL) {
219 _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);
220 // this calls reloc_string_for which calls oop::print_value_on
221 }
223 // Output pc bucket ticks if we have any
224 if (total_ticks() != 0) {
225 address bucket_pc = FlatProfiler::bucket_start_for(pc);
226 if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) {
227 int bucket_count = FlatProfiler::bucket_count_for(pc0);
228 if (bucket_count != 0) {
229 st->bol();
230 st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count);
231 }
232 }
233 }
234 // follow each complete insn by a nice newline
235 st->cr();
236 }
238 address handle_event(const char* event, address arg);
240 outputStream* output() { return _output; }
241 address cur_insn() { return _cur_insn; }
242 int total_ticks() { return _total_ticks; }
243 void set_total_ticks(int n) { _total_ticks = n; }
244 const char* options() { return _option_buf; }
245 };
247 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) {
248 memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields.
249 _output = output ? output : tty;
250 _code = code;
251 if (code != NULL && code->is_nmethod())
252 _nm = (nmethod*) code;
253 _strings.copy(c);
255 // by default, output pc but not bytes:
256 _print_pc = true;
257 _print_bytes = false;
258 _bytes_per_line = Disassembler::pd_instruction_alignment();
260 // parse the global option string:
261 collect_options(Disassembler::pd_cpu_opts());
262 collect_options(PrintAssemblyOptions);
264 if (strstr(options(), "hsdis-")) {
265 if (strstr(options(), "hsdis-print-raw"))
266 _print_raw = (strstr(options(), "xml") ? 2 : 1);
267 if (strstr(options(), "hsdis-print-pc"))
268 _print_pc = !_print_pc;
269 if (strstr(options(), "hsdis-print-bytes"))
270 _print_bytes = !_print_bytes;
271 }
272 if (strstr(options(), "help")) {
273 tty->print_cr("PrintAssemblyOptions help:");
274 tty->print_cr(" hsdis-print-raw test plugin by requesting raw output");
275 tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml");
276 tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)");
277 tty->print_cr(" hsdis-print-bytes turn on instruction byte output");
278 tty->print_cr("combined options: %s", options());
279 }
280 }
282 address decode_env::handle_event(const char* event, address arg) {
283 if (match(event, "insn")) {
284 start_insn(arg);
285 } else if (match(event, "/insn")) {
286 end_insn(arg);
287 } else if (match(event, "addr")) {
288 if (arg != NULL) {
289 print_address(arg);
290 return arg;
291 }
292 } else if (match(event, "mach")) {
293 static char buffer[32] = { 0, };
294 if (strcmp(buffer, (const char*)arg) != 0 ||
295 strlen((const char*)arg) > sizeof(buffer) - 1) {
296 // Only print this when the mach changes
297 strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
298 buffer[sizeof(buffer) - 1] = '\0';
299 output()->print_cr("[Disassembling for mach='%s']", arg);
300 }
301 } else if (match(event, "format bytes-per-line")) {
302 _bytes_per_line = (int) (intptr_t) arg;
303 } else {
304 // ignore unrecognized markup
305 }
306 return NULL;
307 }
309 // called by the disassembler to print out jump targets and data addresses
310 void decode_env::print_address(address adr) {
311 outputStream* st = _output;
313 if (adr == NULL) {
314 st->print("NULL");
315 return;
316 }
318 int small_num = (int)(intptr_t)adr;
319 if ((intptr_t)adr == (intptr_t)small_num
320 && -1 <= small_num && small_num <= 9) {
321 st->print("%d", small_num);
322 return;
323 }
325 if (Universe::is_fully_initialized()) {
326 if (StubRoutines::contains(adr)) {
327 StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
328 if (desc == NULL)
329 desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
330 if (desc != NULL) {
331 st->print("Stub::%s", desc->name());
332 if (desc->begin() != adr)
333 st->print("%+d 0x%p",adr - desc->begin(), adr);
334 else if (WizardMode) st->print(" " PTR_FORMAT, adr);
335 return;
336 }
337 st->print("Stub::<unknown> " PTR_FORMAT, adr);
338 return;
339 }
341 BarrierSet* bs = Universe::heap()->barrier_set();
342 if (bs->kind() == BarrierSet::CardTableModRef &&
343 adr == (address)((CardTableModRefBS*)(bs))->byte_map_base) {
344 st->print("word_map_base");
345 if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr);
346 return;
347 }
349 oop obj;
350 if (_nm != NULL
351 && (obj = _nm->embeddedOop_at(cur_insn())) != NULL
352 && (address) obj == adr
353 && Universe::heap()->is_in(obj)
354 && Universe::heap()->is_in(obj->klass())) {
355 julong c = st->count();
356 obj->print_value_on(st);
357 if (st->count() == c) {
358 // No output. (Can happen in product builds.)
359 st->print("(a %s)", obj->klass()->external_name());
360 }
361 return;
362 }
363 }
365 // Fall through to a simple (hexadecimal) numeral.
366 st->print(PTR_FORMAT, adr);
367 }
369 void decode_env::print_insn_labels() {
370 address p = cur_insn();
371 outputStream* st = output();
372 CodeBlob* cb = _code;
373 if (cb != NULL) {
374 cb->print_block_comment(st, p);
375 }
376 _strings.print_block_comment(st, (intptr_t)(p - _start));
377 if (_print_pc) {
378 st->print(" " PTR_FORMAT ": ", p);
379 }
380 }
382 void decode_env::print_insn_bytes(address pc, address pc_limit) {
383 outputStream* st = output();
384 size_t incr = 1;
385 size_t perline = _bytes_per_line;
386 if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int)
387 && !((uintptr_t)pc % sizeof(int))
388 && !((uintptr_t)pc_limit % sizeof(int))) {
389 incr = sizeof(int);
390 if (perline % incr) perline += incr - (perline % incr);
391 }
392 while (pc < pc_limit) {
393 // tab to the desired column:
394 st->move_to(COMMENT_COLUMN);
395 address pc0 = pc;
396 address pc1 = pc + perline;
397 if (pc1 > pc_limit) pc1 = pc_limit;
398 for (; pc < pc1; pc += incr) {
399 if (pc == pc0)
400 st->print(BYTES_COMMENT);
401 else if ((uint)(pc - pc0) % sizeof(int) == 0)
402 st->print(" "); // put out a space on word boundaries
403 if (incr == sizeof(int))
404 st->print("%08lx", *(int*)pc);
405 else st->print("%02x", (*pc)&0xFF);
406 }
407 st->cr();
408 }
409 }
412 static void* event_to_env(void* env_pv, const char* event, void* arg) {
413 decode_env* env = (decode_env*) env_pv;
414 return env->handle_event(event, (address) arg);
415 }
417 ATTRIBUTE_PRINTF(2, 3)
418 static int printf_to_env(void* env_pv, const char* format, ...) {
419 decode_env* env = (decode_env*) env_pv;
420 outputStream* st = env->output();
421 size_t flen = strlen(format);
422 const char* raw = NULL;
423 if (flen == 0) return 0;
424 if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
425 if (flen < 2 ||
426 strchr(format, '%') == NULL) {
427 raw = format;
428 } else if (format[0] == '%' && format[1] == '%' &&
429 strchr(format+2, '%') == NULL) {
430 // happens a lot on machines with names like %foo
431 flen--;
432 raw = format+1;
433 }
434 if (raw != NULL) {
435 st->print_raw(raw, (int) flen);
436 return (int) flen;
437 }
438 va_list ap;
439 va_start(ap, format);
440 julong cnt0 = st->count();
441 st->vprint(format, ap);
442 julong cnt1 = st->count();
443 va_end(ap);
444 return (int)(cnt1 - cnt0);
445 }
447 address decode_env::decode_instructions(address start, address end) {
448 _start = start; _end = end;
450 assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr");
452 const int show_bytes = false; // for disassembler debugging
454 //_version = Disassembler::pd_cpu_version();
456 if (!Disassembler::can_decode()) {
457 return NULL;
458 }
460 // decode a series of instructions and return the end of the last instruction
462 if (_print_raw) {
463 // Print whatever the library wants to print, w/o fancy callbacks.
464 // This is mainly for debugging the library itself.
465 FILE* out = stdout;
466 FILE* xmlout = (_print_raw > 1 ? out : NULL);
467 return use_new_version ?
468 (address)
469 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
470 start, end - start,
471 NULL, (void*) xmlout,
472 NULL, (void*) out,
473 options(), 0/*nice new line*/)
474 :
475 (address)
476 (*Disassembler::_decode_instructions)(start, end,
477 NULL, (void*) xmlout,
478 NULL, (void*) out,
479 options());
480 }
482 return use_new_version ?
483 (address)
484 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
485 start, end - start,
486 &event_to_env, (void*) this,
487 &printf_to_env, (void*) this,
488 options(), 0/*nice new line*/)
489 :
490 (address)
491 (*Disassembler::_decode_instructions)(start, end,
492 &event_to_env, (void*) this,
493 &printf_to_env, (void*) this,
494 options());
495 }
498 void Disassembler::decode(CodeBlob* cb, outputStream* st) {
499 if (!load_library()) return;
500 decode_env env(cb, st);
501 env.output()->print_cr("Decoding CodeBlob " PTR_FORMAT, cb);
502 env.decode_instructions(cb->code_begin(), cb->code_end());
503 }
505 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c) {
506 if (!load_library()) return;
507 decode_env env(CodeCache::find_blob_unsafe(start), st, c);
508 env.decode_instructions(start, end);
509 }
511 void Disassembler::decode(nmethod* nm, outputStream* st) {
512 if (!load_library()) return;
513 decode_env env(nm, st);
514 env.output()->print_cr("Decoding compiled method " PTR_FORMAT ":", nm);
515 env.output()->print_cr("Code:");
517 #ifdef SHARK
518 SharkEntry* entry = (SharkEntry *) nm->code_begin();
519 unsigned char* p = entry->code_start();
520 unsigned char* end = entry->code_limit();
521 #else
522 unsigned char* p = nm->code_begin();
523 unsigned char* end = nm->code_end();
524 #endif // SHARK
526 // If there has been profiling, print the buckets.
527 if (FlatProfiler::bucket_start_for(p) != NULL) {
528 unsigned char* p1 = p;
529 int total_bucket_count = 0;
530 while (p1 < end) {
531 unsigned char* p0 = p1;
532 p1 += pd_instruction_alignment();
533 address bucket_pc = FlatProfiler::bucket_start_for(p1);
534 if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1)
535 total_bucket_count += FlatProfiler::bucket_count_for(p0);
536 }
537 env.set_total_ticks(total_bucket_count);
538 }
540 // Print constant table.
541 if (nm->consts_size() > 0) {
542 nm->print_nmethod_labels(env.output(), nm->consts_begin());
543 int offset = 0;
544 for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) {
545 if ((offset % 8) == 0) {
546 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, p, offset, *((int32_t*) p), *((int64_t*) p));
547 } else {
548 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT, p, offset, *((int32_t*) p));
549 }
550 }
551 }
553 env.decode_instructions(p, end);
554 }