rust - C 结构到 Rust 的错误映射

标签 rust stdio libc

出于教育目的,我尝试访问 Rust 中的 FILE 结构:

unsafe {
    let passwd = libc::fopen("/etc/passwd".to_ptr(), &('r' as libc::c_char));
    let fp = &mut *(passwd as *mut MY_FILE);
    println!("flags={}, file={}", fp._flags, fp._file);
}

我通过在 stdio.h 上运行 bindgen 获得的 MY_FILE 结构(我在 OS X 上):

bindgen /usr/include/stdio.h

不知何故 _flags 对于以写入模式打开的文件(在读取模式下为 4)总是 8,所以这个标志似乎关闭了(我用 C 验证它确实不是 4 或 8 的代码)。然而,文件指针似乎是正确的。什么会导致这个?我是从错误的头文件中提取绑定(bind)吗?我需要向 #[repr(C,)] 属性添加什么吗?

Here是包含结构的完整代码。

这是来自 an earlier question 的后续问题

最佳答案

首先,您对 ToPtr 的实现会引入不可靠的代码。转载于此:

// code in italics is wrong
impl ToPtr for str {
    fn to_ptr(&self) -> *const i8 {
        CString::new(self).unwrap().as_ptr()
    }
}

这会分配一个新的 CString 并返回指向其内容的指针,但是当 to_ptr 返回时 CString 被丢弃,所以这是一个悬挂指针。 对该指针的任何取消引用都是未定义的行为。 documentation对此有一个很大的警告,但这仍然是一个非常常见的错误。

从字符串文字生成 *const c_char 的一种正确方法是 b"string here\0".as_ptr() as *const c_char。该字符串以 null 结尾,并且没有悬空指针,因为字符串文字在整个程序中都存在。如果要转换非常量字符串,则必须在使用 CString 时保持其事件状态,如下所示:

let s = "foo";
let cs = CString::new(s).unwrap(); // don't call .as_ptr(), so the CString stays alive
unsafe { some_c_function(cs.as_ptr()); }
// CString is dropped here, after we're done with it

旁白:一位编辑“建议”(我是 Stack Overflow 的新手,但发表评论似乎比尝试重写我的答案更有礼貌)上面的代码可以这样写:

let s = "foo";
unsafe {
    // due to temporary drop rules, the CString will be dropped at the end of the statement (the `;`)
    some_c_function(CString::new(s).unwrap().as_ptr());
}

虽然这在技术上是正确的(最正确的一种),但所涉及的“临时删除规则”是微妙的——这是有效的,因为 as_ptr 引用了 CString (实际上是一个 &CStr,因为编译器将方法链更改为 CString::new(s).unwrap().deref().as_ptr()) 而不是消耗它,因为我们只有一个 C 函数要调用。在编写不安全代码时,我不喜欢依赖任何微妙或不明显的东西。


除此之外,我 fixed that unsoundness在你的代码中(你的调用都使用字符串文字,所以我只是使用了上面的第一个策略)。我在 OSX 上得到这个输出:

0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 8, 1, 0, 0, 0, 0, 0, 0, 0
0, 0, 8, 2, 0, 0, 0, 0, 0, 0, 0
0, 0, 4, 3, 0, 0, 0, 0, 0, 0, 0

那么,这符合您的结果,对吧?我还编写了以下 C 程序:

#include <stdio.h>
#include <unistd.h>

int main() {
    struct __sFILE *fp1 = fdopen(STDIN_FILENO, "r");
    struct __sFILE *fp2 = fdopen(STDOUT_FILENO, "w");
    struct __sFILE *fp3 = fdopen(STDERR_FILENO, "w");
    struct __sFILE *passwd = fopen("/etc/passwd", "r");

    printf("%i %i %i %i\n", fp1->_flags, fp2->_flags, fp3->_flags, passwd->_flags);
}

得到输出:

4 8 8 4

这似乎证明了 Rust 的结果。 /usr/include/stdio.h 的顶部有一条评论说:

/*
 * The following always hold:
 *
 *  if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
 *      _lbfsize is -_bf._size, else _lbfsize is 0
 *  if _flags&__SRD, _w is 0
 *  if _flags&__SWR, _r is 0
 */

并查找这些常量:

#define __SLBF  0x0001      /* line buffered */
#define __SRD   0x0004      /* OK to read */
#define __SWR   0x0008      /* OK to write */

这似乎与我们得到的输出相匹配:4 代表以读取模式打开的文件,8 代表写入。那么这里的问题是什么?

关于rust - C 结构到 Rust 的错误映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169146/

相关文章:

vector - 如何将两个 Rust 向量以三个为一组交织成一个新向量?

rust - 入门Actix-web : failed to run custom build command for `brotli-sys v0.3.2` 时出错

c - 无法计算正确的平均值

c - glibc:stdio 流缓冲区何时何地分配和初始化?

c++ - 链接 libc-dbg 和 libc-prof

c - 重定向断言失败消息

rust - 如果不符合要求,即使应该满足

C 编程。尝试循环诱使用户输入正数

c - STDIO锁定: how to check if every flockfile() has been matched by funlockfile()?

rust - 使用现有可变引用的嵌套方法调用