Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/uu/cksum/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cksum-help-raw = emit a raw binary digest, not hexadecimal
cksum-help-strict = exit non-zero for improperly formatted checksum lines
cksum-help-check = read hashsums from the FILEs and check them
cksum-help-base64 = emit a base64 digest, not hexadecimal
cksum-help-debug = indicate which implementation is used
cksum-help-warn = warn about improperly formatted checksum lines
cksum-help-status = don't output anything, status code shows success
cksum-help-quiet = don't print OK for each successfully verified file
Expand Down
1 change: 1 addition & 0 deletions src/uu/cksum/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cksum-help-raw = émettre un condensé binaire brut, pas hexadécimal
cksum-help-strict = sortir avec un code non-zéro pour les lignes de somme de contrôle mal formatées
cksum-help-check = lire les sommes de hachage des FICHIERs et les vérifier
cksum-help-base64 = émettre un condensé base64, pas hexadécimal
cksum-help-debug = indiquer quelle implémentation est utilisée
cksum-help-warn = avertir des lignes de somme de contrôle mal formatées
cksum-help-status = ne rien afficher, le code de statut indique le succès
cksum-help-quiet = ne pas afficher OK pour chaque fichier vérifié avec succès
Expand Down
191 changes: 190 additions & 1 deletion src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) fname, algo
// spell-checker:ignore (ToDO) fname, algo, hwcaps, pclmul, vmull, pmull, vpclmulqdq, pclmulqdq, tunables, behaviour

use clap::builder::ValueParser;
use clap::{Arg, ArgAction, Command};
use std::collections::HashSet;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::io::{BufReader, Read, Write, stdin, stdout};
Expand Down Expand Up @@ -37,6 +39,7 @@ struct Options {
length: Option<usize>,
output_format: OutputFormat,
line_ending: LineEnding,
debug: bool,
}

/// Reading mode used to compute digest.
Expand Down Expand Up @@ -188,6 +191,8 @@ where
{
let mut files = files.peekable();

emit_crc_debug_info(&options);

while let Some(filename) = files.next() {
// Check that in raw mode, we are not provided with several files.
if options.output_format.is_raw() && files.peek().is_some() {
Expand Down Expand Up @@ -268,6 +273,160 @@ where
Ok(())
}

fn log_debug_message(feature: &str, status: Option<bool>) {
match status {
Some(true) => eprintln!("cksum: using {feature} hardware support"),
Some(false) => eprintln!("cksum: {feature} support not detected"),
None => {}
}
}

/// Emit GNU-compatible hardware selection messages for `cksum --debug`.
///
/// GNU cksum prints which SIMD/SW implementation it picked whenever `--debug` is present.
/// The upstream tests rely on that output to ensure every hardware-specific path can be
/// exercised (including cases where GLIBC_TUNABLES forces a feature off). We mirror that
/// behaviour here by checking both `std::arch` feature flags and the `glibc.cpu.hwcaps`
/// overrides, but only for the legacy CRC algorithms where multiple implementations exist.
fn emit_crc_debug_info(options: &Options) {
if !options.debug {
return;
}

if options.algo_name != ALGORITHM_OPTIONS_CRC && options.algo_name != ALGORITHM_OPTIONS_CRC32B {
return;
}

let disabled_caps = glibc_disabled_hwcaps();

log_debug_message("avx512", detect_avx512(&disabled_caps));
log_debug_message("avx2", detect_avx2(&disabled_caps));
log_debug_message("pclmul", detect_pclmul(&disabled_caps));

if options.algo_name == ALGORITHM_OPTIONS_CRC {
log_debug_message("vmull", detect_vmull(&disabled_caps));
}
}

fn detect_avx512(disabled_caps: &HashSet<String>) -> Option<bool> {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if disabled_caps.contains("AVX512F")
|| disabled_caps.contains("AVX512BW")
|| disabled_caps.contains("VPCLMULQDQ")
{
return Some(false);
}

Some(
std::arch::is_x86_feature_detected!("avx512f")
&& std::arch::is_x86_feature_detected!("avx512bw")
&& std::arch::is_x86_feature_detected!("vpclmulqdq"),
)
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
{
let _ = disabled_caps;
None
}
}

fn detect_avx2(disabled_caps: &HashSet<String>) -> Option<bool> {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if disabled_caps.contains("AVX2") || disabled_caps.contains("VPCLMULQDQ") {
return Some(false);
}

Some(
std::arch::is_x86_feature_detected!("avx2")
&& std::arch::is_x86_feature_detected!("vpclmulqdq"),
)
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
{
let _ = disabled_caps;
None
}
}

fn detect_pclmul(disabled_caps: &HashSet<String>) -> Option<bool> {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if disabled_caps.contains("AVX")
|| disabled_caps.contains("PCLMUL")
|| disabled_caps.contains("PCLMULQDQ")
{
return Some(false);
}

Some(
std::arch::is_x86_feature_detected!("avx")
&& std::arch::is_x86_feature_detected!("pclmulqdq"),
)
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
{
let _ = disabled_caps;
None
}
}

fn detect_vmull(disabled_caps: &HashSet<String>) -> Option<bool> {
#[cfg(target_arch = "aarch64")]
{
if disabled_caps.contains("PMULL") {
return Some(false);
}

Some(std::arch::is_aarch64_feature_detected!("pmull"))
}

#[cfg(not(target_arch = "aarch64"))]
{
let _ = disabled_caps;
None
}
}

fn glibc_disabled_hwcaps() -> HashSet<String> {
env::var("GLIBC_TUNABLES")
.ok()
.map(|value| parse_disabled_hwcaps(&value))
.unwrap_or_default()
}

fn parse_disabled_hwcaps(tunables: &str) -> HashSet<String> {
let mut disabled = HashSet::new();

for entry in tunables.split(':') {
let trimmed = entry.trim();
let Some(value) = trimmed.strip_prefix("glibc.cpu.hwcaps=") else {
continue;
};

for token in value.split(',') {
let token = token.trim();
if token.is_empty() {
continue;
}

if let Some(feature) = token.strip_prefix('-') {
let feature = feature.trim();

if !feature.is_empty() {
disabled.insert(feature.to_ascii_uppercase());
}
}
}
}

disabled
}

mod options {
pub const ALGORITHM: &str = "algorithm";
pub const FILE: &str = "file";
Expand All @@ -285,6 +444,7 @@ mod options {
pub const IGNORE_MISSING: &str = "ignore-missing";
pub const QUIET: &str = "quiet";
pub const ZERO: &str = "zero";
pub const DEBUG: &str = "debug";
}

/// cksum has a bunch of legacy behavior. We handle this in this function to
Expand Down Expand Up @@ -395,6 +555,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

let check = matches.get_flag(options::CHECK);
let debug_flag = matches.get_flag(options::DEBUG);

let algo_cli = matches
.get_one::<String>(options::ALGORITHM)
Expand Down Expand Up @@ -470,6 +631,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
length,
output_format,
line_ending,
debug: debug_flag,
};

cksum(opts, files)?;
Expand Down Expand Up @@ -549,6 +711,12 @@ pub fn uu_app() -> Command {
// GNU cksum does not permit these flags to be combined:
.conflicts_with(options::RAW),
)
.arg(
Arg::new(options::DEBUG)
.long(options::DEBUG)
.help(translate!("cksum-help-debug"))
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::TEXT)
.long(options::TEXT)
Expand Down Expand Up @@ -602,3 +770,24 @@ pub fn uu_app() -> Command {
)
.after_help(translate!("cksum-after-help"))
}

#[cfg(test)]
mod tests {
use super::parse_disabled_hwcaps;

#[test]
fn parse_disabled_hwcaps_picks_up_multiple_features() {
let caps = parse_disabled_hwcaps("glibc.cpu.hwcaps=-AVX2,-PMULL,");
assert!(caps.contains("AVX2"));
assert!(caps.contains("PMULL"));
}

#[test]
fn parse_disabled_hwcaps_ignores_other_entries() {
let caps =
parse_disabled_hwcaps("glibc.malloc.trim=0:glibc.cpu.hwcaps=-AVX512F,-VPCLMULQDQ");
assert!(caps.contains("AVX512F"));
assert!(caps.contains("VPCLMULQDQ"));
assert!(!caps.contains("MALLOC"));
}
}
5 changes: 5 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}

#[test]
fn test_debug_flag_is_accepted() {
new_ucmd!().arg("--debug").arg("lorem_ipsum.txt").succeeds();
}

#[test]
fn test_single_file() {
new_ucmd!()
Expand Down
Loading