从 Rust 到 C 的原始指针变为空

Raw pointer turns null passing from Rust to C

我正在尝试从 Rust 中的 C 函数中检索原始指针,并使用相同的原始指针作为另一个库中另一个 C 函数的参数。当我传递原始指针时,我最终在 C 端得到一个 NULL 指针。

我曾尝试制作我的问题的简化版本,但当我这样做时,它会像我期望的那样工作 -

C代码-

struct MyStruct {
    int value;
};

struct MyStruct * get_struct() {
    struct MyStruct * priv_struct = (struct MyStruct*) malloc( sizeof(struct MyStruct));

    priv_struct->value = 0;
    return priv_struct;
}

void put_struct(struct MyStruct *priv_struct) {
    printf("Value - %d\n", priv_struct->value);
}

Rust 代码 -

#[repr(C)]
struct MyStruct {
    value: c_int,
}

extern {
    fn get_struct() -> *mut MyStruct;
}

extern {
    fn put_struct(priv_struct: *mut MyStruct) -> ();
}

fn rust_get_struct() -> *mut MyStruct {
    let ret = unsafe { get_struct() };
    ret
}

fn rust_put_struct(priv_struct: *mut MyStruct) {
    unsafe { put_struct(priv_struct) };
}

fn main() {
    let main_struct = rust_get_struct();
    rust_put_struct(main_struct);
}

当我 运行 时,我得到值的输出 - 0

~/Dev/rust_test$ sudo ./target/debug/rust_test 
Value - 0
~/Dev/rust_test$

但是,当尝试针对 DPDK 库执行此操作时,我以相同的方式检索并传递了一个原始指针,但出现了段错误。如果我使用 gdb 进行调试,我可以看到我在 Rust 端传递了一个指针,但我在 C 端看到它为 NULL -

(gdb) frame 0
#0  rte_eth_rx_queue_setup (port_id=0 '[=13=]0', rx_queue_id=<optimized out>, nb_rx_desc=<optimized out>, socket_id=0, rx_conf=0x0, mp=0x0)
   at /home/kenton/Dev/dpdk-16.07/lib/librte_ether/rte_ethdev.c:1216
1216    if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {

(gdb) frame 1
#1  0x000055555568953b in dpdk::ethdev::dpdk_rte_eth_rx_queue_setup (port_id=0 '[=13=]0', rx_queue_id=0, nb_tx_desc=128, socket_id=0, rx_conf=None, 
   mb=0x7fff3fe47640) at /home/kenton/Dev/dpdk_ffi/src/ethdev/mod.rs:32
32     let retc: c_int = unsafe {ffi::rte_eth_rx_queue_setup(port_id as uint8_t,

在第 1 帧中,mb 有一个地址,正在传递。在第 0 帧中,库中的接收函数将其显示为 mp.

的 0x0

我的接收指针的代码-

let mb = dpdk_rte_pktmbuf_pool_create(CString::new("MBUF_POOL").unwrap().as_ptr(),
       (8191 * nb_ports) as u32 , 250, 0, 2176, dpdk_rte_socket_id());

这会调用 ffi 库 -

pub fn dpdk_rte_pktmbuf_pool_create(name: *const c_char,
                               n: u32,
                               cache_size: u32,
                               priv_size: u16,
                               data_room_size: u16,
                               socket_id: i32) -> *mut rte_mempool::ffi::RteMempool {
    let ret: *mut rte_mempool::ffi::RteMempool = unsafe {
        ffi::shim_rte_pktmbuf_pool_create(name,
                                          n as c_uint,
                                          cache_size as c_uint,
                                          priv_size as uint16_t,
                                          data_room_size as uint16_t,
                                          socket_id as c_int)
    };
    ret
}

ffi -

extern {
    pub fn shim_rte_pktmbuf_pool_create(name: *const c_char,
                                        n: c_uint,
                                        cache_size: c_uint,
                                        priv_size: uint16_t,
                                        data_room_size: uint16_t,
                                        socket_id: c_int) -> *mut rte_mempool::ffi::RteMempool;
}

C 函数 -

struct rte_mempool *
rte_pktmbuf_pool_create(const char *name, unsigned n,
    unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
    int socket_id);

当我传递指针时,它看起来与我上面的简化版本非常相似。我的变量 mb 包含我传递给另一个函数的原始指针 -

ret = dpdk_rte_eth_rx_queue_setup(port,q,128,0,None,mb);

ffi 库 -

pub fn dpdk_rte_eth_rx_queue_setup(port_id: u8,
                                   rx_queue_id: u16,
                                   nb_tx_desc: u16,
                                   socket_id: u32,
                                   rx_conf: Option<*const ffi::RteEthRxConf>,
                                   mb_pool: *mut rte_mempool::ffi::RteMempool ) -> i32 {
    let retc: c_int = unsafe {ffi::rte_eth_rx_queue_setup(port_id as uint8_t,
                                                          rx_queue_id as uint16_t,
                                                          nb_tx_desc as uint16_t,
                                                          socket_id as c_uint,
                                                          rx_conf,
                                                          mb)};
    let ret: i32 = retc as i32;
    ret
}

ffi -

extern {
    pub fn rte_eth_rx_queue_setup(port_id: uint8_t,
                              rx_queue_id: uint16_t,
                              nb_tx_desc: uint16_t,
                              socket_id: c_uint,
                              rx_conf: Option<*const RteEthRxConf>,
                              mb: *mut rte_mempool::ffi::RteMempool ) -> c_int;
}

C函数-

int
rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
               uint16_t nb_rx_desc, unsigned int socket_id,
               const struct rte_eth_rxconf *rx_conf,
               struct rte_mempool *mp);

对于篇幅,我深表歉意,但我觉得我遗漏了一些简单的东西,而且一直无法弄清楚。我已经检查了每个正在传递的字段的结构对齐方式,我什至看到了我所期望的接收到的指针值 -

(gdb) frame 1
#1  0x000055555568dcf4 in dpdk::ethdev::dpdk_rte_eth_rx_queue_setup (port_id=0 '[=22=]0', rx_queue_id=0, nb_tx_desc=128, socket_id=0, rx_conf=None, 
    mb=0x7fff3fe47640) at /home/kenton/Dev/dpdk_ffi/src/ethdev/mod.rs:32
32      let retc: c_int = unsafe {ffi::rte_eth_rx_queue_setup(port_id as uint8_t,

(gdb) print *mb
 = RteMempool = {name = "MBUF_POOL", '[=22=]0' <repeats 22 times>, pool_union = PoolUnionStruct = {data = 140734245862912}, pool_config = 0x0, 
  mz = 0x7ffff7fa4c68, flags = 16, socket_id = 0, size = 8191, cache_size = 250, elt_size = 2304, header_size = 64, trailer_size = 0, 
  private_data_size = 64, ops_index = 0, local_cache = 0x7fff3fe47700, populated_size = 8191, elt_list = RteMempoolObjhdrList = {
    stqh_first = 0x7fff3ebc7f68, stqh_last = 0x7fff3fe46ce8}, nb_mem_chunks = 1, mem_list = RteMempoolMemhdrList = {stqh_first = 0x7fff3ebb7d80, 
stqh_last = 0x7fff3ebb7d80}, __align = 0x7fff3fe47700}

关于为什么指针在 C 端变为 NULL 的任何想法?

CString::new("…").unwrap().as_ptr() 不起作用。 CString 是临时的,因此 as_ptr() 调用 returns 那个临时的内部指针,当你使用它时它可能会悬空。只要您不使用指针,根据 Rust 的安全定义,这是“安全的”,但您最终会在 unsafe 块中这样做。您应该将字符串绑定到一个变量并在该变量上使用 as_ptr

这是一个很常见的问题,甚至还有a proposal to fix the CStr{,ing} API to avoid it

此外,原始指针本身可以为空,因此 const struct rte_eth_rxconf * 的 Rust FFI 等效项将是 *const ffi::RteEthRxConf,而不是 Option<*const ffi::RteEthRxConf>