Fri, 01 Feb 2019 10:47:30 +0100
8145096: Undefined behaviour in HotSpot
Summary: Fix some integer overflows
Reviewed-by: jrose, kvn, kbarrett, adinn, iklam
1 /*
2 * Copyright (c) 1999, 2018, 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 "utilities/globalDefinitions.hpp"
26 #include "prims/jvm.h"
27 #include "runtime/frame.inline.hpp"
28 #include "runtime/os.hpp"
29 #include "utilities/vmError.hpp"
31 #include <signal.h>
32 #include <unistd.h>
33 #include <sys/resource.h>
34 #include <sys/utsname.h>
35 #include <pthread.h>
36 #include <signal.h>
38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
40 // Todo: provide a os::get_max_process_id() or similar. Number of processes
41 // may have been configured, can be read more accurately from proc fs etc.
42 #ifndef MAX_PID
43 #define MAX_PID INT_MAX
44 #endif
45 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID)
47 // Check core dump limit and report possible place where core can be found
48 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) {
49 int n;
50 struct rlimit rlim;
51 bool success;
53 n = get_core_path(buffer, bufferSize);
55 if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
56 jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (may not exist)", current_process_id());
57 success = true;
58 } else {
59 switch(rlim.rlim_cur) {
60 case RLIM_INFINITY:
61 jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d", current_process_id());
62 success = true;
63 break;
64 case 0:
65 jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
66 success = false;
67 break;
68 default:
69 jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (max size %lu kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", current_process_id(), (unsigned long)(rlim.rlim_cur >> 10));
70 success = true;
71 break;
72 }
73 }
74 VMError::report_coredump_status(buffer, success);
75 }
77 int os::get_native_stack(address* stack, int frames, int toSkip) {
78 #ifdef _NMT_NOINLINE_
79 toSkip++;
80 #endif
82 int frame_idx = 0;
83 int num_of_frames; // number of frames captured
84 frame fr = os::current_frame();
85 while (fr.pc() && frame_idx < frames) {
86 if (toSkip > 0) {
87 toSkip --;
88 } else {
89 stack[frame_idx ++] = fr.pc();
90 }
91 if (fr.fp() == NULL || os::is_first_C_frame(&fr)
92 ||fr.sender_pc() == NULL || fr.cb() != NULL) break;
94 if (fr.sender_pc() && !os::is_first_C_frame(&fr)) {
95 fr = os::get_sender_for_C_frame(&fr);
96 } else {
97 break;
98 }
99 }
100 num_of_frames = frame_idx;
101 for (; frame_idx < frames; frame_idx ++) {
102 stack[frame_idx] = NULL;
103 }
105 return num_of_frames;
106 }
109 bool os::unsetenv(const char* name) {
110 assert(name != NULL, "Null pointer");
111 return (::unsetenv(name) == 0);
112 }
114 int os::get_last_error() {
115 return errno;
116 }
118 bool os::is_debugger_attached() {
119 // not implemented
120 return false;
121 }
123 void os::wait_for_keypress_at_exit(void) {
124 // don't do anything on posix platforms
125 return;
126 }
128 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED,
129 // so on posix, unmap the section at the start and at the end of the chunk that we mapped
130 // rather than unmapping and remapping the whole chunk to get requested alignment.
131 char* os::reserve_memory_aligned(size_t size, size_t alignment) {
132 assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
133 "Alignment must be a multiple of allocation granularity (page size)");
134 assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
136 size_t extra_size = size + alignment;
137 assert(extra_size >= size, "overflow, size is too large to allow alignment");
139 char* extra_base = os::reserve_memory(extra_size, NULL, alignment);
141 if (extra_base == NULL) {
142 return NULL;
143 }
145 // Do manual alignment
146 char* aligned_base = (char*) align_size_up((uintptr_t) extra_base, alignment);
148 // [ | | ]
149 // ^ extra_base
150 // ^ extra_base + begin_offset == aligned_base
151 // extra_base + begin_offset + size ^
152 // extra_base + extra_size ^
153 // |<>| == begin_offset
154 // end_offset == |<>|
155 size_t begin_offset = aligned_base - extra_base;
156 size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
158 if (begin_offset > 0) {
159 os::release_memory(extra_base, begin_offset);
160 }
162 if (end_offset > 0) {
163 os::release_memory(extra_base + begin_offset + size, end_offset);
164 }
166 return aligned_base;
167 }
169 int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {
170 int result = ::vsnprintf(buf, len, fmt, args);
171 // If an encoding error occurred (result < 0) then it's not clear
172 // whether the buffer is NUL terminated, so ensure it is.
173 if ((result < 0) && (len > 0)) {
174 buf[len - 1] = '\0';
175 }
176 return result;
177 }
179 void os::Posix::print_load_average(outputStream* st) {
180 st->print("load average:");
181 double loadavg[3];
182 os::loadavg(loadavg, 3);
183 st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
184 st->cr();
185 }
187 void os::Posix::print_rlimit_info(outputStream* st) {
188 st->print("rlimit:");
189 struct rlimit rlim;
191 st->print(" STACK ");
192 getrlimit(RLIMIT_STACK, &rlim);
193 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
194 else st->print("%uk", rlim.rlim_cur >> 10);
196 st->print(", CORE ");
197 getrlimit(RLIMIT_CORE, &rlim);
198 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
199 else st->print("%uk", rlim.rlim_cur >> 10);
201 // Isn't there on solaris
202 #if !defined(TARGET_OS_FAMILY_solaris) && !defined(TARGET_OS_FAMILY_aix)
203 st->print(", NPROC ");
204 getrlimit(RLIMIT_NPROC, &rlim);
205 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
206 else st->print("%d", rlim.rlim_cur);
207 #endif
209 st->print(", NOFILE ");
210 getrlimit(RLIMIT_NOFILE, &rlim);
211 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
212 else st->print("%d", rlim.rlim_cur);
214 st->print(", AS ");
215 getrlimit(RLIMIT_AS, &rlim);
216 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
217 else st->print("%uk", rlim.rlim_cur >> 10);
218 st->cr();
219 }
221 void os::Posix::print_uname_info(outputStream* st) {
222 // kernel
223 st->print("uname:");
224 struct utsname name;
225 uname(&name);
226 st->print("%s ", name.sysname);
227 st->print("%s ", name.release);
228 st->print("%s ", name.version);
229 st->print("%s", name.machine);
230 st->cr();
231 }
233 bool os::has_allocatable_memory_limit(julong* limit) {
234 struct rlimit rlim;
235 int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);
236 // if there was an error when calling getrlimit, assume that there is no limitation
237 // on virtual memory.
238 bool result;
239 if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {
240 result = false;
241 } else {
242 *limit = (julong)rlim.rlim_cur;
243 result = true;
244 }
245 #ifdef _LP64
246 return result;
247 #else
248 // arbitrary virtual space limit for 32 bit Unices found by testing. If
249 // getrlimit above returned a limit, bound it with this limit. Otherwise
250 // directly use it.
251 const julong max_virtual_limit = (julong)3800*M;
252 if (result) {
253 *limit = MIN2(*limit, max_virtual_limit);
254 } else {
255 *limit = max_virtual_limit;
256 }
258 // bound by actually allocatable memory. The algorithm uses two bounds, an
259 // upper and a lower limit. The upper limit is the current highest amount of
260 // memory that could not be allocated, the lower limit is the current highest
261 // amount of memory that could be allocated.
262 // The algorithm iteratively refines the result by halving the difference
263 // between these limits, updating either the upper limit (if that value could
264 // not be allocated) or the lower limit (if the that value could be allocated)
265 // until the difference between these limits is "small".
267 // the minimum amount of memory we care about allocating.
268 const julong min_allocation_size = M;
270 julong upper_limit = *limit;
272 // first check a few trivial cases
273 if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
274 *limit = upper_limit;
275 } else if (!is_allocatable(min_allocation_size)) {
276 // we found that not even min_allocation_size is allocatable. Return it
277 // anyway. There is no point to search for a better value any more.
278 *limit = min_allocation_size;
279 } else {
280 // perform the binary search.
281 julong lower_limit = min_allocation_size;
282 while ((upper_limit - lower_limit) > min_allocation_size) {
283 julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
284 temp_limit = align_size_down_(temp_limit, min_allocation_size);
285 if (is_allocatable(temp_limit)) {
286 lower_limit = temp_limit;
287 } else {
288 upper_limit = temp_limit;
289 }
290 }
291 *limit = lower_limit;
292 }
293 return true;
294 #endif
295 }
297 const char* os::get_current_directory(char *buf, size_t buflen) {
298 return getcwd(buf, buflen);
299 }
301 FILE* os::open(int fd, const char* mode) {
302 return ::fdopen(fd, mode);
303 }
305 // Builds a platform dependent Agent_OnLoad_<lib_name> function name
306 // which is used to find statically linked in agents.
307 // Parameters:
308 // sym_name: Symbol in library we are looking for
309 // lib_name: Name of library to look in, NULL for shared libs.
310 // is_absolute_path == true if lib_name is absolute path to agent
311 // such as "/a/b/libL.so"
312 // == false if only the base name of the library is passed in
313 // such as "L"
314 char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
315 bool is_absolute_path) {
316 char *agent_entry_name;
317 size_t len;
318 size_t name_len;
319 size_t prefix_len = strlen(JNI_LIB_PREFIX);
320 size_t suffix_len = strlen(JNI_LIB_SUFFIX);
321 const char *start;
323 if (lib_name != NULL) {
324 len = name_len = strlen(lib_name);
325 if (is_absolute_path) {
326 // Need to strip path, prefix and suffix
327 if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {
328 lib_name = ++start;
329 }
330 if (len <= (prefix_len + suffix_len)) {
331 return NULL;
332 }
333 lib_name += prefix_len;
334 name_len = strlen(lib_name) - suffix_len;
335 }
336 }
337 len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
338 agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
339 if (agent_entry_name == NULL) {
340 return NULL;
341 }
342 strcpy(agent_entry_name, sym_name);
343 if (lib_name != NULL) {
344 strcat(agent_entry_name, "_");
345 strncat(agent_entry_name, lib_name, name_len);
346 }
347 return agent_entry_name;
348 }
350 // Returned string is a constant. For unknown signals "UNKNOWN" is returned.
351 const char* os::Posix::get_signal_name(int sig, char* out, size_t outlen) {
353 static const struct {
354 int sig; const char* name;
355 }
356 info[] =
357 {
358 { SIGABRT, "SIGABRT" },
359 #ifdef SIGAIO
360 { SIGAIO, "SIGAIO" },
361 #endif
362 { SIGALRM, "SIGALRM" },
363 #ifdef SIGALRM1
364 { SIGALRM1, "SIGALRM1" },
365 #endif
366 { SIGBUS, "SIGBUS" },
367 #ifdef SIGCANCEL
368 { SIGCANCEL, "SIGCANCEL" },
369 #endif
370 { SIGCHLD, "SIGCHLD" },
371 #ifdef SIGCLD
372 { SIGCLD, "SIGCLD" },
373 #endif
374 { SIGCONT, "SIGCONT" },
375 #ifdef SIGCPUFAIL
376 { SIGCPUFAIL, "SIGCPUFAIL" },
377 #endif
378 #ifdef SIGDANGER
379 { SIGDANGER, "SIGDANGER" },
380 #endif
381 #ifdef SIGDIL
382 { SIGDIL, "SIGDIL" },
383 #endif
384 #ifdef SIGEMT
385 { SIGEMT, "SIGEMT" },
386 #endif
387 { SIGFPE, "SIGFPE" },
388 #ifdef SIGFREEZE
389 { SIGFREEZE, "SIGFREEZE" },
390 #endif
391 #ifdef SIGGFAULT
392 { SIGGFAULT, "SIGGFAULT" },
393 #endif
394 #ifdef SIGGRANT
395 { SIGGRANT, "SIGGRANT" },
396 #endif
397 { SIGHUP, "SIGHUP" },
398 { SIGILL, "SIGILL" },
399 { SIGINT, "SIGINT" },
400 #ifdef SIGIO
401 { SIGIO, "SIGIO" },
402 #endif
403 #ifdef SIGIOINT
404 { SIGIOINT, "SIGIOINT" },
405 #endif
406 #ifdef SIGIOT
407 // SIGIOT is there for BSD compatibility, but on most Unices just a
408 // synonym for SIGABRT. The result should be "SIGABRT", not
409 // "SIGIOT".
410 #if (SIGIOT != SIGABRT )
411 { SIGIOT, "SIGIOT" },
412 #endif
413 #endif
414 #ifdef SIGKAP
415 { SIGKAP, "SIGKAP" },
416 #endif
417 { SIGKILL, "SIGKILL" },
418 #ifdef SIGLOST
419 { SIGLOST, "SIGLOST" },
420 #endif
421 #ifdef SIGLWP
422 { SIGLWP, "SIGLWP" },
423 #endif
424 #ifdef SIGLWPTIMER
425 { SIGLWPTIMER, "SIGLWPTIMER" },
426 #endif
427 #ifdef SIGMIGRATE
428 { SIGMIGRATE, "SIGMIGRATE" },
429 #endif
430 #ifdef SIGMSG
431 { SIGMSG, "SIGMSG" },
432 #endif
433 { SIGPIPE, "SIGPIPE" },
434 #ifdef SIGPOLL
435 { SIGPOLL, "SIGPOLL" },
436 #endif
437 #ifdef SIGPRE
438 { SIGPRE, "SIGPRE" },
439 #endif
440 { SIGPROF, "SIGPROF" },
441 #ifdef SIGPTY
442 { SIGPTY, "SIGPTY" },
443 #endif
444 #ifdef SIGPWR
445 { SIGPWR, "SIGPWR" },
446 #endif
447 { SIGQUIT, "SIGQUIT" },
448 #ifdef SIGRECONFIG
449 { SIGRECONFIG, "SIGRECONFIG" },
450 #endif
451 #ifdef SIGRECOVERY
452 { SIGRECOVERY, "SIGRECOVERY" },
453 #endif
454 #ifdef SIGRESERVE
455 { SIGRESERVE, "SIGRESERVE" },
456 #endif
457 #ifdef SIGRETRACT
458 { SIGRETRACT, "SIGRETRACT" },
459 #endif
460 #ifdef SIGSAK
461 { SIGSAK, "SIGSAK" },
462 #endif
463 { SIGSEGV, "SIGSEGV" },
464 #ifdef SIGSOUND
465 { SIGSOUND, "SIGSOUND" },
466 #endif
467 { SIGSTOP, "SIGSTOP" },
468 { SIGSYS, "SIGSYS" },
469 #ifdef SIGSYSERROR
470 { SIGSYSERROR, "SIGSYSERROR" },
471 #endif
472 #ifdef SIGTALRM
473 { SIGTALRM, "SIGTALRM" },
474 #endif
475 { SIGTERM, "SIGTERM" },
476 #ifdef SIGTHAW
477 { SIGTHAW, "SIGTHAW" },
478 #endif
479 { SIGTRAP, "SIGTRAP" },
480 #ifdef SIGTSTP
481 { SIGTSTP, "SIGTSTP" },
482 #endif
483 { SIGTTIN, "SIGTTIN" },
484 { SIGTTOU, "SIGTTOU" },
485 #ifdef SIGURG
486 { SIGURG, "SIGURG" },
487 #endif
488 { SIGUSR1, "SIGUSR1" },
489 { SIGUSR2, "SIGUSR2" },
490 #ifdef SIGVIRT
491 { SIGVIRT, "SIGVIRT" },
492 #endif
493 { SIGVTALRM, "SIGVTALRM" },
494 #ifdef SIGWAITING
495 { SIGWAITING, "SIGWAITING" },
496 #endif
497 #ifdef SIGWINCH
498 { SIGWINCH, "SIGWINCH" },
499 #endif
500 #ifdef SIGWINDOW
501 { SIGWINDOW, "SIGWINDOW" },
502 #endif
503 { SIGXCPU, "SIGXCPU" },
504 { SIGXFSZ, "SIGXFSZ" },
505 #ifdef SIGXRES
506 { SIGXRES, "SIGXRES" },
507 #endif
508 { -1, NULL }
509 };
511 const char* ret = NULL;
513 #ifdef SIGRTMIN
514 if (sig >= SIGRTMIN && sig <= SIGRTMAX) {
515 if (sig == SIGRTMIN) {
516 ret = "SIGRTMIN";
517 } else if (sig == SIGRTMAX) {
518 ret = "SIGRTMAX";
519 } else {
520 jio_snprintf(out, outlen, "SIGRTMIN+%d", sig - SIGRTMIN);
521 return out;
522 }
523 }
524 #endif
526 if (sig > 0) {
527 for (int idx = 0; info[idx].sig != -1; idx ++) {
528 if (info[idx].sig == sig) {
529 ret = info[idx].name;
530 break;
531 }
532 }
533 }
535 if (!ret) {
536 if (!is_valid_signal(sig)) {
537 ret = "INVALID";
538 } else {
539 ret = "UNKNOWN";
540 }
541 }
543 jio_snprintf(out, outlen, ret);
544 return out;
545 }
547 // Returns true if signal number is valid.
548 bool os::Posix::is_valid_signal(int sig) {
549 // MacOS not really POSIX compliant: sigaddset does not return
550 // an error for invalid signal numbers. However, MacOS does not
551 // support real time signals and simply seems to have just 33
552 // signals with no holes in the signal range.
553 #ifdef __APPLE__
554 return sig >= 1 && sig < NSIG;
555 #else
556 // Use sigaddset to check for signal validity.
557 sigset_t set;
558 if (sigaddset(&set, sig) == -1 && errno == EINVAL) {
559 return false;
560 }
561 return true;
562 #endif
563 }
565 #define NUM_IMPORTANT_SIGS 32
566 // Returns one-line short description of a signal set in a user provided buffer.
567 const char* os::Posix::describe_signal_set_short(const sigset_t* set, char* buffer, size_t buf_size) {
568 assert(buf_size == (NUM_IMPORTANT_SIGS + 1), "wrong buffer size");
569 // Note: for shortness, just print out the first 32. That should
570 // cover most of the useful ones, apart from realtime signals.
571 for (int sig = 1; sig <= NUM_IMPORTANT_SIGS; sig++) {
572 const int rc = sigismember(set, sig);
573 if (rc == -1 && errno == EINVAL) {
574 buffer[sig-1] = '?';
575 } else {
576 buffer[sig-1] = rc == 0 ? '0' : '1';
577 }
578 }
579 buffer[NUM_IMPORTANT_SIGS] = 0;
580 return buffer;
581 }
583 // Prints one-line description of a signal set.
584 void os::Posix::print_signal_set_short(outputStream* st, const sigset_t* set) {
585 char buf[NUM_IMPORTANT_SIGS + 1];
586 os::Posix::describe_signal_set_short(set, buf, sizeof(buf));
587 st->print("%s", buf);
588 }
590 // Writes one-line description of a combination of sigaction.sa_flags into a user
591 // provided buffer. Returns that buffer.
592 const char* os::Posix::describe_sa_flags(int flags, char* buffer, size_t size) {
593 char* p = buffer;
594 size_t remaining = size;
595 bool first = true;
596 int idx = 0;
598 assert(buffer, "invalid argument");
600 if (size == 0) {
601 return buffer;
602 }
604 strncpy(buffer, "none", size);
606 const struct {
607 // NB: i is an unsigned int here because SA_RESETHAND is on some
608 // systems 0x80000000, which is implicitly unsigned. Assignining
609 // it to an int field would be an overflow in unsigned-to-signed
610 // conversion.
611 unsigned int i;
612 const char* s;
613 } flaginfo [] = {
614 { SA_NOCLDSTOP, "SA_NOCLDSTOP" },
615 { SA_ONSTACK, "SA_ONSTACK" },
616 { SA_RESETHAND, "SA_RESETHAND" },
617 { SA_RESTART, "SA_RESTART" },
618 { SA_SIGINFO, "SA_SIGINFO" },
619 { SA_NOCLDWAIT, "SA_NOCLDWAIT" },
620 { SA_NODEFER, "SA_NODEFER" },
621 #ifdef AIX
622 { SA_ONSTACK, "SA_ONSTACK" },
623 { SA_OLDSTYLE, "SA_OLDSTYLE" },
624 #endif
625 { 0, NULL }
626 };
628 for (idx = 0; flaginfo[idx].s && remaining > 1; idx++) {
629 if (flags & flaginfo[idx].i) {
630 if (first) {
631 jio_snprintf(p, remaining, "%s", flaginfo[idx].s);
632 first = false;
633 } else {
634 jio_snprintf(p, remaining, "|%s", flaginfo[idx].s);
635 }
636 const size_t len = strlen(p);
637 p += len;
638 remaining -= len;
639 }
640 }
642 buffer[size - 1] = '\0';
644 return buffer;
645 }
647 // Prints one-line description of a combination of sigaction.sa_flags.
648 void os::Posix::print_sa_flags(outputStream* st, int flags) {
649 char buffer[0x100];
650 os::Posix::describe_sa_flags(flags, buffer, sizeof(buffer));
651 st->print("%s", buffer);
652 }
654 // Helper function for os::Posix::print_siginfo_...():
655 // return a textual description for signal code.
656 struct enum_sigcode_desc_t {
657 const char* s_name;
658 const char* s_desc;
659 };
661 static bool get_signal_code_description(const siginfo_t* si, enum_sigcode_desc_t* out) {
663 const struct {
664 int sig; int code; const char* s_code; const char* s_desc;
665 } t1 [] = {
666 { SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode." },
667 { SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand." },
668 { SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode." },
669 { SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap." },
670 { SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode." },
671 { SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register." },
672 { SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error." },
673 { SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error." },
674 #if defined(IA64) && defined(LINUX)
675 { SIGILL, ILL_BADIADDR, "ILL_BADIADDR", "Unimplemented instruction address" },
676 { SIGILL, ILL_BREAK, "ILL_BREAK", "Application Break instruction" },
677 #endif
678 { SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero." },
679 { SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow." },
680 { SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating-point divide by zero." },
681 { SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating-point overflow." },
682 { SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating-point underflow." },
683 { SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating-point inexact result." },
684 { SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating-point operation." },
685 { SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range." },
686 { SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object." },
687 { SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for mapped object." },
688 #ifdef AIX
689 // no explanation found what keyerr would be
690 { SIGSEGV, SEGV_KEYERR, "SEGV_KEYERR", "key error" },
691 #endif
692 #if defined(IA64) && !defined(AIX)
693 { SIGSEGV, SEGV_PSTKOVF, "SEGV_PSTKOVF", "Paragraph stack overflow" },
694 #endif
695 #if defined(__sparc) && defined(SOLARIS)
696 // define Solaris Sparc M7 ADI SEGV signals
697 #if !defined(SEGV_ACCADI)
698 #define SEGV_ACCADI 3
699 #endif
700 { SIGSEGV, SEGV_ACCADI, "SEGV_ACCADI", "ADI not enabled for mapped object." },
701 #if !defined(SEGV_ACCDERR)
702 #define SEGV_ACCDERR 4
703 #endif
704 { SIGSEGV, SEGV_ACCDERR, "SEGV_ACCDERR", "ADI disrupting exception." },
705 #if !defined(SEGV_ACCPERR)
706 #define SEGV_ACCPERR 5
707 #endif
708 { SIGSEGV, SEGV_ACCPERR, "SEGV_ACCPERR", "ADI precise exception." },
709 #endif // defined(__sparc) && defined(SOLARIS)
710 { SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment." },
711 { SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Nonexistent physical address." },
712 { SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object-specific hardware error." },
713 { SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint." },
714 { SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap." },
715 { SIGCHLD, CLD_EXITED, "CLD_EXITED", "Child has exited." },
716 { SIGCHLD, CLD_KILLED, "CLD_KILLED", "Child has terminated abnormally and did not create a core file." },
717 { SIGCHLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally and created a core file." },
718 { SIGCHLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped." },
719 { SIGCHLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped." },
720 { SIGCHLD, CLD_CONTINUED,"CLD_CONTINUED","Stopped child has continued." },
721 #ifdef SIGPOLL
722 { SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available." },
723 { SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available." },
724 { SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error." },
725 { SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available." },
726 { SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected. [Option End]" },
727 #endif
728 { -1, -1, NULL, NULL }
729 };
731 // Codes valid in any signal context.
732 const struct {
733 int code; const char* s_code; const char* s_desc;
734 } t2 [] = {
735 { SI_USER, "SI_USER", "Signal sent by kill()." },
736 { SI_QUEUE, "SI_QUEUE", "Signal sent by the sigqueue()." },
737 { SI_TIMER, "SI_TIMER", "Signal generated by expiration of a timer set by timer_settime()." },
738 { SI_ASYNCIO, "SI_ASYNCIO", "Signal generated by completion of an asynchronous I/O request." },
739 { SI_MESGQ, "SI_MESGQ", "Signal generated by arrival of a message on an empty message queue." },
740 // Linux specific
741 #ifdef SI_TKILL
742 { SI_TKILL, "SI_TKILL", "Signal sent by tkill (pthread_kill)" },
743 #endif
744 #ifdef SI_DETHREAD
745 { SI_DETHREAD, "SI_DETHREAD", "Signal sent by execve() killing subsidiary threads" },
746 #endif
747 #ifdef SI_KERNEL
748 { SI_KERNEL, "SI_KERNEL", "Signal sent by kernel." },
749 #endif
750 #ifdef SI_SIGIO
751 { SI_SIGIO, "SI_SIGIO", "Signal sent by queued SIGIO" },
752 #endif
754 #ifdef AIX
755 { SI_UNDEFINED, "SI_UNDEFINED","siginfo contains partial information" },
756 { SI_EMPTY, "SI_EMPTY", "siginfo contains no useful information" },
757 #endif
759 #ifdef __sun
760 { SI_NOINFO, "SI_NOINFO", "No signal information" },
761 { SI_RCTL, "SI_RCTL", "kernel generated signal via rctl action" },
762 { SI_LWP, "SI_LWP", "Signal sent via lwp_kill" },
763 #endif
765 { -1, NULL, NULL }
766 };
768 const char* s_code = NULL;
769 const char* s_desc = NULL;
771 for (int i = 0; t1[i].sig != -1; i ++) {
772 if (t1[i].sig == si->si_signo && t1[i].code == si->si_code) {
773 s_code = t1[i].s_code;
774 s_desc = t1[i].s_desc;
775 break;
776 }
777 }
779 if (s_code == NULL) {
780 for (int i = 0; t2[i].s_code != NULL; i ++) {
781 if (t2[i].code == si->si_code) {
782 s_code = t2[i].s_code;
783 s_desc = t2[i].s_desc;
784 }
785 }
786 }
788 if (s_code == NULL) {
789 out->s_name = "unknown";
790 out->s_desc = "unknown";
791 return false;
792 }
794 out->s_name = s_code;
795 out->s_desc = s_desc;
797 return true;
798 }
800 // A POSIX conform, platform-independend siginfo print routine.
801 // Short print out on one line.
802 void os::Posix::print_siginfo_brief(outputStream* os, const siginfo_t* si) {
803 char buf[20];
804 os->print("siginfo: ");
806 if (!si) {
807 os->print("<null>");
808 return;
809 }
811 // See print_siginfo_full() for details.
812 const int sig = si->si_signo;
814 os->print("si_signo: %d (%s)", sig, os::Posix::get_signal_name(sig, buf, sizeof(buf)));
816 enum_sigcode_desc_t ed;
817 if (get_signal_code_description(si, &ed)) {
818 os->print(", si_code: %d (%s)", si->si_code, ed.s_name);
819 } else {
820 os->print(", si_code: %d (unknown)", si->si_code);
821 }
823 if (si->si_errno) {
824 os->print(", si_errno: %d", si->si_errno);
825 }
827 const int me = (int) ::getpid();
828 const int pid = (int) si->si_pid;
830 if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
831 if (IS_VALID_PID(pid) && pid != me) {
832 os->print(", sent from pid: %d (uid: %d)", pid, (int) si->si_uid);
833 }
834 } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
835 sig == SIGTRAP || sig == SIGFPE) {
836 os->print(", si_addr: " PTR_FORMAT, si->si_addr);
837 #ifdef SIGPOLL
838 } else if (sig == SIGPOLL) {
839 os->print(", si_band: " PTR64_FORMAT, (uint64_t)si->si_band);
840 #endif
841 } else if (sig == SIGCHLD) {
842 os->print_cr(", si_pid: %d, si_uid: %d, si_status: %d", (int) si->si_pid, si->si_uid, si->si_status);
843 }
844 }
846 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
847 assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
848 }
850 /*
851 * See the caveats for this class in os_posix.hpp
852 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
853 * method and returns false. If none of the signals are raised, returns true.
854 * The callback is supposed to provide the method that should be protected.
855 */
856 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
857 sigset_t saved_sig_mask;
859 assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
860 assert(!WatcherThread::watcher_thread()->has_crash_protection(),
861 "crash_protection already set?");
863 // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
864 // since on at least some systems (OS X) siglongjmp will restore the mask
865 // for the process, not the thread
866 pthread_sigmask(0, NULL, &saved_sig_mask);
867 if (sigsetjmp(_jmpbuf, 0) == 0) {
868 // make sure we can see in the signal handler that we have crash protection
869 // installed
870 WatcherThread::watcher_thread()->set_crash_protection(this);
871 cb.call();
872 // and clear the crash protection
873 WatcherThread::watcher_thread()->set_crash_protection(NULL);
874 return true;
875 }
876 // this happens when we siglongjmp() back
877 pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
878 WatcherThread::watcher_thread()->set_crash_protection(NULL);
879 return false;
880 }
882 void os::WatcherThreadCrashProtection::restore() {
883 assert(WatcherThread::watcher_thread()->has_crash_protection(),
884 "must have crash protection");
886 siglongjmp(_jmpbuf, 1);
887 }
889 void os::WatcherThreadCrashProtection::check_crash_protection(int sig,
890 Thread* thread) {
892 if (thread != NULL &&
893 thread->is_Watcher_thread() &&
894 WatcherThread::watcher_thread()->has_crash_protection()) {
896 if (sig == SIGSEGV || sig == SIGBUS) {
897 WatcherThread::watcher_thread()->crash_protection()->restore();
898 }
899 }
900 }