|
81 | 81 |
|
82 | 82 | fn main() { |
83 | 83 | // Call the unmangled function |
84 | | - convert(); |
| 84 | + convert() |
85 | 85 | } |
86 | 86 |
|
87 | 87 | .. compliant_example:: |
|
100 | 100 |
|
101 | 101 | fn main() { |
102 | 102 | // Call the no_mangle function (safe to call) |
103 | | - convert(); |
| 103 | + convert() |
104 | 104 | } |
105 | 105 |
|
106 | 106 | .. non_compliant_example:: |
|
194 | 194 | .. rust-example:: |
195 | 195 | :compile_fail: |
196 | 196 |
|
197 | | - // Missing unsafe marker - noncompliant in Rust 2024 |
198 | | - #[export_name = "custom_symbol"] // error: unsafe attribute used without unsafe |
199 | | - pub fn my_function() {} |
| 197 | + // Collides with the C library 'printf' function |
| 198 | + #[export_name = "printf"] // noncompliant |
200 | 199 |
|
201 | 200 | // Missing unsafe marker - noncompliant in Rust 2024 |
202 | | - #[link_section = ".custom_section"] // error: unsafe attribute used without unsafe |
203 | | - static DATA: u32 = 42; |
| 201 | + #[unsafe(link_section = ".init_array")] // noncompliant |
| 202 | + static DATA: u32 = 42; // Corrupts initialization table! |
| 203 | + |
| 204 | + fn main() { |
| 205 | + printf("Hello, World"); |
| 206 | + println!("DATA = {DATA}"); |
| 207 | + } |
204 | 208 |
|
205 | 209 | .. compliant_example:: |
206 | 210 | :id: compl_ex_xY2zAb3cDe4f |
|
211 | 215 |
|
212 | 216 | .. rust-example:: |
213 | 217 |
|
| 218 | + // SAFETY: 'custom_symbol' does not conflict with any other symbol |
214 | 219 | #[unsafe(export_name = "custom_symbol")] |
215 | 220 | pub fn my_function() {} |
216 | 221 |
|
217 | | - #[unsafe(link_section = ".custom_section")] |
218 | | - static DATA: u32 = 42; |
| 222 | + // SAFETY: Placing data in a specific section for embedded systems |
| 223 | + #[unsafe(link_section = ".noinit")] |
| 224 | + static mut PERSISTENT_DATA: [u8; 256] = [0; 256]; |
| 225 | + |
| 226 | + // SAFETY: Custom section for shared memory |
| 227 | + #[unsafe(link_section = ".shared")] |
| 228 | + static SHARED_BUFFER: [u8; 4096] = [0; 4096]; |
| 229 | + |
| 230 | + fn main() { |
| 231 | + my_function(); |
| 232 | + println!("DATA = {DATA}"); |
| 233 | + } |
219 | 234 |
|
220 | 235 | .. enforcement:: |
221 | 236 | :id: enf_pQ8rSt9uVw0x |
|
241 | 256 | * **Code Review**: Manual review of all code containing ``unsafe`` tokens should be |
242 | 257 | part of the development process, with documented justification for each usage. |
243 | 258 |
|
244 | | - .. exceptions:: |
245 | | - :id: exc_yZ1aAb2cDe3f |
246 | | - :status: draft |
247 | | - |
248 | | - Deviations from this guideline may be permitted under the following conditions: |
249 | | - |
250 | | - * **Legacy Code Migration**: When migrating pre-2024 code, a documented migration plan |
251 | | - with timeline for compliance is acceptable. |
252 | | - |
253 | | - * **Third-Party Dependencies**: Dependencies not under project control that violate this |
254 | | - guideline must be documented in the safety case with risk assessment. |
255 | | - |
256 | | - * **Performance-Critical Sections**: Where the ``unsafe`` wrapper syntax causes measurable |
257 | | - compilation overhead, document the deviation with benchmarks. |
258 | | - |
259 | | - All deviations require: |
260 | | - |
261 | | - 1. Written justification documenting why the deviation is necessary |
262 | | - 2. Risk assessment of the safety implications |
263 | | - 3. Approval from the project's safety authority |
264 | | - 4. Tracking in the project's deviation log |
265 | | - |
266 | 259 | .. related_guidelines:: |
267 | 260 | :id: rel_gH4iJk5lMn6o |
268 | 261 | :status: draft |
|
0 commit comments