From 478d0d381f8ef1db1c794940a53a3522683f8bb9 Mon Sep 17 00:00:00 2001 From: "Hui, Chunyang" Date: Wed, 15 Mar 2023 15:22:26 +0800 Subject: [PATCH] Add catch unwind for occlum_ecall_init --- src/libos/src/entry.rs | 95 +++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/src/libos/src/entry.rs b/src/libos/src/entry.rs index 4196f06b..bd2458e3 100644 --- a/src/libos/src/entry.rs +++ b/src/libos/src/entry.rs @@ -108,48 +108,14 @@ pub extern "C" fn occlum_ecall_init( std::alloc::set_alloc_error_hook(oom_handle); }); - // Parse host file - let resolv_conf_ptr = unsafe { (*file_buffer).resolv_conf_buf }; - match parse_host_file(HostFile::ResolvConf, resolv_conf_ptr) { - Err(e) => { - error!("failed to parse /etc/resolv.conf: {}", e.backtrace()); - } - Ok(resolv_conf_str) => { - *RESOLV_CONF_STR.write().unwrap() = Some(resolv_conf_str); - if let Err(e) = write_host_file(HostFile::ResolvConf) { - error!("failed to write /etc/resolv.conf: {}", e.backtrace()); - } - } - } - - let hostname_ptr = unsafe { (*file_buffer).hostname_buf }; - match parse_host_file(HostFile::HostName, hostname_ptr) { - Err(e) => { - error!("failed to parse /etc/hostname: {}", e.backtrace()); - } - Ok(hostname_str) => { - misc::init_nodename(&hostname_str); - *HOSTNAME_STR.write().unwrap() = Some(hostname_str); - if let Err(e) = write_host_file(HostFile::HostName) { - error!("failed to write /etc/hostname: {}", e.backtrace()); - } - } - } - - let hosts_ptr = unsafe { (*file_buffer).hosts_buf }; - match parse_host_file(HostFile::Hosts, hosts_ptr) { - Err(e) => { - error!("failed to parse /etc/hosts: {}", e.backtrace()); - } - Ok(hosts_str) => { - *HOSTS_STR.write().unwrap() = Some(hosts_str); - if let Err(e) = write_host_file(HostFile::Hosts) { - error!("failed to write /etc/hosts: {}", e.backtrace()); - } - } - } - - 0 + panic::catch_unwind(|| { + backtrace::__rust_begin_short_backtrace(|| { + // Ignore the error when parsing host files + let _ = parse_host_files(file_buffer); + 0 as i32 + }) + }) + .unwrap_or(ecall_errno!(EFAULT)) } // hook for memory allocation error @@ -453,3 +419,48 @@ fn merge_env(env: *const *const c_char) -> Result> { trace!("env_checked from env untrusted: {:?}", env_checked); Ok([env_keep, env_checked].concat()) } + +// Parse host files +fn parse_host_files(file_buffer: *const host_file_buffer) -> Result { + let resolv_conf_ptr = unsafe { (*file_buffer).resolv_conf_buf }; + match parse_host_file(HostFile::ResolvConf, resolv_conf_ptr) { + Err(e) => { + error!("failed to parse /etc/resolv.conf: {}", e.backtrace()); + } + Ok(resolv_conf_str) => { + *RESOLV_CONF_STR.write().unwrap() = Some(resolv_conf_str); + if let Err(e) = write_host_file(HostFile::ResolvConf) { + error!("failed to write /etc/resolv.conf: {}", e.backtrace()); + } + } + } + + let hostname_ptr = unsafe { (*file_buffer).hostname_buf }; + match parse_host_file(HostFile::HostName, hostname_ptr) { + Err(e) => { + error!("failed to parse /etc/hostname: {}", e.backtrace()); + } + Ok(hostname_str) => { + misc::init_nodename(&hostname_str); + *HOSTNAME_STR.write().unwrap() = Some(hostname_str); + if let Err(e) = write_host_file(HostFile::HostName) { + error!("failed to write /etc/hostname: {}", e.backtrace()); + } + } + } + + let hosts_ptr = unsafe { (*file_buffer).hosts_buf }; + match parse_host_file(HostFile::Hosts, hosts_ptr) { + Err(e) => { + error!("failed to parse /etc/hosts: {}", e.backtrace()); + } + Ok(hosts_str) => { + *HOSTS_STR.write().unwrap() = Some(hosts_str); + if let Err(e) = write_host_file(HostFile::Hosts) { + error!("failed to write /etc/hosts: {}", e.backtrace()); + } + } + } + + Ok(0) +}