From 289cbd82db8eea6509dccee6eca3a2214e3e66ea Mon Sep 17 00:00:00 2001 From: "Yang, Hao Xiang" Date: Mon, 5 Dec 2022 11:32:02 -0800 Subject: [PATCH] Fixed coverity issue in acl_kernel_if.cpp: Type: Reliance on integer endianness (INCOMPATIBLE_CAST) In the three instances where this issue happens, the function acl_kernel_if_read_32b is used at the end of the scope to query about what's inside the acl_kernel_if* kern object. However, they don't do anything with the obtained information. I believe this read operation is performed to confirm that the acl_kernel_if* kern has been written properly. So if the read operation successed, then the write operation must have also succeeded as well. Therefore, instead of type-punning casting to pass an existing variable, I created a new dummy variable with the only purpose for the read operation to confirm that the read is correct. --- src/acl_kernel_if.cpp | 44 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/acl_kernel_if.cpp b/src/acl_kernel_if.cpp index a7c067af..45f20a0d 100644 --- a/src/acl_kernel_if.cpp +++ b/src/acl_kernel_if.cpp @@ -3,12 +3,12 @@ // System headers. #include +#include #include #include #include #include #include -#include // Internal headers. #include @@ -289,8 +289,8 @@ static int acl_kernel_if_read_32b(acl_kernel_if *kern, unsigned int addr, r = kern->io.read(&kern->io, (dev_addr_t)addr, (char *)val, (size_t)size); if (r < size) { kern->io.printf( - "HAL Kern Error: Read failed from addr %x, read %zu expected %zu\n", addr, - r, size); + "HAL Kern Error: Read failed from addr %x, read %zu expected %zu\n", + addr, r, size); return -1; } return 0; @@ -335,8 +335,8 @@ static int acl_kernel_rom_read_block(acl_kernel_if *kern, unsigned int addr, r = kern->io.read(&kern->io, (dev_addr_t)addr, config_rom, (size_t)size); if (r < size) { kern->io.printf( - "HAL Kern Error: Read failed from addr %x, read %zu expected %zu\n", addr, - r, size); + "HAL Kern Error: Read failed from addr %x, read %zu expected %zu\n", + addr, r, size); return -1; } return 0; @@ -382,9 +382,10 @@ static int acl_kernel_if_write_64b(acl_kernel_if *kern, unsigned int addr, r = (int)kern->io.write(&kern->io, (dev_addr_t)addr, (char *)&val, (size_t)size); if (r < size) { - kern->io.printf("HAL Kern Error: Write failed to addr %x with value %" PRIu64 ", " - "wrote %d, expected %d\n", - addr, val, r, size); + kern->io.printf( + "HAL Kern Error: Write failed to addr %x with value %" PRIu64 ", " + "wrote %d, expected %d\n", + addr, val, r, size); return -1; } return 0; @@ -452,8 +453,8 @@ static uintptr_t acl_kernel_cra_set_segment(acl_kernel_if *kern, acl_kernel_if_write_32b(kern, OFFSET_KERNEL_CRA_SEGMENT, (unsigned int)segment); kern->cur_segment = segment; - acl_kernel_if_read_32b(kern, OFFSET_KERNEL_CRA_SEGMENT, - (unsigned int *)&segment); + unsigned int new_segment; + acl_kernel_if_read_32b(kern, OFFSET_KERNEL_CRA_SEGMENT, &new_segment); } return segment_offset; @@ -469,8 +470,8 @@ static uintptr_t acl_kernel_cra_set_segment_rom(acl_kernel_if *kern, acl_kernel_if_write_32b(kern, OFFSET_KERNEL_CRA_SEGMENT, (unsigned int)segment); kern->cur_segment = segment; - acl_kernel_if_read_32b(kern, OFFSET_KERNEL_CRA_SEGMENT, - (unsigned int *)&segment); + unsigned int new_segment; + acl_kernel_if_read_32b(kern, OFFSET_KERNEL_CRA_SEGMENT, &new_segment); } return segment_offset; @@ -931,10 +932,10 @@ int acl_kernel_if_update(const acl_device_def_autodiscovery_t &devdef, OFFSET_KERNEL_CRA + devdef.hal_info[ii].csr.address; kern->accel_csr[ii].bytes = devdef.hal_info[ii].csr.num_bytes; - ACL_KERNEL_IF_DEBUG_MSG(kern, "Kernel_%s CSR { 0x%08" PRIuPTR ", 0x%08" PRIuPTR " }\n", - devdef.accel[ii].iface.name.c_str(), - kern->accel_csr[ii].address, - kern->accel_csr[ii].bytes); + ACL_KERNEL_IF_DEBUG_MSG( + kern, "Kernel_%s CSR { 0x%08" PRIuPTR ", 0x%08" PRIuPTR " }\n", + devdef.accel[ii].iface.name.c_str(), kern->accel_csr[ii].address, + kern->accel_csr[ii].bytes); } // The Kernel performance monitor registers @@ -943,10 +944,10 @@ int acl_kernel_if_update(const acl_device_def_autodiscovery_t &devdef, OFFSET_KERNEL_CRA + devdef.hal_info[ii].perf_mon.address; kern->accel_perf_mon[ii].bytes = devdef.hal_info[ii].perf_mon.num_bytes; - ACL_KERNEL_IF_DEBUG_MSG(kern, "Kernel_%s perf_mon { 0x%08" PRIuPTR ", 0x%08" PRIuPTR " }\n", - devdef.accel[ii].iface.name.c_str(), - kern->accel_perf_mon[ii].address, - kern->accel_perf_mon[ii].bytes); + ACL_KERNEL_IF_DEBUG_MSG( + kern, "Kernel_%s perf_mon { 0x%08" PRIuPTR ", 0x%08" PRIuPTR " }\n", + devdef.accel[ii].iface.name.c_str(), kern->accel_perf_mon[ii].address, + kern->accel_perf_mon[ii].bytes); // printf info kern->accel_num_printfs[ii] = @@ -1614,8 +1615,9 @@ void acl_kernel_if_update_status(acl_kernel_if *kern) { acl_kernel_if_write_32b(kern, OFFSET_KERNEL_CRA_SEGMENT, (unsigned int)segment_pre_irq); kern->cur_segment = segment_pre_irq; + unsigned int new_segment_pre_irq; acl_kernel_if_read_32b(kern, OFFSET_KERNEL_CRA_SEGMENT, - (unsigned int *)&segment_pre_irq); + &new_segment_pre_irq); } }