Skip to content

Commit

Permalink
Fixed coverity issue in acl_kernel_if.cpp: Type: Reliance on integer …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
haoxian2 committed Dec 6, 2022
1 parent ce73b52 commit 289cbd8
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/acl_kernel_if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

// System headers.
#include <cassert>
#include <cinttypes>
#include <cstdint>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <cinttypes>

// Internal headers.
#include <acl_auto_configure.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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] =
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 289cbd8

Please # to comment.