From a27553daec36118dcc5367143114a9972f905045 Mon Sep 17 00:00:00 2001 From: s1341 Date: Mon, 2 Oct 2023 17:48:33 +0300 Subject: [PATCH] libafl_cc: fix configuration support (#1595) * libafl_cc: fix configuration support * fmt * clippy --- libafl_cc/src/clang.rs | 67 +++++++++++++++++++++++----------------- libafl_cc/src/libtool.rs | 4 +-- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/libafl_cc/src/clang.rs b/libafl_cc/src/clang.rs index 0f07a4a48b..e6b3e1494f 100644 --- a/libafl_cc/src/clang.rs +++ b/libafl_cc/src/clang.rs @@ -194,7 +194,7 @@ impl ToolWrapper for ClangWrapper { continue; } } - "--libafl-ignore-configurations" => { + "--libafl-ignore-configurations" | "-print-prog-name=ld" => { self.ignoring_configurations = true; i += 1; continue; @@ -346,43 +346,52 @@ impl ToolWrapper for ClangWrapper { }) .collect::>(); - if let Some(output) = self.output.clone() { + if let crate::Configuration::Default = configuration { + if let Some(output) = self.output.clone() { + let output = configuration.replace_extension(&output); + let new_filename = output.into_os_string().into_string().unwrap(); + args.push("-o".to_string()); + args.push(new_filename); + } + } else if let Some(output) = self.output.clone() { let output = configuration.replace_extension(&output); let new_filename = output.into_os_string().into_string().unwrap(); args.push("-o".to_string()); args.push(new_filename); - args.extend_from_slice(base_args.as_slice()); } else { - // No output specified, we need to rewrite the single .c file's name. - args.extend( - base_args - .iter() - .map(|r| { - let arg_as_path = std::path::PathBuf::from(r); - if r.ends_with('.') { - r.to_string() - } else { - if let Some(extension) = arg_as_path.extension() { - let extension = extension.to_str().unwrap(); - let extension_lowercase = extension.to_lowercase(); - match &extension_lowercase[..] { - "c" | "cc" | "cxx" | "cpp" => { - configuration.replace_extension(&arg_as_path) - } - _ => arg_as_path, - } - } else { - arg_as_path + // No output specified, we need to rewrite the single .c file's name into a -o + // argument. + for arg in &base_args { + let arg_as_path = std::path::PathBuf::from(arg); + if !arg.ends_with('.') && !arg.starts_with('-') { + if let Some(extension) = arg_as_path.extension() { + let extension = extension.to_str().unwrap(); + let extension_lowercase = extension.to_lowercase(); + match &extension_lowercase[..] { + "c" | "cc" | "cxx" | "cpp" => { + args.push("-o".to_string()); + args.push(if self.linking { + configuration + .replace_extension(&std::path::PathBuf::from("a.out")) + .into_os_string() + .into_string() + .unwrap() + } else { + let mut result = configuration.replace_extension(&arg_as_path); + result.set_extension("o"); + result.into_os_string().into_string().unwrap() + }); + break; } - .into_os_string() - .into_string() - .unwrap() + _ => {} } - }) - .collect::>(), - ); + } + } + } } + args.extend_from_slice(base_args.as_slice()); + args.extend_from_slice(&configuration.to_flags()?); if self.need_libafl_arg && !self.has_libafl_arg { diff --git a/libafl_cc/src/libtool.rs b/libafl_cc/src/libtool.rs index 31aea32730..67527e5d30 100644 --- a/libafl_cc/src/libtool.rs +++ b/libafl_cc/src/libtool.rs @@ -178,9 +178,7 @@ impl ToolWrapper for LibtoolWrapper { let extension = extension.to_str().unwrap(); let extension_lowercase = extension.to_lowercase(); match &extension_lowercase[..] { - "o" | "lo" | "a" | "la" | "so" => { - configuration.replace_extension(&arg_as_path) - } + "lo" | "la" | "so" => configuration.replace_extension(&arg_as_path), _ => arg_as_path, } } else {