Skip to content

Commit edfe5b7

Browse files
committed
Use cfg_if macro
There were a few places where this can be used for a slight improvement to code quality.
1 parent ad0ed8a commit edfe5b7

File tree

5 files changed

+83
-81
lines changed

5 files changed

+83
-81
lines changed

polkadot/node/core/pvf/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license.workspace = true
88

99
[dependencies]
1010
always-assert = "0.1"
11+
cfg-if = "1.0"
1112
futures = "0.3.21"
1213
futures-timer = "3.0.2"
1314
gum = { package = "tracing-gum", path = "../../gum" }

polkadot/node/core/pvf/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition.workspace = true
77
license.workspace = true
88

99
[dependencies]
10+
cfg-if = "1.0"
1011
cpu-time = "1.0.0"
1112
futures = "0.3.21"
1213
gum = { package = "tracing-gum", path = "../../../gum" }

polkadot/node/core/pvf/prepare-worker/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition.workspace = true
77
license.workspace = true
88

99
[dependencies]
10+
cfg-if = "1.0"
1011
futures = "0.3.21"
1112
gum = { package = "tracing-gum", path = "../../../gum" }
1213
libc = "0.2.139"

polkadot/node/core/pvf/prepare-worker/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,13 @@ pub fn worker_entrypoint(
212212
Err(err)
213213
},
214214
Ok(ok) => {
215-
#[cfg(not(target_os = "linux"))]
216-
let (artifact, cpu_time_elapsed) = ok;
217-
#[cfg(target_os = "linux")]
218-
let (artifact, cpu_time_elapsed, max_rss) = ok;
215+
cfg_if::cfg_if! {
216+
if #[cfg(target_os = "linux")] {
217+
let (artifact, cpu_time_elapsed, max_rss) = ok;
218+
} else {
219+
let (artifact, cpu_time_elapsed) = ok;
220+
}
221+
}
219222

220223
// Stop the memory stats worker and get its observed memory stats.
221224
#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]

polkadot/node/core/pvf/src/host.rs

Lines changed: 73 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -891,48 +891,46 @@ fn check_can_unshare_user_namespace_and_change_root(
891891
#[cfg_attr(not(target_os = "linux"), allow(unused_variables))]
892892
prepare_worker_program_path: &Path,
893893
) -> bool {
894-
#[cfg(target_os = "linux")]
895-
{
896-
let output = std::process::Command::new(prepare_worker_program_path)
897-
.arg("--check-can-unshare-user-namespace-and-change-root")
898-
.output();
899-
900-
match output {
901-
Ok(output) if output.status.success() => true,
902-
Ok(output) => {
903-
let stderr = std::str::from_utf8(&output.stderr)
904-
.expect("child process writes a UTF-8 string to stderr; qed")
905-
.trim();
906-
gum::warn!(
907-
target: LOG_TARGET,
908-
?prepare_worker_program_path,
909-
// Docs say to always print status using `Display` implementation.
910-
status = %output.status,
911-
%stderr,
912-
"Cannot unshare user namespace and change root, which are Linux-specific kernel security features. Running validation of malicious PVF code has a higher risk of compromising this machine. Consider running with support for unsharing user namespaces for maximum security."
913-
);
914-
false
915-
},
916-
Err(err) => {
917-
gum::warn!(
918-
target: LOG_TARGET,
919-
?prepare_worker_program_path,
920-
"Could not start child process: {}",
921-
err
922-
);
923-
false
924-
},
894+
cfg_if::cfg_if! {
895+
if #[cfg(target_os = "linux")] {
896+
let output = std::process::Command::new(prepare_worker_program_path)
897+
.arg("--check-can-unshare-user-namespace-and-change-root")
898+
.output();
899+
900+
match output {
901+
Ok(output) if output.status.success() => true,
902+
Ok(output) => {
903+
let stderr = std::str::from_utf8(&output.stderr)
904+
.expect("child process writes a UTF-8 string to stderr; qed")
905+
.trim();
906+
gum::warn!(
907+
target: LOG_TARGET,
908+
?prepare_worker_program_path,
909+
// Docs say to always print status using `Display` implementation.
910+
status = %output.status,
911+
%stderr,
912+
"Cannot unshare user namespace and change root, which are Linux-specific kernel security features. Running validation of malicious PVF code has a higher risk of compromising this machine. Consider running with support for unsharing user namespaces for maximum security."
913+
);
914+
false
915+
},
916+
Err(err) => {
917+
gum::warn!(
918+
target: LOG_TARGET,
919+
?prepare_worker_program_path,
920+
"Could not start child process: {}",
921+
err
922+
);
923+
false
924+
},
925+
}
926+
} else {
927+
gum::warn!(
928+
target: LOG_TARGET,
929+
"Cannot unshare user namespace and change root, which are Linux-specific kernel security features. Running validation of malicious PVF code has a higher risk of compromising this machine. Consider running on Linux with support for unsharing user namespaces for maximum security."
930+
);
931+
false
925932
}
926933
}
927-
928-
#[cfg(not(target_os = "linux"))]
929-
{
930-
gum::warn!(
931-
target: LOG_TARGET,
932-
"Cannot unshare user namespace and change root, which are Linux-specific kernel security features. Running validation of malicious PVF code has a higher risk of compromising this machine. Consider running on Linux with support for unsharing user namespaces for maximum security."
933-
);
934-
false
935-
}
936934
}
937935

938936
/// Check if landlock is supported and emit a warning if not.
@@ -944,45 +942,43 @@ fn check_landlock(
944942
#[cfg_attr(not(target_os = "linux"), allow(unused_variables))]
945943
prepare_worker_program_path: &Path,
946944
) -> bool {
947-
#[cfg(target_os = "linux")]
948-
{
949-
match std::process::Command::new(prepare_worker_program_path)
950-
.arg("--check-can-enable-landlock")
951-
.status()
952-
{
953-
Ok(status) if status.success() => true,
954-
Ok(status) => {
955-
let abi =
956-
polkadot_node_core_pvf_common::worker::security::landlock::LANDLOCK_ABI as u8;
957-
gum::warn!(
958-
target: LOG_TARGET,
959-
?prepare_worker_program_path,
960-
?status,
961-
%abi,
962-
"Cannot fully enable landlock, a Linux-specific kernel security feature. Running validation of malicious PVF code has a higher risk of compromising this machine. Consider upgrading the kernel version for maximum security."
963-
);
964-
false
965-
},
966-
Err(err) => {
967-
gum::warn!(
968-
target: LOG_TARGET,
969-
?prepare_worker_program_path,
970-
"Could not start child process: {}",
971-
err
972-
);
973-
false
974-
},
945+
cfg_if::cfg_if! {
946+
if #[cfg(target_os = "linux")] {
947+
match std::process::Command::new(prepare_worker_program_path)
948+
.arg("--check-can-enable-landlock")
949+
.status()
950+
{
951+
Ok(status) if status.success() => true,
952+
Ok(status) => {
953+
let abi =
954+
polkadot_node_core_pvf_common::worker::security::landlock::LANDLOCK_ABI as u8;
955+
gum::warn!(
956+
target: LOG_TARGET,
957+
?prepare_worker_program_path,
958+
?status,
959+
%abi,
960+
"Cannot fully enable landlock, a Linux-specific kernel security feature. Running validation of malicious PVF code has a higher risk of compromising this machine. Consider upgrading the kernel version for maximum security."
961+
);
962+
false
963+
},
964+
Err(err) => {
965+
gum::warn!(
966+
target: LOG_TARGET,
967+
?prepare_worker_program_path,
968+
"Could not start child process: {}",
969+
err
970+
);
971+
false
972+
},
973+
}
974+
} else {
975+
gum::warn!(
976+
target: LOG_TARGET,
977+
"Cannot enable landlock, a Linux-specific kernel security feature. Running validation of malicious PVF code has a higher risk of compromising this machine. Consider running on Linux with landlock support for maximum security."
978+
);
979+
false
975980
}
976981
}
977-
978-
#[cfg(not(target_os = "linux"))]
979-
{
980-
gum::warn!(
981-
target: LOG_TARGET,
982-
"Cannot enable landlock, a Linux-specific kernel security feature. Running validation of malicious PVF code has a higher risk of compromising this machine. Consider running on Linux with landlock support for maximum security."
983-
);
984-
false
985-
}
986982
}
987983

988984
#[cfg(test)]

0 commit comments

Comments
 (0)