fuchsia/
lib.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Macros for creating Fuchsia components and tests.
6//!
7//! These macros work on Fuchsia, and also on host with some limitations (that are called out
8//! where they exist).
9
10// Features from those macros are expected to be implemented by exactly one function in this
11// module. We strive for simple, independent, single purpose functions as building blocks to allow
12// dead code elimination to have the very best chance of removing unused code that might be
13// otherwise pulled in here.
14
15#![deny(missing_docs)]
16
17pub use fuchsia_macro::{main, test};
18use libc as _;
19#[doc(hidden)]
20pub use log::error;
21use std::future::Future;
22
23#[cfg(fuchsia_api_level_less_than = "27")]
24pub use fidl_fuchsia_diagnostics::{Interest, Severity};
25#[cfg(fuchsia_api_level_at_least = "27")]
26pub use fidl_fuchsia_diagnostics_types::{Interest, Severity};
27
28#[cfg(target_os = "fuchsia")]
29pub use fuchsia_async_inspect;
30
31//
32// LOGGING INITIALIZATION
33//
34
35/// Options used when initializing logging.
36#[derive(Default, Clone)]
37pub struct LoggingOptions<'a> {
38    /// Tags with which to initialize the logging system. All logs will carry the tags configured
39    /// here.
40    pub tags: &'a [&'static str],
41
42    /// Allows to configure the minimum severity of the logs being emitted. Logs of lower severity
43    /// won't be emitted.
44    pub interest: Interest,
45
46    /// String to include in logged panic messages.
47    pub panic_prefix: &'static str,
48
49    /// True to always log file/line information, false to only log
50    /// when severity is ERROR or above.
51    pub always_log_file_line: bool,
52}
53
54#[cfg(not(target_os = "fuchsia"))]
55impl<'a> From<LoggingOptions<'a>> for diagnostics_log::PublishOptions<'a> {
56    fn from(logging: LoggingOptions<'a>) -> Self {
57        let mut options = diagnostics_log::PublishOptions::default().tags(logging.tags);
58        if let Some(severity) = logging.interest.min_severity {
59            options = options.minimum_severity(severity);
60        }
61        options = options.panic_prefix(logging.panic_prefix);
62        options
63    }
64}
65
66#[cfg(target_os = "fuchsia")]
67impl<'a> From<LoggingOptions<'a>> for diagnostics_log::PublishOptions<'a> {
68    fn from(logging: LoggingOptions<'a>) -> Self {
69        let mut options = diagnostics_log::PublishOptions::default().tags(logging.tags);
70        if let Some(severity) = logging.interest.min_severity {
71            options = options.minimum_severity(severity);
72        }
73        if logging.always_log_file_line {
74            options = options.always_log_file_line();
75        }
76        options = options.panic_prefix(logging.panic_prefix);
77        options
78    }
79}
80
81/// Initialize logging
82#[doc(hidden)]
83pub fn init_logging_for_component_with_executor<'a, R>(
84    func: impl FnOnce() -> R + 'a,
85    logging: LoggingOptions<'a>,
86) -> impl FnOnce() -> R + 'a {
87    move || {
88        diagnostics_log::initialize(logging.into()).expect("initialize_logging");
89        func()
90    }
91}
92
93#[doc(hidden)]
94pub fn init_default_logging_for_component_with_executor<'a, R>(
95    func: impl FnOnce() -> R + 'a,
96    logging: LoggingOptions<'a>,
97) -> impl FnOnce() -> R + 'a {
98    init_logging_for_component_with_executor(func, logging)
99}
100
101#[doc(hidden)]
102pub fn init_noop_logging_for_component_with_executor<'a, R>(
103    func: impl FnOnce() -> R + 'a,
104    _logging: LoggingOptions<'a>,
105) -> impl FnOnce() -> R + 'a {
106    func
107}
108
109/// Initialize logging
110#[doc(hidden)]
111pub fn init_logging_for_component_with_threads<'a, R>(
112    func: impl FnOnce() -> R + 'a,
113    logging: LoggingOptions<'a>,
114) -> impl FnOnce() -> R + 'a {
115    move || {
116        let _guard = init_logging_with_threads(logging);
117        func()
118    }
119}
120
121#[doc(hidden)]
122pub fn init_default_logging_for_component_with_threads<'a, R>(
123    func: impl FnOnce() -> R + 'a,
124    logging: LoggingOptions<'a>,
125) -> impl FnOnce() -> R + 'a {
126    init_logging_for_component_with_threads(func, logging)
127}
128
129#[doc(hidden)]
130pub fn init_noop_logging_for_component_with_threads<'a, R>(
131    func: impl FnOnce() -> R + 'a,
132    _logging: LoggingOptions<'a>,
133) -> impl FnOnce() -> R + 'a {
134    func
135}
136
137/// Initialize logging
138#[doc(hidden)]
139pub fn init_logging_for_test_with_executor<'a, R>(
140    func: impl Fn(usize) -> R + 'a,
141    logging: LoggingOptions<'a>,
142) -> impl Fn(usize) -> R + 'a {
143    move |n| {
144        diagnostics_log::initialize(logging.clone().into()).expect("initalize logging");
145        func(n)
146    }
147}
148
149#[doc(hidden)]
150pub fn init_default_logging_for_test_with_executor<'a, R>(
151    func: impl Fn(usize) -> R + 'a,
152    logging: LoggingOptions<'a>,
153) -> impl Fn(usize) -> R + 'a {
154    #[cfg(target_os = "fuchsia")]
155    {
156        init_logging_for_test_with_executor(func, logging)
157    }
158    #[cfg(not(target_os = "fuchsia"))]
159    {
160        init_noop_logging_for_test_with_executor(func, logging)
161    }
162}
163
164#[doc(hidden)]
165pub fn init_noop_logging_for_test_with_executor<'a, R>(
166    func: impl Fn(usize) -> R + 'a,
167    _logging: LoggingOptions<'a>,
168) -> impl Fn(usize) -> R + 'a {
169    func
170}
171
172/// Initialize logging
173#[doc(hidden)]
174pub fn init_logging_for_test_with_threads<'a, R>(
175    func: impl Fn(usize) -> R + 'a,
176    logging: LoggingOptions<'a>,
177) -> impl Fn(usize) -> R + 'a {
178    move |n| {
179        init_logging_with_threads(logging.clone());
180        func(n)
181    }
182}
183
184#[doc(hidden)]
185pub fn init_default_logging_for_test_with_threads<'a, R>(
186    func: impl Fn(usize) -> R + 'a,
187    logging: LoggingOptions<'a>,
188) -> impl Fn(usize) -> R + 'a {
189    #[cfg(target_os = "fuchsia")]
190    {
191        init_logging_for_test_with_threads(func, logging)
192    }
193    #[cfg(not(target_os = "fuchsia"))]
194    {
195        // On non-Fuchsia targets, we don't initialize logging by default for tests.
196        init_noop_logging_for_test_with_threads(func, logging)
197    }
198}
199
200#[doc(hidden)]
201pub fn init_noop_logging_for_test_with_threads<'a, R>(
202    func: impl Fn(usize) -> R + 'a,
203    _logging: LoggingOptions<'a>,
204) -> impl Fn(usize) -> R + 'a {
205    func
206}
207
208/// Initializes logging on a background thread, returning a guard which cancels interest listening
209/// when dropped.
210#[cfg(target_os = "fuchsia")]
211fn init_logging_with_threads(logging: LoggingOptions<'_>) {
212    diagnostics_log::initialize_sync(logging.into());
213}
214
215#[cfg(not(target_os = "fuchsia"))]
216fn init_logging_with_threads(logging: LoggingOptions<'_>) {
217    diagnostics_log::initialize(logging.into()).expect("initialize_logging");
218}
219
220#[cfg(target_os = "fuchsia")]
221fn set_thread_role(role_name: &str) {
222    if let Err(e) = fuchsia_scheduler::set_role_for_this_thread(role_name) {
223        log::warn!(e:%, role_name:%; "Couldn't apply thread role.");
224    }
225}
226
227//
228// MAIN FUNCTION WRAPPERS
229//
230
231/// Run a non-async main function.
232#[doc(hidden)]
233pub fn main_not_async<F, R>(f: F) -> R
234where
235    F: FnOnce() -> R,
236{
237    f()
238}
239
240/// Run a non-async main function, applying `role_name` as the thread role.
241#[doc(hidden)]
242pub fn main_not_async_with_role<F, R>(f: F, _role_name: &'static str) -> R
243where
244    F: FnOnce() -> R,
245{
246    #[cfg(target_os = "fuchsia")]
247    set_thread_role(_role_name);
248    f()
249}
250
251/// Run an async main function with a single threaded executor.
252#[doc(hidden)]
253#[cfg(target_os = "fuchsia")]
254pub fn main_singlethreaded<F, Fut, R>(
255    f: F,
256    instrument: Option<std::sync::Arc<dyn fuchsia_async::instrument::TaskInstrument>>,
257) -> R
258where
259    F: FnOnce() -> Fut,
260    Fut: Future<Output = R> + 'static,
261{
262    fuchsia_async::LocalExecutorBuilder::new()
263        .instrument(instrument)
264        .build()
265        .run_singlethreaded(f())
266}
267
268/// Run an async main function with a single threaded executor.
269#[doc(hidden)]
270#[cfg(not(target_os = "fuchsia"))]
271pub fn main_singlethreaded<F, Fut, R>(f: F, _instrument: Option<()>) -> R
272where
273    F: FnOnce() -> Fut,
274    Fut: Future<Output = R> + 'static,
275{
276    fuchsia_async::LocalExecutorBuilder::new().build().run_singlethreaded(f())
277}
278
279/// Run an async main function with a single threaded executor, applying `role_name`.
280#[doc(hidden)]
281#[cfg(target_os = "fuchsia")]
282pub fn main_singlethreaded_with_role<F, Fut, R>(
283    f: F,
284    _role_name: &'static str,
285    instrument: Option<std::sync::Arc<dyn fuchsia_async::instrument::TaskInstrument>>,
286) -> R
287where
288    F: FnOnce() -> Fut,
289    Fut: Future<Output = R> + 'static,
290{
291    #[cfg(target_os = "fuchsia")]
292    set_thread_role(_role_name);
293    fuchsia_async::LocalExecutorBuilder::new()
294        .instrument(instrument)
295        .build()
296        .run_singlethreaded(f())
297}
298
299/// Run an async main function with a single threaded executor, applying `role_name`.
300#[doc(hidden)]
301#[cfg(not(target_os = "fuchsia"))]
302pub fn main_singlethreaded_with_role<F, Fut, R>(
303    f: F,
304    _role_name: &'static str,
305    _instrument: Option<()>,
306) -> R
307where
308    F: FnOnce() -> Fut,
309    Fut: Future<Output = R> + 'static,
310{
311    fuchsia_async::LocalExecutorBuilder::new().build().run_singlethreaded(f())
312}
313
314/// Run an async main function with a multi threaded executor (containing `num_threads`).
315#[doc(hidden)]
316#[cfg(target_os = "fuchsia")]
317pub fn main_multithreaded<F, Fut, R>(
318    f: F,
319    num_threads: u8,
320    instrument: Option<std::sync::Arc<dyn fuchsia_async::instrument::TaskInstrument>>,
321) -> R
322where
323    F: FnOnce() -> Fut,
324    Fut: Future<Output = R> + Send + 'static,
325    R: Send + 'static,
326{
327    fuchsia_async::SendExecutorBuilder::new()
328        .num_threads(num_threads)
329        .instrument(instrument)
330        .build()
331        .run(f())
332}
333
334/// Run an async main function with a multi threaded executor (containing `num_threads`).
335#[doc(hidden)]
336#[cfg(not(target_os = "fuchsia"))]
337pub fn main_multithreaded<F, Fut, R>(f: F, num_threads: u8, _instrument: Option<()>) -> R
338where
339    F: FnOnce() -> Fut,
340    Fut: Future<Output = R> + Send + 'static,
341    R: Send + 'static,
342{
343    fuchsia_async::SendExecutorBuilder::new().num_threads(num_threads).build().run(f())
344}
345
346/// Run an async main function with a multi threaded executor (containing `num_threads`) and apply
347/// `role_name` to all of the threads.
348#[doc(hidden)]
349#[cfg(target_os = "fuchsia")]
350pub fn main_multithreaded_with_role<F, Fut, R>(
351    f: F,
352    num_threads: u8,
353    _role_name: &'static str,
354    instrument: Option<std::sync::Arc<dyn fuchsia_async::instrument::TaskInstrument>>,
355) -> R
356where
357    F: FnOnce() -> Fut,
358    Fut: Future<Output = R> + Send + 'static,
359    R: Send + 'static,
360{
361    #[cfg(target_os = "fuchsia")]
362    set_thread_role(_role_name);
363
364    let builder =
365        fuchsia_async::SendExecutorBuilder::new().num_threads(num_threads).instrument(instrument);
366
367    #[cfg(target_os = "fuchsia")]
368    let builder = builder.worker_init(move || set_thread_role(_role_name));
369
370    builder.build().run(f())
371}
372
373/// Run an async main function with a multi threaded executor (containing `num_threads`) and apply
374/// `role_name` to all of the threads.
375#[doc(hidden)]
376#[cfg(not(target_os = "fuchsia"))]
377pub fn main_multithreaded_with_role<F, Fut, R>(
378    f: F,
379    num_threads: u8,
380    _role_name: &'static str,
381    _instrument: Option<()>,
382) -> R
383where
384    F: FnOnce() -> Fut,
385    Fut: Future<Output = R> + Send + 'static,
386    R: Send + 'static,
387{
388    let builder = fuchsia_async::SendExecutorBuilder::new().num_threads(num_threads);
389
390    #[cfg(target_os = "fuchsia")]
391    let builder = builder.worker_init(move || set_thread_role(_role_name));
392
393    builder.build().run(f())
394}
395
396//
397// TEST FUNCTION WRAPPERS
398//
399
400/// Run a non-async test function.
401#[doc(hidden)]
402pub fn test_not_async<F, R>(f: F) -> R
403where
404    F: FnOnce(usize) -> R,
405    R: fuchsia_async::test_support::TestResult,
406{
407    let result = f(0);
408    if result.is_ok() {
409        install_lsan_hook();
410    }
411    result
412}
413
414/// Run an async test function with a single threaded executor.
415#[doc(hidden)]
416pub fn test_singlethreaded<F, Fut, R>(f: F) -> R
417where
418    F: Fn(usize) -> Fut + Sync + 'static,
419    Fut: Future<Output = R> + 'static,
420    R: fuchsia_async::test_support::TestResult,
421{
422    let result = fuchsia_async::test_support::run_singlethreaded_test(f);
423    if result.is_ok() {
424        install_lsan_hook();
425    }
426    result
427}
428
429/// Run an async test function with a multi threaded executor (containing `num_threads`).
430#[doc(hidden)]
431pub fn test_multithreaded<F, Fut, R>(f: F, num_threads: u8) -> R
432where
433    F: Fn(usize) -> Fut + Sync + 'static,
434    Fut: Future<Output = R> + Send + 'static,
435    R: fuchsia_async::test_support::MultithreadedTestResult,
436{
437    let result = fuchsia_async::test_support::run_test(f, num_threads);
438    if result.is_ok() {
439        install_lsan_hook();
440    }
441    result
442}
443
444/// Run an async test function until it stalls. The executor will also use fake time.
445#[doc(hidden)]
446#[cfg(target_os = "fuchsia")]
447pub fn test_until_stalled<F, Fut, R>(f: F) -> R
448where
449    F: 'static + Sync + Fn(usize) -> Fut,
450    Fut: 'static + Future<Output = R>,
451    R: fuchsia_async::test_support::TestResult,
452{
453    let result = fuchsia_async::test_support::run_until_stalled_test(true, f);
454    if result.is_ok() {
455        install_lsan_hook();
456    }
457    result
458}
459
460//
461// FUNCTION ARGUMENT ADAPTERS
462//
463
464/// Take a main function `f` that takes an argument and return a function that takes none but calls
465/// `f` with the arguments parsed via argh.
466#[doc(hidden)]
467pub fn adapt_to_parse_arguments<A, R>(f: impl FnOnce(A) -> R) -> impl FnOnce() -> R
468where
469    A: argh::TopLevelCommand,
470{
471    move || f(argh::from_env())
472}
473
474/// Take a test function `f` that takes no parameters and return a function that takes the run
475/// number as required by our test runners.
476#[doc(hidden)]
477pub fn adapt_to_take_test_run_number<R>(f: impl Fn() -> R) -> impl Fn(usize) -> R {
478    move |_| f()
479}
480
481//
482// LEAK SANITIZER SUPPORT
483//
484
485// Note that the variant is named variant_asan (for AddressSanitizer), but the specific sanitizer we
486// are targeting is lsan (LeakSanitizer), which is enabled as part of the asan variant.
487
488#[doc(hidden)]
489#[cfg(not(any(feature = "variant_asan", feature = "variant_hwasan")))]
490pub fn disable_lsan_for_should_panic() {}
491
492#[doc(hidden)]
493#[cfg(any(feature = "variant_asan", feature = "variant_hwasan"))]
494pub fn disable_lsan_for_should_panic() {
495    unsafe extern "C" {
496        fn __lsan_disable();
497    }
498    unsafe {
499        __lsan_disable();
500    }
501}
502
503#[cfg(not(any(feature = "variant_asan", feature = "variant_hwasan")))]
504fn install_lsan_hook() {}
505
506#[cfg(any(feature = "variant_asan", feature = "variant_hwasan"))]
507fn install_lsan_hook() {
508    unsafe extern "C" {
509        fn __lsan_do_leak_check();
510    }
511    // Wrap the call because atexit requires a safe function pointer.
512    extern "C" fn lsan_do_leak_check() {
513        unsafe { __lsan_do_leak_check() }
514    }
515    // Wait until atexit hooks are called, in case there is more cleanup left to do.
516    let err = unsafe { libc::atexit(lsan_do_leak_check) };
517    if err != 0 {
518        panic!("Failed to install atexit hook for LeakSanitizer: atexit returned {err}");
519    }
520}