1.0.25.50: detect binding and alien stack exhaustion
[sbcl.git] / src / code / interr.lisp
index 8614280..9014b56 100644 (file)
          :format-arguments (list key-name)))
 
 (deferr invalid-array-index-error (array bound index)
-  (error 'simple-type-error
-         :format-control
-         "invalid array index ~W for ~S (should be nonnegative and <~W)"
-         :format-arguments (list index array bound)
-         :datum index
-         :expected-type `(integer 0 (,bound))))
+  (invalid-array-index-error array bound index))
 
 (deferr object-not-simple-array-error (object)
   (error 'type-error
          :datum object
          :expected-type '(unsigned-byte 32)))
 
+(deferr tls-exhausted-error ()
+  ;; There is nothing we can do about it. A number of entries in the
+  ;; tls could be reserved and made available for recovery but since
+  ;; tls indices are never reused it would be kind of silly and
+  ;; without it signalling an error is more than likely to end in a
+  ;; recursive error.
+  (%primitive print "Thread local storage exhausted.")
+  (sb!impl::%halt))
+
 (macrolet
     ((define-simple-array-internal-errors ()
          `(progn
           (values "<error finding interrupted name -- trapped debug-condition>"
                   nil)))))
 \f
+
 ;;;; INTERNAL-ERROR signal handler
 
 (defun internal-error (context continuable)
   (infinite-error-protect
    (/show0 "about to bind ALIEN-CONTEXT")
    (let ((alien-context (locally
-                          (declare (optimize (inhibit-warnings 3)))
+                            (declare (optimize (inhibit-warnings 3)))
                           (sb!alien:sap-alien context (* os-context-t)))))
      (/show0 "about to bind ERROR-NUMBER and ARGUMENTS")
      (multiple-value-bind (error-number arguments)
          (%primitive print "can't recover from error in cold init, halting")
          (%primitive sb!c:halt))
 
-       (multiple-value-bind (name sb!debug:*stack-top-hint*)
-           (find-interrupted-name-and-frame)
-         (/show0 "back from FIND-INTERRUPTED-NAME")
-         (let ((fp (int-sap (sb!vm:context-register alien-context
-                                                    sb!vm::cfp-offset)))
-               (handler (and (< -1 error-number (length *internal-errors*))
-                             (svref *internal-errors* error-number))))
-           (cond ((null handler)
-                  (error 'simple-error
-                         :format-control
-                         "unknown internal error, ~D, args=~S"
-                         :format-arguments
-                         (list error-number
-                               (mapcar (lambda (sc-offset)
-                                         (sb!di::sub-access-debug-var-slot
-                                          fp sc-offset alien-context))
-                                       arguments))))
-                 ((not (functionp handler))
-                  (error 'simple-error
-                         :format-control "internal error ~D: ~A; args=~S"
-                         :format-arguments
-                         (list error-number
-                               handler
-                               (mapcar (lambda (sc-offset)
-                                         (sb!di::sub-access-debug-var-slot
-                                          fp sc-offset alien-context))
-                                       arguments))))
-                 (t
-                  (funcall handler name fp alien-context arguments)))))))))
+       (with-interrupt-bindings
+         (multiple-value-bind (name sb!debug:*stack-top-hint*)
+             (find-interrupted-name-and-frame)
+           (/show0 "back from FIND-INTERRUPTED-NAME")
+           (let ((fp (int-sap (sb!vm:context-register alien-context
+                                                      sb!vm::cfp-offset)))
+                 (handler (and (< -1 error-number (length *internal-errors*))
+                               (svref *internal-errors* error-number))))
+             (cond ((null handler)
+                    (error 'simple-error
+                           :format-control
+                           "unknown internal error, ~D, args=~S"
+                           :format-arguments
+                           (list error-number
+                                 (mapcar (lambda (sc-offset)
+                                           (sb!di::sub-access-debug-var-slot
+                                            fp sc-offset alien-context))
+                                         arguments))))
+                   ((not (functionp handler))
+                    (error 'simple-error
+                           :format-control "internal error ~D: ~A; args=~S"
+                           :format-arguments
+                           (list error-number
+                                 handler
+                                 (mapcar (lambda (sc-offset)
+                                           (sb!di::sub-access-debug-var-slot
+                                            fp sc-offset alien-context))
+                                         arguments))))
+                   (t
+                    (funcall handler name fp alien-context arguments))))))))))
 
 (defun control-stack-exhausted-error ()
   (let ((sb!debug:*stack-top-hint* nil))
              "Control stack guard page temporarily disabled: proceed with caution~%")
      (error 'control-stack-exhausted))))
 
+(defun binding-stack-exhausted-error ()
+  (let ((sb!debug:*stack-top-hint* nil))
+    (infinite-error-protect
+     (format *error-output*
+             "Binding stack guard page temporarily disabled: proceed with caution~%")
+     (error 'binding-stack-exhausted))))
+
+(defun alien-stack-exhausted-error ()
+  (let ((sb!debug:*stack-top-hint* nil))
+    (infinite-error-protect
+     (format *error-output*
+             "Alien stack guard page temporarily disabled: proceed with caution~%")
+     (error 'alien-stack-exhausted))))
+
 ;;; KLUDGE: we keep a single HEAP-EXHAUSTED-ERROR object around, so
-;;; that we don't need to allocate it when running out of memory. Similarly
-;;; we pass the amounts in special variables as there may be multiple threads
-;;; running into trouble at the same time. The condition is created by GC-REINIT.
+;;; that we don't need to allocate it when running out of
+;;; memory. Similarly we pass the amounts in special variables as
+;;; there may be multiple threads running into trouble at the same
+;;; time. The condition is created by GC-REINIT.
 (defvar *heap-exhausted-error-condition*)
 (defvar *heap-exhausted-error-available-bytes*)
 (defvar *heap-exhausted-error-requested-bytes*)
   (error 'undefined-alien-function-error))
 
 #!-win32
-(define-alien-variable current-memory-fault-address long)
+(define-alien-variable current-memory-fault-address unsigned-long)
 
 #!-win32
 (defun memory-fault-error ()
   (error 'memory-fault-error
          :address current-memory-fault-address))
+
+;;; This is SIGTRAP / EXCEPTION_BREAKPOINT that runtime could not deal
+;;; with. Prior to Windows we just had a Lisp side handler for
+;;; SIGTRAP, but now we need to deal with this portably.
+(defun unhandled-trap-error (context-sap)
+  (declare (type system-area-pointer context-sap))
+  (infinite-error-protect
+   (let ((context (sap-alien context-sap (* os-context-t))))
+     (error 'breakpoint-error
+            :context context
+            :address (sap-int (sb!vm:context-pc context))))))