Source file src/runtime/error.go

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime
     6  
     7  import (
     8  	"internal/abi"
     9  	"internal/bytealg"
    10  	"internal/runtime/sys"
    11  )
    12  
    13  // Error identifies a runtime error used in panic.
    14  //
    15  // The Go runtime triggers panics for a variety of cases, as described by the
    16  // Go Language Spec, such as out-of-bounds slice/array access, close of nil
    17  // channels, type assertion failures, etc.
    18  //
    19  // When these cases occur, the Go runtime panics with an error that implements
    20  // Error. This can be useful when recovering from panics to distinguish between
    21  // custom application panics and fundamental runtime panics.
    22  //
    23  // Packages outside of the Go standard library should not implement Error.
    24  type Error interface {
    25  	error
    26  
    27  	// RuntimeError is a no-op function but
    28  	// serves to distinguish types that are runtime
    29  	// errors from ordinary errors: a type is a
    30  	// runtime error if it has a RuntimeError method.
    31  	RuntimeError()
    32  }
    33  
    34  // A TypeAssertionError explains a failed type assertion.
    35  type TypeAssertionError struct {
    36  	_interface    *_type
    37  	concrete      *_type
    38  	asserted      *_type
    39  	missingMethod string // one method needed by Interface, missing from Concrete
    40  }
    41  
    42  func (*TypeAssertionError) RuntimeError() {}
    43  
    44  func (e *TypeAssertionError) Error() string {
    45  	inter := "interface"
    46  	if e._interface != nil {
    47  		inter = toRType(e._interface).string()
    48  	}
    49  	as := toRType(e.asserted).string()
    50  	if e.concrete == nil {
    51  		return "interface conversion: " + inter + " is nil, not " + as
    52  	}
    53  	cs := toRType(e.concrete).string()
    54  	if e.missingMethod == "" {
    55  		msg := "interface conversion: " + inter + " is " + cs + ", not " + as
    56  		if cs == as {
    57  			// provide slightly clearer error message
    58  			if toRType(e.concrete).pkgpath() != toRType(e.asserted).pkgpath() {
    59  				msg += " (types from different packages)"
    60  			} else {
    61  				msg += " (types from different scopes)"
    62  			}
    63  		}
    64  		return msg
    65  	}
    66  	return "interface conversion: " + cs + " is not " + as +
    67  		": missing method " + e.missingMethod
    68  }
    69  
    70  // itoa converts val to a decimal representation. The result is
    71  // written somewhere within buf and the location of the result is returned.
    72  // buf must be at least 20 bytes.
    73  //
    74  //go:nosplit
    75  func itoa(buf []byte, val uint64) []byte {
    76  	i := len(buf) - 1
    77  	for val >= 10 {
    78  		buf[i] = byte(val%10 + '0')
    79  		i--
    80  		val /= 10
    81  	}
    82  	buf[i] = byte(val + '0')
    83  	return buf[i:]
    84  }
    85  
    86  // An errorString represents a runtime error described by a single string.
    87  type errorString string
    88  
    89  func (e errorString) RuntimeError() {}
    90  
    91  func (e errorString) Error() string {
    92  	return "runtime error: " + string(e)
    93  }
    94  
    95  type errorAddressString struct {
    96  	msg  string  // error message
    97  	addr uintptr // memory address where the error occurred
    98  }
    99  
   100  func (e errorAddressString) RuntimeError() {}
   101  
   102  func (e errorAddressString) Error() string {
   103  	return "runtime error: " + e.msg
   104  }
   105  
   106  // Addr returns the memory address where a fault occurred.
   107  // The address provided is best-effort.
   108  // The veracity of the result may depend on the platform.
   109  // Errors providing this method will only be returned as
   110  // a result of using [runtime/debug.SetPanicOnFault].
   111  func (e errorAddressString) Addr() uintptr {
   112  	return e.addr
   113  }
   114  
   115  // plainError represents a runtime error described a string without
   116  // the prefix "runtime error: " after invoking errorString.Error().
   117  // See Issue #14965.
   118  type plainError string
   119  
   120  func (e plainError) RuntimeError() {}
   121  
   122  func (e plainError) Error() string {
   123  	return string(e)
   124  }
   125  
   126  // A boundsError represents an indexing or slicing operation gone wrong.
   127  type boundsError struct {
   128  	x int64
   129  	y int
   130  	// Values in an index or slice expression can be signed or unsigned.
   131  	// That means we'd need 65 bits to encode all possible indexes, from -2^63 to 2^64-1.
   132  	// Instead, we keep track of whether x should be interpreted as signed or unsigned.
   133  	// y is known to be nonnegative and to fit in an int.
   134  	signed bool
   135  	code   abi.BoundsErrorCode
   136  }
   137  
   138  // boundsErrorFmts provide error text for various out-of-bounds panics.
   139  // Note: if you change these strings, you should adjust the size of the buffer
   140  // in boundsError.Error below as well.
   141  var boundsErrorFmts = [...]string{
   142  	abi.BoundsIndex:      "index out of range [%x] with length %y",
   143  	abi.BoundsSliceAlen:  "slice bounds out of range [:%x] with length %y",
   144  	abi.BoundsSliceAcap:  "slice bounds out of range [:%x] with capacity %y",
   145  	abi.BoundsSliceB:     "slice bounds out of range [%x:%y]",
   146  	abi.BoundsSlice3Alen: "slice bounds out of range [::%x] with length %y",
   147  	abi.BoundsSlice3Acap: "slice bounds out of range [::%x] with capacity %y",
   148  	abi.BoundsSlice3B:    "slice bounds out of range [:%x:%y]",
   149  	abi.BoundsSlice3C:    "slice bounds out of range [%x:%y:]",
   150  	abi.BoundsConvert:    "cannot convert slice with length %y to array or pointer to array with length %x",
   151  }
   152  
   153  // boundsNegErrorFmts are overriding formats if x is negative. In this case there's no need to report y.
   154  var boundsNegErrorFmts = [...]string{
   155  	abi.BoundsIndex:      "index out of range [%x]",
   156  	abi.BoundsSliceAlen:  "slice bounds out of range [:%x]",
   157  	abi.BoundsSliceAcap:  "slice bounds out of range [:%x]",
   158  	abi.BoundsSliceB:     "slice bounds out of range [%x:]",
   159  	abi.BoundsSlice3Alen: "slice bounds out of range [::%x]",
   160  	abi.BoundsSlice3Acap: "slice bounds out of range [::%x]",
   161  	abi.BoundsSlice3B:    "slice bounds out of range [:%x:]",
   162  	abi.BoundsSlice3C:    "slice bounds out of range [%x::]",
   163  }
   164  
   165  func (e boundsError) RuntimeError() {}
   166  
   167  func appendIntStr(b []byte, v int64, signed bool) []byte {
   168  	if signed && v < 0 {
   169  		b = append(b, '-')
   170  		v = -v
   171  	}
   172  	var buf [20]byte
   173  	b = append(b, itoa(buf[:], uint64(v))...)
   174  	return b
   175  }
   176  
   177  func (e boundsError) Error() string {
   178  	fmt := boundsErrorFmts[e.code]
   179  	if e.signed && e.x < 0 {
   180  		fmt = boundsNegErrorFmts[e.code]
   181  	}
   182  	// max message length is 99: "runtime error: slice bounds out of range [::%x] with capacity %y"
   183  	// x can be at most 20 characters. y can be at most 19.
   184  	b := make([]byte, 0, 100)
   185  	b = append(b, "runtime error: "...)
   186  	for i := 0; i < len(fmt); i++ {
   187  		c := fmt[i]
   188  		if c != '%' {
   189  			b = append(b, c)
   190  			continue
   191  		}
   192  		i++
   193  		switch fmt[i] {
   194  		case 'x':
   195  			b = appendIntStr(b, e.x, e.signed)
   196  		case 'y':
   197  			b = appendIntStr(b, int64(e.y), true)
   198  		}
   199  	}
   200  	return string(b)
   201  }
   202  
   203  type stringer interface {
   204  	String() string
   205  }
   206  
   207  // printpanicval prints an argument passed to panic.
   208  // If panic is called with a value that has a String or Error method,
   209  // it has already been converted into a string by preprintpanics.
   210  //
   211  // To ensure that the traceback can be unambiguously parsed even when
   212  // the panic value contains "\ngoroutine" and other stack-like
   213  // strings, newlines in the string representation of v are replaced by
   214  // "\n\t".
   215  func printpanicval(v any) {
   216  	switch v := v.(type) {
   217  	case nil:
   218  		print("nil")
   219  	case bool:
   220  		print(v)
   221  	case int:
   222  		print(v)
   223  	case int8:
   224  		print(v)
   225  	case int16:
   226  		print(v)
   227  	case int32:
   228  		print(v)
   229  	case int64:
   230  		print(v)
   231  	case uint:
   232  		print(v)
   233  	case uint8:
   234  		print(v)
   235  	case uint16:
   236  		print(v)
   237  	case uint32:
   238  		print(v)
   239  	case uint64:
   240  		print(v)
   241  	case uintptr:
   242  		print(v)
   243  	case float32:
   244  		print(v)
   245  	case float64:
   246  		print(v)
   247  	case complex64:
   248  		print(v)
   249  	case complex128:
   250  		print(v)
   251  	case string:
   252  		printindented(v)
   253  	default:
   254  		printanycustomtype(v)
   255  	}
   256  }
   257  
   258  // Invariant: each newline in the string representation is followed by a tab.
   259  func printanycustomtype(i any) {
   260  	eface := efaceOf(&i)
   261  	typestring := toRType(eface._type).string()
   262  
   263  	switch eface._type.Kind_ {
   264  	case abi.String:
   265  		print(typestring, `("`)
   266  		printindented(*(*string)(eface.data))
   267  		print(`")`)
   268  	case abi.Bool:
   269  		print(typestring, "(", *(*bool)(eface.data), ")")
   270  	case abi.Int:
   271  		print(typestring, "(", *(*int)(eface.data), ")")
   272  	case abi.Int8:
   273  		print(typestring, "(", *(*int8)(eface.data), ")")
   274  	case abi.Int16:
   275  		print(typestring, "(", *(*int16)(eface.data), ")")
   276  	case abi.Int32:
   277  		print(typestring, "(", *(*int32)(eface.data), ")")
   278  	case abi.Int64:
   279  		print(typestring, "(", *(*int64)(eface.data), ")")
   280  	case abi.Uint:
   281  		print(typestring, "(", *(*uint)(eface.data), ")")
   282  	case abi.Uint8:
   283  		print(typestring, "(", *(*uint8)(eface.data), ")")
   284  	case abi.Uint16:
   285  		print(typestring, "(", *(*uint16)(eface.data), ")")
   286  	case abi.Uint32:
   287  		print(typestring, "(", *(*uint32)(eface.data), ")")
   288  	case abi.Uint64:
   289  		print(typestring, "(", *(*uint64)(eface.data), ")")
   290  	case abi.Uintptr:
   291  		print(typestring, "(", *(*uintptr)(eface.data), ")")
   292  	case abi.Float32:
   293  		print(typestring, "(", *(*float32)(eface.data), ")")
   294  	case abi.Float64:
   295  		print(typestring, "(", *(*float64)(eface.data), ")")
   296  	case abi.Complex64:
   297  		print(typestring, *(*complex64)(eface.data))
   298  	case abi.Complex128:
   299  		print(typestring, *(*complex128)(eface.data))
   300  	default:
   301  		print("(", typestring, ") ", eface.data)
   302  	}
   303  }
   304  
   305  // printindented prints s, replacing "\n" with "\n\t".
   306  func printindented(s string) {
   307  	for {
   308  		i := bytealg.IndexByteString(s, '\n')
   309  		if i < 0 {
   310  			break
   311  		}
   312  		i += len("\n")
   313  		print(s[:i])
   314  		print("\t")
   315  		s = s[i:]
   316  	}
   317  	print(s)
   318  }
   319  
   320  // panicwrap generates a panic for a call to a wrapped value method
   321  // with a nil pointer receiver.
   322  //
   323  // It is called from the generated wrapper code.
   324  func panicwrap() {
   325  	pc := sys.GetCallerPC()
   326  	name := funcNameForPrint(funcname(findfunc(pc)))
   327  	// name is something like "main.(*T).F".
   328  	// We want to extract pkg ("main"), typ ("T"), and meth ("F").
   329  	// Do it by finding the parens.
   330  	i := bytealg.IndexByteString(name, '(')
   331  	if i < 0 {
   332  		throw("panicwrap: no ( in " + name)
   333  	}
   334  	pkg := name[:i-1]
   335  	if i+2 >= len(name) || name[i-1:i+2] != ".(*" {
   336  		throw("panicwrap: unexpected string after package name: " + name)
   337  	}
   338  	name = name[i+2:]
   339  	i = bytealg.IndexByteString(name, ')')
   340  	if i < 0 {
   341  		throw("panicwrap: no ) in " + name)
   342  	}
   343  	if i+2 >= len(name) || name[i:i+2] != ")." {
   344  		throw("panicwrap: unexpected string after type name: " + name)
   345  	}
   346  	typ := name[:i]
   347  	meth := name[i+2:]
   348  	panic(plainError("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer"))
   349  }
   350  

View as plain text