Is it possible to intercept runtime futex function call?

230 views
Skip to first unread message

Yosef Yo

unread,
Aug 5, 2020, 11:02:07 PM8/5/20
to golang-nuts
In go/src/runtime/os/linux.go, function futexsleep is defined as to call futex function of linux. runtime. futex is implemented in syscall assembly instruction. I am currently dynamically loading a golang c-shared library as a plugin, and I wanted to intercept the futex syscall and use my own implementation. Can anyone give me a hint on how to intercept runtime functions? OR is it even possible?

Thanks in advance! 

Ian Lance Taylor

unread,
Aug 5, 2020, 11:15:16 PM8/5/20
to Yosef Yo, golang-nuts
On Wed, Aug 5, 2020 at 8:01 PM Yosef Yo <yngeta...@gmail.com> wrote:
>
> In go/src/runtime/os/linux.go, function futexsleep is defined as to call futex function of linux. runtime. futex is implemented in syscall assembly instruction. I am currently dynamically loading a golang c-shared library as a plugin, and I wanted to intercept the futex syscall and use my own implementation. Can anyone give me a hint on how to intercept runtime functions? OR is it even possible?

There is no support for this. As far as I know it is not possible.

Ian

Kurtis Rader

unread,
Aug 5, 2020, 11:19:29 PM8/5/20
to Yosef Yo, golang-nuts
On Wed, Aug 5, 2020 at 8:02 PM Yosef Yo <yngeta...@gmail.com> wrote:
In go/src/runtime/os/linux.go, function futexsleep is defined as to call futex function of linux. runtime. futex is implemented in syscall assembly instruction. I am currently dynamically loading a golang c-shared library as a plugin, and I wanted to intercept the futex syscall and use my own implementation. Can anyone give me a hint on how to intercept runtime functions? OR is it even possible?

What you want to do isn't possible without resorting to "monkey patching" (see https://en.wikipedia.org/wiki/Monkey_patch) since the wrapper you want to intercept is part of the runtime support code that is statically linked into the binary. At least not in any practical manner. The impractical solutions involve mechanisms such as the platform (not Go) "ptrace" API. The only practical solution is to create your own custom Go compiler that incorporates your modification to the use of the futex syscall.

On the other hand your question is unclear, even puzzling, because the use of a dynamically loaded shared library, written in C, doesn't depend on the Go runtime use of the futex API. At least not directly. That dynamically loaded code won't be using the futex code in the Go runtime.

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Yosef Yo

unread,
Aug 6, 2020, 12:11:02 AM8/6/20
to golang-nuts
Thank you for your reply. What I meant was actually dynamically loading a C-shared library written in go (buildmode=c-shared).

Brian Candler

unread,
Aug 6, 2020, 1:56:50 AM8/6/20
to golang-nuts
Do you want to intercept all syscalls, from both the main program and the loaded library?  Maybe running under a modified gvisor would do it.
Reply all
Reply to author
Forward
0 new messages