Skip to content

Commit

Permalink
Rework: platform_buffer_deinit() can only be called on bh that was su…
Browse files Browse the repository at this point in the history
…ccessfully init()'ed previously.

Fix error handling in clockcache_init() -> deinit(), to now only call
platform_buffer_deinit() on cc->variables that are validly pointing to
buffer handle addresses that were previously successfully init()'ed.

This addresses Rob's last remaining comment on the error handling aspects
of this fix.

Adjust unit-tests accordingly. Backout prelim version of clockcache_test.c
as it was felt to be not necessary at this time.

Minor bugfix: Correct use of uint32 to uint64 to avoid overflow.
  • Loading branch information
gapisback committed Jan 10, 2023
1 parent 72dba4c commit b588554
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 182 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,6 @@ $(BINDIR)/$(UNITDIR)/task_system_test: $(UTIL_SYS)
$(BINDIR)/$(UNITDIR)/platform_apis_test: $(UTIL_SYS) \
$(PLATFORM_SYS)

$(BINDIR)/$(UNITDIR)/clockcache_test: $(CLOCKCACHE_SYS) \
$(OBJDIR)/$(TESTS_DIR)/config.o \
$(UTIL_SYS)

########################################
# Convenience mini unit-test targets
unit/util_test: $(BINDIR)/$(UNITDIR)/util_test
Expand Down
11 changes: 8 additions & 3 deletions src/clockcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1902,16 +1902,21 @@ clockcache_deinit(clockcache *cc) // IN/OUT
platform_free(cc->heap_id, cc->entry);
}

platform_buffer_deinit(&cc->bh);
platform_buffer_deinit(&cc->rc_bh);
if (cc->data) {
platform_buffer_deinit(&cc->bh);
cc->data = NULL;
}
if (cc->refcount) {
platform_buffer_deinit(&cc->rc_bh);
cc->refcount = NULL;
}

if (cc->pincount) {
platform_free_volatile(cc->heap_id, cc->pincount);
}
if (cc->batch_busy) {
platform_free_volatile(cc->heap_id, cc->batch_busy);
}
ZERO_CONTENTS(cc);
}

/*
Expand Down
16 changes: 8 additions & 8 deletions src/platform_linux/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ platform_buffer_getaddr(const buffer_handle *bh)
/*
* platform_buffer_deinit() - Deinit the buffer handle, which involves
* unmapping the memory for the large buffer created using mmap().
* This is expected to be called on a 'bh' that has been successfully
* initialized, thru a prior platform_buffer_init() call.
*/
platform_status
platform_buffer_deinit(buffer_handle *bh)
{
if (bh->addr) {
int ret;
ret = munmap(bh->addr, bh->length);
if (ret) {
return CONST_STATUS(errno);
}

bh->addr = NULL;
int ret;
ret = munmap(bh->addr, bh->length);
if (ret) {
return CONST_STATUS(errno);
}

bh->addr = NULL;
bh->length = 0;
return STATUS_OK;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rc_allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ rc_allocator_mount(rc_allocator *al,
platform_assert(cfg->capacity
== cfg->io_cfg->page_size * cfg->page_capacity);

uint32 buffer_size = cfg->extent_capacity * sizeof(uint8);
uint64 buffer_size = cfg->extent_capacity * sizeof(uint8);
buffer_size = ROUNDUP(buffer_size, cfg->io_cfg->page_size);
status = platform_buffer_init(&al->bh, buffer_size);
if (!SUCCESS(status)) {
Expand Down
163 changes: 0 additions & 163 deletions tests/unit/clockcache_test.c

This file was deleted.

9 changes: 6 additions & 3 deletions tests/unit/platform_apis_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ CTEST2(platform_api, test_platform_buffer_init_fails_for_very_large_length)

size_t length = (1024 * KiB * GiB);

// This is expected to fail as length is very large
// On most test machines we use, this is expected to fail as mmap() under
// here will fail for very large lengths. (If this test case ever fails,
// check the 'length' here and the machine's configuration to see why
// mmap() unexpectedly succeeded.)
rc = platform_buffer_init(&bh, length);
ASSERT_FALSE(SUCCESS(rc));
ASSERT_TRUE(bh.addr == NULL);
ASSERT_TRUE(bh.length == 0);

// deinit() should still work even when nothing was successfully mmap()'ed
// deinit() would fail horribly when nothing was successfully mmap()'ed
rc = platform_buffer_deinit(&bh);
ASSERT_TRUE(SUCCESS(rc), "rc='%s'", platform_status_to_string(rc));
ASSERT_FALSE(SUCCESS(rc));
}

0 comments on commit b588554

Please sign in to comment.