如何在Rust中安全地构造胖指针/指向LV2原子的DST?

问题描述 投票:0回答:1

我正在研究用于Rust的LV2原子的集成,它们是基于切片的动态大小类型(DST)。通常,原子是由主机或其他插件创建的,我的代码只接收一个指向它们的精简指针。因此,我需要从一个瘦指针创建一个指向基于切片的DST的胖指针。这就是我的代码粗略的样子:

#[repr(C)]
struct Atom {
    /// The len of the `data` field.
    len: u32,
    /// The data. 
    data: [u8],
}

/// This is the host. It creates the atom in a generally unknown way and calls
/// the plugin's run function. This isn't really part of my code, but it is
/// needed to understand it.
fn host() {
    // The raw representation of the atom
    let raw_representation: [u8; 8] = [
        // len: Raw representation of a `u32`. We have four data bytes.
        4, 0, 0, 0,
        // The actual data:
        1, 2, 3, 4
    ];

    let ptr: *const u8 = raw_representation.as_ptr();

    plugin_run(ptr);
}

/// This function represents the plugin's run function:
/// It only knows the pointer to the atom, nothing more.
fn plugin_run(ptr: *const u8) {
    // The length of the data.
    let len: u32 = *unsafe { (ptr as *const u32).as_ref() }.unwrap();

    // The "true" representation of the fat pointer.
    let fat_pointer: (*const u8, usize) = (ptr, len as usize);

    // transmuting the tuple into the actuall raw pointer.
    let atom: *const Atom = unsafe { std::mem::transmute(fat_pointer) };

    println!("{:?}", &atom.data);
}

有趣的一行是plugin_run中的倒数第二行:这里,通过转换包含细指针和长度的元组来创建一个胖指针。这种方法有效,但它使用了非常不安全的transmute方法。根据文档,这种方法应该是绝对的最后手段,但据我所知,Rust的设计,我什么都不做,应该是不安全的。我只是创建一个指针!

我不想坚持这个解决方案。除了使用transmute之外,还有一种安全的方法来创建指向基于数组的结构的指针吗?

这方面唯一的问题是std::slice::from_raw_parts,但它也不安全,直接创建一个引用,而不是一个指针,并专门用于切片。我在标准库中找不到任何内容,我在crates.io上找不到任何内容,我甚至在git repo中找不到任何问题。我在搜索错误的关键字吗? Rust真的很受欢迎吗?

如果不存在这样的情况,我会在Rust的Github回购中为此创建一个问题。

编辑:我不应该谈论LV2,它使讨论朝着完全不同的方向发展。一切正常,我已深入思考数据模型并对其进行测试;我唯一想要的是使用transmute创建胖指针的安全替代方法。 :(

pointers types reference rust unsafe
1个回答
0
投票

我正在研究用于Rust的LV2原子的集成

有没有理由你没有使用existing crate that deals with this

Rust的LV2原子,它们是基于切片的动态大小类型

根据the source code I could find的说法,它们不是。相反,它只使用结构组合:

typedef struct {
    uint32_t size;  /**< Size in bytes, not including type and size. */
    uint32_t type;  /**< Type of this atom (mapped URI). */
} LV2_Atom;

/** An atom:Int or atom:Bool.  May be cast to LV2_Atom. */
typedef struct {
    LV2_Atom atom;  /**< Atom header. */
    int32_t  body;  /**< Integer value. */
} LV2_Atom_Int;

official documentation似乎同意。这意味着,由于您只复制标题而不是正文,因此按值获取LV2_Atom可能无效。这通常被称为结构撕裂。


在Rust中,这可以实现为:

use libc::{int32_t, uint32_t};

#[repr(C)]
struct Atom {
    size: uint32_t,
    r#type: uint32_t,
}

#[repr(C)]
struct Int {
    atom: Atom,
    body: int32_t,
}

const INT_TYPE: uint32_t = 42; // Not real

extern "C" {
    fn get_some_atom() -> *const Atom;
}

fn get_int() -> Option<*const Int> {
    unsafe {
        let a = get_some_atom();
        if (*a).r#type == INT_TYPE {
            Some(a as *const Int)
        } else {
            None
        }
    }
}

fn use_int() {
    if let Some(i) = get_int() {
        let val = unsafe { (*i).body };
        println!("{}", val);
    }
}

如果你看一下lv2_raw crate的源代码,你会看到很多相同的东西。还有一个lv2 crate试图带来更高级别的界面。

© www.soinside.com 2019 - 2024. All rights reserved.