Skip to content

Commit

Permalink
Merge pull request #233 from frasercrmck/error-handling
Browse files Browse the repository at this point in the history
Suppress cert-err33-c in error handling code
  • Loading branch information
frasercrmck authored Nov 23, 2023
2 parents c119c2b + fb7bfb5 commit 3fee0d6
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 175 deletions.
4 changes: 2 additions & 2 deletions modules/cargo/include/cargo/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
#ifndef NDEBUG
#define CARGO_ASSERT(CONDITION, MESSAGE) \
if (!(CONDITION)) { \
std::fprintf(stderr, "%s:%d: assert: %s\n %s: %s\n", __FILE__, __LINE__, \
#CONDITION, CARGO_PRETTY_FUNCTION, MESSAGE); \
(void)std::fprintf(stderr, "%s:%d: assert: %s\n %s: %s\n", __FILE__, \
__LINE__, #CONDITION, CARGO_PRETTY_FUNCTION, MESSAGE); \
std::abort(); \
} \
(void)0
Expand Down
20 changes: 10 additions & 10 deletions modules/compiler/builtins/libimg/include/libimg/validate.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@
///
/// @param CONDITION Expression to evaluate and ensure is true.
/// @param MESSAGE A human readable message describing the failure.
#define IMG_ASSERT(CONDITION, MESSAGE) \
if (!(CONDITION)) { \
fprintf(stderr, "%s: %d: libimg assert: %s\n", __FILE__, __LINE__, \
MESSAGE); \
abort(); \
#define IMG_ASSERT(CONDITION, MESSAGE) \
if (!(CONDITION)) { \
(void)fprintf(stderr, "%s: %d: libimg assert: %s\n", __FILE__, __LINE__, \
MESSAGE); \
abort(); \
}

/// @brief Abort due to a situation that should never happen. Disabled by NDEBUG
///
/// @param MESSAGE A human readable message describing the failure.
#define IMG_UNREACHABLE(MESSAGE) \
{ \
fprintf(stderr, "%s: %d: libimg unreachable: %s\n", __FILE__, __LINE__, \
MESSAGE); \
abort(); \
#define IMG_UNREACHABLE(MESSAGE) \
{ \
(void)fprintf(stderr, "%s: %d: libimg unreachable: %s\n", __FILE__, \
__LINE__, MESSAGE); \
abort(); \
}
#else
#define IMG_ASSERT(CONDITION, MESSAGE)
Expand Down
50 changes: 28 additions & 22 deletions modules/compiler/builtins/source/bakery.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,23 @@ cargo::array_view<const uint8_t> get_pch_file(
resource = rc::builtins::builtins64_pch;
break;
default:
fprintf(stderr, "Illegal capabilities_bitfield\n");
(void)fprintf(stderr, "Illegal capabilities_bitfield\n");
abort();
}
if (resource.size() == 1) {
fprintf(stderr,
"Attempted to load an unavailable PCH file with capabilities:\n");
fprintf(stderr, "Bit width: %d\n", (caps & file::CAPS_32BIT) ? 32 : 64);
fprintf(stderr, "Doubles: %s\n",
(caps & file::CAPS_FP64) ? "Enabled" : "Disabled");
fprintf(stderr, "Halfs: %s\n",
(caps & file::CAPS_FP16) ? "Enabled" : "Disabled");
fprintf(stderr,
"This usually indicates that the device capabilities were "
"incorrectly listed in the target device's CMakeLists.txt.\n");
(void)fprintf(
stderr,
"Attempted to load an unavailable PCH file with capabilities:\n");
(void)fprintf(stderr, "Bit width: %d\n",
(caps & file::CAPS_32BIT) ? 32 : 64);
(void)fprintf(stderr, "Doubles: %s\n",
(caps & file::CAPS_FP64) ? "Enabled" : "Disabled");
(void)fprintf(stderr, "Halfs: %s\n",
(caps & file::CAPS_FP16) ? "Enabled" : "Disabled");
(void)fprintf(
stderr,
"This usually indicates that the device capabilities were "
"incorrectly listed in the target device's CMakeLists.txt.\n");
abort();
}
return resource;
Expand Down Expand Up @@ -114,20 +117,23 @@ cargo::array_view<const uint8_t> get_bc_file(file::capabilities_bitfield caps) {
resource = rc::builtins::builtins64_bc;
break;
default:
fprintf(stderr, "Illegal capabilities_bitfield\n");
(void)fprintf(stderr, "Illegal capabilities_bitfield\n");
abort();
}
if (resource.size() == 1) {
fprintf(stderr,
"Attempted to load an unavailable BC file with capabilities:\n");
fprintf(stderr, "Bit width: %d\n", (caps & file::CAPS_32BIT) ? 32 : 64);
fprintf(stderr, "Doubles: %s\n",
(caps & file::CAPS_FP64) ? "Enabled" : "Disabled");
fprintf(stderr, "Halfs: %s\n",
(caps & file::CAPS_FP16) ? "Enabled" : "Disabled");
fprintf(stderr,
"This usually indicates that the device capabilities were "
"incorrectly listed in the target device's CMakeLists.txt.\n");
(void)fprintf(
stderr,
"Attempted to load an unavailable BC file with capabilities:\n");
(void)fprintf(stderr, "Bit width: %d\n",
(caps & file::CAPS_32BIT) ? 32 : 64);
(void)fprintf(stderr, "Doubles: %s\n",
(caps & file::CAPS_FP64) ? "Enabled" : "Disabled");
(void)fprintf(stderr, "Halfs: %s\n",
(caps & file::CAPS_FP16) ? "Enabled" : "Disabled");
(void)fprintf(
stderr,
"This usually indicates that the device capabilities were "
"incorrectly listed in the target device's CMakeLists.txt.\n");
abort();
}
return resource;
Expand Down
10 changes: 5 additions & 5 deletions modules/compiler/builtins/source/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

// TODO: Should we have a CA_ASSERT, if so where should it live?
#ifndef NDEBUG
#define ASSERT(CONDITION, MESSAGE) \
if (CONDITION) { \
fprintf(stderr, "%s: %d: %s\n", __FILE__, __LINE__, MESSAGE); \
std::abort(); \
} \
#define ASSERT(CONDITION, MESSAGE) \
if (CONDITION) { \
(void)fprintf(stderr, "%s: %d: %s\n", __FILE__, __LINE__, MESSAGE); \
std::abort(); \
} \
(void)0
#else
#define ASSERT(CONDITION, MESSAGE)
Expand Down
6 changes: 3 additions & 3 deletions modules/compiler/spirv-ll/include/spirv-ll/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
#include <cstdio>
#include <cstdlib>

#define SPIRV_LL_ABORT(MESSAGE) \
std::fprintf(stderr, "%s:%d: %s\n %s\n", __FILE__, __LINE__, MESSAGE, \
SPIRV_LL_FUNCTION); \
#define SPIRV_LL_ABORT(MESSAGE) \
(void)std::fprintf(stderr, "%s:%d: %s\n %s\n", __FILE__, __LINE__, MESSAGE, \
SPIRV_LL_FUNCTION); \
std::abort()

#define SPIRV_LL_ASSERT(CONDITION, MESSAGE) \
Expand Down
18 changes: 8 additions & 10 deletions modules/compiler/spirv-ll/source/builder_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <llvm/IR/Metadata.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/Debug.h>
#include <llvm/Support/FormatVariadic.h>
#include <llvm/Support/MathExtras.h>
#include <llvm/Support/TypeSize.h>
#include <multi_llvm/llvm_version.h>
Expand Down Expand Up @@ -531,10 +532,9 @@ llvm::Error Builder::create<OpTypeImage>(const OpTypeImage *op) {
}

if (!imageType) {
(void)fprintf(
stderr, "Unsupported type (Dim = %d) passed to 'create<OpTypeImage>'\n",
op->Dim());
std::abort();
return makeStringError(llvm::formatv(
"Unsupported type (Dim = {0}) passed to 'create<OpTypeImage>'\n",
op->Dim()));
}

module.addID(op->IdResult(), op, imageType);
Expand Down Expand Up @@ -2069,10 +2069,9 @@ llvm::Error Builder::create<OpFunction>(const OpFunction *op) {

} break;
default:
fprintf(stderr, "Execution model (ID = %d) is not supported.\n",
ep_op->ExecutionModel());
std::abort();
break;
return makeStringError(
llvm::formatv("Execution model (ID = {0}) is not supported",
ep_op->ExecutionModel()));
}
} else {
// DPC++ rejects variadic functions in SYCL code, with the exception of
Expand Down Expand Up @@ -4480,8 +4479,7 @@ llvm::Error Builder::create<OpISubBorrow>(const OpISubBorrow *op) {
std::to_string(operandType->getIntegerBitWidth());
break;
default:
fprintf(stderr, "Unsupported integer type passed to OpISubBorrow\n");
abort();
return makeStringError("Unsupported integer type passed to OpISubBorrow");
}

llvm::Function *intrinsic = module.llvmModule->getFunction(functionName);
Expand Down
10 changes: 5 additions & 5 deletions modules/compiler/vecz/tools/source/veczc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ static llvm::TargetMachine *initLLVMTarget(llvm::StringRef triple_string,
const llvm::Target *target =
llvm::TargetRegistry::lookupTarget(triple.getTriple(), e);
if (!target) {
::fprintf(stderr, "can't get target %s:%s\n", triple.getTriple().c_str(),
e.c_str());
(void)::fprintf(stderr, "can't get target %s:%s\n",
triple.getTriple().c_str(), e.c_str());
::exit(1);
}
llvm::PassRegistry &registry = *llvm::PassRegistry::getPassRegistry();
Expand Down Expand Up @@ -331,9 +331,9 @@ int main(const int argc, const char *const argv[]) {
llvm::StringRef name;
llvm::SmallVector<vecz::VeczPassOptions, 1> opts;
if (!parsePassOptionsSwitch(S, name, opts)) {
fprintf(stderr,
"failed to parse kernel vectorization specification%s\n",
name.str().c_str());
(void)::fprintf(
stderr, "failed to parse kernel vectorization specification%s\n",
name.str().c_str());
return 1;
}
if (!module->getFunction(name)) {
Expand Down
32 changes: 19 additions & 13 deletions modules/loader/source/relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ bool resolveX86_32(const loader::Relocation &r, loader::ElfMap &map) {
if (symbol_target_address == 0) {
#ifndef NDEBUG
auto name = map.getSymbolName(r.symbol_index).value_or("<unknown symbol>");
fprintf(stderr, "Missing symbol: %.*s\n", (int)name.size(), name.data());
(void)fprintf(stderr, "Missing symbol: %.*s\n", (int)name.size(),
name.data());
#endif
return false;
}
Expand Down Expand Up @@ -121,7 +122,8 @@ bool resolveX86_32(const loader::Relocation &r, loader::ElfMap &map) {
#ifndef NDEBUG
// If this warning is ever triggered, then we can investigate further
if (implicit_addend & uint32_t(0x80008000)) {
fprintf(stderr, "WARNING: Relocation with possibly negative offset\n");
(void)fprintf(stderr,
"WARNING: Relocation with possibly negative offset\n");
}
#endif
uint32_t value = symbol_target_address + implicit_addend;
Expand Down Expand Up @@ -173,7 +175,8 @@ bool resolveX86_64(const loader::Relocation &r, loader::ElfMap &map) {
if (symbol_target_address == 0) {
#ifndef NDEBUG
auto name = map.getSymbolName(r.symbol_index).value_or("<unknown symbol>");
fprintf(stderr, "Missing symbol: %.*s\n", (int)name.size(), name.data());
(void)fprintf(stderr, "Missing symbol: %.*s\n", (int)name.size(),
name.data());
#endif
return false;
}
Expand Down Expand Up @@ -282,7 +285,8 @@ bool resolveArm(const loader::Relocation &r, loader::ElfMap &map,
if (symbol_target_address == 0) {
#ifndef NDEBUG
auto name = map.getSymbolName(r.symbol_index).value_or("<unknown symbol>");
fprintf(stderr, "Missing symbol: %.*s\n", (int)name.size(), name.data());
(void)fprintf(stderr, "Missing symbol: %.*s\n", (int)name.size(),
name.data());
#endif
return false;
}
Expand Down Expand Up @@ -396,8 +400,8 @@ bool resolveAArch64(const loader::Relocation &r, loader::ElfMap &map,
map.getSymbolTargetAddress(r.symbol_index).value_or(0);
if (symbol_target_address == 0) {
#ifndef NDEBUG
fprintf(stderr, "Missing symbol: %.*s\n", (int)symbol_name.size(),
symbol_name.data());
(void)fprintf(stderr, "Missing symbol: %.*s\n", (int)symbol_name.size(),
symbol_name.data());
#endif
return false;
}
Expand Down Expand Up @@ -547,9 +551,10 @@ bool resolveAArch64(const loader::Relocation &r, loader::ElfMap &map,
uint64_t stub_target = getOrCreateStub(symbol_target_address);
if (0LU == stub_target) {
#ifndef NDEBUG
fprintf(stderr,
"Out of stub space when constructing linker veneer for: %.*s\n",
(int)symbol_name.size(), symbol_name.data());
(void)fprintf(
stderr,
"Out of stub space when constructing linker veneer for: %.*s\n",
(int)symbol_name.size(), symbol_name.data());
#endif
return false;
}
Expand All @@ -558,10 +563,11 @@ bool resolveAArch64(const loader::Relocation &r, loader::ElfMap &map,
if (static_cast<int64_t>(relative_value) < -(1LL << 27) ||
static_cast<int64_t>(relative_value) >= (1LL << 27)) {
#ifndef NDEBUG
fprintf(stderr,
"Linker veneer for symbol %.*s beyond addressable span of "
"branch instruction",
(int)symbol_name.size(), symbol_name.data());
(void)fprintf(
stderr,
"Linker veneer for symbol %.*s beyond addressable span of "
"branch instruction",
(int)symbol_name.size(), symbol_name.data());
#endif
return false;
}
Expand Down
22 changes: 11 additions & 11 deletions modules/tracer/source/tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ struct TracerVirtualMemFileImpl {
int f = open(tmp_name.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);

if (f == 0) {
fprintf(stderr, "Could not open %s temp file for tracing.\n",
tmp_name.c_str());
(void)fprintf(stderr, "Could not open %s temp file for tracing.\n",
tmp_name.c_str());
return;
}

Expand All @@ -90,8 +90,8 @@ struct TracerVirtualMemFileImpl {
lseek64(f, bytes, SEEK_SET);
auto written = write(f, "", 1);
if (0 == written) {
fprintf(stderr, "Failed to resize %s.\n", tmp_name.c_str());
remove(tmp_name.c_str());
(void)fprintf(stderr, "Failed to resize %s.\n", tmp_name.c_str());
(void)remove(tmp_name.c_str());
return;
}
lseek64(f, 0, SEEK_SET);
Expand All @@ -101,8 +101,8 @@ struct TracerVirtualMemFileImpl {
close(f);

if (map == MAP_FAILED) {
fprintf(stderr, "Failed to map tmp file:%s.\n", strerror(errno));
remove(tmp_name.c_str());
(void)fprintf(stderr, "Failed to map tmp file:%s.\n", strerror(errno));
(void)remove(tmp_name.c_str());
}

// Buffer to keep things stack allocated.
Expand Down Expand Up @@ -132,9 +132,9 @@ struct TracerVirtualMemFileImpl {
const char *ending = "\n\t]\n}\n";

if (std::strlen(ending) + offset > max_offset) {
fprintf(stderr,
"Trace overflow, failed to write data, increase "
"CA_TRACE_FILE_BUFFER_MB");
(void)fprintf(stderr,
"Trace overflow, failed to write data, increase "
"CA_TRACE_FILE_BUFFER_MB");
}

writeToMemMap(ending, std::strlen(ending));
Expand All @@ -149,7 +149,7 @@ struct TracerVirtualMemFileImpl {
fclose(file);

if (idx != offset.load()) {
fprintf(stderr, "Trace file could not be shrunk down.");
(void)fprintf(stderr, "Trace file could not be shrunk down.");
}
}

Expand Down Expand Up @@ -216,7 +216,7 @@ struct TracerFileImpl {
file = fopen(env, "w");

if (nullptr == file) {
fprintf(stderr, "Could not open '%s' for tracing.\n", env);
(void)fprintf(stderr, "Could not open '%s' for tracing.\n", env);
return;
}

Expand Down
8 changes: 5 additions & 3 deletions modules/utils/targets/host/source/relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void *dbg_memcpy(void *dest, const void *src, size_t count) {
size_t write_count = static_cast<size_t>(write(null_fd, src, count));
close(null_fd);
if (write_count != count) {
fprintf(stderr, "memcpy (called from kernel) out-of-bounds read\n");
(void)fprintf(stderr, "memcpy (called from kernel) out-of-bounds read\n");
std::abort();
}
}
Expand All @@ -90,7 +90,8 @@ void *dbg_memcpy(void *dest, const void *src, size_t count) {
size_t read_count = static_cast<size_t>(read(zero_fd, dest, count));
close(zero_fd);
if (read_count != count) {
fprintf(stderr, "memcpy (called from kernel) out-of-bounds write\n");
(void)fprintf(stderr,
"memcpy (called from kernel) out-of-bounds write\n");
std::abort();
}
}
Expand All @@ -115,7 +116,8 @@ void *dbg_memset(void *dest, int ch, size_t count) {
size_t cnt = static_cast<size_t>(read(zero_fd, dest, count));
close(zero_fd);
if (count != cnt) {
fprintf(stderr, "memset (called from kernel) out-of-bounds write\n");
(void)fprintf(stderr,
"memset (called from kernel) out-of-bounds write\n");
std::abort();
}
}
Expand Down
Loading

0 comments on commit 3fee0d6

Please sign in to comment.