在返回 Go Runtime 之前,Cgo 在 x_cgo_notify_runtime_init_done 中被阻塞

标签 c go pthreads cgo

我正在尝试自己编写 runc exec,但是在实现 nsenter 模块时遇到了问题。

这是示例代码:

package main
import "fmt"
/*
#define JUMP_PARENT 0x00
#define JUMP_CHILD  0xA0
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <setjmp.h>

char child_stack[4096] __attribute__ ((aligned(16)));

int child_func(void *arg) {
    jmp_buf* env  = (jmp_buf*)arg;
    longjmp(*env, JUMP_CHILD);
}

__attribute__((constructor)) void init(void) {
    printf("init...\n");
    jmp_buf env;
    switch(setjmp(env)) {
        case JUMP_PARENT: 
        printf("JUMP_PARENT\n");
            int child_pid = clone(child_func, child_stack, CLONE_PARENT, env);
            printf("CHILD_PID: %d\n", child_pid);
            exit(0);
        case JUMP_CHILD:
            printf("JUMP_CHILD\n");
            return;
    }
}
*/
import "C"

func main() {
    fmt.Println("main...")
}

这是 CentOS7 中的输出:

[root@localhost cgo-practive]# go build .
[root@localhost cgo-practive]# ls
cgo-practive  main.go
[root@localhost cgo-practive]# ./cgo-practive
init...
JUMP_PARENT
CHILD_PID: 14348
[root@localhost cgo-practive]# JUMP_CHILD
// program blocked here

然后我用gdb找出了被屏蔽的原因:

(gdb) list
28              exit(0);
29          case JUMP_CHILD:
30              printf("JUMP_CHILD\n");
31              return;
32      }
33  }
34  */
35  import "C"
36
37  func main() {
(gdb) info stack
#0  0x00007efd6f9684ed in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x00007efd6f966170 in pthread_cond_broadcast@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#2  0x00000000004862e6 in x_cgo_notify_runtime_init_done (dummy=<optimized out>) at gcc_libinit.c:69
#3  0x0000000000451070 in runtime.asmcgocall () at /usr/local/go/src/runtime/asm_amd64.s:637
#4  0x00007ffdec4b5c30 in ?? ()
#5  0x000000000044efd1 in runtime.malg.func1 () at /usr/local/go/src/runtime/proc.go:3289
#6  0x000000000044f886 in runtime.systemstack () at /usr/local/go/src/runtime/asm_amd64.s:351
#7  0x000000000042c5b0 in ?? () at /usr/local/go/src/runtime/proc.go:1146
#8  0x000000000044f719 in runtime.rt0_go () at /usr/local/go/src/runtime/asm_amd64.s:201
#9  0x0000000000000000 in ?? ()

程序好像是在x_cgo_notify_runtime_init_done中被阻塞了,但是我不擅长cgo,想不通为什么会被阻塞。

谢谢。

最佳答案

作为man page

Stacks grow downward on all processors that run Linux (except the HP PA processors), so child_stack usually points to the topmost address of the memory space set up for the child stack.

所以你应该改用下面的代码

int child_pid = clone(child_func, &child_stack[4096], CLONE_PARENT, &env);

关于在返回 Go Runtime 之前,Cgo 在 x_cgo_notify_runtime_init_done 中被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55194065/

相关文章:

c - 为什么 binary.Read() 不能正确读取整数?

go - 将单独的值传递给模板函数

c - 将 char* 传递给 pthread 崩溃

go - 在 Ragel 中使用带有扫描仪 block 的堆栈的正确方法是什么?

c++ - Visual Studio 在 vmware 中与 gcc/gdb 交叉编译 -> 内存访问错误

c - C 中与网络相关的低级软件的读物、工具和库

c - SDL OpenGL 纹理受到绿色调的困扰

c - 错误 - pthread_mutex_lock() 失败 : Invalid argument

multithreading - pthread_join跳过第一个线程

c++ - ImageMagick 使用我的真实字体 (Magick++) 转换 SVG