From 3acb97477f261bf11c866b5764deabe693607bc3 Mon Sep 17 00:00:00 2001 From: John Levon Date: Mon, 19 Apr 2021 13:10:02 +0100 Subject: vfu_realize_ctx(): fix default PCI config space region (#445) Fix check for an un-configured PCI config space region (the previous method was not accounting for the initialized ->fd). Signed-off-by: John Levon Reviewed-by: Thanos Makatos --- lib/libvfio-user.c | 12 +++++------- test/unit-tests.c | 12 ++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c index a0f6fe3..82fe0ca 100644 --- a/lib/libvfio-user.c +++ b/lib/libvfio-user.c @@ -1013,7 +1013,6 @@ int vfu_realize_ctx(vfu_ctx_t *vfu_ctx) { vfu_reg_info_t *cfg_reg; - const vfu_reg_info_t zero_reg = { 0 }; uint32_t max_ivs = 0, i; size_t size; @@ -1024,8 +1023,7 @@ vfu_realize_ctx(vfu_ctx_t *vfu_ctx) cfg_reg = &vfu_ctx->reg_info[VFU_PCI_DEV_CFG_REGION_IDX]; // Set a default config region if none provided. - /* TODO should it be enough to check that the size of region is 0? */ - if (memcmp(cfg_reg, &zero_reg, sizeof(*cfg_reg)) == 0) { + if (cfg_reg->size == 0) { cfg_reg->flags = VFU_REGION_FLAG_RW; cfg_reg->size = PCI_CFG_SPACE_SIZE; } @@ -1227,6 +1225,10 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, goto err_out; } + for (i = 0; i < vfu_ctx->nr_regions; i++) { + vfu_ctx->reg_info[i].fd = -1; + } + if (vfu_setup_device_nr_irqs(vfu_ctx, VFU_DEV_ERR_IRQ, 1) == -1) { goto err_out; } @@ -1241,10 +1243,6 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, } } - for (i = 0; i< vfu_ctx->nr_regions; i++) { - vfu_ctx->reg_info[i].fd = -1; - } - return vfu_ctx; err_out: diff --git a/test/unit-tests.c b/test/unit-tests.c index 7f03fb7..9aa1106 100644 --- a/test/unit-tests.c +++ b/test/unit-tests.c @@ -737,6 +737,18 @@ test_vfu_ctx_create(void **state UNUSED) will_return(close, 0); vfu_destroy_ctx(vfu_ctx); + + /* Test "bare" vfu_create_ctx(). */ + vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, "", LIBVFIO_USER_FLAG_ATTACH_NB, + NULL, VFU_DEV_TYPE_PCI); + assert_non_null(vfu_ctx); + + assert_int_equal(0, vfu_realize_ctx(vfu_ctx)); + + expect_value(close, fd, ((tran_sock_t *)vfu_ctx->tran_data)->fd); + will_return(close, 0); + + vfu_destroy_ctx(vfu_ctx); } bool pci_caps_writing = true; -- cgit v1.1