更新libclamav库1.0.0版本

This commit is contained in:
2023-01-14 18:28:39 +08:00
parent b879ee0b2e
commit 45fe15f472
8531 changed files with 1222046 additions and 177272 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,271 @@
// SPDX-License-Identifier: Apache-2.0
//================================================
// Macros
//================================================
#[cfg(feature = "runtime")]
macro_rules! link {
(
@LOAD:
$(#[doc=$doc:expr])*
#[cfg($cfg:meta)]
fn $name:ident($($pname:ident: $pty:ty), *) $(-> $ret:ty)*
) => (
$(#[doc=$doc])*
#[cfg($cfg)]
pub fn $name(library: &mut super::SharedLibrary) {
let symbol = unsafe { library.library.get(stringify!($name).as_bytes()) }.ok();
library.functions.$name = match symbol {
Some(s) => *s,
None => None,
};
}
#[cfg(not($cfg))]
pub fn $name(_: &mut super::SharedLibrary) {}
);
(
@LOAD:
fn $name:ident($($pname:ident: $pty:ty), *) $(-> $ret:ty)*
) => (
link!(@LOAD: #[cfg(feature = "runtime")] fn $name($($pname: $pty), *) $(-> $ret)*);
);
(
$(
$(#[doc=$doc:expr] #[cfg($cfg:meta)])*
pub fn $name:ident($($pname:ident: $pty:ty), *) $(-> $ret:ty)*;
)+
) => (
use std::cell::{RefCell};
use std::sync::{Arc};
use std::path::{Path, PathBuf};
/// The (minimum) version of a `libclang` shared library.
#[allow(missing_docs)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Version {
V3_5 = 35,
V3_6 = 36,
V3_7 = 37,
V3_8 = 38,
V3_9 = 39,
V4_0 = 40,
V5_0 = 50,
V6_0 = 60,
V7_0 = 70,
V8_0 = 80,
V9_0 = 90,
}
/// The set of functions loaded dynamically.
#[derive(Debug, Default)]
pub struct Functions {
$(
$(#[doc=$doc] #[cfg($cfg)])*
pub $name: Option<unsafe extern fn($($pname: $pty), *) $(-> $ret)*>,
)+
}
/// A dynamically loaded instance of the `libclang` library.
#[derive(Debug)]
pub struct SharedLibrary {
library: libloading::Library,
path: PathBuf,
pub functions: Functions,
}
impl SharedLibrary {
fn new(library: libloading::Library, path: PathBuf) -> Self {
Self { library, path, functions: Functions::default() }
}
/// Returns the path to this `libclang` shared library.
pub fn path(&self) -> &Path {
&self.path
}
/// Returns the (minimum) version of this `libclang` shared library.
///
/// If this returns `None`, it indicates that the version is too old
/// to be supported by this crate (i.e., `3.4` or earlier). If the
/// version of this shared library is more recent than that fully
/// supported by this crate, the most recent fully supported version
/// will be returned.
pub fn version(&self) -> Option<Version> {
macro_rules! check {
($fn:expr, $version:ident) => {
if self.library.get::<unsafe extern fn()>($fn).is_ok() {
return Some(Version::$version);
}
};
}
unsafe {
check!(b"clang_Cursor_isAnonymousRecordDecl", V9_0);
check!(b"clang_Cursor_getObjCPropertyGetterName", V8_0);
check!(b"clang_File_tryGetRealPathName", V7_0);
check!(b"clang_CXIndex_setInvocationEmissionPathOption", V6_0);
check!(b"clang_Cursor_isExternalSymbol", V5_0);
check!(b"clang_EvalResult_getAsLongLong", V4_0);
check!(b"clang_CXXConstructor_isConvertingConstructor", V3_9);
check!(b"clang_CXXField_isMutable", V3_8);
check!(b"clang_Cursor_getOffsetOfField", V3_7);
check!(b"clang_Cursor_getStorageClass", V3_6);
check!(b"clang_Type_getNumTemplateArguments", V3_5);
}
None
}
}
thread_local!(static LIBRARY: RefCell<Option<Arc<SharedLibrary>>> = RefCell::new(None));
/// Returns whether a `libclang` shared library is loaded on this thread.
pub fn is_loaded() -> bool {
LIBRARY.with(|l| l.borrow().is_some())
}
fn with_library<T, F>(f: F) -> Option<T> where F: FnOnce(&SharedLibrary) -> T {
LIBRARY.with(|l| {
match l.borrow().as_ref() {
Some(library) => Some(f(&library)),
_ => None,
}
})
}
$(
#[cfg_attr(feature="cargo-clippy", allow(clippy::missing_safety_doc))]
#[cfg_attr(feature="cargo-clippy", allow(clippy::too_many_arguments))]
$(#[doc=$doc] #[cfg($cfg)])*
pub unsafe fn $name($($pname: $pty), *) $(-> $ret)* {
let f = with_library(|l| {
l.functions.$name.expect(concat!(
"`libclang` function not loaded: `",
stringify!($name),
"`. This crate requires that `libclang` 3.9 or later be installed on your ",
"system. For more information on how to accomplish this, see here: ",
"https://rust-lang.github.io/rust-bindgen/requirements.html#installing-clang-39"))
}).expect("a `libclang` shared library is not loaded on this thread");
f($($pname), *)
}
$(#[doc=$doc] #[cfg($cfg)])*
pub mod $name {
pub fn is_loaded() -> bool {
super::with_library(|l| l.functions.$name.is_some()).unwrap_or(false)
}
}
)+
mod load {
$(link!(@LOAD: $(#[cfg($cfg)])* fn $name($($pname: $pty), *) $(-> $ret)*);)+
}
/// Loads a `libclang` shared library and returns the library instance.
///
/// This function does not attempt to load any functions from the shared library. The caller
/// is responsible for loading the functions they require.
///
/// # Failures
///
/// * a `libclang` shared library could not be found
/// * the `libclang` shared library could not be opened
pub fn load_manually() -> Result<SharedLibrary, String> {
mod build {
pub mod common { include!(concat!(env!("OUT_DIR"), "/common.rs")); }
pub mod dynamic { include!(concat!(env!("OUT_DIR"), "/dynamic.rs")); }
}
let (directory, filename) = build::dynamic::find(true)?;
let path = directory.join(filename);
unsafe {
let library = libloading::Library::new(&path).map_err(|e| {
format!(
"the `libclang` shared library at {} could not be opened: {}",
path.display(),
e,
)
});
let mut library = SharedLibrary::new(library?, path);
$(load::$name(&mut library);)+
Ok(library)
}
}
/// Loads a `libclang` shared library for use in the current thread.
///
/// This functions attempts to load all the functions in the shared library. Whether a
/// function has been loaded can be tested by calling the `is_loaded` function on the
/// module with the same name as the function (e.g., `clang_createIndex::is_loaded()` for
/// the `clang_createIndex` function).
///
/// # Failures
///
/// * a `libclang` shared library could not be found
/// * the `libclang` shared library could not be opened
#[allow(dead_code)]
pub fn load() -> Result<(), String> {
let library = Arc::new(load_manually()?);
LIBRARY.with(|l| *l.borrow_mut() = Some(library));
Ok(())
}
/// Unloads the `libclang` shared library in use in the current thread.
///
/// # Failures
///
/// * a `libclang` shared library is not in use in the current thread
pub fn unload() -> Result<(), String> {
let library = set_library(None);
if library.is_some() {
Ok(())
} else {
Err("a `libclang` shared library is not in use in the current thread".into())
}
}
/// Returns the library instance stored in TLS.
///
/// This functions allows for sharing library instances between threads.
pub fn get_library() -> Option<Arc<SharedLibrary>> {
LIBRARY.with(|l| l.borrow_mut().clone())
}
/// Sets the library instance stored in TLS and returns the previous library.
///
/// This functions allows for sharing library instances between threads.
pub fn set_library(library: Option<Arc<SharedLibrary>>) -> Option<Arc<SharedLibrary>> {
LIBRARY.with(|l| mem::replace(&mut *l.borrow_mut(), library))
}
)
}
#[cfg(not(feature = "runtime"))]
macro_rules! link {
(
$(
$(#[doc=$doc:expr] #[cfg($cfg:meta)])*
pub fn $name:ident($($pname:ident: $pty:ty), *) $(-> $ret:ty)*;
)+
) => (
extern {
$(
$(#[doc=$doc] #[cfg($cfg)])*
pub fn $name($($pname: $pty), *) $(-> $ret)*;
)+
}
$(
$(#[doc=$doc] #[cfg($cfg)])*
pub mod $name {
pub fn is_loaded() -> bool { true }
}
)+
)
}

View File

@@ -0,0 +1,236 @@
// SPDX-License-Identifier: Apache-2.0
//! Provides helper functionality.
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, io};
use glob::{self, Pattern};
use libc::c_int;
use super::CXVersion;
//================================================
// Structs
//================================================
/// A `clang` executable.
#[derive(Clone, Debug)]
pub struct Clang {
/// The path to this `clang` executable.
pub path: PathBuf,
/// The version of this `clang` executable if it could be parsed.
pub version: Option<CXVersion>,
/// The directories searched by this `clang` executable for C headers if
/// they could be parsed.
pub c_search_paths: Option<Vec<PathBuf>>,
/// The directories searched by this `clang` executable for C++ headers if
/// they could be parsed.
pub cpp_search_paths: Option<Vec<PathBuf>>,
}
impl Clang {
fn new(path: impl AsRef<Path>, args: &[String]) -> Self {
Self {
path: path.as_ref().into(),
version: parse_version(path.as_ref()),
c_search_paths: parse_search_paths(path.as_ref(), "c", args),
cpp_search_paths: parse_search_paths(path.as_ref(), "c++", args),
}
}
/// Returns a `clang` executable if one can be found.
///
/// If the `CLANG_PATH` environment variable is set, that is the instance of
/// `clang` used. Otherwise, a series of directories are searched. First, if
/// a path is supplied, that is the first directory searched. Then, the
/// directory returned by `llvm-config --bindir` is searched. On macOS
/// systems, `xcodebuild -find clang` will next be queried. Last, the
/// directories in the system's `PATH` are searched.
///
/// ## Cross-compilation
///
/// If target arguments are provided (e.g., `-target` followed by a target
/// like `x86_64-unknown-linux-gnu`) then this method will prefer a
/// target-prefixed instance of `clang` (e.g.,
/// `x86_64-unknown-linux-gnu-clang` for the above example).
pub fn find(path: Option<&Path>, args: &[String]) -> Option<Clang> {
if let Ok(path) = env::var("CLANG_PATH") {
let p = Path::new(&path);
if p.is_file() && is_executable(&p).unwrap_or(false) {
return Some(Clang::new(p, args));
}
}
// Determine the cross-compilation target, if any.
let mut target = None;
for i in 0..args.len() {
if args[i] == "-target" && i + 1 < args.len() {
target = Some(&args[i + 1]);
}
}
// Collect the paths to search for a `clang` executable in.
let mut paths = vec![];
if let Some(path) = path {
paths.push(path.into());
}
if let Ok(path) = run_llvm_config(&["--bindir"]) {
if let Some(line) = path.lines().next() {
paths.push(line.into());
}
}
if cfg!(target_os = "macos") {
if let Ok((path, _)) = run("xcodebuild", &["-find", "clang"]) {
if let Some(line) = path.lines().next() {
paths.push(line.into());
}
}
}
if let Ok(path) = env::var("PATH") {
paths.extend(env::split_paths(&path));
}
// First, look for a target-prefixed `clang` executable.
if let Some(target) = target {
let default = format!("{}-clang{}", target, env::consts::EXE_SUFFIX);
let versioned = format!("{}-clang-[0-9]*{}", target, env::consts::EXE_SUFFIX);
let patterns = &[&default[..], &versioned[..]];
for path in &paths {
if let Some(path) = find(path, patterns) {
return Some(Clang::new(path, args));
}
}
}
// Otherwise, look for any other `clang` executable.
let default = format!("clang{}", env::consts::EXE_SUFFIX);
let versioned = format!("clang-[0-9]*{}", env::consts::EXE_SUFFIX);
let patterns = &[&default[..], &versioned[..]];
for path in paths {
if let Some(path) = find(&path, patterns) {
return Some(Clang::new(path, args));
}
}
None
}
}
//================================================
// Functions
//================================================
/// Returns the first match to the supplied glob patterns in the supplied
/// directory if there are any matches.
fn find(directory: &Path, patterns: &[&str]) -> Option<PathBuf> {
// Escape the directory in case it contains characters that have special
// meaning in glob patterns (e.g., `[` or `]`).
let directory = if let Some(directory) = directory.to_str() {
Path::new(&Pattern::escape(directory)).to_owned()
} else {
return None;
};
for pattern in patterns {
let pattern = directory.join(pattern).to_string_lossy().into_owned();
if let Some(path) = glob::glob(&pattern).ok()?.filter_map(|p| p.ok()).next() {
if path.is_file() && is_executable(&path).unwrap_or(false) {
return Some(path);
}
}
}
None
}
#[cfg(unix)]
fn is_executable(path: &Path) -> io::Result<bool> {
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
let path = CString::new(path.as_os_str().as_bytes())?;
unsafe { Ok(libc::access(path.as_ptr(), libc::X_OK) == 0) }
}
#[cfg(not(unix))]
fn is_executable(_: &Path) -> io::Result<bool> {
Ok(true)
}
/// Attempts to run an executable, returning the `stdout` and `stderr` output if
/// successful.
fn run(executable: &str, arguments: &[&str]) -> Result<(String, String), String> {
Command::new(executable)
.args(arguments)
.output()
.map(|o| {
let stdout = String::from_utf8_lossy(&o.stdout).into_owned();
let stderr = String::from_utf8_lossy(&o.stderr).into_owned();
(stdout, stderr)
})
.map_err(|e| format!("could not run executable `{}`: {}", executable, e))
}
/// Runs `clang`, returning the `stdout` and `stderr` output.
fn run_clang(path: &Path, arguments: &[&str]) -> (String, String) {
run(&path.to_string_lossy().into_owned(), arguments).unwrap()
}
/// Runs `llvm-config`, returning the `stdout` output if successful.
fn run_llvm_config(arguments: &[&str]) -> Result<String, String> {
let config = env::var("LLVM_CONFIG_PATH").unwrap_or_else(|_| "llvm-config".to_string());
run(&config, arguments).map(|(o, _)| o)
}
/// Parses a version number if possible, ignoring trailing non-digit characters.
fn parse_version_number(number: &str) -> Option<c_int> {
number
.chars()
.take_while(|c| c.is_digit(10))
.collect::<String>()
.parse()
.ok()
}
/// Parses the version from the output of a `clang` executable if possible.
fn parse_version(path: &Path) -> Option<CXVersion> {
let output = run_clang(path, &["--version"]).0;
let start = output.find("version ")? + 8;
let mut numbers = output[start..].split_whitespace().next()?.split('.');
let major = numbers.next().and_then(parse_version_number)?;
let minor = numbers.next().and_then(parse_version_number)?;
let subminor = numbers.next().and_then(parse_version_number).unwrap_or(0);
Some(CXVersion {
Major: major,
Minor: minor,
Subminor: subminor,
})
}
/// Parses the search paths from the output of a `clang` executable if possible.
fn parse_search_paths(path: &Path, language: &str, args: &[String]) -> Option<Vec<PathBuf>> {
let mut clang_args = vec!["-E", "-x", language, "-", "-v"];
clang_args.extend(args.iter().map(|s| &**s));
let output = run_clang(path, &clang_args).1;
let start = output.find("#include <...> search starts here:")? + 34;
let end = output.find("End of search list.")?;
let paths = output[start..end].replace("(framework directory)", "");
Some(
paths
.lines()
.filter(|l| !l.is_empty())
.map(|l| Path::new(l.trim()).into())
.collect(),
)
}