| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| use core::ptr; | |
| use kstring::interned_string::InternedString; | |
| use kstring::{declare_interned_string, intern_string}; | |
| declare_interned_string!(RUST_DECLARED, "rust.declared.interned"); | |
| // SAFETY: Each function is defined in `src/lib/kstring/tests/cpp_interned.cc` with `extern "C"` | |
| // linkage, `noexcept` non-unwinding behavior, and matching C-ABI return types (`InternedString` is | |
| // `#[repr(transparent)]` over `*const u8` matching `fxt::InternedString`'s single `const char*` | |
| // field). In addition, `get_cpp_only_ptr` guarantees returning a non-null, aligned pointer to a | |
| // statically constant-initialized, immutable `'static` `const fxt::InternedString` instance in | |
| // `__fxt_interned_string_table` whose `'static` NUL-terminated string literal satisfies | |
| // `InternedString`'s field invariant. | |
| unsafe extern "C" { | |
| fn get_cpp_only_ptr() -> *const InternedString; | |
| fn get_cpp_only_id() -> u16; | |
| } | |
| /// Verifies that C++ `FXT_INTERNED_STRING_SECTION`, Rust `declare_interned_string!`, and Rust | |
| /// `intern_string!` entries share a contiguous `__fxt_interned_string_table` section with | |
| /// identical 1-based IDs across C++ `fxt::InternedString::id()` and Rust | |
| /// `kstring::InternedString::id()`. | |
| #[test] | |
| fn test_cpp_rust_iter_section_and_ids() { | |
| let rust_inline = intern_string!("rust.inline.interned"); | |
| // SAFETY: `get_cpp_only_ptr` and `get_cpp_only_id` are `extern "C" noexcept` test helpers in | |
| // `cpp_interned.cc` with no preconditions. | |
| let (cpp_only_ptr, cpp_only_id) = unsafe { (get_cpp_only_ptr(), get_cpp_only_id()) }; | |
| assert!(!cpp_only_ptr.is_null()); | |
| // SAFETY: By `get_cpp_only_ptr`'s documented contract in `cpp_interned.cc` (and the `extern | |
| // "C"` block above), `cpp_only_ptr` is non-null, aligned to `alignof(fxt::InternedString) == | |
| // align_of::<InternedString>()`, points to a statically constant-initialized `const | |
| // fxt::InternedString` satisfying `InternedString`'s field invariant, and is never mutably | |
| // aliased for `'static`. | |
| let cpp_only: &'static InternedString = unsafe { &*cpp_only_ptr }; | |
| assert_eq!(cpp_only.id(), cpp_only_id); | |
| assert_eq!(cpp_only.as_c_str(), c"cpp.only.interned"); | |
| assert!(!ptr::eq(cpp_only, RUST_DECLARED)); | |
| assert!(!ptr::eq(cpp_only, rust_inline)); | |
| // SAFETY: | |
| // - This ELF `rustc_test` binary is linked by LLD with `EMPTY_SECTION_ANCHOR` and all dynamic | |
| // relocations have completed before `#[test]` execution. | |
| // - All non-empty `__fxt_interned_string_table` entries in this binary come from | |
| // `declare_interned_string!`, `intern_string!`, or C++ `FXT_INTERNED_STRING_SECTION`, each | |
| // emitting an initialized, immutable `'static` `InternedString` aligned to 8 bytes with no | |
| // overaligned inputs or mutable aliases. | |
| // - C++ `SPECIAL_SECTION` sets `no_sanitize("address")`, and `rustc_test` binaries do not | |
| // enable `-Zsanitizer=address` (`//build/config/sanitizers/BUILD.gn`, | |
| // https://fxbug.dev/521921032, https://fxbug.dev/543031813), so no sanitizer global redzone | |
| // padding separates entries. | |
| let iter = unsafe { InternedString::iter_section() }; | |
| assert!(iter.len() >= 3); | |
| let (mut saw_rust_declared, mut saw_cpp_only, mut saw_rust_inline) = (false, false, false); | |
| for (index, entry) in iter.enumerate() { | |
| let expected_id = (index + 1) as u16; | |
| assert!(expected_id >= 1 && expected_id <= InternedString::MAX_ID); | |
| assert_eq!(entry.id(), expected_id); | |
| if ptr::eq(entry, RUST_DECLARED) { | |
| assert_eq!(entry.as_c_str(), c"rust.declared.interned"); | |
| saw_rust_declared = true; | |
| } else if ptr::eq(entry, cpp_only) { | |
| assert_eq!(entry.as_c_str(), c"cpp.only.interned"); | |
| saw_cpp_only = true; | |
| } else if ptr::eq(entry, rust_inline) { | |
| assert_eq!(entry.as_c_str(), c"rust.inline.interned"); | |
| saw_rust_inline = true; | |
| } | |
| } | |
| assert!(saw_rust_declared, "missing Rust declare_interned_string! entry in iter_section()"); | |
| assert!(saw_cpp_only, "missing C++ FXT_INTERNED_STRING_SECTION entry in iter_section()"); | |
| assert!(saw_rust_inline, "missing Rust intern_string! entry in iter_section()"); | |
| } |