Skip to content

Fixed coverity issues in src/acl_mem.cpp #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 10, 2022

Conversation

haoxian2
Copy link
Contributor

@haoxian2 haoxian2 commented Dec 2, 2022

Fixed coverity issues in acl_mem.cpp: Constant expression result

This issues happen when a condition can only have one outcome.

  1. Removed a lot of the && clauses because CL_MEM_OBJECT_IMAGE1D, CL_MEM_OBJECT_IMAGE1D_ARRAY, and CL_MEM_OBJECT_IMAGE1D_BUFFER are all defined as strictly different values respectively 0x10F4, 0x10F5, and 0x10F6. Therefore that section of the statement would never be accessed. Since all 1D image objects do not use image_height as per specifications, it should be an || operator instead of && operator.
  2. (line 2957) ((map_flags & CL_MAP_READ) & (map_flags & CL_MAP_WRITE_INVALIDATE_REGION)), both will only return the common bit between map_flags and the 2nd value, CL_MAP_READ being 1 and CL_MAP_WRITE_INVALIDATE_REGION being 4, but there are no common bits between 1 and 4, therefore this statement will always be false. From the comments, I believe that this block takes care of the state where map_flags contains both one of CL_MAP_READ or CL_MAP_WRITE and CL_MAP_WRITE_INVALIDATE_REGION. and therefore should replace the middle && to &.

Added new checks in acl_mem_test.cpp

Checking if enqueue functions can handle 1D image. These functions access the image_desc.height variable, making sure it is initialized by default.

Fixed coverity issue in acl_mem.cpp: Dereference after null check

  1. (Line 2036) Previously, if image is NULL, src_element_size is set to 0. However, there are a bunch of 'image' dereference after this check, which does not make sense since this if statement makes it seem like this function can handle a NULL image. Therefore, I replaced it with return CL_INVALID_MEM_OBJECT like some of the other functions. Rewrote some blocks of code that are similar to this one to conform to better verticality of code.
  2. (Line 6829) Except these two lines, all the other line querying about mem->block_allocation are contained in the block with a null check for mem->block_allocation. Moving the two lines with the other lines.

Fixed coverity issue in acl_mem.cpp: Memory leak

(Line 4172) Missing free for a malloc object before returning, same with (line 4183)
Turned needs_release_on_fail into a vector to avoid malloc and use built in destructor when it goes out of scope.

Copy link
Contributor

@pcolberg pcolberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @haoxian2! Two minor style tweaks and this is good to go 🙂

@pcolberg pcolberg added the bug Something isn't working label Dec 3, 2022
@pcolberg pcolberg added this to the 2023.1 milestone Dec 3, 2022
@haoxian2 haoxian2 force-pushed the coverity-acl-mem branch 2 times, most recently from 3046edd to 207bd89 Compare December 5, 2022 16:44
@haoxian2 haoxian2 requested a review from pcolberg December 5, 2022 16:50
Copy link
Contributor

@pcolberg pcolberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @haoxian2! You can use the type bool for the auxiliary vector.

@haoxian2 haoxian2 requested a review from pcolberg December 6, 2022 01:17
@haoxian2 haoxian2 force-pushed the coverity-acl-mem branch 2 times, most recently from 68cbc2a to 6708bc5 Compare December 8, 2022 20:40
Changed a lot of the && to || because CL_MEM_OBJECT_IMAGE1D, CL_MEM_OBJECT_IMAGE1D_ARRAY, and CL_MEM_OBJECT_IMAGE1D_BUFFER are all defined as strictly different values respectively 0x10F4, 0x10F5, and 0x10F6. Therefore that section of the statement would never be accessed. Logically, if the object is a 1D image, no matter which one it is, the slice pitch will not have a height and would just be equal to the row pitch, therefore I think logical OR makes sense.
(line 2957) ((map_flags & CL_MAP_READ) & (map_flags & CL_MAP_WRITE_INVALIDATE_REGION)), both will only return the common bit between map_flags and the 2nd value, CL_MAP_READ being 1 and CL_MAP_WRITE_INVALIDATE_REGION being 4, but there are no common bits between 1 and 4, therefore this statement will always be false. From the comments, I believe that this block takes care of the state where map_flags contains both one of CL_MAP_READ or CL_MAP_WRITE and CL_MAP_WRITE_INVALIDATE_REGION. and therefore should replace the middle & to &&.
1D image does not have initialization of height and depth members. Some functions check for that before they check for height and depth.
Added an image with 0 height and 0 depth to make sure that these functions don't mistakenly use the height and depth in image_desc.
(Line 2036) Previously, if image is NULL, src_element_size is set to 0. However, there are a bunch of 'image' dereference after this check, which does not make sense since this if statement makes it seem like this function can handle a NULL image. Therefore, I replaced it with return CL_INVALID_MEM_OBJECT like some of the other functions.
(Line 6829) Except these two lines, all the other line querying about mem->block_allocation are contained in the block with a null check for mem->block_allocation. Moving the two lines with the other lines.
(Line 4172) Missing free for a malloc object before returning, same with (line 4183)
Solution: Turning `needs_release_on_fail` into vector since it is only present in the scope of this function
Copy link
Contributor

@pcolberg pcolberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @haoxian2!

@pcolberg pcolberg merged commit 5994a1c into intel:main Dec 10, 2022
@haoxian2 haoxian2 deleted the coverity-acl-mem branch December 12, 2022 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants