<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Nov 25, 2016 at 6:34 AM, Mun Gwan-gyeong <span dir="ltr"><<a href="mailto:elongbug@gmail.com" target="_blank">elongbug@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This patch adds missing error-checking and fixes resource leak in<br>
allocation failure path on anv_CreateDevice()<br>
<br>
Signed-off-by: Mun Gwan-gyeong <<a href="mailto:elongbug@gmail.com">elongbug@gmail.com</a>><br>
---<br>
 src/intel/vulkan/anv_device.c | 59 ++++++++++++++++++++++++++++++<wbr>++++++-------<br>
 1 file changed, 50 insertions(+), 9 deletions(-)<br>
<br>
diff --git a/src/intel/vulkan/anv_device.<wbr>c b/src/intel/vulkan/anv_device.<wbr>c<br>
index 0fd7d41..1964fb7 100644<br>
--- a/src/intel/vulkan/anv_device.<wbr>c<br>
+++ b/src/intel/vulkan/anv_device.<wbr>c<br>
@@ -893,31 +893,57 @@ VkResult anv_CreateDevice(<br>
    device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&<br>
       pCreateInfo->pEnabledFeatures-<wbr>>robustBufferAccess;<br>
<br>
-   pthread_mutex_init(&device-><wbr>mutex, NULL);<br>
+   if (pthread_mutex_init(&device-><wbr>mutex, NULL) != 0) {<br>
+      result = vk_error(VK_ERROR_<wbr>INITIALIZATION_FAILED);<br>
+      goto fail_context_id;<br>
+   }<br>
<br>
    pthread_condattr_t condattr;<br>
-   pthread_condattr_init(&<wbr>condattr);<br>
-   pthread_condattr_setclock(&<wbr>condattr, CLOCK_MONOTONIC);<br>
-   pthread_cond_init(&device-><wbr>queue_submit, NULL);<br>
+   if (pthread_condattr_init(&<wbr>condattr) != 0) {<br>
+      result = vk_error(VK_ERROR_<wbr>INITIALIZATION_FAILED);<br>
+      goto fail_mutex;<br>
+   }<br>
+   if (pthread_condattr_setclock(&<wbr>condattr, CLOCK_MONOTONIC) != 0) {<br>
+      pthread_condattr_destroy(&<wbr>condattr);<br>
+      result = vk_error(VK_ERROR_<wbr>INITIALIZATION_FAILED);<br>
+      goto fail_mutex;<br>
+   }<br>
+   if (pthread_cond_init(&device-><wbr>queue_submit, NULL) != 0) {<br>
+      pthread_condattr_destroy(&<wbr>condattr);<br>
+      result = vk_error(VK_ERROR_<wbr>INITIALIZATION_FAILED);<br>
+      goto fail_mutex;<br>
+   }<br>
    pthread_condattr_destroy(&<wbr>condattr);<br>
<br>
    anv_bo_pool_init(&device-><wbr>batch_bo_pool, device);<br></blockquote><div><br></div><div>You're missing the destructor for this below as well as destructors for all of the state pools.  While I realize that those don't do anything interesting if no states have been allocated yet, it'd be bets to include them for completeness.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
-   anv_block_pool_init(&device-><wbr>dynamic_state_block_pool, device, 16384);<br>
+   result = anv_block_pool_init(&device-><wbr>dynamic_state_block_pool, device,<br>
+                                16384);<br>
+   if (result != VK_SUCCESS)<br>
+      goto fail_queue_submit;<br>
<br>
    anv_state_pool_init(&device-><wbr>dynamic_state_pool,<br>
                        &device->dynamic_state_block_<wbr>pool);<br>
<br>
-   anv_block_pool_init(&device-><wbr>instruction_block_pool, device, 128 * 1024);<br>
+   result = anv_block_pool_init(&device-><wbr>instruction_block_pool, device,<br>
+                                128 * 1024);<br>
+   if (result != VK_SUCCESS)<br>
+      goto fail_dynamic_state_block_pool;<br>
+<br>
    anv_state_pool_init(&device-><wbr>instruction_state_pool,<br>
                        &device->instruction_block_<wbr>pool);<br>
<br>
-   anv_block_pool_init(&device-><wbr>surface_state_block_pool, device, 4096);<br>
+   result = anv_block_pool_init(&device-><wbr>surface_state_block_pool, device,<br>
+                                4096);<br>
+   if (result != VK_SUCCESS)<br>
+      goto fail_instruction_block_pool;<br>
<br>
    anv_state_pool_init(&device-><wbr>surface_state_pool,<br>
                        &device->surface_state_block_<wbr>pool);<br>
<br>
-   anv_bo_init_new(&device-><wbr>workaround_bo, device, 1024);<br>
+   result = anv_bo_init_new(&device-><wbr>workaround_bo, device, 1024);<br>
+   if (result != VK_SUCCESS)<br>
+      goto fail_surface_state_block_pool;<br>
<br>
    anv_scratch_pool_init(device, &device->scratch_pool);<br>
<br>
@@ -942,7 +968,7 @@ VkResult anv_CreateDevice(<br>
       unreachable("unhandled gen");<br>
    }<br>
    if (result != VK_SUCCESS)<br>
-      goto fail_fd;<br>
+      goto fail_workaround_bo;<br>
<br>
    anv_device_init_blorp(device);<br>
<br>
@@ -952,6 +978,21 @@ VkResult anv_CreateDevice(<br>
<br>
    return VK_SUCCESS;<br>
<br>
+ fail_workaround_bo:<br>
+   anv_gem_munmap(device-><wbr>workaround_bo.map, device->workaround_bo.size);<br>
+   anv_gem_close(device, device->workaround_bo.gem_<wbr>handle);<br>
+ fail_surface_state_block_pool:<br>
+   anv_block_pool_finish(&device-<wbr>>surface_state_block_pool);<br>
+ fail_instruction_block_pool:<br>
+   anv_block_pool_finish(&device-<wbr>>instruction_block_pool);<br>
+ fail_dynamic_state_block_pool:<br>
+   anv_block_pool_finish(&device-<wbr>>dynamic_state_block_pool);<br>
+ fail_queue_submit:<br>
+   pthread_cond_destroy(&device-><wbr>queue_submit);<br>
+ fail_mutex:<br>
+   pthread_mutex_destroy(&device-<wbr>>mutex);<br>
+ fail_context_id:<br>
+   anv_gem_destroy_context(<wbr>device, device->context_id);<br>
  fail_fd:<br>
    close(device->fd);<br>
  fail_device:<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.10.2<br>
<br>
</font></span></blockquote></div><br></div></div>