Bug Summary

File:jdk/src/hotspot/share/compiler/disassembler.cpp
Warning:line 182, column 20
Access to field 'next' results in a dereference of a null pointer (loaded from field 'tail')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name disassembler.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mthread-model posix -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/libjvm/objs/precompiled -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D _GNU_SOURCE -D _REENTRANT -D LIBC=gnu -D LINUX -D VM_LITTLE_ENDIAN -D _LP64=1 -D ASSERT -D CHECK_UNHANDLED_OOPS -D TARGET_ARCH_x86 -D INCLUDE_SUFFIX_OS=_linux -D INCLUDE_SUFFIX_CPU=_x86 -D INCLUDE_SUFFIX_COMPILER=_gcc -D TARGET_COMPILER_gcc -D AMD64 -D HOTSPOT_LIB_ARCH="amd64" -D COMPILER1 -D COMPILER2 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc/adfiles -I /home/daniel/Projects/java/jdk/src/hotspot/share -I /home/daniel/Projects/java/jdk/src/hotspot/os/linux -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix -I /home/daniel/Projects/java/jdk/src/hotspot/cpu/x86 -I /home/daniel/Projects/java/jdk/src/hotspot/os_cpu/linux_x86 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc -I /home/daniel/Projects/java/jdk/src/hotspot/share/precompiled -I /home/daniel/Projects/java/jdk/src/hotspot/share/include -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix/include -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/modules_include/java.base -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/modules_include/java.base/linux -I /home/daniel/Projects/java/jdk/src/java.base/share/native/libjimage -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc/adfiles -I /home/daniel/Projects/java/jdk/src/hotspot/share -I /home/daniel/Projects/java/jdk/src/hotspot/os/linux -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix -I /home/daniel/Projects/java/jdk/src/hotspot/cpu/x86 -I /home/daniel/Projects/java/jdk/src/hotspot/os_cpu/linux_x86 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc -D _FORTIFY_SOURCE=2 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-format-zero-length -Wno-unused-parameter -Wno-unused -Wno-parentheses -Wno-comment -Wno-unknown-pragmas -Wno-address -Wno-delete-non-virtual-dtor -Wno-char-subscripts -Wno-array-bounds -Wno-int-in-bool-context -Wno-ignored-qualifiers -Wno-missing-field-initializers -Wno-implicit-fallthrough -Wno-empty-body -Wno-strict-overflow -Wno-sequence-point -Wno-maybe-uninitialized -Wno-misleading-indentation -Wno-cast-function-type -Wno-shift-negative-value -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /home/daniel/Projects/java/jdk/make/hotspot -ferror-limit 19 -fmessage-length 0 -fvisibility hidden -stack-protector 1 -fno-rtti -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /home/daniel/Projects/java/scan/2021-12-21-193737-8510-1 -x c++ /home/daniel/Projects/java/jdk/src/hotspot/share/compiler/disassembler.cpp
1/*
2 * Copyright (c) 2008, 2021, 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 */
24
25#include "precompiled.hpp"
26#include "asm/assembler.inline.hpp"
27#include "asm/macroAssembler.hpp"
28#include "ci/ciUtilities.hpp"
29#include "classfile/javaClasses.hpp"
30#include "code/codeCache.hpp"
31#include "compiler/disassembler.hpp"
32#include "gc/shared/cardTable.hpp"
33#include "gc/shared/cardTableBarrierSet.hpp"
34#include "gc/shared/collectedHeap.hpp"
35#include "memory/resourceArea.hpp"
36#include "memory/universe.hpp"
37#include "oops/oop.inline.hpp"
38#include "runtime/handles.inline.hpp"
39#include "runtime/os.hpp"
40#include "runtime/stubCodeGenerator.hpp"
41#include "runtime/stubRoutines.hpp"
42#include "utilities/resourceHash.hpp"
43
44void* Disassembler::_library = NULL__null;
45bool Disassembler::_tried_to_load_library = false;
46bool Disassembler::_library_usable = false;
47
48// This routine is in the shared library:
49Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL__null;
50
51static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH"amd64";
52static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";
53#define COMMENT_COLUMN52 +8 52 LP64_ONLY(+8)+8 /*could be an option*/
54#define BYTES_COMMENT";..." ";..." /* funky byte display comment */
55
56class decode_env {
57 private:
58 outputStream* _output; // where the disassembly is directed to
59 CodeBlob* _codeBlob; // != NULL only when decoding a CodeBlob
60 nmethod* _nm; // != NULL only when decoding a nmethod
61
62 address _start; // != NULL when decoding a range of unknown type
63 address _end; // != NULL when decoding a range of unknown type
64
65 char _option_buf[512];
66 char _print_raw;
67 address _cur_insn; // address of instruction currently being decoded
68 int _bytes_per_line; // arch-specific formatting option
69 int _pre_decode_alignment;
70 int _post_decode_alignment;
71 bool _print_file_name;
72 bool _print_help;
73 bool _helpPrinted;
74 static bool _optionsParsed;
75#ifndef PRODUCT
76 const AsmRemarks* _remarks; // Used with start/end range to provide code remarks.
77 ptrdiff_t _disp; // Adjustment to offset -> remark mapping.
78#endif
79
80 enum {
81 tabspacing = 8
82 };
83
84 // Check if the event matches the expected tag
85 // The tag must be a substring of the event, and
86 // the tag must be a token in the event, i.e. separated by delimiters
87 static bool match(const char* event, const char* tag) {
88 size_t eventlen = strlen(event);
89 size_t taglen = strlen(tag);
90 if (eventlen < taglen) // size mismatch
91 return false;
92 if (strncmp(event, tag, taglen) != 0) // string mismatch
93 return false;
94 char delim = event[taglen];
95 return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
96 }
97
98 // Merge new option string with previously recorded options
99 void collect_options(const char* p) {
100 if (p == NULL__null || p[0] == '\0') return;
101 size_t opt_so_far = strlen(_option_buf);
102 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return;
103 char* fillp = &_option_buf[opt_so_far];
104 if (opt_so_far > 0) *fillp++ = ',';
105 strcat(fillp, p);
106 // replace white space by commas:
107 char* q = fillp;
108 while ((q = strpbrk(q, " \t\n")) != NULL__null)
109 *q++ = ',';
110 }
111
112 void process_options(outputStream* ost);
113
114 void print_insn_labels();
115 void print_insn_prefix();
116 void print_address(address value);
117
118 // Properly initializes _start/_end. Overwritten too often if
119 // printing of instructions is called for each instruction.
120 void set_start(address s) { _start = s; }
121 void set_end (address e) { _end = e; }
122 void set_nm (nmethod* nm) { _nm = nm; }
123 void set_output(outputStream* st) { _output = st; }
124
125#if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
126 // The disassembler library (sometimes) uses tabs to nicely align the instruction operands.
127 // Depending on the mnemonic length and the column position where the
128 // mnemonic is printed, alignment may turn out to be not so nice.
129 // To improve, we assume 8-character tab spacing and left-align the mnemonic on a tab position.
130 // Instruction comments are aligned 4 tab positions to the right of the mnemonic.
131 void calculate_alignment() {
132 _pre_decode_alignment = ((output()->position()+tabspacing-1)/tabspacing)*tabspacing;
133 _post_decode_alignment = _pre_decode_alignment + 4*tabspacing;
134 }
135
136 void start_insn(address pc) {
137 _cur_insn = pc;
138 output()->bol();
139 print_insn_labels();
140 print_insn_prefix();
141 }
142
143 void end_insn(address pc) {
144 address pc0 = cur_insn();
145 outputStream* st = output();
146
147 if (AbstractDisassembler::show_comment()) {
148 if ((_nm != NULL__null) && _nm->has_code_comment(pc0, pc)) {
149 _nm->print_code_comment_on
150 (st,
151 _post_decode_alignment ? _post_decode_alignment : COMMENT_COLUMN52 +8,
152 pc0, pc);
153 // this calls reloc_string_for which calls oop::print_value_on
154 }
155 print_hook_comments(pc0, _nm != NULL__null);
156 }
157 Disassembler::annotate(pc0, output());
158 // follow each complete insn by a nice newline
159 st->bol();
160 }
161#endif
162
163 struct SourceFileInfo {
164 struct Link : public CHeapObj<mtCode> {
165 const char* file;
166 int line;
167 Link* next;
168 Link(const char* f, int l) : file(f), line(l), next(NULL__null) {}
169 };
170 Link *head, *tail;
171
172 void append(const char* file, int line) {
173 if (tail != NULL__null && tail->file == file && tail->line == line) {
4
Assuming field 'tail' is equal to NULL
5
Assuming pointer value is null
174 // Don't print duplicated lines at the same address. This could happen with C
175 // macros that end up having multiple "__" tokens on the same __LINE__.
176 return;
177 }
178 Link *link = new Link(file, line);
179 if (head == NULL__null) {
6
Assuming field 'head' is not equal to NULL
7
Taking false branch
180 head = tail = link;
181 } else {
182 tail->next = link;
8
Access to field 'next' results in a dereference of a null pointer (loaded from field 'tail')
183 tail = link;
184 }
185 }
186 SourceFileInfo(const char* file, int line) : head(NULL__null), tail(NULL__null) {
187 append(file, line);
188 }
189 };
190
191 typedef ResourceHashtable<
192 address, SourceFileInfo,
193 15889, // prime number
194 ResourceObj::C_HEAP> SourceFileInfoTable;
195
196 static SourceFileInfoTable* _src_table;
197 static const char* _cached_src;
198 static GrowableArray<const char*>* _cached_src_lines;
199
200 static SourceFileInfoTable& src_table() {
201 if (_src_table == NULL__null) {
202 _src_table = new (ResourceObj::C_HEAP, mtCode)SourceFileInfoTable();
203 }
204 return *_src_table;
205 }
206
207 public:
208 decode_env(CodeBlob* code, outputStream* output);
209 decode_env(nmethod* code, outputStream* output);
210 // Constructor for a 'decode_env' to decode an arbitrary
211 // piece of memory, hopefully containing code.
212 decode_env(address start, address end, outputStream* output
213 NOT_PRODUCT(COMMA const AsmRemarks* remarks = NULL COMMA ptrdiff_t disp = 0), const AsmRemarks* remarks = __null , ptrdiff_t disp = 0);
214
215 // Add 'original_start' argument which is the the original address
216 // the instructions were located at (if this is not equal to 'start').
217 address decode_instructions(address start, address end, address original_start = NULL__null);
218
219 address handle_event(const char* event, address arg);
220
221 outputStream* output() { return _output; }
222 address cur_insn() { return _cur_insn; }
223 const char* options() { return _option_buf; }
224 static void hook(const char* file, int line, address pc);
225 void print_hook_comments(address pc, bool newline);
226};
227
228bool decode_env::_optionsParsed = false;
229
230decode_env::SourceFileInfoTable* decode_env::_src_table = NULL__null;
231const char* decode_env::_cached_src = NULL__null;
232GrowableArray<const char*>* decode_env::_cached_src_lines = NULL__null;
233
234void decode_env::hook(const char* file, int line, address pc) {
235 // For simplication, we never free from this table. It's really not
236 // necessary as we add to the table only when PrintInterpreter is true,
237 // which means we are debugging the VM and a little bit of extra
238 // memory usage doesn't matter.
239 SourceFileInfo* found = src_table().get(pc);
240 if (found
1.1
'found' is not equal to NULL
!= NULL__null) {
2
Taking true branch
241 found->append(file, line);
3
Calling 'SourceFileInfo::append'
242 } else {
243 SourceFileInfo sfi(file, line);
244 src_table().put(pc, sfi); // sfi is copied by value
245 }
246}
247
248void decode_env::print_hook_comments(address pc, bool newline) {
249 SourceFileInfo* found = src_table().get(pc);
250 outputStream* st = output();
251 if (found != NULL__null) {
252 for (SourceFileInfo::Link *link = found->head; link; link = link->next) {
253 const char* file = link->file;
254 int line = link->line;
255 if (_cached_src == NULL__null || strcmp(_cached_src, file) != 0) {
256 FILE* fp;
257
258 // _cached_src_lines is a single cache of the lines of a source file, and we refill this cache
259 // every time we need to print a line from a different source file. It's not the fastest,
260 // but seems bearable.
261 if (_cached_src_lines != NULL__null) {
262 for (int i=0; i<_cached_src_lines->length(); i++) {
263 os::free((void*)_cached_src_lines->at(i));
264 }
265 _cached_src_lines->clear();
266 } else {
267 _cached_src_lines = new (ResourceObj::C_HEAP, mtCode)GrowableArray<const char*>(0, mtCode);
268 }
269
270 if ((fp = fopen(file, "r")) == NULL__null) {
271 _cached_src = NULL__null;
272 return;
273 }
274 _cached_src = file;
275
276 char line[500]; // don't write lines that are too long in your source files!
277 while (fgets(line, sizeof(line), fp) != NULL__null) {
278 size_t len = strlen(line);
279 if (len > 0 && line[len-1] == '\n') {
280 line[len-1] = '\0';
281 }
282 _cached_src_lines->append(os::strdup(line));
283 }
284 fclose(fp);
285 _print_file_name = true;
286 }
287
288 if (_print_file_name) {
289 // We print the file name whenever we switch to a new file, or when
290 // Disassembler::decode is called to disassemble a new block of code.
291 _print_file_name = false;
292 if (newline) {
293 st->cr();
294 }
295 st->move_to(COMMENT_COLUMN52 +8);
296 st->print(";;@FILE: %s", file);
297 newline = true;
298 }
299
300 int index = line - 1; // 1-based line number -> 0-based index.
301 if (index >= _cached_src_lines->length()) {
302 // This could happen if source file is mismatched.
303 } else {
304 const char* source_line = _cached_src_lines->at(index);
305 if (newline) {
306 st->cr();
307 }
308 st->move_to(COMMENT_COLUMN52 +8);
309 st->print(";;%5d: %s", line, source_line);
310 newline = true;
311 }
312 }
313 }
314}
315
316decode_env::decode_env(CodeBlob* code, outputStream* output) :
317 _output(output ? output : tty),
318 _codeBlob(code),
319 _nm(_codeBlob != NULL__null && _codeBlob->is_nmethod() ? (nmethod*) code : NULL__null),
320 _start(NULL__null),
321 _end(NULL__null),
322 _option_buf(),
323 _print_raw(0),
324 _cur_insn(NULL__null),
325 _bytes_per_line(0),
326 _pre_decode_alignment(0),
327 _post_decode_alignment(0),
328 _print_file_name(false),
329 _print_help(false),
330 _helpPrinted(false)
331 NOT_PRODUCT(COMMA _remarks(nullptr)), _remarks(nullptr)
332 NOT_PRODUCT(COMMA _disp(0)), _disp(0)
333{
334 memset(_option_buf, 0, sizeof(_option_buf));
335 process_options(_output);
336}
337
338decode_env::decode_env(nmethod* code, outputStream* output) :
339 _output(output ? output : tty),
340 _codeBlob(NULL__null),
341 _nm(code),
342 _start(_nm->code_begin()),
343 _end(_nm->code_end()),
344 _option_buf(),
345 _print_raw(0),
346 _cur_insn(NULL__null),
347 _bytes_per_line(0),
348 _pre_decode_alignment(0),
349 _post_decode_alignment(0),
350 _print_file_name(false),
351 _print_help(false),
352 _helpPrinted(false)
353 NOT_PRODUCT(COMMA _remarks(nullptr)), _remarks(nullptr)
354 NOT_PRODUCT(COMMA _disp(0)), _disp(0)
355{
356 memset(_option_buf, 0, sizeof(_option_buf));
357 process_options(_output);
358}
359
360// Constructor for a 'decode_env' to decode a memory range [start, end)
361// of unknown origin, assuming it contains code.
362decode_env::decode_env(address start, address end, outputStream* output
363 NOT_PRODUCT(COMMA const AsmRemarks* remarks COMMA ptrdiff_t disp), const AsmRemarks* remarks , ptrdiff_t disp) :
364 _output(output ? output : tty),
365 _codeBlob(NULL__null),
366 _nm(NULL__null),
367 _start(start),
368 _end(end),
369 _option_buf(),
370 _print_raw(0),
371 _cur_insn(NULL__null),
372 _bytes_per_line(0),
373 _pre_decode_alignment(0),
374 _post_decode_alignment(0),
375 _print_file_name(false),
376 _print_help(false),
377 _helpPrinted(false)
378 NOT_PRODUCT(COMMA _remarks(remarks)), _remarks(remarks)
379 NOT_PRODUCT(COMMA _disp(disp)), _disp(disp)
380{
381 assert(start < end, "Range must have a positive size, [" PTR_FORMAT ".." PTR_FORMAT ").", p2i(start), p2i(end))do { if (!(start < end)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/compiler/disassembler.cpp"
, 381, "assert(" "start < end" ") failed", "Range must have a positive size, ["
"0x%016" "l" "x" ".." "0x%016" "l" "x" ").", p2i(start), p2i
(end)); ::breakpoint(); } } while (0)
;
382 memset(_option_buf, 0, sizeof(_option_buf));
383 process_options(_output);
384}
385
386void decode_env::process_options(outputStream* ost) {
387 // by default, output pc but not bytes:
388 _print_help = false;
389 _bytes_per_line = Disassembler::pd_instruction_alignment();
390 _print_file_name = true;
391
392 // parse the global option string
393 // We need to fill the options buffer for each newly created
394 // decode_env instance. The hsdis_* library looks for options
395 // in that buffer.
396 collect_options(Disassembler::pd_cpu_opts());
397 collect_options(PrintAssemblyOptions);
398
399 if (strstr(options(), "print-raw")) {
400 _print_raw = (strstr(options(), "xml") ? 2 : 1);
401 }
402
403 if (_optionsParsed) return; // parse only once
404
405 if (strstr(options(), "help")) {
406 _print_help = true;
407 }
408 if (strstr(options(), "align-instr")) {
409 AbstractDisassembler::toggle_align_instr();
410 }
411 if (strstr(options(), "show-pc")) {
412 AbstractDisassembler::toggle_show_pc();
413 }
414 if (strstr(options(), "show-offset")) {
415 AbstractDisassembler::toggle_show_offset();
416 }
417 if (strstr(options(), "show-bytes")) {
418 AbstractDisassembler::toggle_show_bytes();
419 }
420 if (strstr(options(), "show-data-hex")) {
421 AbstractDisassembler::toggle_show_data_hex();
422 }
423 if (strstr(options(), "show-data-int")) {
424 AbstractDisassembler::toggle_show_data_int();
425 }
426 if (strstr(options(), "show-data-float")) {
427 AbstractDisassembler::toggle_show_data_float();
428 }
429 if (strstr(options(), "show-structs")) {
430 AbstractDisassembler::toggle_show_structs();
431 }
432 if (strstr(options(), "show-comment")) {
433 AbstractDisassembler::toggle_show_comment();
434 }
435 if (strstr(options(), "show-block-comment")) {
436 AbstractDisassembler::toggle_show_block_comment();
437 }
438 _optionsParsed = true;
439
440 if (_print_help && ! _helpPrinted) {
441 _helpPrinted = true;
442 ost->print_cr("PrintAssemblyOptions help:");
443 ost->print_cr(" print-raw test plugin by requesting raw output");
444 ost->print_cr(" print-raw-xml test plugin by requesting raw xml");
445 ost->cr();
446 ost->print_cr(" show-pc toggle printing current pc, currently %s", AbstractDisassembler::show_pc() ? "ON" : "OFF");
447 ost->print_cr(" show-offset toggle printing current offset, currently %s", AbstractDisassembler::show_offset() ? "ON" : "OFF");
448 ost->print_cr(" show-bytes toggle printing instruction bytes, currently %s", AbstractDisassembler::show_bytes() ? "ON" : "OFF");
449 ost->print_cr(" show-data-hex toggle formatting data as hex, currently %s", AbstractDisassembler::show_data_hex() ? "ON" : "OFF");
450 ost->print_cr(" show-data-int toggle formatting data as int, currently %s", AbstractDisassembler::show_data_int() ? "ON" : "OFF");
451 ost->print_cr(" show-data-float toggle formatting data as float, currently %s", AbstractDisassembler::show_data_float() ? "ON" : "OFF");
452 ost->print_cr(" show-structs toggle compiler data structures, currently %s", AbstractDisassembler::show_structs() ? "ON" : "OFF");
453 ost->print_cr(" show-comment toggle instruction comments, currently %s", AbstractDisassembler::show_comment() ? "ON" : "OFF");
454 ost->print_cr(" show-block-comment toggle block comments, currently %s", AbstractDisassembler::show_block_comment() ? "ON" : "OFF");
455 ost->print_cr(" align-instr toggle instruction alignment, currently %s", AbstractDisassembler::align_instr() ? "ON" : "OFF");
456 ost->print_cr("combined options: %s", options());
457 }
458}
459
460// Disassembly Event Handler.
461// This method receives events from the disassembler library hsdis
462// via event_to_env for each decoding step (installed by
463// Disassembler::decode_instructions(), replacing the default
464// callback method). This enables dumping additional info
465// and custom line formatting.
466// In a future extension, calling a custom decode method will be
467// supported. We can use such a method to decode instructions the
468// binutils decoder does not handle to our liking (suboptimal
469// formatting, incomplete information, ...).
470// Returns:
471// - NULL for all standard invocations. The function result is not
472// examined (as of now, 20190409) by the hsdis decoder loop.
473// - next for 'insn0' invocations.
474// next == arg: the custom decoder didn't do anything.
475// next > arg: the custom decoder did decode the instruction.
476// next points to the next undecoded instruction
477// (continuation point for decoder loop).
478//
479// "Normal" sequence of events:
480// insns - start of instruction stream decoding
481// mach - display architecture
482// format - display bytes-per-line
483// for each instruction:
484// insn - start of instruction decoding
485// insn0 - custom decoder invocation (if any)
486// addr - print address value
487// /insn - end of instruction decoding
488// /insns - premature end of instruction stream due to no progress
489//
490address decode_env::handle_event(const char* event, address arg) {
491
492#if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
493
494 //---< Event: end decoding loop (error, no progress) >---
495 if (decode_env::match(event, "/insns")) {
496 // Nothing to be done here.
497 return NULL__null;
498 }
499
500 //---< Event: start decoding loop >---
501 if (decode_env::match(event, "insns")) {
502 // Nothing to be done here.
503 return NULL__null;
504 }
505
506 //---< Event: finish decoding an instruction >---
507 if (decode_env::match(event, "/insn")) {
508 output()->fill_to(_post_decode_alignment);
509 end_insn(arg);
510 return NULL__null;
511 }
512
513 //---< Event: start decoding an instruction >---
514 if (decode_env::match(event, "insn")) {
515 start_insn(arg);
516 } else if (match(event, "/insn")) {
517 end_insn(arg);
518 } else if (match(event, "addr")) {
519 if (arg != NULL__null) {
520 print_address(arg);
521 return arg;
522 }
523 calculate_alignment();
524 output()->fill_to(_pre_decode_alignment);
525 return NULL__null;
526 }
527
528 //---< Event: call custom decoder (platform specific) >---
529 if (decode_env::match(event, "insn0")) {
530 return Disassembler::decode_instruction0(arg, output(), arg);
531 }
532
533 //---< Event: Print address >---
534 if (decode_env::match(event, "addr")) {
535 print_address(arg);
536 return arg;
537 }
538
539 //---< Event: mach (inform about machine architecture) >---
540 // This event is problematic because it messes up the output.
541 // The event is fired after the instruction address has already
542 // been printed. The decoded instruction (event "insn") is
543 // printed afterwards. That doesn't look nice.
544 if (decode_env::match(event, "mach")) {
545 guarantee(arg != NULL, "event_to_env - arg must not be NULL for event 'mach'")do { if (!(arg != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/compiler/disassembler.cpp"
, 545, "guarantee(" "arg != NULL" ") failed", "event_to_env - arg must not be NULL for event 'mach'"
); ::breakpoint(); } } while (0)
;
546 static char buffer[64] = { 0, };
547 // Output suppressed because it messes up disassembly.
548 // Only print this when the mach changes.
549 if (false && (strcmp(buffer, (const char*)arg) != 0 ||
550 strlen((const char*)arg) > sizeof(buffer) - 1)) {
551 // Only print this when the mach changes
552 strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
553 buffer[sizeof(buffer) - 1] = '\0';
554 output()->print_cr("[Disassembling for mach='%s']", (const char*)arg);
555 }
556 return NULL__null;
557 }
558
559 //---< Event: format bytes-per-line >---
560 if (decode_env::match(event, "format bytes-per-line")) {
561 _bytes_per_line = (int) (intptr_t) arg;
562 return NULL__null;
563 }
564#endif
565 return NULL__null;
566}
567
568static void* event_to_env(void* env_pv, const char* event, void* arg) {
569 decode_env* env = (decode_env*) env_pv;
570 return env->handle_event(event, (address) arg);
571}
572
573// called by the disassembler to print out jump targets and data addresses
574void decode_env::print_address(address adr) {
575 outputStream* st = output();
576
577 if (adr == NULL__null) {
578 st->print("NULL");
579 return;
580 }
581
582 int small_num = (int)(intptr_t)adr;
583 if ((intptr_t)adr == (intptr_t)small_num
584 && -1 <= small_num && small_num <= 9) {
585 st->print("%d", small_num);
586 return;
587 }
588
589 if (Universe::is_fully_initialized()) {
590 if (StubRoutines::contains(adr)) {
591 StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
592 if (desc == NULL__null) {
593 desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
594 }
595 if (desc != NULL__null) {
596 st->print("Stub::%s", desc->name());
597 if (desc->begin() != adr) {
598 st->print(INTX_FORMAT_W(+)"%" "+" "l" "d" " " PTR_FORMAT"0x%016" "l" "x", adr - desc->begin(), p2i(adr));
599 } else if (WizardMode) {
600 st->print(" " PTR_FORMAT"0x%016" "l" "x", p2i(adr));
601 }
602 return;
603 }
604 st->print("Stub::<unknown> " PTR_FORMAT"0x%016" "l" "x", p2i(adr));
605 return;
606 }
607
608 BarrierSet* bs = BarrierSet::barrier_set();
609 if (bs->is_a(BarrierSet::CardTableBarrierSet) &&
610 adr == ci_card_table_address_as<address>()) {
611 st->print("word_map_base");
612 if (WizardMode) st->print(" " INTPTR_FORMAT"0x%016" "l" "x", p2i(adr));
613 return;
614 }
615 }
616
617 if (_nm == NULL__null) {
618 // Don't do this for native methods, as the function name will be printed in
619 // nmethod::reloc_string_for().
620 // Allocate the buffer on the stack instead of as RESOURCE array.
621 // In case we do DecodeErrorFile, Thread will not be initialized,
622 // causing a "assert(current != __null) failed" failure.
623 const int buflen = 1024;
624 char buf[buflen];
625 int offset;
626 if (os::dll_address_to_function_name(adr, buf, buflen, &offset)) {
627 st->print(PTR_FORMAT"0x%016" "l" "x" " = %s", p2i(adr), buf);
628 if (offset != 0) {
629 st->print("+%d", offset);
630 }
631 return;
632 }
633 }
634
635 // Fall through to a simple (hexadecimal) numeral.
636 st->print(PTR_FORMAT"0x%016" "l" "x", p2i(adr));
637}
638
639void decode_env::print_insn_labels() {
640 if (AbstractDisassembler::show_block_comment()) {
641 address p = cur_insn();
642 outputStream* st = output();
643
644 //---< Block comments for nmethod >---
645 // Outputs a bol() before and a cr() after, but only if a comment is printed.
646 // Prints nmethod_section_label as well.
647 if (_nm != nullptr) {
648 _nm->print_block_comment(st, p);
649 }
650 else if (_codeBlob != nullptr) {
651 _codeBlob->print_block_comment(st, p);
652 }
653#ifndef PRODUCT
654 else if (_remarks != nullptr) {
655 _remarks->print((p - _start) + _disp, st);
656 }
657#endif
658 }
659}
660
661void decode_env::print_insn_prefix() {
662 address p = cur_insn();
663 outputStream* st = output();
664 AbstractDisassembler::print_location(p, _start, _end, st, false, false);
665 AbstractDisassembler::print_instruction(p, Assembler::instr_len(p), Assembler::instr_maxlen(), st, true, false);
666}
667
668ATTRIBUTE_PRINTF(2, 3)__attribute__((format(printf, 2, 3)))
669static int printf_to_env(void* env_pv, const char* format, ...) {
670 decode_env* env = (decode_env*) env_pv;
671 outputStream* st = env->output();
672 size_t flen = strlen(format);
673 const char* raw = NULL__null;
674 if (flen == 0) return 0;
675 if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
676 if (flen < 2 ||
677 strchr(format, '%') == NULL__null) {
678 raw = format;
679 } else if (format[0] == '%' && format[1] == '%' &&
680 strchr(format+2, '%') == NULL__null) {
681 // happens a lot on machines with names like %foo
682 flen--;
683 raw = format+1;
684 }
685 if (raw != NULL__null) {
686 st->print_raw(raw, flen);
687 return (int) flen;
688 }
689 va_list ap;
690 va_start(ap, format)__builtin_va_start(ap, format);
691 julong cnt0 = st->count();
692 st->vprint(format, ap);
693 julong cnt1 = st->count();
694 va_end(ap)__builtin_va_end(ap);
695 return (int)(cnt1 - cnt0);
696}
697
698// The 'original_start' argument holds the the original address where
699// the instructions were located in the originating system. If zero (NULL)
700// is passed in, there is no original address.
701address decode_env::decode_instructions(address start, address end, address original_start /* = 0*/) {
702 // CodeComment in Stubs.
703 // Properly initialize _start/_end. Overwritten too often if
704 // printing of instructions is called for each instruction.
705 assert((_start == NULL) || (start == NULL) || (_start == start), "don't overwrite CTOR values")do { if (!((_start == __null) || (start == __null) || (_start
== start))) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/compiler/disassembler.cpp"
, 705, "assert(" "(_start == __null) || (start == __null) || (_start == start)"
") failed", "don't overwrite CTOR values"); ::breakpoint(); }
} while (0)
;
706 assert((_end == NULL) || (end == NULL) || (_end == end ), "don't overwrite CTOR values")do { if (!((_end == __null) || (end == __null) || (_end == end
))) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/compiler/disassembler.cpp"
, 706, "assert(" "(_end == __null) || (end == __null) || (_end == end )"
") failed", "don't overwrite CTOR values"); ::breakpoint(); }
} while (0)
;
707 if (start != NULL__null) set_start(start);
708 if (end != NULL__null) set_end(end);
709 if (original_start == NULL__null) {
710 original_start = start;
711 }
712
713 //---< Check (and correct) alignment >---
714 // Don't check alignment of end, it is not aligned.
715 if (((uint64_t)start & ((uint64_t)Disassembler::pd_instruction_alignment() - 1)) != 0) {
716 output()->print_cr("Decode range start:" PTR_FORMAT"0x%016" "l" "x" ": ... (unaligned)", p2i(start));
717 start = (address)((uint64_t)start & ~((uint64_t)Disassembler::pd_instruction_alignment() - 1));
718 }
719
720 // Trying to decode instructions doesn't make sense if we
721 // couldn't load the disassembler library.
722 if (Disassembler::is_abstract()) {
723 return NULL__null;
724 }
725
726 // decode a series of instructions and return the end of the last instruction
727
728 if (_print_raw) {
729 // Print whatever the library wants to print, w/o fancy callbacks.
730 // This is mainly for debugging the library itself.
731 FILE* out = stdoutstdout;
732 FILE* xmlout = (_print_raw > 1 ? out : NULL__null);
733 return
734 (address)
735 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
736 start, end - start,
737 NULL__null, (void*) xmlout,
738 NULL__null, (void*) out,
739 options(), 0/*nice new line*/);
740 }
741
742 return
743 (address)
744 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
745 start, end - start,
746 &event_to_env, (void*) this,
747 &printf_to_env, (void*) this,
748 options(), 0/*nice new line*/);
749}
750
751// ----------------------------------------------------------------------------
752// Disassembler
753// Used as a static wrapper for decode_env.
754// Each method will create a decode_env before decoding.
755// You can call the decode_env methods directly if you already have one.
756
757void* Disassembler::dll_load(char* buf, int buflen, int offset, char* ebuf, int ebuflen, outputStream* st) {
758 int sz = buflen - offset;
759 int written = jio_snprintf(&buf[offset], sz, "%s%s", hsdis_library_name, os::dll_file_extension());
760 if (written < sz) { // written successfully, not truncated.
761 if (Verbose) st->print_cr("Trying to load: %s", buf);
762 return os::dll_load(buf, ebuf, ebuflen);
763 } else if (Verbose) {
764 st->print_cr("Try to load hsdis library failed: the length of path is beyond the OS limit");
765 }
766 return NULL__null;
767}
768
769bool Disassembler::load_library(outputStream* st) {
770 // Do not try to load multiple times. Failed once -> fails always.
771 // To force retry in debugger: assign _tried_to_load_library=0
772 if (_tried_to_load_library) {
773 return _library_usable;
774 }
775
776#if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
777 // Print to given stream, if any.
778 // Print to tty if Verbose is on and no stream given.
779 st = ((st == NULL__null) && Verbose) ? tty : st;
780
781 // Compute fully qualified library name.
782 char ebuf[1024];
783 char buf[JVM_MAXPATHLEN4096 + 1];
784 os::jvm_path(buf, sizeof(buf));
785 int jvm_offset = -1;
786 int lib_offset = -1;
787#ifdef STATIC_BUILD
788 char* p = strrchr(buf, '/');
789 *p = '\0';
790 strcat(p, "/lib/");
791 lib_offset = jvm_offset = strlen(buf);
792#else
793 {
794 // Match "libjvm" instead of "jvm" on *nix platforms. Creates better matches.
795 // Match "[lib]jvm[^/]*" in jvm_path.
796 const char* base = buf;
797 const char* p = strrchr(buf, *os::file_separator());
798 if (p != NULL__null) lib_offset = p - base + 1; // this points to the first char after separator
799#ifdef _WIN32
800 p = strstr(p ? p : base, "jvm");
801 if (p != NULL__null) jvm_offset = p - base; // this points to 'j' in jvm.
802#else
803 p = strstr(p ? p : base, "libjvm");
804 if (p != NULL__null) jvm_offset = p - base + 3; // this points to 'j' in libjvm.
805#endif
806 }
807#endif
808
809 // Find the disassembler shared library.
810 // Search for several paths derived from libjvm, in this order:
811 // 1. <home>/lib/<vm>/libhsdis-<arch>.so (for compatibility)
812 // 2. <home>/lib/<vm>/hsdis-<arch>.so
813 // 3. <home>/lib/hsdis-<arch>.so
814 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
815 if (jvm_offset >= 0) {
816 // 1. <home>/lib/<vm>/libhsdis-<arch>.so
817 _library = dll_load(buf, sizeof buf, jvm_offset, ebuf, sizeof ebuf, st);
818 if (_library == NULL__null && lib_offset >= 0) {
819 // 2. <home>/lib/<vm>/hsdis-<arch>.so
820 _library = dll_load(buf, sizeof buf, lib_offset, ebuf, sizeof ebuf, st);
821 }
822 if (_library == NULL__null && lib_offset > 0) {
823 // 3. <home>/lib/hsdis-<arch>.so
824 buf[lib_offset - 1] = '\0';
825 const char* p = strrchr(buf, *os::file_separator());
826 if (p != NULL__null) {
827 lib_offset = p - buf + 1;
828 _library = dll_load(buf, sizeof buf, lib_offset, ebuf, sizeof ebuf, st);
829 }
830 }
831 }
832 if (_library == NULL__null) {
833 _library = dll_load(buf, sizeof buf, 0, ebuf, sizeof ebuf, st);
834 }
835
836 // load the decoder function to use.
837 if (_library != NULL__null) {
838 _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual,(reinterpret_cast<Disassembler::decode_func_virtual>(os
::dll_lookup(_library, decode_instructions_virtual_name)))
839 os::dll_lookup(_library, decode_instructions_virtual_name))(reinterpret_cast<Disassembler::decode_func_virtual>(os
::dll_lookup(_library, decode_instructions_virtual_name)))
;
840 }
841 _tried_to_load_library = true;
842 _library_usable = _decode_instructions_virtual != NULL__null;
843
844 // Create a dummy environment to initialize PrintAssemblyOptions.
845 // The PrintAssemblyOptions must be known for abstract disassemblies as well.
846 decode_env dummy((unsigned char*)(&buf[0]), (unsigned char*)(&buf[1]), st);
847
848 // Report problems during dll_load or dll_lookup, if any.
849 if (st != NULL__null) {
850 // Success.
851 if (_library_usable) {
852 st->print_cr("Loaded disassembler from %s", buf);
853 } else {
854 st->print_cr("Could not load %s; %s; %s",
855 buf,
856 ((_library != NULL__null)
857 ? "entry point is missing"
858 : ((WizardMode || PrintMiscellaneous)
859 ? (const char*)ebuf
860 : "library not loadable")),
861 "PrintAssembly defaults to abstract disassembly.");
862 }
863 }
864#endif
865 return _library_usable;
866}
867
868
869// Directly disassemble code blob.
870void Disassembler::decode(CodeBlob* cb, outputStream* st) {
871#if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
872 if (cb->is_nmethod()) {
873 // If we have an nmethod at hand,
874 // call the specialized decoder directly.
875 ((nmethod*)cb)->decode2(st);
876 return;
877 }
878
879 decode_env env(cb, st);
880 env.output()->print_cr("--------------------------------------------------------------------------------");
881 env.output()->print("Decoding CodeBlob");
882 if (cb->name() != NULL__null) {
883 env.output()->print(", name: %s,", cb->name());
884 }
885 env.output()->print_cr(" at [" PTR_FORMAT"0x%016" "l" "x" ", " PTR_FORMAT"0x%016" "l" "x" "] " JLONG_FORMAT"%" "l" "d" " bytes", p2i(cb->code_begin()), p2i(cb->code_end()), ((jlong)(cb->code_end() - cb->code_begin())));
886
887 if (is_abstract()) {
888 AbstractDisassembler::decode_abstract(cb->code_begin(), cb->code_end(), env.output(), Assembler::instr_maxlen());
889 } else {
890 env.decode_instructions(cb->code_begin(), cb->code_end());
891 }
892 env.output()->print_cr("--------------------------------------------------------------------------------");
893#endif
894}
895
896// Decode a nmethod.
897// This includes printing the constant pool and all code segments.
898// The nmethod data structures (oop maps, relocations and the like) are not printed.
899void Disassembler::decode(nmethod* nm, outputStream* st) {
900#if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
901 ttyLocker ttyl;
902
903 decode_env env(nm, st);
904 env.output()->print_cr("--------------------------------------------------------------------------------");
905 nm->print_constant_pool(env.output());
906 env.output()->print_cr("--------------------------------------------------------------------------------");
907 env.output()->cr();
908 if (is_abstract()) {
909 AbstractDisassembler::decode_abstract(nm->code_begin(), nm->code_end(), env.output(), Assembler::instr_maxlen());
910 } else {
911 env.decode_instructions(nm->code_begin(), nm->code_end());
912 }
913 env.output()->print_cr("--------------------------------------------------------------------------------");
914#endif
915}
916
917// Decode a range, given as [start address, end address)
918void Disassembler::decode(address start, address end, outputStream* st
919 NOT_PRODUCT(COMMA const AsmRemarks* remarks COMMA ptrdiff_t disp), const AsmRemarks* remarks , ptrdiff_t disp) {
920#if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
921 //---< Test memory before decoding >---
922 if (!os::is_readable_range(start, end)) {
923 //---< Allow output suppression, but prevent writing to a NULL stream. Could happen with +PrintStubCode. >---
924 if (st != NULL__null) {
925 st->print("Memory range [" PTR_FORMAT"0x%016" "l" "x" ".." PTR_FORMAT"0x%016" "l" "x" "] not readable", p2i(start), p2i(end));
926 }
927 return;
928 }
929
930 if (is_abstract()) {
931 AbstractDisassembler::decode_abstract(start, end, st, Assembler::instr_maxlen());
932 } else {
933 // This seems to be just a chunk of memory.
934 decode_env env(start, end, st NOT_PRODUCT(COMMA remarks COMMA disp), remarks , disp);
935 env.output()->print_cr("--------------------------------------------------------------------------------");
936 env.decode_instructions(start, end);
937 env.output()->print_cr("--------------------------------------------------------------------------------");
938 }
939#endif
940}
941
942// To prevent excessive code expansion in the interpreter generator, we
943// do not inline this function into Disassembler::hook().
944void Disassembler::_hook(const char* file, int line, MacroAssembler* masm) {
945 decode_env::hook(file, line, masm->code_section()->end());
1
Calling 'decode_env::hook'
946}