Runtime panic identification

121 views
Skip to first unread message

Nikhilesh Susarla

unread,
Dec 26, 2022, 12:49:24 PM12/26/22
to golang-nuts
Hello Gophers, 


An array out of bounds throws panic at runtime. 

How does the internal runtime know that we are accessing the memory which we are not allocated? Interested in knowing more depth of the internals. 

This Link talks about the compile time on how the compiler avoids the bound checks. 

But during runtime I am more interested to know what happens when we access the invalid memory local through indexing. 

Does the OS return a signal and go runtime converts that into a panic? Any links or more reading links are appreciated.

Thank you

Ian Lance Taylor

unread,
Dec 26, 2022, 1:19:12 PM12/26/22
to Nikhilesh Susarla, golang-nuts
Yes, on Unix systems, that is what happens. See
runtime/signal_unix.go, the function sighandler, the case where
SigPanic is set for the signal.

Ian

Nikhilesh Susarla

unread,
Dec 26, 2022, 1:56:28 PM12/26/22
to golang-nuts
So the SigPanic() captures the  "_SIGSEGV" and then panics the whole program right?

Ian Lance Taylor

unread,
Dec 26, 2022, 2:29:33 PM12/26/22
to Nikhilesh Susarla, golang-nuts
On Mon, Dec 26, 2022 at 10:56 AM Nikhilesh Susarla
<nikhil...@gmail.com> wrote:
>
> So the SigPanic() captures the "_SIGSEGV" and then panics the whole program right?

It depends on precisely what you mean by that. SIGSEGV is marked with
the SigPanic flag in runtime/sigtab_linux_generic.go or whatever is
the equivalent for your GOOS. The SIGSEGV is turned into a runtime
panic, just as though the user code called panic(someValue). If
nothing recovers the panic, then the program will crash just as for
any unrecovered panic.

Ian
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7a176761-0f85-417d-8df1-a743c48dfd44n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages