rev 8113 : 8076475: Misuses of strncpy/strncat
Summary: Various small fixes around strncpy and strncat
Reviewed-by: dsamersoff
1 /*
2 * Copyright (c) 2008, 2013, 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 /* hsdis.c -- dump a range of addresses as native instructions
26 This implements the plugin protocol required by the
27 HotSpot PrintAssembly option.
28 */
29
30 #include <config.h> /* required by bfd.h */
31 #include <libiberty.h>
32 #include <bfd.h>
33 #include <dis-asm.h>
34 #include <inttypes.h>
35 #include <string.h>
36 #include <errno.h>
37 #include "hsdis.h"
38
39 #ifndef bool
40 #define bool int
41 #define true 1
42 #define false 0
43 #endif /*bool*/
44
45 /* short names for stuff in hsdis.h */
46 typedef decode_instructions_event_callback_ftype event_callback_t;
47 typedef decode_instructions_printf_callback_ftype printf_callback_t;
48
49 /* disassemble_info.application_data object */
50 struct hsdis_app_data {
51 /* virtual address of data */
52 uintptr_t start_va, end_va;
53 /* the instructions to be decoded */
54 unsigned char* buffer;
55 uintptr_t length;
56 event_callback_t event_callback; void* event_stream;
57 printf_callback_t printf_callback; void* printf_stream;
58 bool losing;
59 bool do_newline;
60
61 /* the architecture being disassembled */
62 const char* arch_name;
63 const bfd_arch_info_type* arch_info;
64
65 /* the disassembler we are going to use: */
66 disassembler_ftype dfn;
67 struct disassemble_info dinfo; /* the actual struct! */
68
69 char mach_option[64];
70 char insn_options[256];
71 };
72
73 static void* decode(struct hsdis_app_data* app_data, const char* options);
74
75 #define DECL_APP_DATA(dinfo) \
76 struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
77
78 #define DECL_EVENT_CALLBACK(app_data) \
79 event_callback_t event_callback = (app_data)->event_callback; \
80 void* event_stream = (app_data)->event_stream
81
82 #define DECL_PRINTF_CALLBACK(app_data) \
83 printf_callback_t printf_callback = (app_data)->printf_callback; \
84 void* printf_stream = (app_data)->printf_stream
85
86
87 static void print_help(struct hsdis_app_data* app_data,
88 const char* msg, const char* arg);
89 static void setup_app_data(struct hsdis_app_data* app_data,
90 const char* options);
91 static const char* format_insn_close(const char* close,
92 disassemble_info* dinfo,
93 char* buf, size_t bufsize);
94
95 void*
96 #ifdef DLL_ENTRY
97 DLL_ENTRY
98 #endif
99 decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
100 unsigned char* buffer, uintptr_t length,
101 event_callback_t event_callback_arg, void* event_stream_arg,
102 printf_callback_t printf_callback_arg, void* printf_stream_arg,
103 const char* options, int newline) {
104 struct hsdis_app_data app_data;
105 memset(&app_data, 0, sizeof(app_data));
106 app_data.start_va = start_va;
107 app_data.end_va = end_va;
108 app_data.buffer = buffer;
109 app_data.length = length;
110 app_data.event_callback = event_callback_arg;
111 app_data.event_stream = event_stream_arg;
112 app_data.printf_callback = printf_callback_arg;
113 app_data.printf_stream = printf_stream_arg;
114 app_data.do_newline = newline == 0 ? false : true;
115
116 return decode(&app_data, options);
117 }
118
119 /* This is the compatability interface for older version of hotspot */
120 void*
121 #ifdef DLL_ENTRY
122 DLL_ENTRY
123 #endif
124 decode_instructions(void* start_pv, void* end_pv,
125 event_callback_t event_callback_arg, void* event_stream_arg,
126 printf_callback_t printf_callback_arg, void* printf_stream_arg,
127 const char* options) {
128 decode_instructions_virtual((uintptr_t)start_pv,
129 (uintptr_t)end_pv,
130 (unsigned char*)start_pv,
131 (uintptr_t)end_pv - (uintptr_t)start_pv,
132 event_callback_arg,
133 event_stream_arg,
134 printf_callback_arg,
135 printf_stream_arg,
136 options, false);
137 }
138
139 static void* decode(struct hsdis_app_data* app_data, const char* options) {
140 setup_app_data(app_data, options);
141 char buf[128];
142
143 {
144 /* now reload everything from app_data: */
145 DECL_EVENT_CALLBACK(app_data);
146 DECL_PRINTF_CALLBACK(app_data);
147 uintptr_t start = app_data->start_va;
148 uintptr_t end = app_data->end_va;
149 uintptr_t p = start;
150
151 (*event_callback)(event_stream, "insns", (void*)start);
152
153 (*event_callback)(event_stream, "mach name='%s'",
154 (void*) app_data->arch_info->printable_name);
155 if (app_data->dinfo.bytes_per_line != 0) {
156 (*event_callback)(event_stream, "format bytes-per-line='%p'/",
157 (void*)(intptr_t) app_data->dinfo.bytes_per_line);
158 }
159
160 while (p < end && !app_data->losing) {
161 (*event_callback)(event_stream, "insn", (void*) p);
162
163 /* reset certain state, so we can read it with confidence */
164 app_data->dinfo.insn_info_valid = 0;
165 app_data->dinfo.branch_delay_insns = 0;
166 app_data->dinfo.data_size = 0;
167 app_data->dinfo.insn_type = 0;
168
169 int size = (*app_data->dfn)((bfd_vma) p, &app_data->dinfo);
170
171 if (size > 0) p += size;
172 else app_data->losing = true;
173
174 if (!app_data->losing) {
175 const char* insn_close = format_insn_close("/insn", &app_data->dinfo,
176 buf, sizeof(buf));
177 (*event_callback)(event_stream, insn_close, (void*) p);
178
179 if (app_data->do_newline) {
180 /* follow each complete insn by a nice newline */
181 (*printf_callback)(printf_stream, "\n");
182 }
183 }
184 }
185
186 if (app_data->losing) (*event_callback)(event_stream, "/insns", (void*) p);
187 return (void*) p;
188 }
189 }
190
191 /* take the address of the function, for luck, and also test the typedef: */
192 const decode_func_vtype decode_func_virtual_address = &decode_instructions_virtual;
193 const decode_func_stype decode_func_address = &decode_instructions;
194
195 static const char* format_insn_close(const char* close,
196 disassemble_info* dinfo,
197 char* buf, size_t bufsize) {
198 if (!dinfo->insn_info_valid)
199 return close;
200 enum dis_insn_type itype = dinfo->insn_type;
201 int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
202 if ((itype == dis_nonbranch && (dsize | delays) == 0)
203 || (strlen(close) + 3*20 > bufsize))
204 return close;
205
206 const char* type = "unknown";
207 switch (itype) {
208 case dis_nonbranch: type = NULL; break;
209 case dis_branch: type = "branch"; break;
210 case dis_condbranch: type = "condbranch"; break;
211 case dis_jsr: type = "jsr"; break;
212 case dis_condjsr: type = "condjsr"; break;
213 case dis_dref: type = "dref"; break;
214 case dis_dref2: type = "dref2"; break;
215 }
216
217 strcpy(buf, close);
218 char* p = buf;
219 if (type) sprintf(p += strlen(p), " type='%s'", type);
220 if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
221 if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
222 return buf;
223 }
224
225 /* handler functions */
226
227 static int
228 hsdis_read_memory_func(bfd_vma memaddr,
229 bfd_byte* myaddr,
230 unsigned int length,
231 struct disassemble_info* dinfo) {
232 DECL_APP_DATA(dinfo);
233 /* convert the virtual address memaddr into an address within memory buffer */
234 uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va;
235 if (offset + length > app_data->length) {
236 /* read is out of bounds */
237 return EIO;
238 } else {
239 memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length);
240 return 0;
241 }
242 }
243
244 static void
245 hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
246 /* the actual value to print: */
247 void* addr_value = (void*) (uintptr_t) vma;
248 DECL_APP_DATA(dinfo);
249 DECL_EVENT_CALLBACK(app_data);
250
251 /* issue the event: */
252 void* result =
253 (*event_callback)(event_stream, "addr/", addr_value);
254 if (result == NULL) {
255 /* event declined */
256 generic_print_address(vma, dinfo);
257 }
258 }
259
260
261 /* configuration */
262
263 static void set_optional_callbacks(struct hsdis_app_data* app_data);
264 static void parse_caller_options(struct hsdis_app_data* app_data,
265 const char* caller_options);
266 static const char* native_arch_name();
267 static enum bfd_endian native_endian();
268 static const bfd_arch_info_type* find_arch_info(const char* arch_nane);
269 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
270 /* to avoid malloc: */
271 bfd* empty_bfd, bfd_target* empty_xvec);
272 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
273 void *stream,
274 fprintf_ftype fprintf_func,
275 bfd* bfd,
276 char* disassembler_options);
277 static void parse_fake_insn(disassembler_ftype dfn,
278 struct disassemble_info* dinfo);
279
280 static void setup_app_data(struct hsdis_app_data* app_data,
281 const char* caller_options) {
282 /* Make reasonable defaults for null callbacks.
283 A non-null stream for a null callback is assumed to be a FILE* for output.
284 Events are rendered as XML.
285 */
286 set_optional_callbacks(app_data);
287
288 /* Look into caller_options for anything interesting. */
289 if (caller_options != NULL)
290 parse_caller_options(app_data, caller_options);
291
292 /* Discover which architecture we are going to disassemble. */
293 app_data->arch_name = &app_data->mach_option[0];
294 if (app_data->arch_name[0] == '\0')
295 app_data->arch_name = native_arch_name();
296 app_data->arch_info = find_arch_info(app_data->arch_name);
297
298 /* Make a fake bfd to hold the arch. and byteorder info. */
299 struct {
300 bfd_target empty_xvec;
301 bfd empty_bfd;
302 } buf;
303 bfd* native_bfd = get_native_bfd(app_data->arch_info,
304 /* to avoid malloc: */
305 &buf.empty_bfd, &buf.empty_xvec);
306 init_disassemble_info_from_bfd(&app_data->dinfo,
307 app_data->printf_stream,
308 app_data->printf_callback,
309 native_bfd,
310 /* On PowerPC we get warnings, if we pass empty options */
311 (caller_options == NULL) ? NULL : app_data->insn_options);
312
313 /* Finish linking together the various callback blocks. */
314 app_data->dinfo.application_data = (void*) app_data;
315 app_data->dfn = disassembler(native_bfd);
316 app_data->dinfo.print_address_func = hsdis_print_address_func;
317 app_data->dinfo.read_memory_func = hsdis_read_memory_func;
318
319 if (app_data->dfn == NULL) {
320 const char* bad = app_data->arch_name;
321 static bool complained;
322 if (bad == &app_data->mach_option[0])
323 print_help(app_data, "bad mach=%s", bad);
324 else if (!complained)
325 print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad);
326 complained = true;
327 /* must bail out */
328 app_data->losing = true;
329 return;
330 }
331
332 parse_fake_insn(app_data->dfn, &app_data->dinfo);
333 }
334
335
336 /* ignore all events, return a null */
337 static void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) {
338 return NULL;
339 }
340
341 /* print all events as XML markup */
342 static void* xml_event_callback(void* stream, const char* event, void* arg) {
343 FILE* fp = (FILE*) stream;
344 #define NS_PFX "dis:"
345 if (event[0] != '/') {
346 /* issue the tag, with or without a formatted argument */
347 fprintf(fp, "<"NS_PFX);
348 fprintf(fp, event, arg);
349 fprintf(fp, ">");
350 } else {
351 ++event; /* skip slash */
352 const char* argp = strchr(event, ' ');
353 if (argp == NULL) {
354 /* no arguments; just issue the closing tag */
355 fprintf(fp, "</"NS_PFX"%s>", event);
356 } else {
357 /* split out the closing attributes as <dis:foo_done attr='val'/> */
358 int event_prefix = (argp - event);
359 fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event);
360 fprintf(fp, argp, arg);
361 fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event);
362 }
363 }
364 return NULL;
365 }
366
367 static void set_optional_callbacks(struct hsdis_app_data* app_data) {
368 if (app_data->printf_callback == NULL) {
369 int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf;
370 FILE* fprintf_stream = stdout;
371 app_data->printf_callback = (printf_callback_t) fprintf_callback;
372 if (app_data->printf_stream == NULL)
373 app_data->printf_stream = (void*) fprintf_stream;
374 }
375 if (app_data->event_callback == NULL) {
376 if (app_data->event_stream == NULL)
377 app_data->event_callback = &null_event_callback;
378 else
379 app_data->event_callback = &xml_event_callback;
380 }
381
382 }
383
384 static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
385 char* iop_base = app_data->insn_options;
386 char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
387 char* iop = iop_base;
388 const char* p;
389 for (p = caller_options; p != NULL; ) {
390 const char* q = strchr(p, ',');
391 size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
392 if (plen == 4 && strncmp(p, "help", plen) == 0) {
393 print_help(app_data, NULL, NULL);
394 } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
395 char* mach_option = app_data->mach_option;
396 size_t mach_size = sizeof(app_data->mach_option);
397 mach_size -= 1; /*leave room for the null*/
398 if (plen > mach_size) plen = mach_size;
399 strncpy(mach_option, p, plen);
400 mach_option[plen] = '\0';
401 } else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) {
402 // do not pass these to the next level
403 } else {
404 /* just copy it; {i386,sparc}-dis.c might like to see it */
405 if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
406 if (iop + plen > iop_limit)
407 plen = iop_limit - iop;
408 strncpy(iop, p, plen);
409 iop += plen;
410 }
411 p = q;
412 }
413 *iop = '\0';
414 }
415
416 static void print_help(struct hsdis_app_data* app_data,
417 const char* msg, const char* arg) {
418 DECL_PRINTF_CALLBACK(app_data);
419 if (msg != NULL) {
420 (*printf_callback)(printf_stream, "hsdis: ");
421 (*printf_callback)(printf_stream, msg, arg);
422 (*printf_callback)(printf_stream, "\n");
423 }
424 (*printf_callback)(printf_stream, "hsdis output options:\n");
425 if (printf_callback == (printf_callback_t) &fprintf)
426 disassembler_usage((FILE*) printf_stream);
427 else
428 disassembler_usage(stderr); /* better than nothing */
429 (*printf_callback)(printf_stream, " mach=<arch> select disassembly mode\n");
430 #if defined(LIBARCH_i386) || defined(LIBARCH_amd64)
431 (*printf_callback)(printf_stream, " mach=i386 select 32-bit mode\n");
432 (*printf_callback)(printf_stream, " mach=x86-64 select 64-bit mode\n");
433 (*printf_callback)(printf_stream, " suffix always print instruction suffix\n");
434 #endif
435 (*printf_callback)(printf_stream, " help print this message\n");
436 }
437
438
439 /* low-level bfd and arch stuff that binutils doesn't do for us */
440
441 static const bfd_arch_info_type* find_arch_info(const char* arch_name) {
442 const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name);
443 if (arch_info == NULL) {
444 extern const bfd_arch_info_type bfd_default_arch_struct;
445 arch_info = &bfd_default_arch_struct;
446 }
447 return arch_info;
448 }
449
450 static const char* native_arch_name() {
451 const char* res = NULL;
452 #ifdef LIBARCH_i386
453 res = "i386";
454 #endif
455 #ifdef LIBARCH_amd64
456 res = "i386:x86-64";
457 #endif
458 #ifdef LIBARCH_sparc
459 res = "sparc:v8plusb";
460 #endif
461 #ifdef LIBARCH_sparcv9
462 res = "sparc:v9b";
463 #endif
464 #ifdef LIBARCH_ppc64
465 res = "powerpc:common64";
466 #endif
467 #ifdef LIBARCH_aarch64
468 res = "aarch64";
469 #endif
470 if (res == NULL)
471 res = "architecture not set in Makefile!";
472 return res;
473 }
474
475 static enum bfd_endian native_endian() {
476 int32_t endian_test = 'x';
477 if (*(const char*) &endian_test == 'x')
478 return BFD_ENDIAN_LITTLE;
479 else
480 return BFD_ENDIAN_BIG;
481 }
482
483 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
484 bfd* empty_bfd, bfd_target* empty_xvec) {
485 memset(empty_bfd, 0, sizeof(*empty_bfd));
486 memset(empty_xvec, 0, sizeof(*empty_xvec));
487 empty_xvec->flavour = bfd_target_unknown_flavour;
488 empty_xvec->byteorder = native_endian();
489 empty_bfd->xvec = empty_xvec;
490 empty_bfd->arch_info = arch_info;
491 return empty_bfd;
492 }
493
494 static int read_zero_data_only(bfd_vma ignore_p,
495 bfd_byte* myaddr, unsigned int length,
496 struct disassemble_info *ignore_info) {
497 memset(myaddr, 0, length);
498 return 0;
499 }
500 static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
501 return 0;
502 }
503
504 /* Prime the pump by running the selected disassembler on a null input.
505 This forces the machine-specific disassembler to divulge invariant
506 information like bytes_per_line.
507 */
508 static void parse_fake_insn(disassembler_ftype dfn,
509 struct disassemble_info* dinfo) {
510 typedef int (*read_memory_ftype)
511 (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
512 struct disassemble_info *info);
513 read_memory_ftype read_memory_func = dinfo->read_memory_func;
514 fprintf_ftype fprintf_func = dinfo->fprintf_func;
515
516 dinfo->read_memory_func = &read_zero_data_only;
517 dinfo->fprintf_func = &print_to_dev_null;
518 (*dfn)(0, dinfo);
519
520 /* put it back */
521 dinfo->read_memory_func = read_memory_func;
522 dinfo->fprintf_func = fprintf_func;
523 }
524
525 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
526 void *stream,
527 fprintf_ftype fprintf_func,
528 bfd* abfd,
529 char* disassembler_options) {
530 init_disassemble_info(dinfo, stream, fprintf_func);
531
532 dinfo->flavour = bfd_get_flavour(abfd);
533 dinfo->arch = bfd_get_arch(abfd);
534 dinfo->mach = bfd_get_mach(abfd);
535 dinfo->disassembler_options = disassembler_options;
536 dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
537 dinfo->skip_zeroes = sizeof(void*) * 2;
538 dinfo->skip_zeroes_at_end = sizeof(void*)-1;
539 dinfo->disassembler_needs_relocs = FALSE;
540
541 if (bfd_big_endian(abfd))
542 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG;
543 else if (bfd_little_endian(abfd))
544 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE;
545 else
546 dinfo->endian = native_endian();
547
548 disassemble_init_for_target(dinfo);
549 }
--- EOF ---