分析生锈生成的 C 库的崩溃
Analyzing a crash from a rust-generated C library
我需要从我的 Go
代码中调用 Rust
代码。然后我使用 C
作为我的界面。我这样做了:
我创建了一个 Rust 库,它接受 CStr
作为参数,returns 一个新处理的字符串作为 CStr
。
此代码被静态编译为静态 C 库 my_lib.a
.
然后,这个库与我的 Go
代码静态链接,然后使用 CGo
调用库 API(Go 对 C String 的表示,就像 Rusts 的 Cstr
)。
最终的 Go 二进制文件位于我的 kubernetes 中的 docker 容器中。现在,我的问题是在调用库的 API 的某些情况下,我的 pod(容器)崩溃了。由于使用 CStr
和他的朋友的性质,我必须在某些地方使用 unsafe
范围,我高度怀疑由我的代码中使用的 ptrs 之一引起的段错误,但我有无法将错误传达回可以打印的 Go 代码,或者从 Rust/C 获取某种核心转储,这样我就可以指出有问题的代码。吊舱只是崩溃,没有任何信息,至少据我所知..
所以我的问题是,我怎样才能:
- 从
unsafe
代码中发生的 panic/crashes 中恢复?或者用可恢复的安全范围包裹它?
- 覆盖 SIG 处理程序以便我至少可以“捕获”错误而不是崩溃?所以我可以调试它。
- 也许将 Rust 生成的 c-lib 中引起的信号中断传回给调用者?
我意识到一旦 Rust 被编译成 c 库,这是 C
的问题,但我不知道如何解决这个问题。
谢谢!
I've created a Rust library that takes a CStr as a parameter, and returns a new processed string back as CStr.
这两个操作似乎都不行:
CStr
文档特别指出 CStr
不是 repr(C)
CStr
是借来的字符串,必须拥有“新处理的字符串”(所以 CString
,也不是 repr(C)
)。
Due to the nature of using CStr and his friends, I must use unsafe scopes in some places, and I highly suspect a segfault that is caused by one of the ptrs used in my code, but I have no way of communicating the error back to the Go code that could be then printed OR alternatively get some sort of a core dump from Rust/C so I can point out the problematic code. [...] Recover from panic/crashes that happen inside an unsafe code? or maybe wrap it with a recoverable safe scope?
如果你在进行段错误,那么 Rust 不会以任何方式捕获或操纵任何恐慌或崩溃。段错误意味着 OS 本身会使您的程序消失。但是,您应该以通常的方式进行核心转储,这可能是您的容器配置问题。
Override the SIG handlers so I can at least "catch" the errors and not crash? So I can debug it.
您当然可以尝试处理 SIGSEGV,但在 SIGSEGV 之后我希望程序状态被完全控制,这不是一个无害的信号。
我需要从我的 Go
代码中调用 Rust
代码。然后我使用 C
作为我的界面。我这样做了:
我创建了一个 Rust 库,它接受 CStr
作为参数,returns 一个新处理的字符串作为 CStr
。
此代码被静态编译为静态 C 库 my_lib.a
.
然后,这个库与我的 Go
代码静态链接,然后使用 CGo
调用库 API(Go 对 C String 的表示,就像 Rusts 的 Cstr
)。
最终的 Go 二进制文件位于我的 kubernetes 中的 docker 容器中。现在,我的问题是在调用库的 API 的某些情况下,我的 pod(容器)崩溃了。由于使用 CStr
和他的朋友的性质,我必须在某些地方使用 unsafe
范围,我高度怀疑由我的代码中使用的 ptrs 之一引起的段错误,但我有无法将错误传达回可以打印的 Go 代码,或者从 Rust/C 获取某种核心转储,这样我就可以指出有问题的代码。吊舱只是崩溃,没有任何信息,至少据我所知..
所以我的问题是,我怎样才能:
- 从
unsafe
代码中发生的 panic/crashes 中恢复?或者用可恢复的安全范围包裹它? - 覆盖 SIG 处理程序以便我至少可以“捕获”错误而不是崩溃?所以我可以调试它。
- 也许将 Rust 生成的 c-lib 中引起的信号中断传回给调用者?
我意识到一旦 Rust 被编译成 c 库,这是 C
的问题,但我不知道如何解决这个问题。
谢谢!
I've created a Rust library that takes a CStr as a parameter, and returns a new processed string back as CStr.
这两个操作似乎都不行:
CStr
文档特别指出CStr
不是repr(C)
CStr
是借来的字符串,必须拥有“新处理的字符串”(所以CString
,也不是repr(C)
)。
Due to the nature of using CStr and his friends, I must use unsafe scopes in some places, and I highly suspect a segfault that is caused by one of the ptrs used in my code, but I have no way of communicating the error back to the Go code that could be then printed OR alternatively get some sort of a core dump from Rust/C so I can point out the problematic code. [...] Recover from panic/crashes that happen inside an unsafe code? or maybe wrap it with a recoverable safe scope?
如果你在进行段错误,那么 Rust 不会以任何方式捕获或操纵任何恐慌或崩溃。段错误意味着 OS 本身会使您的程序消失。但是,您应该以通常的方式进行核心转储,这可能是您的容器配置问题。
Override the SIG handlers so I can at least "catch" the errors and not crash? So I can debug it.
您当然可以尝试处理 SIGSEGV,但在 SIGSEGV 之后我希望程序状态被完全控制,这不是一个无害的信号。