Skip to content
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@
All notable changes to the AVA1 crates and C library. The wire format is version 1 throughout;
see [README](README.md#versions).

## Unreleased

Fixes from an audit of what a peer can make a node do. The wire format and what is written on disk do
not change; a correct peer sees no difference.

C library:

- A FileRoot that arrives while a new manifest of the same job is being taken up waits for it. Before,
it could be written into a large-file state the job thread had just freed (a use-after-free).
- A download `JobOpen` starts over only a download of its id, and only this device's or one no session
drives. Before, it freed whatever job had the id: another device's upload, a copy, a send to another
console. Another kind of job under the id is refused with `ERR_PROTOCOL`, another device's running
download with `ERR_UNKNOWN_JOB`.
- An upload whose commit or end failed on an I/O error (a rename, a folder sync) resumes from its
journal. Before, the journaled `Done(ERR_IO)` answered every later `JobOpen` with "all done" and the
same failure. Only `OK`, `ERR_EXISTS` and `ERR_CROSS_DEVICE` end a job for good, as for a job still in
memory; journals written by older versions resume the same way.
- A sender's `JobMap` is read against the manifest: each done file once, durable ranges inside a file
and no more of them than it has groups. A map that names more ends the job with `ERR_PROTOCOL`
(before, the pages were stored as they came).
- A FileRoot, ManifestEnd or Resume waiting in a job's inbox is kept at its own size, not as the 64 KiB
frame it may come in.
- The download sender uses at most 64 MiB of window, whatever it is granted (its frames in flight stay
in memory until their `Received`).
- A device blocked by the `refused` hook stays blocked when its ClientInfo carries a token: only a
ticket this node holds for the key skips the device rules.
- A merge refuses (`ERR_PATH`) a manifest that names both a file `P` and `P.ava-part` in any letter
case: `P` is written through `P.ava-part`, so the two shared one file. Refused before the manifest is
stored or journaled. `ava1_mstore_part_clash`.

Rust sender:

- The receiver (`LocalSink`) refuses the same `P` + `P.ava-part` manifest in a merge, and runs the
manifest checks that need no disk (`Sink::check`, new, default none) before it writes the job's
journal and stored manifest. Before, a merge download of such a tree reported success with the two
files mixed, and a refused manifest was journaled first. `manifest::part_name_clash`, `PART_SUFFIX`.
- `Need::add_page_bounded` reads a map against the job's manifest, and the sender uses it; a `Durable`
naming files past the manifest ends the job with a protocol error. `RangeSet::len` and `is_empty`.

## 1.0.12

Read-back progress, and no second read-back of a resumed file in the same process. The wire format gains one
Expand Down
13 changes: 11 additions & 2 deletions c/ava1_apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,15 @@ int ava1_apply_bundle_pooled(ava1_job_t *j, uint8_t *owned, size_t owned_len, si
int ava1_apply_root(ava1_job_t *j, uint32_t file_id, const uint8_t root[32]) {
int rc = 0;
pthread_mutex_lock(&j->mu);
/* A FileRoot that follows a ManifestEnd names the new manifest's ids, and while the job thread
* adopts it the old large-file states are moved and freed without j->mu (remap): wait until
* the new manifest is the job's. Frames are taken in order, so one sent before that ManifestEnd
* was applied before it was handed over (ev_end). */
while ((j->ev_end || j->adopting || j->feed_hold) && !j->stopping) pthread_cond_wait(&j->cv, &j->mu);
if (j->stopping) {
pthread_mutex_unlock(&j->mu);
return 0; /* the job ends: the root goes with it */
}
if (file_id >= j->m.n || j->m.e[file_id].kind != AVA1_ENTRY_FILE) {
rc = AVA1_E_PROTO;
} else {
Expand Down Expand Up @@ -3848,8 +3857,8 @@ static void commit_large(ava1_job_t *j, uint32_t id) {
return;
}
}
if (rename(part, fin) != 0) {
int e = errno;
if ((err = ava1_apply_fault ? ava1_apply_fault(j, AVA1_HOOK_RENAMED, id) : 0) != 0 || rename(part, fin) != 0) {
int e = err ? err : errno;
if (in_the_way(e)) commit_fail(j, AVA1_ERR_EXISTS, "something is already where the file goes", e, 1);
else commit_fail(j, AVA1_ERR_IO, "rename into place failed", e, 1);
return;
Expand Down
3 changes: 2 additions & 1 deletion c/ava1_apply.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ extern size_t ava1_test_pwrite_max;
extern uint32_t ava1_test_chunk_delay_ms;
extern void (*ava1_apply_hook)(ava1_job_t *j, int point, uint32_t id);
/* Tests only (NULL in the payload): an errno to inject at a point instead of doing the work.
* Consulted at AVA1_HOOK_DIR_SYNCED of the staged tree's rename. */
* Consulted at AVA1_HOOK_DIR_SYNCED of the staged tree's rename and at AVA1_HOOK_RENAMED of a large
* file's rename into place (before it). */
extern int (*ava1_apply_fault)(ava1_job_t *j, int point, uint32_t id);
/* Tests only (NULL in the payload): JF_READBACK is about to read bytes [off, off + len) of file `id` back
* through `fd` (its own descriptor, opened for writing too), `live` while the file still arrives, else in
Expand Down
37 changes: 33 additions & 4 deletions c/ava1_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ uint32_t ava1_data_test_map_delay_ms;
int ava1_data_test_ack_fail;
int ava1_data_test_feeder_fail;
uint32_t ava1_data_test_feed_delay_ms;
uint32_t ava1_data_test_feed_check_delay_ms, ava1_data_test_reprep_delay_ms;
uint32_t ava1_data_test_feed_check_delay_ms, ava1_data_test_reprep_delay_ms, ava1_data_test_remap_delay_ms;
int ava1_data_test_reserve_fail;
int ava1_data_test_lane_alloc_fail;
int ava1_data_test_fb_force;
Expand Down Expand Up @@ -392,7 +392,7 @@ int ava1_data_start(const ava1_data_cfg_t *cfg) {
D.admitted = 0;
__atomic_store_n(&g_unswept_total, 0, __ATOMIC_RELAXED); /* a new data layer holds no job's log bytes */
ava1_data_test_open_delay_ms = ava1_data_test_map_delay_ms = ava1_data_test_feed_delay_ms = 0;
ava1_data_test_feed_check_delay_ms = ava1_data_test_reprep_delay_ms = 0;
ava1_data_test_feed_check_delay_ms = ava1_data_test_reprep_delay_ms = ava1_data_test_remap_delay_ms = 0;
ava1_data_test_open_work_delay_ms = 0;
ava1_data_test_ack_fail = ava1_data_test_feeder_fail = 0;
ava1_data_test_reserve_fail = ava1_data_test_lane_alloc_fail = ava1_data_test_fb_force = 0;
Expand Down Expand Up @@ -690,6 +690,7 @@ static void feed_done(ava1_job_t *j) {
void ava1_data_feed_hold(ava1_job_t *j, int hold) {
pthread_mutex_lock(&j->mu);
j->feed_hold = hold;
if (!hold) pthread_cond_broadcast(&j->cv); /* a FileRoot waits for the end of a hold (ava1_apply_root) */
while (hold && j->feeding) pthread_cond_wait(&j->cv, &j->mu);
pthread_mutex_unlock(&j->mu);
}
Expand Down Expand Up @@ -825,9 +826,37 @@ static void *feed_main(void *arg) {
return NULL;
}

/* A FileRoot, ManifestEnd or Resume has a fixed size, but its control frame may be 64 KiB: what
* waits in an inbox is the message re-encoded (unknown ext fields dropped, as the decoder drops
* them), not the frame. One that does not decode is kept empty: it fails to decode again where
* it is taken, as before. Other frames are kept as they came. */
static size_t compact_ctl(uint8_t type, const uint8_t **body, size_t len, uint8_t *buf, size_t cap) {
ava1_w_t w;
int rc = -1;
ava1_w_init(&w, buf, cap);
if (type == AVA1_TYPE_FILE_ROOT) {
ava1_file_root_t r;
rc = ava1_file_root_decode(*body, len, &r) == 0 ? ava1_file_root_encode(&r, &w) : -1;
} else if (type == AVA1_TYPE_MANIFEST_END) {
ava1_manifest_end_t e;
rc = ava1_manifest_end_decode(*body, len, &e) == 0 ? ava1_manifest_end_encode(&e, &w) : -1;
} else if (type == AVA1_TYPE_RESUME) {
ava1_resume_t r;
rc = ava1_resume_decode(*body, len, &r) == 0 ? ava1_resume_encode(&r, &w) : -1;
} else {
return len;
}
*body = buf;
return rc == 0 ? w.len : 0;
}

static void inbox_add(ava1_job_t *j, uint8_t type, const uint8_t *body, size_t len) {
ava1_inframe_t *f = calloc(1, sizeof *f);
uint8_t *b = malloc(len ? len : 1);
uint8_t small[128];
ava1_inframe_t *f;
uint8_t *b;
len = compact_ctl(type, &body, len, small, sizeof small);
f = calloc(1, sizeof *f);
b = malloc(len ? len : 1);
if (!f || !b) {
free(f);
free(b);
Expand Down
1 change: 1 addition & 0 deletions c/ava1_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ extern int ava1_data_test_feeder_fail; /* a job's feeder thread "cannot star
extern uint32_t ava1_data_test_feed_delay_ms; /* the feeder waits this long after taking lane frames */
extern uint32_t ava1_data_test_feed_check_delay_ms; /* ... and this long between checking one and queuing it */
extern uint32_t ava1_data_test_reprep_delay_ms; /* a new manifest's re-prepare waits this long first */
extern uint32_t ava1_data_test_remap_delay_ms; /* ... and this long inside the remap, the old states freed */
extern int ava1_data_test_reserve_fail; /* ava1_apply_reserve "fails" for every lane frame */
extern int ava1_data_test_lane_alloc_fail; /* the lane frame's calloc "fails" */
extern int ava1_data_test_fb_force; /* every Received goes through the waiting-send fallback */
Expand Down
13 changes: 13 additions & 0 deletions c/ava1_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,19 @@ int ava1_job_retire_parked(ava1_job_t *j) {
return rc;
}

int ava1_job_unlist(ava1_job_t *j, int parked_only) {
int rc = 0;
pthread_mutex_lock(&T.mu);
if (!listed_locked(j)) rc = -2;
else if (parked_only && j->attached) rc = -1; /* `attached` is written under T.mu */
else {
unlist(j);
j->refs--; /* the table's: the caller still holds one */
}
pthread_mutex_unlock(&T.mu);
return rc;
}

void ava1_job_emit(ava1_job_t *j, uint8_t type, uint8_t flags, const uint8_t *body, size_t len) {
ava1_emit_fn fn;
pthread_mutex_lock(&j->cmu);
Expand Down
9 changes: 7 additions & 2 deletions c/ava1_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ struct ava1_job {
int attached, refs, stopping, finished, prepared;
/* Under mu. feeding: the feeder is between its `prepared` check and the engine's queue (it reads
* m and enqueues work against it); feed_hold: the job thread is about to replace m (a new manifest
* in the same session), so the feeder hands nothing more to the engine until it is done. */
int feeding, feed_hold;
* in the same session), so the feeder hands nothing more to the engine until it is done.
* adopting: the job thread took a ManifestEnd and has not answered it yet (ava1_apply_root waits). */
int feeding, feed_hold, adopting;
int discard_parts; /* a cancelled local copy: destroy removes the .ava-part files this job itself was writing */
uint64_t parked_at_ms;
uint64_t concluded_ms; /* an upload or download: when its end went out (our JobDone) or came in (the peer's),
Expand Down Expand Up @@ -333,6 +334,10 @@ int ava1_job_retire(ava1_job_t *j);
* caller's reference. 0 = retired; -1 = a session is attached to it; -2 = someone else holds
* it for the moment or its files are still settling (try again). */
int ava1_job_retire_parked(ava1_job_t *j);
/* Unlists `j` itself (never another job that holds its id by then) and drops the table's
* reference; the caller's stays, and its ava1_job_put may free the job. parked_only: not while a
* session drives it. 0 = unlisted; -1 = a session is attached to it; -2 = it was no longer listed. */
int ava1_job_unlist(ava1_job_t *j, int parked_only);
void ava1_job_emit(ava1_job_t *j, uint8_t type, uint8_t flags, const uint8_t *body, size_t len);

/* Control-frame bytes queued across all jobs (their inboxes and the frames behind a
Expand Down
61 changes: 61 additions & 0 deletions c/ava1_manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,64 @@ void ava1_mstore_free(ava1_mstore_t *m) {
free(m->arena);
memset(m, 0, sizeof *m);
}

/* ---- part names (ava1_mstore_part_clash) ------------------------------------------- */

#define PART_SUFFIX ".ava-part"
#define PART_SUFFIX_LEN 9u

static int lower(int c) { return c >= 'A' && c <= 'Z' ? c + 32 : c; }

static uint32_t fnv_ci(const char *s, size_t n) {
uint32_t h = 2166136261u;
size_t i;
for (i = 0; i < n; i++) h = (h ^ (uint8_t)lower((uint8_t)s[i])) * 16777619u;
return h;
}

static int eq_ci(const char *a, const char *b, size_t n) {
size_t i;
for (i = 0; i < n; i++)
if (lower((uint8_t)a[i]) != lower((uint8_t)b[i])) return 0;
return 1;
}

int ava1_mstore_part_clash(const ava1_mstore_t *m, uint32_t *file, uint32_t *entry) {
uint32_t i, k = 0, cap = 1, *slot;
int found = 0;
/* Only the entries named like a part file go in the set: usually none at all. */
for (i = 0; i < m->n; i++) {
const char *p = ava1_mstore_path(m, i);
size_t l = strlen(p);
if (l > PART_SUFFIX_LEN && eq_ci(p + l - PART_SUFFIX_LEN, PART_SUFFIX, PART_SUFFIX_LEN)) k++;
}
if (!k) return 0;
while (cap < 2u * k + 2u) cap *= 2;
if (!(slot = calloc(cap, sizeof *slot))) return -1;
for (i = 0; i < m->n; i++) {
const char *p = ava1_mstore_path(m, i);
size_t l = strlen(p);
uint32_t h;
if (l <= PART_SUFFIX_LEN || !eq_ci(p + l - PART_SUFFIX_LEN, PART_SUFFIX, PART_SUFFIX_LEN)) continue;
h = fnv_ci(p, l - PART_SUFFIX_LEN) & (cap - 1);
while (slot[h]) h = (h + 1) & (cap - 1);
slot[h] = i + 1;
}
for (i = 0; i < m->n && !found; i++) {
const char *p = ava1_mstore_path(m, i);
size_t l = strlen(p);
uint32_t h, q;
if (m->e[i].kind != AVA1_ENTRY_FILE) continue;
for (h = fnv_ci(p, l) & (cap - 1); (q = slot[h]) != 0; h = (h + 1) & (cap - 1)) {
const char *qp = ava1_mstore_path(m, q - 1);
if (strlen(qp) == l + PART_SUFFIX_LEN && eq_ci(qp, p, l)) {
*file = i;
*entry = q - 1;
found = 1;
break;
}
}
}
free(slot);
return found;
}
4 changes: 4 additions & 0 deletions c/ava1_manifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ int ava1_mstore_walk_deny(ava1_mstore_t *m, const char *root, unsigned flags, in
int ava1_open_read_safe(const char *path, int (*deny)(const char *abs));
int ava1_mstore_single(ava1_mstore_t *m, const char *file); /* JF_SINGLE_FILE */
void ava1_mstore_free(ava1_mstore_t *m);
/* In a merge (SPEC.md §11.6) a file `P` is written as `P.ava-part` beside it, so a manifest that
* also names `P.ava-part` (in any letter case: a drive may fold it) would have the two share a name.
* 1 with the file's id and the clashing entry's id, 0 when there is none, -1 out of memory. */
int ava1_mstore_part_clash(const ava1_mstore_t *m, uint32_t *file, uint32_t *entry);

#endif
50 changes: 49 additions & 1 deletion c/ava1_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ static void drop_state(ava1_job_t *j) {
j->have_manifest = j->replay_done = j->staged = j->dest_held = 0;
}

/* The statuses that end a job for good, as for one still in memory (ava1_recv_open): done, or a
* destination the job must not touch. Any other (an I/O failure at a commit or at the end) is
* where its last run stopped, and a JobOpen takes it up again from its journal. */
static int done_is_final(uint16_t status) {
return status == AVA1_STATUS_OK || status == AVA1_ERR_EXISTS || status == AVA1_ERR_CROSS_DEVICE;
}

/* Loads <dir>/manifest and replays <dir>/journal. 0 when a usable earlier job was found:
* the journal opens for this job and root, and was written for exactly this manifest. */
static int load_from_disk(ava1_job_t *j) {
Expand All @@ -327,6 +334,10 @@ static int load_from_disk(ava1_job_t *j) {
return -1;
}
j->have_manifest = 1;
/* A journaled Done(ERR_IO) is not an answer: the map is the replayed state, and the commit or
* rename that failed runs again (reconcile finds a file whose rename went through). Before, a
* resume was told every file was done and got the same failure, every time. */
if (j->replay_done && !done_is_final(j->replay_status)) j->replay_done = 0;
/* Durable-by-log recovery (§4): the files the journal calls done but not yet swept are made again from
* the pack log where a crash lost them, and swept, before anything is counted or answered. */
refs_prune(&r);
Expand Down Expand Up @@ -624,6 +635,7 @@ ava1_job_t *ava1_recv_open(const ava1_recv_spec_t *s, ava1_job_open_ack_t *ack,
ava1_mstore_free(&j->m_in);
if (s->entries) (void)ava1_mstore_reserve(&j->m_in, s->entries);
j->ev_end = j->ev_resume = 0;
pthread_cond_broadcast(&j->cv); /* a FileRoot waiting for that ManifestEnd (ava1_apply_root) */
/* The new sender starts from what is free now. Credit freed while detached is
* already counted here, so it must not also be flushed as a Credit message. */
ack->credit = j->credit > j->outstanding ? j->credit - j->outstanding : 0;
Expand Down Expand Up @@ -832,12 +844,17 @@ static int remap(ava1_job_t *j, ava1_mstore_t *in, ava1_bits_t *changed) {
drop_path(p, 1, d, &nd);
}
}
/* Second step of the outboard renames; drop the outboards nobody carried. */
/* Second step of the outboard renames; drop the outboards nobody carried. The states freed
* here are only safe to free because nothing else touches j->lf while the job thread
* re-prepares: the feeder hands no lane frame over (feed_hold) and a FileRoot waits for the
* new manifest (ava1_apply_root). */
for (i = 0; i < j->m.n; i++) {
snprintf(a, sizeof a, "%s/%u.ob", j->dir, i);
(void)unlink(a);
free_lf(j, i, j->lf[i]);
j->lf[i] = NULL;
}
if (ava1_data_test_remap_delay_ms) ava1_platform_sleep_ms(ava1_data_test_remap_delay_ms);
for (i = 0; i < in->n; i++)
if (nlf[i]) {
snprintf(a, sizeof a, "%s/%u.ob.m", j->dir, i);
Expand Down Expand Up @@ -1378,7 +1395,19 @@ static uint16_t adopt(ava1_job_t *j, ava1_mstore_t *in, const uint8_t hash[32],
return AVA1_STATUS_OK;
}

static void recv_events_take(ava1_job_t *j);

/* `adopting` covers a ManifestEnd from the moment it is taken until its map is out: a FileRoot
* that follows it names the new manifest's ids, and remap frees the old large-file states. */
static void recv_events(ava1_job_t *j) {
recv_events_take(j);
pthread_mutex_lock(&j->mu);
j->adopting = 0;
pthread_cond_broadcast(&j->cv);
pthread_mutex_unlock(&j->mu);
}

static void recv_events_take(ava1_job_t *j) {
int end, res;
ava1_mstore_t in;
uint32_t efiles;
Expand All @@ -1390,6 +1419,7 @@ static void recv_events(ava1_job_t *j) {
end = j->ev_end;
res = j->ev_resume;
j->ev_end = j->ev_resume = 0;
if (end) j->adopting = 1;
in = j->m_in;
memset(&j->m_in, 0, sizeof j->m_in);
efiles = j->end_files;
Expand All @@ -1416,6 +1446,24 @@ static void recv_events(ava1_job_t *j) {
emit_map(j, AVA1_ERR_PROTOCOL, "the manifest does not match its end");
return;
}
/* A merge writes each file through `<path>.ava-part` beside it: an entry of that name would share
* it with the file's part, and either one's bytes would land in the other. Refused before the
* manifest is stored or journaled (and for one an earlier version stored: it is checked again). */
if (!j->staged && !(j->flags & AVA1_JF_SINGLE_FILE)) {
uint32_t pf, pe;
int c = ava1_mstore_part_clash(&in, &pf, &pe);
if (c != 0) {
if (c > 0)
snprintf(msg, sizeof msg, "%.48s is the name %.48s is written under until it is complete",
ava1_mstore_path(&in, pe), ava1_mstore_path(&in, pf));
else snprintf(msg, sizeof msg, "out of memory");
ava1_mstore_free(&in);
st = c > 0 ? AVA1_ERR_PATH : AVA1_ERR_INTERNAL;
emit_map(j, st, msg);
ava1_apply_fail(j, st, msg, 0, 0);
return;
}
}
if (j->have_manifest && memcmp(hash, j->manifest_hash, 32) == 0) {
ava1_mstore_free(&in); /* the same manifest: the replayed state stands */
} else {
Expand Down
Loading
Loading