Skip to content

Commit

Permalink
Merge pull request #226 from urbit/barter-simsum/pma-record-node-part…
Browse files Browse the repository at this point in the history
…itions-in-flist-on-restore

pma: fix memory corruption restoration bug
  • Loading branch information
barter-simsum authored Apr 19, 2024
2 parents 06b62d3 + 4cf532c commit 34c4d03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rust/ares_pma/c-src/btest.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ int main(int argc, char *argv[])
assert(SUCC(bt_state_open(state4, "./pmatest4", 0, 0644)));

assert(state4->file_size_p == PMA_INITIAL_SIZE_p + PMA_GROW_SIZE_p * 2);
assert(state4->flist->next->hi == state4->file_size_p);
assert(state4->flist->hi == state4->file_size_p);

for (size_t i = 0; i < PMA_GROW_SIZE_b * 2; i++)
assert(t4a_copy[i] == t4a[i]);
Expand Down
24 changes: 21 additions & 3 deletions rust/ares_pma/c-src/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,6 @@ _flist_grow(BT_state *state, size_t pages)
static int
_flist_new(BT_state *state, size_t size_p)
#define FLIST_PG_START (BT_META_SECTION_WIDTH / BT_PAGESIZE)
/* #define FLIST_PG_START ((BT_META_SECTION_WIDTH + BLK_BASE_LEN0_b) / BT_PAGESIZE) */
{
BT_flistnode *head = calloc(1, sizeof *head);
head->next = 0;
Expand Down Expand Up @@ -2407,6 +2406,22 @@ _freelist_restore2(BT_state *state, BT_page *node,
}
}

static void
_flist_restore_partitions(BT_state *state)
{
BT_meta *meta = state->meta_pages[state->which];
assert(meta->blk_base[0] == BT_NUMMETAS);

for (size_t i = 0
; i < BT_NUMPARTS && meta->blk_base[i] != 0
; i++) {
pgno_t partoff_p = meta->blk_base[i];
size_t partlen_p = BLK_BASE_LENS_b[i] / BT_PAGESIZE;

_flist_record_alloc(state, partoff_p, partoff_p + partlen_p);
}
}

static void
_freelist_restore(BT_state *state)
/* restores the mlist, nlist, and mlist */
Expand All @@ -2416,7 +2431,11 @@ _freelist_restore(BT_state *state)
assert(SUCC(_flist_new(state, state->file_size_p)));
assert(SUCC(_nlist_load(state)));
assert(SUCC(_mlist_new(state)));
/* first record root's allocation */

/* record node partitions in flist */
_flist_restore_partitions(state);

/* record root's allocation and then handle subtree */
_nlist_record_alloc(state, root);
_freelist_restore2(state, root, 1, meta->depth);
}
Expand Down Expand Up @@ -2515,7 +2534,6 @@ _bt_state_load(BT_state *state)
/* map the node segment */
_bt_state_map_node_segment(state);

/* new db, so populate metadata */
if (new) {
assert(SUCC(_flist_new(state, PMA_GROW_SIZE_p)));
assert(SUCC(_nlist_new(state)));
Expand Down

0 comments on commit 34c4d03

Please sign in to comment.