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-vector-error (object)
112 :expected-type 'vector))
114 (deferr object-not-string-error (object)
117 :expected-type 'string))
119 (deferr object-not-base-string-error (object)
122 :expected-type 'base-string))
124 (deferr object-not-vector-nil-error (object)
127 :expected-type '(vector nil)))
130 (deferr object-not-character-string-error (object)
133 :expected-type '(vector character)))
135 (deferr object-not-bit-vector-error (object)
138 :expected-type 'bit-vector))
140 (deferr object-not-array-error (object)
143 :expected-type 'array))
145 (deferr object-not-number-error (object)
148 :expected-type 'number))
150 (deferr object-not-rational-error (object)
153 :expected-type 'rational))
155 (deferr object-not-float-error (object)
158 :expected-type 'float))
160 (deferr object-not-real-error (object)
163 :expected-type 'real))
165 (deferr object-not-integer-error (object)
168 :expected-type 'integer))
170 (deferr object-not-cons-error (object)
173 :expected-type 'cons))
175 (deferr object-not-symbol-error (object)
178 :expected-type 'symbol))
180 (deferr undefined-fun-error (fdefn-or-symbol)
181 (error 'undefined-function
182 :name (etypecase fdefn-or-symbol
183 (symbol fdefn-or-symbol)
184 (fdefn (fdefn-name fdefn-or-symbol)))))
186 (deferr invalid-arg-count-error (nargs)
187 (error 'simple-program-error
188 :format-control "invalid number of arguments: ~S"
189 :format-arguments (list nargs)))
191 (deferr bogus-arg-to-values-list-error (list)
192 (error 'simple-type-error
196 "~@<attempt to use VALUES-LIST on a dotted list: ~2I~_~S~:>"
197 :format-arguments (list list)))
199 (deferr unbound-symbol-error (symbol)
200 (error 'unbound-variable :name symbol))
202 (deferr object-not-character-error (object)
205 :expected-type 'character))
207 (deferr object-not-sap-error (object)
210 :expected-type 'system-area-pointer))
212 (deferr invalid-unwind-error ()
213 (error 'simple-control-error
215 "attempt to RETURN-FROM a block or GO to a tag that no longer exists"
218 (deferr unseen-throw-tag-error (tag)
219 (error 'simple-control-error
220 :format-control "attempt to THROW to a tag that does not exist: ~S"
221 :format-arguments (list tag)))
223 (deferr nil-fun-returned-error (function)
224 (error 'simple-control-error
226 "A function with declared result type NIL returned:~% ~S"
227 :format-arguments (list function)))
229 (deferr nil-array-accessed-error (array)
230 (error 'nil-array-accessed-error
231 :datum array :expected-type '(not (array nil))))
233 (deferr division-by-zero-error (this that)
234 (error 'division-by-zero
236 :operands (list this that)))
238 (deferr object-not-type-error (object type)
239 (if (invalid-array-p object)
240 (invalid-array-error object)
241 (error (if (and (%instancep object)
242 (layout-invalid (%instance-layout object)))
246 :expected-type type)))
248 (deferr layout-invalid-error (object layout)
249 (error 'layout-invalid
251 :expected-type (layout-classoid layout)))
253 (deferr odd-key-args-error ()
254 (error 'simple-program-error
255 :format-control "odd number of &KEY arguments"))
257 (deferr unknown-key-arg-error (key-name)
258 (error 'simple-program-error
259 :format-control "unknown &KEY argument: ~S"
260 :format-arguments (list key-name)))
262 (deferr invalid-array-index-error (array bound index)
263 (invalid-array-index-error array index bound))
265 (deferr object-not-simple-array-error (object)
268 :expected-type 'simple-array))
270 (deferr object-not-signed-byte-32-error (object)
273 :expected-type '(signed-byte 32)))
275 (deferr object-not-unsigned-byte-32-error (object)
278 :expected-type '(unsigned-byte 32)))
280 (deferr tls-exhausted-error ()
281 ;; There is nothing we can do about it. A number of entries in the
282 ;; tls could be reserved and made available for recovery but since
283 ;; tls indices are never reused it would be kind of silly and
284 ;; without it signalling an error is more than likely to end in a
286 (%primitive print "Thread local storage exhausted.")
290 ((define-simple-array-internal-errors ()
294 `(deferr ,(symbolicate
296 (sb!vm:saetp-primitive-type-name saetp)
301 :expected-type '(simple-array
302 ,(sb!vm:saetp-specifier saetp)
304 sb!vm:*specialized-array-element-type-properties*))))
305 (define-simple-array-internal-errors))
307 (deferr object-not-complex-error (object)
310 :expected-type 'complex))
312 (deferr object-not-complex-rational-error (object)
315 :expected-type '(complex rational)))
317 (deferr object-not-complex-single-float-error (object)
320 :expected-type '(complex single-float)))
322 (deferr object-not-complex-double-float-error (object)
325 :expected-type '(complex double-float)))
328 (deferr object-not-complex-long-float-error (object)
331 :expected-type '(complex long-float)))
333 (deferr object-not-weak-pointer-error (object)
336 :expected-type 'weak-pointer))
338 (deferr object-not-instance-error (object)
341 :expected-type 'instance))
343 (deferr object-not-complex-vector-error (object)
346 :expected-type '(and vector (not simple-array))))
348 ;;;; fetching errorful function name
350 ;;; This flag is used to prevent infinite recursive lossage when
351 ;;; we can't find the caller for some reason.
352 (defvar *finding-name* nil)
354 (defun find-caller-name-and-frame ()
356 (values "<error finding caller name -- already finding name>" nil)
358 (let* ((*finding-name* t)
359 (frame (sb!di:frame-down (sb!di:frame-down (sb!di:top-frame))))
360 (name (sb!di:debug-fun-name
361 (sb!di:frame-debug-fun frame))))
362 (sb!di:flush-frames-above frame)
365 (values "<error finding caller name -- trapped error>" nil))
366 (sb!di:debug-condition ()
367 (values "<error finding caller name -- trapped debug-condition>"
370 (defun find-interrupted-name-and-frame ()
371 (/show0 "entering FIND-INTERRUPTED-NAME-AND-FRAME")
373 (values "<error finding interrupted name -- already finding name>" nil)
375 (let ((*finding-name* t))
376 (/show0 "in ordinary case")
377 (do ((frame (sb!di:top-frame) (sb!di:frame-down frame)))
379 (/show0 "null frame")
380 (values "<error finding interrupted name -- null frame>" nil))
381 (/show0 "at head of DO loop")
382 (when (and (sb!di::compiled-frame-p frame)
383 (sb!di::compiled-frame-escaped frame))
384 (sb!di:flush-frames-above frame)
385 (/show0 "returning from within DO loop")
386 (return (values (sb!di:debug-fun-name
387 (sb!di:frame-debug-fun frame))
390 (/show0 "trapped ERROR")
391 (values "<error finding interrupted name -- trapped error>" nil))
392 (sb!di:debug-condition ()
393 (/show0 "trapped DEBUG-CONDITION")
394 (values "<error finding interrupted name -- trapped debug-condition>"
397 (defun find-caller-of-named-frame (name)
398 (unless *finding-name*
400 (let ((*finding-name* t))
401 (do ((frame (sb!di:top-frame) (sb!di:frame-down frame)))
403 (when (and (sb!di::compiled-frame-p frame)
404 (eq name (sb!debug::clean-debug-fun-name
405 (sb!di:debug-fun-name
406 (sb!di:frame-debug-fun frame)))))
407 (let ((caller (sb!di:frame-down frame)))
408 (sb!di:flush-frames-above caller)
410 ((or error sb!di:debug-condition) ()
412 (sb!di:debug-condition ()
416 ;;;; INTERNAL-ERROR signal handler
418 (defun internal-error (context continuable)
419 (declare (type system-area-pointer context))
420 (declare (ignore continuable))
421 (/show0 "entering INTERNAL-ERROR, CONTEXT=..")
423 (infinite-error-protect
424 (/show0 "about to bind ALIEN-CONTEXT")
425 (let ((alien-context (locally
426 (declare (optimize (inhibit-warnings 3)))
427 (sb!alien:sap-alien context (* os-context-t)))))
428 (/show0 "about to bind ERROR-NUMBER and ARGUMENTS")
429 (multiple-value-bind (error-number arguments)
430 (sb!vm:internal-error-args alien-context)
432 ;; There's a limit to how much error reporting we can usefully
433 ;; do before initialization is complete, but try to be a little
434 ;; bit helpful before we die.
435 (/show0 "back from INTERNAL-ERROR-ARGS, ERROR-NUMBER=..")
436 (/hexstr error-number)
437 (/show0 "cold/low ARGUMENTS=..")
439 (unless *cold-init-complete-p*
440 (%primitive print "can't recover from error in cold init, halting")
441 (%primitive sb!c:halt))
443 (with-interrupt-bindings
444 (multiple-value-bind (name sb!debug:*stack-top-hint*)
445 (find-interrupted-name-and-frame)
446 (/show0 "back from FIND-INTERRUPTED-NAME")
447 (let ((fp (int-sap (sb!vm:context-register alien-context
449 (handler (and (< -1 error-number (length *internal-errors*))
450 (svref *internal-errors* error-number))))
451 (cond ((null handler)
454 "unknown internal error, ~D, args=~S"
457 (mapcar (lambda (sc-offset)
458 (sb!di::sub-access-debug-var-slot
459 fp sc-offset alien-context))
461 ((not (functionp handler))
463 :format-control "internal error ~D: ~A; args=~S"
467 (mapcar (lambda (sc-offset)
468 (sb!di::sub-access-debug-var-slot
469 fp sc-offset alien-context))
472 (funcall handler name fp alien-context arguments))))))))))
474 (defun control-stack-exhausted-error ()
475 (let ((sb!debug:*stack-top-hint* nil))
476 (infinite-error-protect
477 (format *error-output*
478 "Control stack guard page temporarily disabled: proceed with caution~%")
479 (error 'control-stack-exhausted))))
481 (defun binding-stack-exhausted-error ()
482 (let ((sb!debug:*stack-top-hint* nil))
483 (infinite-error-protect
484 (format *error-output*
485 "Binding stack guard page temporarily disabled: proceed with caution~%")
486 (error 'binding-stack-exhausted))))
488 (defun alien-stack-exhausted-error ()
489 (let ((sb!debug:*stack-top-hint* nil))
490 (infinite-error-protect
491 (format *error-output*
492 "Alien stack guard page temporarily disabled: proceed with caution~%")
493 (error 'alien-stack-exhausted))))
495 ;;; KLUDGE: we keep a single HEAP-EXHAUSTED-ERROR object around, so
496 ;;; that we don't need to allocate it when running out of
497 ;;; memory. Similarly we pass the amounts in special variables as
498 ;;; there may be multiple threads running into trouble at the same
499 ;;; time. The condition is created by GC-REINIT.
500 (defvar *heap-exhausted-error-condition*)
501 (defvar *heap-exhausted-error-available-bytes*)
502 (defvar *heap-exhausted-error-requested-bytes*)
504 (defun heap-exhausted-error (available requested)
505 (infinite-error-protect
506 (let ((*heap-exhausted-error-available-bytes* available)
507 (*heap-exhausted-error-requested-bytes* requested))
508 (error *heap-exhausted-error-condition*))))
510 (defun undefined-alien-variable-error ()
511 (error 'undefined-alien-variable-error))
513 (defun undefined-alien-function-error ()
514 (error 'undefined-alien-function-error))
517 (define-alien-variable current-memory-fault-address unsigned)
520 (defun memory-fault-error ()
521 (error 'memory-fault-error
522 :address current-memory-fault-address))
524 ;;; This is SIGTRAP / EXCEPTION_BREAKPOINT that runtime could not deal
525 ;;; with. Prior to Windows we just had a Lisp side handler for
526 ;;; SIGTRAP, but now we need to deal with this portably.
527 (defun unhandled-trap-error (context-sap)
528 (declare (type system-area-pointer context-sap))
529 (infinite-error-protect
530 (let ((context (sap-alien context-sap (* os-context-t))))
531 (error 'breakpoint-error
533 :address (sap-int (sb!vm:context-pc context))))))