Skip to content
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ permissions:
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
EMULEBB_BUILD_REF: 43515795e85beceeb73b1bbbd77e5ab167ef13b6
EMULEBB_ANDROID_BUILD_REF: 43515795e85beceeb73b1bbbd77e5ab167ef13b6
EMULEBB_BUILD_TESTS_REF: 281ccbcdf9f4c9960c5efda4705df966418b6bb2
EMULEBB_BUILD_REF: 177bb6c7118d46268fd9e73d4ae78f5d1516d4e9
EMULEBB_ANDROID_BUILD_REF: 177bb6c7118d46268fd9e73d4ae78f5d1516d4e9
EMULEBB_BUILD_TESTS_REF: 17438846c1a62fa8228c821071cc79e7cf56145b
EMULEBB_TOOLING_REF: f88a8ce608bc75a619aea6909f54a9cdecdddb6e
# Pin the native NAT sources linked by the sys crates.
MINIUPNP_REF: 4e7a10948004ca2085ac8103e14fc77edb898b4c
Expand Down Expand Up @@ -602,7 +602,7 @@ jobs:
with:
persist-credentials: false
repository: emulebb/emulebb-build-tests
ref: 281ccbcdf9f4c9960c5efda4705df966418b6bb2
ref: 17438846c1a62fa8228c821071cc79e7cf56145b
path: repos/emulebb-build-tests

- name: Checkout pinned Rust OpenAPI contract
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ permissions:
contents: read

env:
EMULEBB_APK_DIAGNOSTIC_BUILD_REF: 43515795e85beceeb73b1bbbd77e5ab167ef13b6
EMULEBB_APK_DIAGNOSTIC_HARNESS_REF: 281ccbcdf9f4c9960c5efda4705df966418b6bb2
EMULEBB_APK_DIAGNOSTIC_BUILD_REF: 177bb6c7118d46268fd9e73d4ae78f5d1516d4e9
EMULEBB_APK_DIAGNOSTIC_HARNESS_REF: 17438846c1a62fa8228c821071cc79e7cf56145b
EMULEBB_APK_DIAGNOSTIC_TOOLING_REF: f88a8ce608bc75a619aea6909f54a9cdecdddb6e
EMULEBB_APK_DIAGNOSTIC_MINIUPNP_REF: 4e7a10948004ca2085ac8103e14fc77edb898b4c
EMULEBB_APK_DIAGNOSTIC_PCPNATPMP_REF: a6e787f837e507336a8774522c33b54264ef4a4b
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/optional-platforms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ permissions:
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
EMULEBB_BUILD_REF: 43515795e85beceeb73b1bbbd77e5ab167ef13b6
EMULEBB_ANDROID_BUILD_REF: 43515795e85beceeb73b1bbbd77e5ab167ef13b6
EMULEBB_BUILD_TESTS_REF: 281ccbcdf9f4c9960c5efda4705df966418b6bb2
EMULEBB_BUILD_REF: 177bb6c7118d46268fd9e73d4ae78f5d1516d4e9
EMULEBB_ANDROID_BUILD_REF: 177bb6c7118d46268fd9e73d4ae78f5d1516d4e9
EMULEBB_BUILD_TESTS_REF: 17438846c1a62fa8228c821071cc79e7cf56145b
EMULEBB_TOOLING_REF: f88a8ce608bc75a619aea6909f54a9cdecdddb6e
# Pin the native NAT sources linked by the sys crates.
MINIUPNP_REF: 4e7a10948004ca2085ac8103e14fc77edb898b4c
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ permissions:
env:
RELEASE_PLATFORM_PROFILE: ${{ inputs.platform_profile || 'full' }}
RELEASE_CHANNEL: ${{ inputs.channel || (startsWith(github.ref, 'refs/tags/rust-v') && 'beta') || 'candidate' }}
EMULEBB_BUILD_REF: 43515795e85beceeb73b1bbbd77e5ab167ef13b6
EMULEBB_LINUX_PREPARED_IMAGE: ghcr.io/emulebb/emulebb-build-ci/ubuntu-rust@sha256:c94812f3ec606ada847fed87aea7192ba64c1ae3fe11c5cb117f75b6319da62b
EMULEBB_BUILD_TESTS_REF: 281ccbcdf9f4c9960c5efda4705df966418b6bb2
EMULEBB_BUILD_REF: 177bb6c7118d46268fd9e73d4ae78f5d1516d4e9
EMULEBB_LINUX_PREPARED_IMAGE: ghcr.io/emulebb/emulebb-build-ci/ubuntu-rust@sha256:ba73b285184dcf88bd7208e07bae12de90fba707fbc82670cc4c2bf71318ba80
EMULEBB_BUILD_TESTS_REF: 17438846c1a62fa8228c821071cc79e7cf56145b
EMULEBB_TOOLING_REF: f88a8ce608bc75a619aea6909f54a9cdecdddb6e
EMULEBB_RELEASE_VALIDATOR_REF: 43515795e85beceeb73b1bbbd77e5ab167ef13b6
EMULEBB_APK_BUILD_REF: 43515795e85beceeb73b1bbbd77e5ab167ef13b6
EMULEBB_APK_BUILD_TESTS_REF: 281ccbcdf9f4c9960c5efda4705df966418b6bb2
EMULEBB_RELEASE_VALIDATOR_REF: 177bb6c7118d46268fd9e73d4ae78f5d1516d4e9
EMULEBB_APK_BUILD_REF: 177bb6c7118d46268fd9e73d4ae78f5d1516d4e9
EMULEBB_APK_BUILD_TESTS_REF: 17438846c1a62fa8228c821071cc79e7cf56145b
EMULEBB_ANDROID_SIGNER_SHA256: 9edc742d7230a714a0149c20eb2afb235b8ad5237166090f10e7ac69e2bb359f
MINIUPNP_REF: 4e7a10948004ca2085ac8103e14fc77edb898b4c
PCPNATPMP_REF: a6e787f837e507336a8774522c33b54264ef4a4b
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 25 additions & 5 deletions crates/emulebb-daemon/tests/profile_ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ fn profile_clones_and_metadata_handles_share_one_logical_lease() {
fn profile_ownership_child() {
let profile_path = PathBuf::from(env::var_os(CHILD_PROFILE).unwrap());
let mode = env::var(CHILD_MODE).unwrap();
if mode == "prepare-executable" {
let current = env::current_exe().unwrap();
fs::copy(&current, profile_path.join(current.file_name().unwrap())).unwrap();
return;
}
let result = match mode.as_str() {
"executable-default" => DaemonProfile::load(None),
"explicit-dot" => DaemonProfile::load(Some(PathBuf::from("."))),
Expand Down Expand Up @@ -333,11 +338,19 @@ fn executable_default_ignores_shell_cwd_and_explicit_profiles_resolve_from_it()
for directory in [&executable_dir, &other_cwd, &unused_home] {
fs::create_dir(directory).unwrap();
}
// WHY: invoking the original unit-test executable with no profile would
// write beside a shared Cargo artifact. Copy the fixture into owned output.
// WHY: tmpfs profile scratch can be on a different mount from Cargo artifacts.
// Stage in a child that spawns no descendants and wait for its exit so parallel
// parent launches cannot inherit a writer and cause ETXTBSY.
let current = env::current_exe().unwrap();
let copied = executable_dir.join(current.file_name().unwrap());
fs::copy(current, &copied).unwrap();
let isolated_executable = executable_dir.join(current.file_name().unwrap());
{
let mut preparation = OwnedChild::start(
&executable_dir,
"prepare-executable",
&temporary.path().join("ready"),
);
preparation.finish();
}
fs::write(other_cwd.join("not-a-directory"), b"synthetic blocked path").unwrap();

for (mode, cwd, expected) in [
Expand All @@ -362,7 +375,7 @@ fn executable_default_ignores_shell_cwd_and_explicit_profiles_resolve_from_it()
other_cwd.join("not-a-directory"),
),
] {
let mut command = Command::new(&copied);
let mut command = Command::new(&isolated_executable);
command
.current_dir(cwd)
.env("HOME", &unused_home)
Expand All @@ -374,4 +387,11 @@ fn executable_default_ignores_shell_cwd_and_explicit_profiles_resolve_from_it()
}
assert!(!other_cwd.join("emulebb-rust-settings.toml").exists());
assert!(fs::read_dir(unused_home).unwrap().next().is_none());
temporary
.close()
.expect("remove owned profile executable fixture");
assert!(
current.is_file(),
"fixture cleanup removed the original test executable"
);
}
1 change: 1 addition & 0 deletions crates/emulebb-ed2k/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ libc.workspace = true
rustix.workspace = true

[dev-dependencies]
rusqlite.workspace = true
tempfile.workspace = true
tokio = { workspace = true, features = ["test-util"] }
5 changes: 5 additions & 0 deletions crates/emulebb-ed2k/src/ed2k_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ pub struct Ed2kTransferRuntime {
/// When false a corrupted part is fully re-downloaded, never re-hashed
/// early against its retained stale bytes.
ich_enabled: AtomicBool,
/// Per-runtime test probe, incremented before attempting to open a raw part.
#[cfg(test)]
raw_part_read_attempts: AtomicUsize,
/// In-memory client ban store (eMule `CClientList` ban lists), keyed by IP +
/// user hash with a 4h `CLIENTBANTIME` TTL. Shared with the inbound listener,
/// the download driver, the UDP reask runtime, and core via this runtime's
Expand Down Expand Up @@ -790,6 +793,8 @@ impl Ed2kTransferRuntime {
shared_source_invalidations: Ed2kSharedSourceInvalidationSignal::default(),
credit_system_enabled: AtomicBool::new(true),
ich_enabled: AtomicBool::new(true),
#[cfg(test)]
raw_part_read_attempts: AtomicUsize::new(0),
ban_store,
tracked_client_identities,
corruption_blackbox: Arc::new(StdMutex::new(HashMap::new())),
Expand Down
113 changes: 110 additions & 3 deletions crates/emulebb-ed2k/src/ed2k_transfer/credit_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,18 @@ impl Ed2kTransferRuntime {
pub(super) fn settle_parked_peer_credit(&self, user_hash: [u8; 16]) -> anyhow::Result<()> {
let _gate = self.credit_flush_gate.lock();
let parked = self.parked_credit.lock().peers.remove(&user_hash);
if let Some((uploaded, downloaded)) = parked {
self.metadata
.apply_credit_deltas(&[(hex::encode(user_hash), uploaded, downloaded)], &[])?;
if let Some((uploaded, downloaded)) = parked
&& let Err(error) = self
.metadata
.apply_credit_deltas(&[(hex::encode(user_hash), uploaded, downloaded)], &[])
{
// WHY: a failed settlement must retain its deltas, merging any
// additions parked while SQL ran instead of overwriting them.
self.parked_credit.lock().restore(
HashMap::from([(user_hash, (uploaded, downloaded))]),
HashMap::new(),
);
return Err(error);
}
Ok(())
}
Expand Down Expand Up @@ -245,9 +254,107 @@ impl Ed2kTransferRuntime {

#[cfg(test)]
mod tests {
use super::*;
use crate::ed2k_transfer::Ed2kTransferRuntime;
use crate::paths::unique_test_dir;

#[test]
fn failed_peer_settlement_preserves_credit_until_exactly_once_retry_and_reopen() {
let root = unique_test_dir("ed2k-credit-settlement-failure");
let runtime = Ed2kTransferRuntime::load_or_create(&root).unwrap();
let user = [0x7Cu8; 16];
runtime.record_peer_credit_totals(user, 100, 200).unwrap();
runtime.add_peer_credit_delta(user, 1000, 2000).unwrap();
let database = rusqlite::Connection::open(root.join("metadata.sqlite")).unwrap();
let stored_row = || {
database
.query_row(
"SELECT uploaded_bytes, downloaded_bytes, first_seen_at_ms, last_seen_at_ms
FROM peers WHERE user_hash = ?1",
[user.as_slice()],
|row| {
Ok((
row.get::<_, i64>(0)?,
row.get::<_, i64>(1)?,
row.get::<_, i64>(2)?,
row.get::<_, i64>(3)?,
))
},
)
.unwrap()
};
let before = stored_row();
database
.execute_batch(
"CREATE TRIGGER synthetic_peer_settlement_failure BEFORE INSERT ON peers
BEGIN SELECT RAISE(ABORT, 'synthetic peer settlement failure'); END;",
)
.unwrap();

let error = runtime.settle_parked_peer_credit(user).unwrap_err();
assert!(matches!(
error.downcast_ref::<rusqlite::Error>(),
Some(rusqlite::Error::SqliteFailure(_, Some(message)))
if message == "synthetic peer settlement failure"
));
assert_eq!(
stored_row(),
before,
"failed SQL leaves the durable row unchanged"
);
assert_eq!(runtime.parked_peer_credit_delta(user), (1000, 2000));
let visible = runtime.peer_credit_by_hash(user).unwrap().unwrap();
assert_eq!(
(visible.uploaded_bytes, visible.downloaded_bytes),
(1100, 2200)
);

// A later add must survive alongside the failed settlement's deltas.
runtime.add_peer_credit_delta(user, 7, 11).unwrap();
database
.execute_batch("DROP TRIGGER synthetic_peer_settlement_failure")
.unwrap();
runtime.settle_parked_peer_credit(user).unwrap();
assert_eq!(runtime.parked_peer_credit_delta(user), (0, 0));
assert_eq!((stored_row().0, stored_row().1), (1107, 2211));
runtime.settle_parked_peer_credit(user).unwrap();
assert_eq!((stored_row().0, stored_row().1), (1107, 2211));
drop(database);
drop(runtime);

let reopened = Ed2kTransferRuntime::load_or_create(&root).unwrap();
let stored = reopened.peer_credit_by_hash(user).unwrap().unwrap();
assert_eq!(
(stored.uploaded_bytes, stored.downloaded_bytes),
(1107, 2211)
);
assert_eq!(reopened.parked_peer_credit_delta(user), (0, 0));
}

#[test]
fn failed_settlement_restore_merges_additions_parked_during_sql_with_saturation() {
let runtime =
Ed2kTransferRuntime::load_or_create(&unique_test_dir("ed2k-credit-settlement-merge"))
.unwrap();
let user = [0x8Du8; 16];
let other = [0x9Eu8; 16];
runtime
.add_peer_credit_delta(user, u64::MAX - 3, 20)
.unwrap();
// Model the controlled interleaving while settlement owns the flush
// gate: SQL has the removed bucket, and producers can still park adds.
let _gate = runtime.credit_flush_gate.lock();
let removed = runtime.parked_credit.lock().peers.remove(&user).unwrap();
runtime.add_peer_credit_delta(user, 10, 30).unwrap();
runtime.add_peer_credit_delta(other, 7, 11).unwrap();
runtime
.parked_credit
.lock()
.restore(HashMap::from([(user, removed)]), HashMap::new());
assert_eq!(runtime.parked_peer_credit_delta(user), (u64::MAX, 50));
assert_eq!(runtime.parked_peer_credit_delta(other), (7, 11));
}

#[tokio::test]
async fn parked_credit_is_read_through_and_flushes_durably() {
let root = unique_test_dir("ed2k-credit-ledger");
Expand Down
9 changes: 5 additions & 4 deletions crates/emulebb-ed2k/src/ed2k_transfer/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1795,14 +1795,15 @@ async fn validate_transfer_directory_authority(
let matches = tokio::task::spawn_blocking(move || -> Result<bool> {
use std::io::Read;

let mut marker = match super::ingest::open_regular_file_without_follow(&marker_path) {
let marker = match super::ingest::open_regular_file_without_follow(&marker_path) {
Ok(marker) => marker,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
Err(error) => return Err(error.into()),
};
let mut contents = Vec::new();
marker.read_to_end(&mut contents)?;
Ok(contents == marker_id.as_bytes())
// WHY: Bound corrupt-marker reads while requiring the exact UUID plus no overflow byte.
let mut contents = [0_u8; 17];
let length = std::io::copy(&mut marker.take(17), &mut contents.as_mut_slice())?;
Ok(length == 16 && contents[..16] == marker_id.as_bytes()[..])
})
.await
.context("transfer directory deletion marker validation task failed")??;
Expand Down
13 changes: 8 additions & 5 deletions crates/emulebb-ed2k/src/ed2k_transfer/salvage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ impl Ed2kTransferRuntime {
return Ok(None);
}

// Read the current (corrupt) local part bytes.
let part_bytes = self
.read_part_raw_unlocked(file_hash, part_start, part_len)
.await?;

// Verify the recovery data against the trusted master hash and recover
// the per-block trusted hashes for this part.
let trusted_block_hashes = trusted_part_block_hashes_from_recovery(
Expand All @@ -104,6 +99,11 @@ impl Ed2kTransferRuntime {
u64::from(part_index),
recovery_body,
)?;
// WHY: reject untrusted recovery proofs before any raw payload read;
// proof validation depends only on geometry and the trusted root.
let part_bytes = self
.read_part_raw_unlocked(file_hash, part_start, part_len)
.await?;
let recovery = compute_part_recovery(
manifest.file_size,
u64::from(part_index),
Expand Down Expand Up @@ -386,6 +386,9 @@ impl Ed2kTransferRuntime {
start: u64,
len: u64,
) -> Result<Vec<u8>> {
#[cfg(test)]
self.raw_part_read_attempts
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let payload_path = self.transfer_dir(file_hash).join(PAYLOAD_FILE_NAME);
let mut file = tokio::fs::OpenOptions::new()
.read(true)
Expand Down
Loading
Loading