1 ;;;; functions and macros to define and deal with internal errors
2 ;;;; (i.e. problems that can be signaled from assembler code)
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!KERNEL")
17 (defvar *internal-errors*
18 #.(map 'vector #'cdr sb!c:*backend-internal-errors*))
20 (eval-when (:compile-toplevel :execute)
22 (sb!xc:defmacro deferr (name args &rest body)
23 (let* ((rest-pos (position '&rest args))
24 (required (if rest-pos (subseq args 0 rest-pos) args))
25 (fn-name (symbolicate name "-HANDLER")))
26 (with-unique-names (fp context sc-offsets)
28 ;; FIXME: Having a separate full DEFUN for each error doesn't
29 ;; seem to add much value, and it takes a lot of space. Perhaps
30 ;; we could do this dispatch with a big CASE statement instead?
31 (defun ,fn-name (name ,fp ,context ,sc-offsets)
32 ;; FIXME: It would probably be good to do *STACK-TOP-HINT*
33 ;; tricks to hide this internal error-handling logic from the
34 ;; poor high level user, so his debugger tells him about
35 ;; where his error was detected instead of telling him where
36 ;; he ended up inside the system error-handling logic.
37 (declare (ignorable name ,fp ,context ,sc-offsets))
38 (let (,@(let ((offset -1))
40 `(,var (sb!di::sub-access-debug-var-slot
47 `((,(nth (1+ rest-pos) args)
48 (mapcar (lambda (sc-offset)
49 (sb!di::sub-access-debug-var-slot
53 (nthcdr ,rest-pos ,sc-offsets))))))
55 (setf (svref *internal-errors* ,(error-number-or-lose name))
60 (deferr unknown-error (&rest args)
61 (error "unknown error:~{ ~S~})" args))
63 (deferr object-not-fun-error (object)
66 :expected-type 'function))
68 (deferr object-not-list-error (object)
71 :expected-type 'list))
73 (deferr object-not-bignum-error (object)
76 :expected-type 'bignum))
78 (deferr object-not-ratio-error (object)
81 :expected-type 'ratio))
83 (deferr object-not-single-float-error (object)
86 :expected-type 'single-float))
88 (deferr object-not-double-float-error (object)
91 :expected-type 'double-float))
94 (deferr object-not-long-float-error (object)
97 :expected-type 'long-float))
99 (deferr object-not-simple-string-error (object)
102 :expected-type 'simple-string))
104 (deferr object-not-fixnum-error (object)
107 :expected-type 'fixnum))
109 (deferr object-not-mod-error (object limit)
112 :expected-type `(mod ,(1+ limit))))
114 (deferr object-not-vector-error (object)
117 :expected-type 'vector))
119 (deferr object-not-string-error (object)
122 :expected-type 'string))
124 (deferr object-not-base-string-error (object)
127 :expected-type 'base-string))
129 (deferr object-not-vector-nil-error (object)
132 :expected-type '(vector nil)))
135 (deferr object-not-character-string-error (object)
138 :expected-type '(vector character)))
140 (deferr object-not-bit-vector-error (object)
143 :expected-type 'bit-vector))
145 (deferr object-not-array-error (object)
148 :expected-type 'array))
150 (deferr object-not-number-error (object)
153 :expected-type 'number))
155 (deferr object-not-rational-error (object)
158 :expected-type 'rational))
160 (deferr object-not-float-error (object)
163 :expected-type 'float))
165 (deferr object-not-real-error (object)
168 :expected-type 'real))
170 (deferr object-not-integer-error (object)
173 :expected-type 'integer))
175 (deferr object-not-cons-error (object)
178 :expected-type 'cons))
180 (deferr object-not-symbol-error (object)
183 :expected-type 'symbol))
185 (deferr undefined-fun-error (fdefn-or-symbol)
186 (error 'undefined-function
187 :name (etypecase fdefn-or-symbol
188 (symbol fdefn-or-symbol)
189 (fdefn (fdefn-name fdefn-or-symbol)))))
191 (deferr invalid-arg-count-error (nargs)
192 (error 'simple-program-error
193 :format-control "invalid number of arguments: ~S"
194 :format-arguments (list nargs)))
196 (deferr bogus-arg-to-values-list-error (list)
197 (error 'simple-type-error
201 "~@<attempt to use VALUES-LIST on a dotted list: ~2I~_~S~:>"
202 :format-arguments (list list)))
204 (deferr unbound-symbol-error (symbol)
205 (error 'unbound-variable :name symbol))
207 (deferr object-not-character-error (object)
210 :expected-type 'character))
212 (deferr object-not-sap-error (object)
215 :expected-type 'system-area-pointer))
217 (deferr invalid-unwind-error ()
218 (error 'simple-control-error
220 "attempt to RETURN-FROM a block or GO to a tag that no longer exists"
223 (deferr unseen-throw-tag-error (tag)
224 (error 'simple-control-error
225 :format-control "attempt to THROW to a tag that does not exist: ~S"
226 :format-arguments (list tag)))
228 (deferr nil-fun-returned-error (function)
229 (error 'simple-control-error
231 "A function with declared result type NIL returned:~% ~S"
232 :format-arguments (list function)))
234 (deferr nil-array-accessed-error (array)
235 (error 'nil-array-accessed-error
236 :datum array :expected-type '(not (array nil))))
238 (deferr division-by-zero-error (this that)
239 (error 'division-by-zero
241 :operands (list this that)))
243 (deferr object-not-type-error (object type)
244 (if (invalid-array-p object)
245 (invalid-array-error object)
246 (error (if (and (%instancep object)
247 (layout-invalid (%instance-layout object)))
251 :expected-type type)))
253 (deferr layout-invalid-error (object layout)
254 (error 'layout-invalid
256 :expected-type (layout-classoid layout)))
258 (deferr odd-key-args-error ()
259 (error 'simple-program-error
260 :format-control "odd number of &KEY arguments"))
262 (deferr unknown-key-arg-error (key-name)
263 (error 'simple-program-error
264 :format-control "unknown &KEY argument: ~S"
265 :format-arguments (list key-name)))
267 (deferr invalid-array-index-error (array bound index)
268 (invalid-array-index-error array index bound))
270 (deferr object-not-simple-array-error (object)
273 :expected-type 'simple-array))
275 (deferr object-not-signed-byte-32-error (object)
278 :expected-type '(signed-byte 32)))
280 (deferr object-not-unsigned-byte-32-error (object)
283 :expected-type '(unsigned-byte 32)))
285 (deferr tls-exhausted-error ()
286 ;; There is nothing we can do about it. A number of entries in the
287 ;; tls could be reserved and made available for recovery but since
288 ;; tls indices are never reused it would be kind of silly and
289 ;; without it signalling an error is more than likely to end in a
291 (%primitive print "Thread local storage exhausted.")
295 ((define-simple-array-internal-errors ()
299 `(deferr ,(symbolicate
301 (sb!vm:saetp-primitive-type-name saetp)
306 :expected-type '(simple-array
307 ,(sb!vm:saetp-specifier saetp)
309 sb!vm:*specialized-array-element-type-properties*))))
310 (define-simple-array-internal-errors))
312 (deferr object-not-complex-error (object)
315 :expected-type 'complex))
317 (deferr object-not-complex-rational-error (object)
320 :expected-type '(complex rational)))
322 (deferr object-not-complex-single-float-error (object)
325 :expected-type '(complex single-float)))
327 (deferr object-not-complex-double-float-error (object)
330 :expected-type '(complex double-float)))
333 (deferr object-not-complex-long-float-error (object)
336 :expected-type '(complex long-float)))
339 (deferr object-not-simd-pack-error (object)
342 :expected-type 'simd-pack))
344 (deferr object-not-weak-pointer-error (object)
347 :expected-type 'weak-pointer))
349 (deferr object-not-instance-error (object)
352 :expected-type 'instance))
354 (deferr object-not-complex-vector-error (object)
357 :expected-type '(and vector (not simple-array))))
359 ;;;; fetching errorful function name
361 ;;; This flag is used to prevent infinite recursive lossage when
362 ;;; we can't find the caller for some reason.
363 (defvar *finding-name* nil)
365 (defun find-caller-name-and-frame ()
367 (values "<error finding caller name -- already finding name>" nil)
369 (let* ((*finding-name* t)
370 (frame (sb!di:frame-down (sb!di:frame-down (sb!di:top-frame))))
371 (name (sb!di:debug-fun-name
372 (sb!di:frame-debug-fun frame))))
373 (sb!di:flush-frames-above frame)
376 (values "<error finding caller name -- trapped error>" nil))
377 (sb!di:debug-condition ()
378 (values "<error finding caller name -- trapped debug-condition>"
381 (defun find-interrupted-name-and-frame ()
382 (/show0 "entering FIND-INTERRUPTED-NAME-AND-FRAME")
384 (values "<error finding interrupted name -- already finding name>" nil)
386 (let ((*finding-name* t))
387 (/show0 "in ordinary case")
388 (do ((frame (sb!di:top-frame) (sb!di:frame-down frame)))
390 (/show0 "null frame")
391 (values "<error finding interrupted name -- null frame>" nil))
392 (/show0 "at head of DO loop")
393 (when (and (sb!di::compiled-frame-p frame)
394 (sb!di::compiled-frame-escaped frame))
395 (sb!di:flush-frames-above frame)
396 (/show0 "returning from within DO loop")
397 (return (values (sb!di:debug-fun-name
398 (sb!di:frame-debug-fun frame))
401 (/show0 "trapped ERROR")
402 (values "<error finding interrupted name -- trapped error>" nil))
403 (sb!di:debug-condition ()
404 (/show0 "trapped DEBUG-CONDITION")
405 (values "<error finding interrupted name -- trapped debug-condition>"
408 (defun find-caller-of-named-frame (name)
409 (unless *finding-name*
411 (let ((*finding-name* t))
412 (do ((frame (sb!di:top-frame) (sb!di:frame-down frame)))
414 (when (and (sb!di::compiled-frame-p frame)
415 (eq name (sb!di:debug-fun-name
416 (sb!di:frame-debug-fun frame))))
417 (let ((caller (sb!di:frame-down frame)))
418 (sb!di:flush-frames-above caller)
420 ((or error sb!di:debug-condition) ()
422 (sb!di:debug-condition ()
426 ;;;; INTERNAL-ERROR signal handler
428 (defun internal-error (context continuable)
429 (declare (type system-area-pointer context))
430 (declare (ignore continuable))
431 (/show0 "entering INTERNAL-ERROR, CONTEXT=..")
433 (infinite-error-protect
434 (/show0 "about to bind ALIEN-CONTEXT")
435 (let* ((alien-context (locally
436 (declare (optimize (inhibit-warnings 3)))
437 (sb!alien:sap-alien context (* os-context-t))))
438 #!+c-stack-is-control-stack
440 (cons (cons (%make-lisp-obj (sb!vm:context-register
443 (sb!vm:context-pc alien-context))
444 (when (boundp '*saved-fp-and-pcs*)
445 *saved-fp-and-pcs*))))
446 (declare (truly-dynamic-extent *saved-fp-and-pcs*))
447 (/show0 "about to bind ERROR-NUMBER and ARGUMENTS")
448 (multiple-value-bind (error-number arguments)
449 (sb!vm:internal-error-args alien-context)
451 ;; There's a limit to how much error reporting we can usefully
452 ;; do before initialization is complete, but try to be a little
453 ;; bit helpful before we die.
454 (/show0 "back from INTERNAL-ERROR-ARGS, ERROR-NUMBER=..")
455 (/hexstr error-number)
456 (/show0 "cold/low ARGUMENTS=..")
458 (unless *cold-init-complete-p*
459 (%primitive print "can't recover from error in cold init, halting")
460 (%primitive sb!c:halt))
462 (with-interrupt-bindings
463 (multiple-value-bind (name sb!debug:*stack-top-hint*)
464 (find-interrupted-name-and-frame)
465 (/show0 "back from FIND-INTERRUPTED-NAME")
466 (let ((fp (int-sap (sb!vm:context-register alien-context
468 (handler (and (< -1 error-number (length *internal-errors*))
469 (svref *internal-errors* error-number))))
470 (cond ((null handler)
473 "unknown internal error, ~D, args=~S"
476 (mapcar (lambda (sc-offset)
477 (sb!di::sub-access-debug-var-slot
478 fp sc-offset alien-context))
480 ((not (functionp handler))
482 :format-control "internal error ~D: ~A; args=~S"
486 (mapcar (lambda (sc-offset)
487 (sb!di::sub-access-debug-var-slot
488 fp sc-offset alien-context))
491 (funcall handler name fp alien-context arguments))))))))))
493 (defun control-stack-exhausted-error ()
494 (let ((sb!debug:*stack-top-hint* nil))
495 (infinite-error-protect
496 (format *error-output*
497 "Control stack guard page temporarily disabled: proceed with caution~%")
498 (error 'control-stack-exhausted))))
500 (defun binding-stack-exhausted-error ()
501 (let ((sb!debug:*stack-top-hint* nil))
502 (infinite-error-protect
503 (format *error-output*
504 "Binding stack guard page temporarily disabled: proceed with caution~%")
505 (error 'binding-stack-exhausted))))
507 (defun alien-stack-exhausted-error ()
508 (let ((sb!debug:*stack-top-hint* nil))
509 (infinite-error-protect
510 (format *error-output*
511 "Alien stack guard page temporarily disabled: proceed with caution~%")
512 (error 'alien-stack-exhausted))))
514 ;;; KLUDGE: we keep a single HEAP-EXHAUSTED-ERROR object around, so
515 ;;; that we don't need to allocate it when running out of
516 ;;; memory. Similarly we pass the amounts in special variables as
517 ;;; there may be multiple threads running into trouble at the same
518 ;;; time. The condition is created by GC-REINIT.
519 (defvar *heap-exhausted-error-condition*)
520 (defvar *heap-exhausted-error-available-bytes*)
521 (defvar *heap-exhausted-error-requested-bytes*)
523 (defun heap-exhausted-error (available requested)
524 (infinite-error-protect
525 (let ((*heap-exhausted-error-available-bytes* available)
526 (*heap-exhausted-error-requested-bytes* requested))
527 (error *heap-exhausted-error-condition*))))
529 (defun undefined-alien-variable-error ()
530 (error 'undefined-alien-variable-error))
532 (defun undefined-alien-function-error ()
533 (error 'undefined-alien-function-error))
536 (define-alien-variable current-memory-fault-address unsigned)
539 (defun memory-fault-error ()
540 (error 'memory-fault-error
541 :address current-memory-fault-address))
543 ;;; This is SIGTRAP / EXCEPTION_BREAKPOINT that runtime could not deal
544 ;;; with. Prior to Windows we just had a Lisp side handler for
545 ;;; SIGTRAP, but now we need to deal with this portably.
546 (defun unhandled-trap-error (context-sap)
547 (declare (type system-area-pointer context-sap))
548 (infinite-error-protect
549 (let ((context (sap-alien context-sap (* os-context-t))))
550 (error 'breakpoint-error
552 :address (sap-int (sb!vm:context-pc context))))))