gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:07:12 25 4
gpt4 key购买 nike

我正在尝试自己编写 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com