Jieyou Xu afeed50677
Adjust run_make_support::symbols helpers
Massage the `symbols` helpers to fill out {match all, match any} x
{substring match, exact match}:

|           | Substring match                        | Exact match                   |
|-----------|----------------------------------------|-------------------------------|
| Match any | `object_contains_any_symbol_substring` | `object_contains_any_symbol`  |
| Match all | `object_contains_all_symbol_substring` | `object_contains_all_symbols` |

As part of this, rename `any_symbol_contains` to
`object_contains_any_symbol_substring` for accuracy.
2025-07-13 14:28:41 +08:00

93 lines
3.6 KiB
Rust

//! `run_make_support::symbols` helpers self test.
// Only intended as a basic smoke test, does not try to account for platform or calling convention
// specific symbol decorations.
//@ only-x86_64-unknown-linux-gnu
//@ ignore-cross-compile
use std::collections::BTreeSet;
use object::{Object, ObjectSymbol};
use run_make_support::symbols::{
ContainsAllSymbolSubstringsOutcome, ContainsAllSymbolsOutcome,
object_contains_all_symbol_substring, object_contains_all_symbols, object_contains_any_symbol,
object_contains_any_symbol_substring,
};
use run_make_support::{object, rfs, rust_lib_name, rustc};
fn main() {
rustc().input("sample.rs").emit("obj").edition("2024").run();
// `sample.rs` has two `no_mangle` functions, `eszett` and `beta`, in addition to `main`.
//
// These two symbol names and the test substrings used below are carefully picked to make sure
// they do not overlap with `sample` and contain non-hex characters, to avoid accidentally
// matching against CGU names like `sample.dad0f15d00c84e70-cgu.0`.
let obj_filename = "sample.o";
let blob = rfs::read(obj_filename);
let obj = object::File::parse(&*blob).unwrap();
eprintln!("found symbols:");
for sym in obj.symbols() {
eprintln!("symbol = {}", sym.name().unwrap());
}
// `hello` contains `hel`
assert!(object_contains_any_symbol_substring(obj_filename, &["zett"]));
assert!(object_contains_any_symbol_substring(obj_filename, &["zett", "does_not_exist"]));
assert!(!object_contains_any_symbol_substring(obj_filename, &["does_not_exist"]));
assert!(object_contains_any_symbol(obj_filename, &["eszett"]));
assert!(object_contains_any_symbol(obj_filename, &["eszett", "beta"]));
assert!(!object_contains_any_symbol(obj_filename, &["zett"]));
assert!(!object_contains_any_symbol(obj_filename, &["does_not_exist"]));
assert_eq!(
object_contains_all_symbol_substring(obj_filename, &["zett"]),
ContainsAllSymbolSubstringsOutcome::Ok
);
assert_eq!(
object_contains_all_symbol_substring(obj_filename, &["zett", "bet"]),
ContainsAllSymbolSubstringsOutcome::Ok
);
assert_eq!(
object_contains_all_symbol_substring(obj_filename, &["does_not_exist"]),
ContainsAllSymbolSubstringsOutcome::MissingSymbolSubstrings(BTreeSet::from([
"does_not_exist"
]))
);
assert_eq!(
object_contains_all_symbol_substring(obj_filename, &["zett", "does_not_exist"]),
ContainsAllSymbolSubstringsOutcome::MissingSymbolSubstrings(BTreeSet::from([
"does_not_exist"
]))
);
assert_eq!(
object_contains_all_symbol_substring(obj_filename, &["zett", "bet", "does_not_exist"]),
ContainsAllSymbolSubstringsOutcome::MissingSymbolSubstrings(BTreeSet::from([
"does_not_exist"
]))
);
assert_eq!(
object_contains_all_symbols(obj_filename, &["eszett"]),
ContainsAllSymbolsOutcome::Ok
);
assert_eq!(
object_contains_all_symbols(obj_filename, &["eszett", "beta"]),
ContainsAllSymbolsOutcome::Ok
);
assert_eq!(
object_contains_all_symbols(obj_filename, &["zett"]),
ContainsAllSymbolsOutcome::MissingSymbols(BTreeSet::from(["zett"]))
);
assert_eq!(
object_contains_all_symbols(obj_filename, &["zett", "beta"]),
ContainsAllSymbolsOutcome::MissingSymbols(BTreeSet::from(["zett"]))
);
assert_eq!(
object_contains_all_symbols(obj_filename, &["does_not_exist"]),
ContainsAllSymbolsOutcome::MissingSymbols(BTreeSet::from(["does_not_exist"]))
);
}