aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-boot / src / wcslen.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! `wcslen` for the UEFI target. |
| 17 | //! |
| 18 | //! Since rustc 1.99 (LLVM 23) the loop-idiom pass turns any "count 16-bit |
| 19 | //! units until zero" loop into a call to `wcslen`, and the UEFI targets ship |
| 20 | //! no C library to provide it (rust-lang/rust#160827, #163614). The `uefi` |
| 21 | //! crate has such loops (`proto::shell`, `proto::driver::component_name`), |
| 22 | //! so every image that links it fails with `undefined symbol: wcslen`. |
| 23 | //! |
| 24 | //! This defines the symbol for the UEFI target only. The counting loop uses |
| 25 | //! volatile reads so LLVM cannot turn the body of `wcslen` back into a call |
| 26 | //! to itself. |
| 27 | |
| 28 | /// Number of `u16` units before the first zero unit. |
| 29 | /// |
| 30 | /// # Safety |
| 31 | /// `s` must point to a readable, zero-terminated run of `u16`. |
| 32 | pub unsafe fn wide_len(s: *const u16) -> usize { |
| 33 | let mut n = 0usize; |
| 34 | // SAFETY: the caller guarantees the run is readable up to its terminator. |
| 35 | while unsafe { core::ptr::read_volatile(s.add(n)) } != 0 { |
| 36 | n += 1; |
| 37 | } |
| 38 | n |
| 39 | } |
| 40 | |
| 41 | /// C `wcslen` for a 16-bit `wchar_t` (the UEFI ABI). Only for target_os = "uefi". |
| 42 | /// |
| 43 | /// # Safety |
| 44 | /// `s` must point to a readable, zero-terminated run of `u16`. |
| 45 | #[cfg(target_os = "uefi")] |
| 46 | #[no_mangle] |
| 47 | pub unsafe extern "C" fn wcslen(s: *const u16) -> usize { |
| 48 | // SAFETY: same contract as this function's. |
| 49 | unsafe { wide_len(s) } |
| 50 | } |
| 51 | |
| 52 | #[cfg(test)] |
| 53 | mod tests { |
| 54 | use super::wide_len; |
| 55 | |
| 56 | fn len(v: &[u16]) -> usize { |
| 57 | unsafe { wide_len(v.as_ptr()) } |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn empty_string_is_zero() { |
| 62 | assert_eq!(len(&[0]), 0); |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn counts_units_before_first_zero() { |
| 67 | assert_eq!(len(&[b'a' as u16, b'b' as u16, b'c' as u16, 0]), 3); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn stops_at_first_zero_not_slice_end() { |
| 72 | assert_eq!(len(&[1, 2, 0, 3, 4, 0]), 2); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn high_unit_values_are_not_terminators() { |
| 77 | assert_eq!(len(&[0xFFFF, 0x8000, 0x0100, 0xD800, 0]), 4); |
| 78 | } |
| 79 | } |