Skip to content

Commit

Permalink
Add asserts for snprintf, sprintf, and vsnprintf (#2910)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Essenmacher <[email protected]>
  • Loading branch information
mikeessen authored Aug 20, 2024
1 parent 3824321 commit 2e9bcf2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Runtime/OMIndexLookup.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ static inline uint32_t hash_string(uint32_t hval, const char *str) {
// Adaptation of 32-bit FNV for int64_t values.
static inline uint32_t hash_int64(uint32_t hval, int64_t val) {
char str[20];
snprintf(str, sizeof(str), "%lld", (long long)val);
int num_chars_written = snprintf(str, sizeof(str), "%lld", (long long)val);
assert(num_chars_written >= 0 && "snprintf write error to str");
return hash_string(hval, str);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Runtime/OMInstrument.inc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ static void ReportMemory() {
char memOutput[200];
FILE *memPipe;
mypid = getpid();
snprintf(memCommand, sizeof(memCommand), "ps -o vsz='' -p %d", mypid);
int num_chars_written = snprintf(memCommand, sizeof(memCommand), "ps -o vsz='' -p %d", mypid);
assert(num_chars_written >= 0 && "snprintf write error to memCommand");
memPipe = popen(memCommand, "r");
if (!memPipe) {
fprintf(fout, ", error-failed-to-execute-ps\n");
Expand Down
22 changes: 16 additions & 6 deletions src/Runtime/jni/jnilog.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,25 @@ void log_printf(int level, const char *file, const char *func, int line,
time_t now;
struct tm *tm;
char buf[LOG_MAX_LEN];
int num_chars_written = 0;

/* Get local time and format as 2020-07-03 05:17:42 -0400 */
if (time(&now) == -1 || (tm = localtime(&now)) == NULL ||
strftime(buf, sizeof(buf), "[%F %T %z]", tm) == 0)
sprintf(buf, "[-]");
strftime(buf, sizeof(buf), "[%F %T %z]", tm) == 0) {
num_chars_written = sprintf(buf, "[-]");
assert(num_chars_written >= 0 && "sprintf write error to buf");
}

/* Output thread ID, log level, file name, function number, and line number.
* Note that pthread_t on most platforms is unsigned long but is a struct
* of 8 bytes on z/OS.
*/
pthread_t tid = get_threadid();
assert(LOG_MAX_LEN >= strlen(buf) && "error in snprintf length");
snprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf),
num_chars_written = snprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf),
"[%016lx][%s]%s:%s:%d ", *(unsigned long *)&tid, log_level_name[level],
get_filename(file), func, line);
assert(num_chars_written >= 0 && "snprintf write error to buf");

/* Output actual log data */
/* Definition of vsnprintf:
Expand Down Expand Up @@ -203,11 +207,15 @@ void log_printf(int level, const char *file, const char *func, int line,

va_list log_data;
va_start(log_data, fmt);
vsnprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf), fmt, log_data);
num_chars_written =
vsnprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf), fmt, log_data);
assert(num_chars_written >= 0 && "vsnprintf write error to buf");
va_end(log_data);

/* Add new line */
snprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf), "\n");
num_chars_written =
snprintf(buf + strlen(buf), LOG_MAX_LEN - strlen(buf), "\n");
assert(num_chars_written >= 0 && "snprintf write error to buf");

/* Write out and flush the output buffer */
FILE *fp = get_log_fp();
Expand Down Expand Up @@ -238,9 +246,11 @@ static FILE *get_log_file_by_name(char *name) {
char *tname = (char *)malloc(strlen(name) + 32);
if (tname) {
pthread_t tid = get_threadid();
snprintf(
int num_chars_written = snprintf(
tname, strlen(name) + 32, "%s.%016lx", name, *(unsigned long *)&tid);
assert(num_chars_written >= 0 && "snprintf write error to tname");
fp = fopen(tname, "w");
assert(fp != NULL && "fopen error on tname");
free(tname);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/Runtime/jni/jnilog.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL };
*/ \
while (__i < __l && \
(__k = snprintf(__p, __j, format, ((type *)data)[__i])) < __j) { \
assert(__k >= 0 && "snprintf write error to __p"); \
__p += __k; \
__j -= __k; \
__i++; \
Expand All @@ -55,8 +56,9 @@ enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL };
* add "... " at the end to denote that the last element is \
* truncated. \
*/ \
snprintf(buf + strlen(buf), 6, \
int __m = snprintf(buf + strlen(buf), 6, \
(__i == __l) ? " " : (__j == 1) ? " ... " : "... "); \
assert(__m >= 0 && "snprintf write error to buf"); \
} while (0)

/* Construct string of up to LOG_MAX_NUM elements of an array of ONNX type.
Expand Down Expand Up @@ -90,8 +92,10 @@ enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL };
case ONNX_TYPE_DOUBLE: \
LOG_BUF_C_TYPE(const double, hex ? " %016x" : " %lf", buf, data, n); \
break; \
default: \
sprintf(buf, " unsupported data type %d ", type); \
default: { \
int __a = sprintf(buf, " unsupported data type %d ", type); \
assert(__a >= 0 && "sprintf write error to buf"); \
} \
} \
} while (0)

Expand Down
3 changes: 2 additions & 1 deletion src/Runtime/jni/jniwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,8 @@ Java_com_ibm_onnxmlir_OMModel_query_1entry_1points(JNIEnv *env, jclass cls) {
*/
for (int64_t i = 0; i < neps; i++) {
char ep[32];
sprintf(ep, "ep[%lld]", (long long)i);
int num_chars_written = sprintf(ep, "ep[%lld]", (long long)i);
assert(num_chars_written >= 0 && "sprintf write error to ep");
HEX_DEBUG(ep, jni_eps[i], strlen(jni_eps[i]));
LOG_PRINTF(LOG_DEBUG, "ep[%d](%ld):%s", i, strlen(jni_eps[i]), jni_eps[i]);

Expand Down

0 comments on commit 2e9bcf2

Please sign in to comment.