Query Regarding ASAN C Call from Go Code

434 views
Skip to first unread message

Evgeny Chormonov

unread,
Mar 3, 2024, 9:25:48 PM3/3/24
to golang-nuts
I noticed that ASAN checker in the Go source code is defined using CGO https://github.com/golang/go/blob/master/src/runtime/asan/asan.go
But the C ASAN functions are called from the Go runtime using assembly code instead of a common CGO call. https://github.com/golang/go/blob/master/src/runtime/asan_amd64.s#L36

This discovery has sparked my interest, and I am curious to know if I can employ a similar method to execute my own C functions from my Go code. Could you clarify if this is a viable option?

Ian Lance Taylor

unread,
Mar 3, 2024, 9:43:09 PM3/3/24
to Evgeny Chormonov, golang-nuts
Viable is in the eyes of the beholder. You can use a similar method.
But you have to hand-write the assembler code for each C function you
want to call. And if the C function ever blocks on, say, network I/O,
your entire Go program can hang unexpectedly. We only use this
technique for ASAN because we have a very limited number of functions
to call, and we know for sure that those functions can never block.

Ian

Evgeny Chormonov

unread,
Mar 4, 2024, 5:54:24 PM3/4/24
to golang-nuts
Thanks for your answer, Ian. I've decided to use a code generator based on cgo instead of writing assembly code for each function.

I'm using Linux as my platform, but I'm curious why this solution is restricted to Linux systems only?
понедельник, 4 марта 2024 г. в 00:43:09 UTC+3, Ian Lance Taylor:

Ian Lance Taylor

unread,
Mar 4, 2024, 6:00:11 PM3/4/24
to Evgeny Chormonov, golang-nuts
On Mon, Mar 4, 2024 at 9:55 AM Evgeny Chormonov <en.cho...@gmail.com> wrote:
>
> I'm using Linux as my platform, but I'm curious why this solution is restricted to Linux systems only?
> https://github.com/golang/go/blob/master/src/runtime/asan/asan.go#L5

That's just the list of systems for which we support asan. Offhand I
don't know what other systems the asan library supports. I don't
think there is any obstacle to porting Go's asan support to other
systems that the asan library supports, though of course some such
ports would require new Go assembly code.

Ian


> понедельник, 4 марта 2024 г. в 00:43:09 UTC+3, Ian Lance Taylor:
>>
>> On Sun, Mar 3, 2024 at 1:25 PM Evgeny Chormonov <en.cho...@gmail.com> wrote:
>> >
>> > I noticed that ASAN checker in the Go source code is defined using CGO https://github.com/golang/go/blob/master/src/runtime/asan/asan.go
>> > But the C ASAN functions are called from the Go runtime using assembly code instead of a common CGO call. https://github.com/golang/go/blob/master/src/runtime/asan_amd64.s#L36
>> >
>> > This discovery has sparked my interest, and I am curious to know if I can employ a similar method to execute my own C functions from my Go code. Could you clarify if this is a viable option?
>>
>> Viable is in the eyes of the beholder. You can use a similar method.
>> But you have to hand-write the assembler code for each C function you
>> want to call. And if the C function ever blocks on, say, network I/O,
>> your entire Go program can hang unexpectedly. We only use this
>> technique for ASAN because we have a very limited number of functions
>> to call, and we know for sure that those functions can never block.
>>
>> 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/e5dd08b6-2e05-4bf2-9f51-89d2cb55622fn%40googlegroups.com.

Evgeny Chormonov

unread,
Mar 6, 2024, 5:04:23 PM3/6/24
to golang-nuts

I encountered an issue when trying to use pragma //go:cgo_export_static in my Go code similar to the ASAN code.
https://github.com/golang/go/blob/f0d1195e13e06acdf8999188decc63306f9903f5/src/runtime/asan.go#L65
 It seems there is a check in the compile utility preventing this.
 https://github.com/golang/go/blob/f0d1195e13e06acdf8999188decc63306f9903f5/src/cmd/compile/internal/noder/noder.go#L301

 As a workaround, I'm using the -gcflags="-std" flag, but I'm unsure if this is a viable option.

How can I correctly import C symbols in Go code for static linking without using //go:cgo_import_dynamic?
понедельник, 4 марта 2024 г. в 21:00:11 UTC+3, Ian Lance Taylor:
Reply all
Reply to author
Forward
0 new messages