From 9b41254b817f9cd60a7ec8b4f717e547afe85a70 Mon Sep 17 00:00:00 2001 From: dmed256 Date: Sun, 25 Mar 2018 09:33:14 -0500 Subject: [PATCH] [C] Added magic header to occaType for kernel launch safety --- include/occa/c/defines.h | 2 ++ include/occa/c/types.h | 1 + src/c/kernel.cpp | 3 +++ src/c/types.cpp | 26 ++++++++++++++++++++++---- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/occa/c/defines.h b/include/occa/c/defines.h index a7280f440..9297411fc 100644 --- a/include/occa/c/defines.h +++ b/include/occa/c/defines.h @@ -38,4 +38,6 @@ # endif #endif +#define OCCA_C_TYPE_MAGIC_HEADER 0x5514E455 + #endif diff --git a/include/occa/c/types.h b/include/occa/c/types.h index e808bf35a..c8986533a 100644 --- a/include/occa/c/types.h +++ b/include/occa/c/types.h @@ -38,6 +38,7 @@ typedef struct { } occaDim; typedef struct { + int magicHeader; int type; occaUDim_t bytes; diff --git a/src/c/kernel.cpp b/src/c/kernel.cpp index 992688f24..f131751a2 100644 --- a/src/c/kernel.cpp +++ b/src/c/kernel.cpp @@ -107,6 +107,9 @@ void OCCA_RFUNC occaKernelRunN(occaKernel kernel, occa::kernelArg kArg; occaType arg = va_arg(args, occaType); + OCCA_ERROR("A non-occaType argument was passed", + arg.magicHeader == OCCA_C_TYPE_MAGIC_HEADER); + switch (arg.type) { case occa::c::typeType::none: { kArg.add(NULL, false, false); break; diff --git a/src/c/types.cpp b/src/c/types.cpp index d6a1d8e99..e2a17e1e2 100644 --- a/src/c/types.cpp +++ b/src/c/types.cpp @@ -25,15 +25,17 @@ namespace occa { namespace c { occaType defaultOccaType() { - occaType type; - type.type = occa::c::typeType::none; - type.value.ptr = NULL; - return type; + occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; + oType.type = occa::c::typeType::none; + oType.value.ptr = NULL; + return oType; } template <> occaType newOccaType(const bool &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::bool_; oType.bytes = sizeof(int8_t); oType.value.int8_ = value; @@ -43,6 +45,7 @@ namespace occa { template <> occaType newOccaType(const int8_t &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::int8_; oType.bytes = sizeof(int8_t); oType.value.int8_ = value; @@ -52,6 +55,7 @@ namespace occa { template <> occaType newOccaType(const uint8_t &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::uint8_; oType.bytes = sizeof(uint8_t); oType.value.uint8_ = value; @@ -61,6 +65,7 @@ namespace occa { template <> occaType newOccaType(const int16_t &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::int16_; oType.bytes = sizeof(int16_t); oType.value.int16_ = value; @@ -70,6 +75,7 @@ namespace occa { template <> occaType newOccaType(const uint16_t &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::uint16_; oType.bytes = sizeof(uint16_t); oType.value.uint16_ = value; @@ -79,6 +85,7 @@ namespace occa { template <> occaType newOccaType(const int32_t &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::int32_; oType.bytes = sizeof(int32_t); oType.value.int32_ = value; @@ -88,6 +95,7 @@ namespace occa { template <> occaType newOccaType(const uint32_t &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::uint32_; oType.bytes = sizeof(uint32_t); oType.value.uint32_ = value; @@ -97,6 +105,7 @@ namespace occa { template <> occaType newOccaType(const int64_t &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::int64_; oType.bytes = sizeof(int64_t); oType.value.int64_ = value; @@ -106,6 +115,7 @@ namespace occa { template <> occaType newOccaType(const uint64_t &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::uint64_; oType.bytes = sizeof(uint64_t); oType.value.uint64_ = value; @@ -115,6 +125,7 @@ namespace occa { template <> occaType newOccaType(const float &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::float_; oType.bytes = sizeof(float); oType.value.float_ = value; @@ -124,6 +135,7 @@ namespace occa { template <> occaType newOccaType(const double &value) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::double_; oType.bytes = sizeof(double); oType.value.double_ = value; @@ -132,6 +144,7 @@ namespace occa { occaType newOccaType(occa::device device) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::device; oType.bytes = sizeof(void*); oType.value.ptr = (char*) device.getDHandle(); @@ -140,6 +153,7 @@ namespace occa { occaType newOccaType(occa::kernel kernel) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::kernel; oType.bytes = sizeof(void*); oType.value.ptr = (char*) kernel.getKHandle(); @@ -148,6 +162,7 @@ namespace occa { occaType newOccaType(occa::memory memory) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::memory; oType.bytes = sizeof(void*); oType.value.ptr = (char*) memory.getMHandle(); @@ -156,6 +171,7 @@ namespace occa { occaType newOccaType(occa::properties &properties) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = typeType::properties; oType.bytes = sizeof(void*); oType.value.ptr = (char*) &properties; @@ -335,6 +351,7 @@ OCCA_LFUNC occaType OCCA_RFUNC occaDouble(double value) { OCCA_LFUNC occaType OCCA_RFUNC occaStruct(void *value, occaUDim_t bytes) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = occa::c::typeType::struct_; oType.bytes = bytes; oType.value.ptr = (char*) value; @@ -343,6 +360,7 @@ OCCA_LFUNC occaType OCCA_RFUNC occaStruct(void *value, OCCA_LFUNC occaType OCCA_RFUNC occaString(const char *str) { occaType oType; + oType.magicHeader = OCCA_C_TYPE_MAGIC_HEADER; oType.type = occa::c::typeType::string; oType.bytes = strlen(str); oType.value.ptr = const_cast(str);