2 /*--------------------------------------------------------------------*/
3 /*--- Assertions and panics. m_libcassert.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2017 Julian Seward
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_vki.h"
33 #include "pub_core_vkiscnums.h"
34 #include "pub_core_threadstate.h"
35 #include "pub_core_gdbserver.h"
36 #include "pub_core_aspacemgr.h"
37 #include "pub_core_libcbase.h"
38 #include "pub_core_libcassert.h"
39 #include "pub_core_libcprint.h"
40 #include "pub_core_libcproc.h" // For VG_(gettid)()
41 #include "pub_core_machine.h"
42 #include "pub_core_stacks.h"
43 #include "pub_core_stacktrace.h"
44 #include "pub_core_syscall.h"
45 #include "pub_core_syswrap.h"
46 #include "pub_core_tooliface.h" // For VG_(details).{name,bug_reports_to}
47 #include "pub_core_options.h" // For VG_(clo_xml)
49 /* ---------------------------------------------------------------------
51 ------------------------------------------------------------------ */
53 #if defined(VGP_x86_linux) || defined(VGP_x86_darwin) \
54 || defined(VGP_x86_solaris)
55 # define GET_STARTREGS(srP) \
56 { UInt eip, esp, ebp; \
57 __asm__ __volatile__( \
62 : "=r" (eip), "=r" (esp), "=r" (ebp) \
66 (srP)->r_pc = (ULong)eip; \
67 (srP)->r_sp = (ULong)esp; \
68 (srP)->misc.X86.r_ebp = ebp; \
70 #elif defined(VGP_amd64_linux) || defined(VGP_amd64_darwin) \
71 || defined(VGP_amd64_solaris)
72 # define GET_STARTREGS(srP) \
73 { ULong rip, rsp, rbp; \
74 __asm__ __volatile__( \
75 "leaq 0(%%rip), %0;" \
78 : "=r" (rip), "=r" (rsp), "=r" (rbp) \
84 (srP)->misc.AMD64.r_rbp = rbp; \
86 #elif defined(VGP_ppc32_linux)
87 # define GET_STARTREGS(srP) \
89 __asm__ __volatile__( \
90 "mflr 0;" /* r0 = lr */ \
91 "bl 0f;" /* lr = pc */ \
93 "mflr %0;" /* %0 = pc */ \
94 "mtlr 0;" /* restore lr */ \
95 "mr %1,1;" /* %1 = r1 */ \
96 "mr %2,0;" /* %2 = lr */ \
97 : "=r" (cia), "=r" (r1), "=r" (lr) \
99 : "r0" /* trashed */ \
101 (srP)->r_pc = (ULong)cia; \
102 (srP)->r_sp = (ULong)r1; \
103 (srP)->misc.PPC32.r_lr = lr; \
105 #elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
106 # define GET_STARTREGS(srP) \
107 { ULong cia, r1, lr; \
108 __asm__ __volatile__( \
109 "mflr 0;" /* r0 = lr */ \
110 "bl 0f;" /* lr = pc */ \
112 "mflr %0;" /* %0 = pc */ \
113 "mtlr 0;" /* restore lr */ \
114 "mr %1,1;" /* %1 = r1 */ \
115 "mr %2,0;" /* %2 = lr */ \
116 : "=r" (cia), "=r" (r1), "=r" (lr) \
118 : "r0" /* trashed */ \
122 (srP)->misc.PPC64.r_lr = lr; \
124 #elif defined(VGP_arm_linux)
125 # define GET_STARTREGS(srP) \
127 __asm__ __volatile__( \
128 "str r15, [%0, #+0];" \
129 "str r14, [%0, #+4];" \
130 "str r13, [%0, #+8];" \
131 "str r12, [%0, #+12];" \
132 "str r11, [%0, #+16];" \
133 "str r7, [%0, #+20];" \
135 : /* in */ "r"(&block[0]) \
136 : /* trash */ "memory" \
138 (srP)->r_pc = block[0] - 8; \
139 (srP)->misc.ARM.r14 = block[1]; \
140 (srP)->r_sp = block[2]; \
141 (srP)->misc.ARM.r12 = block[3]; \
142 (srP)->misc.ARM.r11 = block[4]; \
143 (srP)->misc.ARM.r7 = block[5]; \
145 #elif defined(VGP_arm64_linux)
146 # define GET_STARTREGS(srP) \
148 __asm__ __volatile__( \
150 "str x19, [%0, #+0];" /* pc */ \
152 "str x19, [%0, #+8];" /* sp */ \
153 "str x29, [%0, #+16];" /* fp */ \
154 "str x30, [%0, #+24];" /* lr */ \
156 : /* in */ "r"(&block[0]) \
157 : /* trash */ "memory","x19" \
159 (srP)->r_pc = block[0]; \
160 (srP)->r_sp = block[1]; \
161 (srP)->misc.ARM64.x29 = block[2]; \
162 (srP)->misc.ARM64.x30 = block[3]; \
164 #elif defined(VGP_s390x_linux)
165 # define GET_STARTREGS(srP) \
168 __asm__ __volatile__( \
171 "stg %%r15, 0(%1);" \
172 "stg %%r11, 8(%1);" \
173 "stg %%r14, 16(%1);" \
174 "std %%f0, 24(%1);" \
175 "std %%f1, 32(%1);" \
176 "std %%f2, 40(%1);" \
177 "std %%f3, 48(%1);" \
178 "std %%f4, 56(%1);" \
179 "std %%f5, 64(%1);" \
180 "std %%f6, 72(%1);" \
181 "std %%f7, 80(%1);" \
182 : /* out */ "=r" (ia) \
183 : /* in */ "a" (&block[0]) \
184 : /* trash */ "memory" \
187 (srP)->r_sp = block[0]; \
188 (srP)->misc.S390X.r_fp = block[1]; \
189 (srP)->misc.S390X.r_lr = block[2]; \
190 (srP)->misc.S390X.r_f0 = block[3]; \
191 (srP)->misc.S390X.r_f1 = block[4]; \
192 (srP)->misc.S390X.r_f2 = block[5]; \
193 (srP)->misc.S390X.r_f3 = block[6]; \
194 (srP)->misc.S390X.r_f4 = block[7]; \
195 (srP)->misc.S390X.r_f5 = block[8]; \
196 (srP)->misc.S390X.r_f6 = block[9]; \
197 (srP)->misc.S390X.r_f7 = block[10]; \
199 #elif defined(VGP_mips32_linux)
200 # define GET_STARTREGS(srP) \
201 { UInt pc, sp, fp, ra, gp; \
202 asm("move $8, $31;" /* t0 = ra */ \
203 "bal 0f;" /* ra = pc */ \
206 "move $31, $8;" /* restore lr */ \
217 : "$8" /* trashed */ ); \
218 (srP)->r_pc = (ULong)pc - 8; \
219 (srP)->r_sp = (ULong)sp; \
220 (srP)->misc.MIPS32.r30 = (ULong)fp; \
221 (srP)->misc.MIPS32.r31 = (ULong)ra; \
222 (srP)->misc.MIPS32.r28 = (ULong)gp; \
224 #elif defined(VGP_mips64_linux)
225 # define GET_STARTREGS(srP) \
226 { ULong pc, sp, fp, ra, gp; \
227 asm("move $8, $31;" /* t0 = ra */ \
228 "bal 0f;" /* ra = pc */ \
231 "move $31, $8;" /* restore lr */ \
242 : "$8" /* trashed */ ); \
243 (srP)->r_pc = (ULong)pc - 8; \
244 (srP)->r_sp = (ULong)sp; \
245 (srP)->misc.MIPS64.r30 = (ULong)fp; \
246 (srP)->misc.MIPS64.r31 = (ULong)ra; \
247 (srP)->misc.MIPS64.r28 = (ULong)gp; \
250 # error Unknown platform
253 #define BACKTRACE_DEPTH 100 // nice and deep!
255 __attribute__ ((__noreturn__
))
256 static void exit_wrk( Int status
, Bool gdbserver_call_allowed
)
258 static Bool exit_called
= False
;
259 // avoid recursive exit during gdbserver call.
261 if (gdbserver_call_allowed
&& !exit_called
) {
262 const ThreadId atid
= 1; // Arbitrary tid used to call/terminate gdbsrv.
265 && VgdbStopAtiS(VgdbStopAt_ValgrindAbExit
, VG_(clo_vgdb_stop_at
))) {
266 if (VG_(gdbserver_init_done
)()) {
267 VG_(umsg
)("(action at valgrind abnormal exit) vgdb me ... \n");
268 VG_(gdbserver
) (atid
);
270 VG_(umsg
)("(action at valgrind abnormal exit)\n"
271 "valgrind exit is too early => vgdb not yet usable\n");
274 if (VG_(gdbserver_init_done
)()) {
275 // Always terminate the gdbserver when Valgrind exits, so as
276 // to e.g. cleanup the FIFOs.
277 VG_(gdbserver_exit
) (atid
,
278 status
== 0 ? VgSrc_ExitProcess
: VgSrc_FatalSig
);
283 VG_(exit_now
) (status
);
286 /* Call the appropriate system call and nothing else. This function should
287 be called in places where the dependencies of VG_(exit) need to be
289 __attribute__ ((__noreturn__
))
290 void VG_(exit_now
)( Int status
)
292 #if defined(VGO_linux)
293 (void)VG_(do_syscall1
)(__NR_exit_group
, status
);
294 #elif defined(VGO_darwin) || defined(VGO_solaris)
295 (void)VG_(do_syscall1
)(__NR_exit
, status
);
300 // We really shouldn't reach here. Just in case we do, use some very crude
301 // methods to force abort
303 *(volatile Int
*)0 = 'x';
306 /* Pull down the entire world */
307 void VG_(exit
)( Int status
)
309 exit_wrk (status
, True
);
312 /* Pull down the entire world */
313 void VG_(client_exit
)( Int status
)
315 exit_wrk (status
, False
);
318 static void print_thread_state (Bool stack_usage
,
319 const HChar
* prefix
, ThreadId i
)
322 = (VgStack
*)VG_(threads
)[i
].os_state
.valgrind_stack_base
;
324 // must be large enough for VG_SYSNUM_STRING result + 10.
326 if (VG_(is_in_syscall
) (i
))
327 VG_(sprintf
)(syscallno
, " syscall %s",
328 VG_SYSNUM_STRING(VG_(is_in_syscall_no
)(i
)));
331 VG_(printf
)("\n%sThread %u: status = %s%s (lwpid %d)\n", prefix
, i
,
332 VG_(name_of_ThreadStatus
)(VG_(threads
)[i
].status
),
334 VG_(threads
)[i
].os_state
.lwpid
);
335 if (VG_(threads
)[i
].status
!= VgTs_Empty
)
336 VG_(get_and_pp_StackTrace
)( i
, BACKTRACE_DEPTH
);
337 if (stack_usage
&& VG_(threads
)[i
].client_stack_highest_byte
!= 0 ) {
341 VG_(stack_limits
)(VG_(get_SP
)(i
), &start
, &end
);
343 VG_(printf
)("%sclient stack range: [%p %p] client SP: %p\n",
345 (void*)start
, (void*)end
, (void*)VG_(get_SP
)(i
));
347 VG_(printf
)("%sclient stack range: ??????? client SP: %p\n",
349 (void*)VG_(get_SP
)(i
));
351 if (stack_usage
&& stack
!= 0) {
352 Addr stack_low_addr
= VG_(am_valgrind_stack_low_addr
) (stack
);
355 ("%svalgrind stack range: [%p %p] top usage: %lu of %lu\n",
357 (void*)stack_low_addr
,
358 (void*)(stack_low_addr
+ VG_(clo_valgrind_stacksize
) - 1),
359 VG_(clo_valgrind_stacksize
)
360 - VG_(am_get_VgStack_unused_szB
) (stack
,
361 VG_(clo_valgrind_stacksize
)),
362 (SizeT
) VG_(clo_valgrind_stacksize
));
366 // Print the scheduler status.
367 static void show_sched_status_wrk ( Bool host_stacktrace
,
370 const UnwindStartRegs
* startRegsIN
)
373 if (host_stacktrace
) {
374 const Bool save_clo_xml
= VG_(clo_xml
);
376 Addr ips
[BACKTRACE_DEPTH
];
379 = VG_(get_ThreadState
)( VG_(lwpid_to_vgtid_dead_ok
)(VG_(gettid
)()));
381 // If necessary, fake up an ExeContext which is of our actual real CPU
382 // state. Could cause problems if we got the panic/exception within the
383 // execontext/stack dump/symtab code. But it's better than nothing.
384 UnwindStartRegs startRegs
;
385 VG_(memset
)(&startRegs
, 0, sizeof(startRegs
));
387 if (startRegsIN
== NULL
) {
388 GET_STARTREGS(&startRegs
);
390 startRegs
= *startRegsIN
;
393 stacktop
= tst
->os_state
.valgrind_stack_init_SP
;
396 VG_(get_StackTrace_wrk
)(
398 ips
, BACKTRACE_DEPTH
,
399 NULL
/*array to dump SP values in*/,
400 NULL
/*array to dump FP values in*/,
403 VG_(printf
)("\nhost stacktrace:\n");
404 VG_(clo_xml
) = False
;
405 VG_(pp_StackTrace
) (VG_(current_DiEpoch
)(), ips
, n_ips
);
406 VG_(clo_xml
) = save_clo_xml
;
409 VG_(printf
)("\nsched status:\n");
410 if (VG_(threads
) == NULL
) {
411 VG_(printf
)(" scheduler not yet initialised\n");
413 VG_(printf
)(" running_tid=%u\n", VG_(get_running_tid
)());
414 for (i
= 1; i
< VG_N_THREADS
; i
++) {
416 = (VgStack
*)VG_(threads
)[i
].os_state
.valgrind_stack_base
;
417 /* If a thread slot was never used (yet), valgrind_stack_base is 0.
418 If a thread slot is used by a thread or was used by a thread which
419 has exited, then valgrind_stack_base points to the stack base. */
420 if (VG_(threads
)[i
].status
== VgTs_Empty
421 && (!exited_threads
|| stack
== 0)) continue;
422 print_thread_state(stack_usage
, "", i
);
423 if (VG_(inner_threads
) != NULL
) {
424 /* An inner V has informed us (the outer) of its thread array.
425 Report the inner guest stack trace. */
428 for (inner_tid
= 1; inner_tid
< VG_N_THREADS
; inner_tid
++) {
429 if (VG_(threads
)[i
].os_state
.lwpid
430 == VG_(inner_threads
)[inner_tid
].os_state
.lwpid
) {
431 ThreadState
* save_outer_vg_threads
= VG_(threads
);
433 VG_(threads
) = VG_(inner_threads
);
434 print_thread_state(stack_usage
, "INNER ", inner_tid
);
435 VG_(threads
) = save_outer_vg_threads
;
445 void VG_(show_sched_status
) ( Bool host_stacktrace
,
449 show_sched_status_wrk (host_stacktrace
,
455 __attribute__ ((noreturn
))
456 static void report_and_quit ( const HChar
* report
,
457 const UnwindStartRegs
* startRegsIN
)
459 show_sched_status_wrk (True
, // host_stacktrace
461 False
, // exited_threads
465 "Note: see also the FAQ in the source distribution.\n"
466 "It contains workarounds to several common problems.\n"
467 "In particular, if Valgrind aborted or crashed after\n"
468 "identifying problems in your program, there's a good chance\n"
469 "that fixing those problems will prevent Valgrind aborting or\n"
470 "crashing, especially if it happened in m_mallocfree.c.\n"
472 "If that doesn't help, please report this bug to: %s\n\n"
473 "In the bug report, send all the above text, the valgrind\n"
474 "version, and what OS and version you are using. Thanks.\n\n",
479 void VG_(assert_fail
) ( Bool isCore
, const HChar
* expr
, const HChar
* file
,
480 Int line
, const HChar
* fn
, const HChar
* format
, ... )
482 va_list vargs
, vargs_copy
;
483 const HChar
* component
;
484 const HChar
* bugs_to
;
487 static Bool entered
= False
;
493 component
= "valgrind";
494 bugs_to
= VG_BUGS_TO
;
496 component
= VG_(details
).name
;
497 bugs_to
= VG_(details
).bug_reports_to
;
501 VG_(printf_xml
)("</valgrindoutput>\n");
503 // Treat vg_assert2(0, "foo") specially, as a panicky abort
504 if (VG_STREQ(expr
, "0")) {
505 VG_(printf
)("\n%s: %s:%d (%s): the 'impossible' happened.\n",
506 component
, file
, line
, fn
);
508 VG_(printf
)("\n%s: %s:%d (%s): Assertion '%s' failed.\n",
509 component
, file
, line
, fn
, expr
);
512 /* Check whether anything will be written */
514 va_start(vargs
, format
);
515 va_copy(vargs_copy
, vargs
);
516 written
= VG_(vsnprintf
) ( buf
, sizeof(buf
), format
, vargs
);
520 VG_(printf
)("%s: ", component
);
521 VG_(vprintf
)(format
, vargs_copy
);
525 report_and_quit(bugs_to
, NULL
);
528 __attribute__ ((noreturn
))
529 static void panic ( const HChar
* name
, const HChar
* report
, const HChar
* str
,
530 const UnwindStartRegs
* startRegs
)
533 VG_(printf_xml
)("</valgrindoutput>\n");
534 VG_(printf
)("\n%s: the 'impossible' happened:\n %s\n", name
, str
);
535 report_and_quit(report
, startRegs
);
538 void VG_(core_panic_at
) ( const HChar
* str
, const UnwindStartRegs
* startRegs
)
540 panic("valgrind", VG_BUGS_TO
, str
, startRegs
);
543 void VG_(core_panic
) ( const HChar
* str
)
545 VG_(core_panic_at
)(str
, NULL
);
548 void VG_(tool_panic
) ( const HChar
* str
)
550 panic(VG_(details
).name
, VG_(details
).bug_reports_to
, str
, NULL
);
553 /* Print some helpful-ish text about unimplemented things, and give up. */
554 void VG_(unimplemented
) ( const HChar
* format
, ... )
559 va_start(vargs
, format
);
560 VG_(vsnprintf
)(msg
, sizeof(msg
), format
, vargs
);
564 VG_(printf_xml
)("</valgrindoutput>\n");
566 VG_(umsg
)("Valgrind detected that your program requires\n");
567 VG_(umsg
)("the following unimplemented functionality:\n");
568 VG_(umsg
)(" %s\n", msg
);
569 VG_(umsg
)("This may be because the functionality is hard to implement,\n");
570 VG_(umsg
)("or because no reasonable program would behave this way,\n");
571 VG_(umsg
)("or because nobody has yet needed it. "
572 "In any case, let us know at\n");
573 VG_(umsg
)("%s and/or try to work around the problem, if you can.\n",
576 VG_(umsg
)("Valgrind has to exit now. Sorry. Bye!\n");
578 VG_(show_sched_status
)(False
, // host_stacktrace
579 False
, // stack_usage
580 False
); // exited_threads
584 /*--------------------------------------------------------------------*/
586 /*--------------------------------------------------------------------*/