Does the Allocator API allow sending pointer ownership across an FFI boundary?
还没有人认领这个 Issue。
评估
调研方向
No repository files or tests are named. Start by reading the Allocator documentation section on currently allocated memory and the linked unsafe-code-guidelines discussions on provenance and allocation removal. Done would be a settled specification of whether foreign allocations may be passed to Allocator::deallocate, including the relevant safety requirements.
由索引模型根据 Issue 内容生成。
描述
Does the following code have UB?
extern "C" {
// Hooks for a well-behaved memory allocator implemented in another language.
fn ffi_malloc(...) -> NonNull<[u8]>;
fn ffi_free(...);
// Returns a pointer which was allocated using ffi_malloc. The caller is responsible for freeing it using ffi_free.
fn get_foo_data() -> *mut u8;
}
struct FfiAllocator{};
impl std::alloc::Allocator for FfiAllocator {
// ...Implements allocate() and deallocate() by passing through to ffi_malloc() and ffi_free()
// (omitted for brevity)
};
fn main() {
// Safety(?): get_foo_data() returns a pointer which is owned by the caller and was allocated by the FFI allocator
let _ = unsafe {
Box::from_raw_in(get_foo_data(), FfiAllocator{})
};
}
The summary is:
- A pointer is allocated using FFI. The allocation does not directly use an
Allocator::allocatecall in Rust because it's implemented in another language. - The pointer is deallocated using
Allocator::deallocate, using a custom implementation ofAllocatorwhich forwards to the appropriate FFI method to free the memory.
Based on a strict reading of https://doc.rust-lang.org/std/alloc/trait.Allocator.html#currently-allocated-memory, this is a bit dubious. The result of get_foo_data() is not "currently allocated" via FfiAllocator{} because it was not previously returned by FfiAllocator::allocate.
However, the "currently allocated" precondition is a requirement imposed on the caller of Allocator::deallocate. Are specific implementations of the Allocator trait allowed to loosen this requirement? (For example, FfiAllocator::deallocate could document that it also accepts live pointers that were previously returned from ffi_malloc, even if those pointers were not returned from FfiAllocator::allocate.)
For a "normal" trait, a specific implementation of the trait would be allowed to weaken preconditions like this. However, the Allocator trait has some special interactions with opsem (e.g. allocating memory "mints" a new provenance https://github.com/rust-lang/unsafe-code-guidelines/issues/442, and memory allocations can nondeterministically be removed https://github.com/rust-lang/unsafe-code-guidelines/issues/328), and the details of this are still being figured out. So it's unclear whether allocators are allowed to do this.
Reasons it could theoretically be useful to allow this:
- Allows using the allocator API for allocations that are created by another language. Arguably we could bless this to provide a baseline level of usability for
Allocator, even as we figure out the other questions surroundingAllocator.
Reasons it could theoretically be useful to disallow this:
-
If we allow this, and there are multiple implementations of
FfiAllocator(FfiAllocator1,FfiAllocator2, etc.) which all pass through to the same implementations offfi_mallocandffi_free, then it would probably be valid to allocate a pointer withFfiAllocator1and deallocate the same pointer withFfiAllocator2. This could interfere with alias analysis in the following scenario:let ptr1 = FfiAllocator1::allocate(...)?; let ptr2 = black_box(ptr1, ...); { // performance-sensitive code involving ptr1 and ptr2 } FfiAllocator2::deallocate(ptr2);Hypothetically it might be useful for the compiler to be able to assume that ptr1 and ptr2 don't alias. It's not clear how realistic this is though.
- 主要语言
- 没有语言数据
- 星标
- 760
- 派生
- 68
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
rust-lang/unsafe-code-guidelines 的其他 Issue
-
I-opsem-nominated
难度 4/5 3-5 天 新手友好度 35/100
rust-lang/unsafe-code-guidelines#634 · 5 条评论 ·
-
难度 5/5 一周以上 新手友好度 15/100
-
难度 5/5 一周以上 新手友好度 12/100
rust-lang/unsafe-code-guidelines#632 · 8 条评论 · 1 个 reaction ·
-
难度 5/5 一周以上 新手友好度 10/100
rust-lang/unsafe-code-guidelines#631 · 7 条评论 ·
-
I-opsem-nominated
难度 5/5 一周以上 新手友好度 20/100
rust-lang/unsafe-code-guidelines#630 · 1 个 reaction ·
查看 rust-lang/unsafe-code-guidelines 的全部 Issue
相似的 Issue
-
bug core-lang
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Call-argument syntax diagnostic needs example spacing and removal of confusing trailing sentence未关闭
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
mw00/project-maya#1368 ·
维护者通常 2 天内回复
-
test(bind-json): legacy Solidity fixture requires an unavailable solc on Linux ARM64可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
foundry-rs/foundry#18828 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复