runtime: use a specialized runtime panic function
This specifically fixes unix alloc(): previously when allocation fails it would (recursively) call alloc() again to create an interface due to lacking escape analysis. Also, all other cases shouldn't try to allocate just because something bad happens at runtime. TODO: implement escape analysis.
Этот коммит содержится в:
родитель
4ad6df3227
коммит
31e0662856
2 изменённых файлов: 12 добавлений и 9 удалений
|
@ -1,5 +1,6 @@
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
|
// Builtin function panic(msg), used as a compiler intrinsic.
|
||||||
func _panic(message interface{}) {
|
func _panic(message interface{}) {
|
||||||
printstring("panic: ")
|
printstring("panic: ")
|
||||||
printitf(message)
|
printitf(message)
|
||||||
|
@ -7,28 +8,30 @@ func _panic(message interface{}) {
|
||||||
abort()
|
abort()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cause a runtime panic, which is (currently) always a string.
|
||||||
|
func runtimePanic(msg string) {
|
||||||
|
printstring("panic: runtime error: ")
|
||||||
|
println(msg)
|
||||||
|
abort()
|
||||||
|
}
|
||||||
|
|
||||||
// Check for bounds in *ssa.Index, *ssa.IndexAddr and *ssa.Lookup.
|
// Check for bounds in *ssa.Index, *ssa.IndexAddr and *ssa.Lookup.
|
||||||
func lookupBoundsCheck(length, index int) {
|
func lookupBoundsCheck(length, index int) {
|
||||||
if index < 0 || index >= length {
|
if index < 0 || index >= length {
|
||||||
// printstring() here is safe as this function is excluded from bounds
|
runtimePanic("index out of range")
|
||||||
// checking.
|
|
||||||
printstring("panic: runtime error: index out of range\n")
|
|
||||||
abort()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for bounds in *ssa.Slice.
|
// Check for bounds in *ssa.Slice.
|
||||||
func sliceBoundsCheck(length, low, high uint) {
|
func sliceBoundsCheck(length, low, high uint) {
|
||||||
if !(0 <= low && low <= high && high <= length) {
|
if !(0 <= low && low <= high && high <= length) {
|
||||||
printstring("panic: runtime error: slice out of range\n")
|
runtimePanic("slice out of range")
|
||||||
abort()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for bounds in *ssa.MakeSlice.
|
// Check for bounds in *ssa.MakeSlice.
|
||||||
func sliceBoundsCheckMake(length, capacity uint) {
|
func sliceBoundsCheckMake(length, capacity uint) {
|
||||||
if !(0 <= length && length <= capacity) {
|
if !(0 <= length && length <= capacity) {
|
||||||
printstring("panic: runtime error: slice size out of range\n")
|
runtimePanic("slice size out of range")
|
||||||
abort()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ func abort() {
|
||||||
func alloc(size uintptr) unsafe.Pointer {
|
func alloc(size uintptr) unsafe.Pointer {
|
||||||
buf := _Cfunc_calloc(1, size)
|
buf := _Cfunc_calloc(1, size)
|
||||||
if buf == nil {
|
if buf == nil {
|
||||||
panic("cannot allocate memory")
|
runtimePanic("cannot allocate memory")
|
||||||
}
|
}
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче