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

View as plain text