Segfault при разыменовании указателя с определением типа, переданного из Rust в C - PullRequest
0 голосов
/ 31 марта 2019

Я пытаюсь написать модуль плагина на Rust для существующего C-проекта. Я писал привязки Rust для C-проекта так, как они мне нужны, и мой последний - каждый раз генерирует segfault независимо от того, как я пытаюсь с ним взаимодействовать.

Вот подпись функции C, к которой я привязан:

switch_xml_t switch_xml_open_cfg(const char *file_path, switch_xml_t *node, switch_event_t *params) 
{
    switch_xml_t xml = NULL, cfg = NULL;
    *node = NULL; // <-- seg-fault happens here
    assert(MAIN_XML_ROOT != NULL);

    if (switch_xml_locate("configuration", "configuration", "name", file_path, &xml, &cfg, params, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
        *node = cfg;
    }
    return xml;
}

и вот привязка Rust и вызов:

extern "C" {
    pub fn switch_xml_open_cfg(
        file_path: *const c_char,
        node: *mut switch_xml_t,
        params: *mut switch_event_t,
    ) -> switch_xml_t;
} 
let mut cfg = switch_xml_t::default();
unsafe {
    libfreeswitch_sys::switch_xml_open_cfg(c_str!("skeleton_rust_raw.conf"), &mut cfg, std::ptr::null_mut());
}

Я попытался передать структуру C как:

  • &mut cfg
  • Box::into_raw указатель
  • &mut cfg as *mut
  • с mem::forget на всякий случай.

Я полностью потерян, когда указатель, который я передаю, обнуляется, когда он разыменовывается.

switch_xml_t - псевдоним типа:

C:

typedef struct switch_xml *switch_xml_t;

Ржавчина:

pub type switch_xml_t = switch_xml;

Для следующей структуры:

C

struct switch_xml {
    /*! tag name */
    char *name;
    /*! tag attributes { name, value, name, value, ... NULL } */
    char **attr;
    /*! tag character content, empty string if none */
    char *txt;
    /*! path to free on destroy */
    char *free_path;
    /*! tag offset from start of parent tag character content */
    switch_size_t off;
    /*! next tag with same name in this section at this depth */
    switch_xml_t next;
    /*! next tag with different name in same section and depth */
    switch_xml_t sibling;
    /*! next tag, same section and depth, in original order */
    switch_xml_t ordered;
    /*! head of sub tag list, NULL if none */
    switch_xml_t child;
    /*! parent tag, NULL if current tag is root tag */
    switch_xml_t parent;
    /*! flags */
    uint32_t flags;
    /*! is_switch_xml_root bool */
    switch_bool_t is_switch_xml_root_t;
    uint32_t refs;
};

Ржавчина:

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct switch_xml {
    pub name: *mut c_char,
    pub attr: *mut *mut c_char,
    pub txt: *mut c_char,
    pub free_path: *mut c_char,
    pub off: switch_size_t,
    pub next: *mut switch_xml_t,
    pub sibling: *mut switch_xml_t,
    pub ordered: *mut switch_xml_t,
    pub child: *mut switch_xml_t,
    pub parent: *mut switch_xml_t,
    pub flags: u32,
    pub is_switch_xml_root_t: switch_bool_t,
    pub refs: u32,
}

Определение Rust также реализует Default:

impl Default for switch_xml {
    fn default() -> Self {
        switch_xml {
            name: std::ptr::null_mut(),
            attr: std::ptr::null_mut(),
            txt: std::ptr::null_mut(),
            free_path: std::ptr::null_mut(),
            off: 0,
            next: std::ptr::null_mut(),
            sibling: std::ptr::null_mut(),
            ordered: std::ptr::null_mut(),
            child: std::ptr::null_mut(),
            parent: std::ptr::null_mut(),
            flags: 0,
            is_switch_xml_root_t: switch_bool_t::SWITCH_FALSE,
            refs: 0
        }
    }
}

1 Ответ

2 голосов
/ 31 марта 2019
pub type switch_xml_t = switch_xml;

Не эквивалентно

typedef struct switch_xml *switch_xml_t;

Этот typedef скрывает указатель.Я действительно не рекомендую этот стиль - одна из причин, почему этот вопрос существует.

Сторона Rust должна быть:

#[allow(non_camel_case_types)]
pub type switch_xml_t = *mut switch_xml;
...