Skip to content

Commit 0f31d8e

Browse files
felix91grdarkwisebear
authored andcommitted
Add guideline for issue #137
Co-authored-by: darkwisebear <darkwisebear@users.noreply.github.com>
1 parent 3dab1e4 commit 0f31d8e

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

src/coding-guidelines/ffi.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,87 @@
66
FFI
77
===
88

9+
10+
.. guideline:: Use matching type declarations at the language boundary
11+
:id: gui_QmEmKMYSuQSl
12+
:category: required
13+
:status: draft
14+
:release: 1.0.0-latest
15+
:fls: fls_v24ino4hix3m
16+
:decidability: decidable
17+
:scope: crate
18+
:tags: undefined-behavior,reduce-human-error
19+
20+
If it is required or advisable (e.g. to provide a "rusty" interface) to use different types on the Rust side than on the foreign function side, the respective type changes shall not be done on the FFI boundary but on an additional layer above the FFI level.
21+
22+
.. rationale::
23+
:id: rat_LnIHMWRaC19F
24+
:status: draft
25+
26+
If the languages on the FFI boundary to not agree on the type or its layout, size and alignment properties, undefined behavior might be invoked. It is therefore critical to reduce the possibility of such misalignment to an absolute minimum. Developers and/or tools shall therefore follow a set of strict rules that govern how a foreign type definition shall be translated to Rust.
27+
28+
Since the only ubiquitously supported foreign calling ABI is the calling ABI of the C programming language, this rule will state how C types are to be translated. For any other language or ABI, it is required to document the respective translation rules.
29+
30+
It is recommended that tooling is used to automate the generation of matching declarations where possible. If this is not possible (e.g. due to the pre-existence of code), it is recommended to set up tooling that is able to check the consistency of the type declarations.
31+
32+
.. non_compliant_example::
33+
:id: non_compl_ex_9uNGhTr1I20O
34+
:status: draft
35+
36+
The example shows the import of a single function from C which populates a given out parameter with data about a file. Looking at both type definitions of the second parameter, we can see that types are used which might or might not match, depending on the used platform triple and the definition of size_t. While the resulting program might run perfectly well on your favorite 64 bit host platform, other platforms like a 16 bit embedded platform will most likely fail.
37+
38+
.. code-block:: rust
39+
40+
/* C side */
41+
typedef struct __file_info {
42+
size_t size;
43+
int epoch_time;
44+
} file_info;
45+
46+
int get_name_size(const char* path, file_info* info_out) { ... }
47+
```
48+
49+
```rust
50+
// Rust side
51+
use std::ffi;
52+
53+
#[repr(C)]
54+
struct FileInfo {
55+
size: i64,
56+
epoch_time: i32,
57+
}
58+
59+
unsafe extern "C" {
60+
fn get_name_size(path: *const ffi::c_char, file_info: *mut FileInfo) -> std::ffi::c_int;
61+
}
62+
63+
.. compliant_example::
64+
:id: compl_ex_TPV54bWJEmft
65+
:status: draft
66+
67+
By picking matching types for the Rust ``extern`` declaration, we ensure the usage of a type that is understood in the same way on both the Rust and the C side. The type to choose is unambiguous - for each type on C side, it is exactly specified which basic type or compound type to use on Rust side.
68+
69+
.. code-block:: rust
70+
71+
/* C side */
72+
typedef struct __file_info {
73+
size_t size;
74+
int epoch_time;
75+
} file_info;
76+
77+
int get_name_size(const char* path, file_info* info_out) { ... }
78+
```
79+
80+
```rust
81+
// Rust side
82+
use std::ffi;
83+
84+
#[repr(C)]
85+
struct FileInfo {
86+
size: libc::size_t,
87+
epoch_time: std::ffi::c_int,
88+
}
89+
90+
unsafe extern "C" {
91+
fn get_name_size(path: *const ffi::c_char, file_info: *mut FileInfo) -> std::ffi::c_int;
92+
}

0 commit comments

Comments
 (0)