elf_runner/
crash_handler.rs1use crate::crash_info::{ComponentCrashInfo, CrashRecords};
6use crate::error::ExceptionError;
7use fuchsia_async as fasync;
8use futures::TryStreamExt;
9use log::error;
10use moniker::Moniker;
11use zx::{self as zx, AsHandleRef};
12
13pub fn run_exceptions_server(
17 component_job: &zx::Job,
18 moniker: Moniker,
19 resolved_url: String,
20 crash_records: CrashRecords,
21) -> Result<(), zx::Status> {
22 let mut task_exceptions_stream =
23 task_exceptions::ExceptionsStream::register_with_task(component_job)?;
24 fasync::Task::spawn(async move {
25 loop {
26 match task_exceptions_stream.try_next().await {
27 Ok(Some(exception_info)) => {
28 if let Err(error) = record_exception(
29 resolved_url.clone(),
30 moniker.clone(),
31 exception_info,
32 &crash_records,
33 )
34 .await
35 {
36 error!(url:% = resolved_url, error:?; "failed to handle exception");
37 }
38 }
39 Ok(None) => break,
40 Err(error) => {
41 error!(
42 url:% = resolved_url, error:?;
43 "failed to read message stream for fuchsia.sys2.CrashIntrospect",
44 );
45 break;
46 }
47 }
48 }
49 })
50 .detach();
51 Ok(())
52}
53
54async fn record_exception(
55 resolved_url: String,
56 moniker: Moniker,
57 exception_info: task_exceptions::ExceptionInfo,
58 crash_records: &CrashRecords,
59) -> Result<(), ExceptionError> {
60 let thread_koid = exception_info.thread.get_koid().map_err(ExceptionError::GetThreadKoid)?;
63 crash_records.add_report(thread_koid, ComponentCrashInfo { url: resolved_url, moniker }).await;
64
65 exception_info
68 .exception_handle
69 .set_exception_state(&zx::sys::ZX_EXCEPTION_STATE_TRY_NEXT)
70 .map_err(ExceptionError::SetState)?;
71
72 Ok(())
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80 use anyhow::{Context as _, Error};
81 use fuchsia_component::client as fclient;
82 use zx::HandleBased;
83 use {fidl_fuchsia_io as fio, fidl_fuchsia_process as fprocess, fuchsia_runtime as fruntime};
84
85 #[fuchsia::test]
86 async fn crash_test() -> Result<(), Error> {
87 let crash_records = CrashRecords::new();
88 let url = "example://component#url".to_string();
89 let moniker = Moniker::try_from(["a"]).unwrap();
90
91 let child_job =
92 fruntime::job_default().create_child_job().context("failed to create child job")?;
93
94 run_exceptions_server(&child_job, moniker.clone(), url.clone(), crash_records.clone())?;
95
96 let launcher_proxy = fclient::connect_to_protocol::<fprocess::LauncherMarker>()?;
98
99 let (ll_client_chan, ll_service_chan) = zx::Channel::create();
101 library_loader::start(
102 fuchsia_fs::directory::open_in_namespace(
103 "/pkg/lib",
104 fio::PERM_READABLE | fio::PERM_EXECUTABLE,
105 )?,
106 ll_service_chan,
107 );
108 let handle_infos = vec![fprocess::HandleInfo {
109 handle: ll_client_chan.into_handle(),
110 id: fruntime::HandleInfo::new(fruntime::HandleType::LdsvcLoader, 0).as_raw(),
111 }];
112 launcher_proxy.add_handles(handle_infos).context("failed to add loader service handle")?;
113
114 let executable_file_proxy = fuchsia_fs::file::open_in_namespace(
116 "/pkg/bin/panic_on_start",
117 fio::PERM_READABLE | fio::PERM_EXECUTABLE,
118 )?;
119 let vmo = executable_file_proxy
120 .get_backing_memory(fio::VmoFlags::READ | fio::VmoFlags::EXECUTE)
121 .await?
122 .map_err(zx::Status::from_raw)
123 .context("failed to get VMO of executable")?;
124
125 let child_job_dup = child_job.duplicate_handle(zx::Rights::SAME_RIGHTS)?;
127 let launch_info = fprocess::LaunchInfo {
128 executable: vmo,
129 job: child_job_dup,
130 name: "panic_on_start".to_string(),
131 };
132 let (status, process_start_data) = launcher_proxy
133 .create_without_starting(launch_info)
134 .await
135 .context("failed to launch process")?;
136 zx::Status::ok(status).context("error returned by process launcher")?;
137 let process_start_data = process_start_data.unwrap();
138
139 let thread_koid = process_start_data.thread.get_koid()?;
142
143 process_start_data.process.start(
145 &process_start_data.thread,
146 process_start_data.entry.try_into().unwrap(),
148 process_start_data.stack.try_into().unwrap(),
149 process_start_data.bootstrap.into_handle(),
150 process_start_data.vdso_base.try_into().unwrap(),
151 )?;
152
153 fasync::OnSignals::new(&process_start_data.process, zx::Signals::PROCESS_TERMINATED)
155 .await?;
156 let crash_info = crash_records
157 .take_report(&thread_koid)
158 .await
159 .expect("crash_records is missing crash information");
160 assert_eq!(ComponentCrashInfo { url, moniker }, crash_info);
161 Ok(())
162 }
163}