From b05bb61f454eeb8a85164c8a31510aeb9d79129c Mon Sep 17 00:00:00 2001 From: Gert Wollny Date: Tue, 30 Nov 2021 09:29:42 +0100 Subject: [PATCH] vrend: clear memory when allocating a host-backed memory resource Closes: #249 Signed-off-by: Gert Wollny Reviewed-by: Chia-I Wu --- src/vrend_renderer.c | 2 +- tests/test_virgl_transfer.c | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/vrend_renderer.c b/src/vrend_renderer.c index 53c52d0..28f6697 100644 --- a/src/vrend_renderer.c +++ b/src/vrend_renderer.c @@ -7063,7 +7063,7 @@ vrend_resource_alloc_buffer(struct vrend_resource *gr, uint32_t flags) if (bind == VIRGL_BIND_CUSTOM) { /* use iovec directly when attached */ gr->storage_bits |= VREND_STORAGE_HOST_SYSTEM_MEMORY; - gr->ptr = malloc(size); + gr->ptr = calloc(1, size); if (!gr->ptr) return -ENOMEM; } else if (bind == VIRGL_BIND_STAGING) { diff --git a/tests/test_virgl_transfer.c b/tests/test_virgl_transfer.c index bf7f438..3c53c3d 100644 --- a/tests/test_virgl_transfer.c +++ b/tests/test_virgl_transfer.c @@ -952,6 +952,56 @@ START_TEST(virgl_test_transfer_near_res_bounds_with_stride_succeeds) } END_TEST +START_TEST(test_vrend_host_backed_memory_no_data_leak) +{ + struct iovec iovs[1]; + int niovs = 1; + + struct virgl_context ctx = {0}; + + int ret = testvirgl_init_ctx_cmdbuf(&ctx); + + struct virgl_renderer_resource_create_args res; + res.handle = 0x400; + res.target = PIPE_BUFFER; + res.format = VIRGL_FORMAT_R8_UNORM; + res.nr_samples = 0; + res.last_level = 0; + res.array_size = 1; + res.bind = VIRGL_BIND_CUSTOM; + res.depth = 1; + res.width = 32; + res.height = 1; + res.flags = 0; + + uint32_t size = 32; + uint8_t* data = calloc(1, size); + memset(data, 1, 32); + iovs[0].iov_base = data; + iovs[0].iov_len = size; + + struct pipe_box box = {0,0,0, size, 1,1}; + + virgl_renderer_resource_create(&res, NULL, 0); + virgl_renderer_ctx_attach_resource(ctx.ctx_id, res.handle); + + ret = virgl_renderer_transfer_read_iov(res.handle, ctx.ctx_id, 0, 0, 0, + (struct virgl_box *)&box, 0, iovs, niovs); + + ck_assert_int_eq(ret, 0); + + for (int i = 0; i < 32; ++i) + ck_assert_int_eq(data[i], 0); + + virgl_renderer_ctx_detach_resource(1, res.handle); + + virgl_renderer_resource_unref(res.handle); + free(data); + +} +END_TEST + + static Suite *virgl_init_suite(void) { Suite *s; @@ -981,6 +1031,7 @@ static Suite *virgl_init_suite(void) tcase_add_test(tc_core, virgl_test_transfer_buffer_bad_strides); tcase_add_test(tc_core, virgl_test_transfer_2d_array_bad_layer_stride); tcase_add_test(tc_core, virgl_test_transfer_2d_bad_level); + tcase_add_test(tc_core, test_vrend_host_backed_memory_no_data_leak); tcase_add_loop_test(tc_core, virgl_test_transfer_res_read_valid, 0, PIPE_MAX_TEXTURE_TYPES); tcase_add_loop_test(tc_core, virgl_test_transfer_res_write_valid, 0, PIPE_MAX_TEXTURE_TYPES);