From 1637c19e08494e508a7279c53f24ae614fcbab73 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 15 Sep 2010 23:03:30 +0000 Subject: [PATCH] - API and sample apps: pass buf length arg to boinc_msg_prefix() and use snprintf() to avoid overrun. Can't see why this could ever happen. Attempt to fix crash in E@h apps. svn path=/trunk/boinc/; revision=22363 --- api/boinc_api.cpp | 54 +++++++++++++----------- api/boinc_api.h | 2 +- api/graphics2_unix.cpp | 2 +- api/graphics2_win.cpp | 23 +++++----- apps/upper_case.cpp | 16 +++---- checkin_notes | 14 +++++++ samples/atiopencl/atiopencl.cpp | 16 +++---- samples/example_app/uc2.cpp | 16 +++---- samples/multi_thread/multi_thread.cpp | 14 ++++--- samples/nvcuda/cuda.cpp | 60 +++++++++++++++------------ samples/nvopencl/nvopencl.cpp | 38 ++++++++++------- samples/wrapper/wrapper.cpp | 14 ++++--- 12 files changed, 157 insertions(+), 112 deletions(-) diff --git a/api/boinc_api.cpp b/api/boinc_api.cpp index c7bcacd844..26e451e7da 100644 --- a/api/boinc_api.cpp +++ b/api/boinc_api.cpp @@ -193,17 +193,18 @@ static void boinc_exit(int); static void block_sigalrm(); static int start_worker_signals(); -char* boinc_msg_prefix(char* sbuf) { +char* boinc_msg_prefix(char* sbuf, int len) { char buf[256]; time_t x = time(0); struct tm* tm = localtime(&x); strftime(buf, sizeof(buf)-1, "%H:%M:%S", tm); #ifdef _WIN32 - sprintf(sbuf, "%s (%d):", buf, GetCurrentProcessId()); + snprintf(sbuf, len, "%s (%d):", buf, GetCurrentProcessId()); #else - sprintf(sbuf, "%s (%d):", buf, getpid()); + snprintf(sbuf, len, "%s (%d):", buf, getpid()); #endif + sbuf[len-1] = 0; // just in case return sbuf; } @@ -212,7 +213,7 @@ static int setup_shared_mem() { if (standalone) { fprintf(stderr, "%s Standalone mode, so not using shared memory.\n", - boinc_msg_prefix(buf) + boinc_msg_prefix(buf, sizeof(buf)) ); return 0; } @@ -367,7 +368,7 @@ int boinc_init_options_general(BOINC_OPTIONS& opt) { // give any previous occupant a chance to timeout and exit // fprintf(stderr, "%s Can't acquire lockfile (%d) - waiting %ds\n", - boinc_msg_prefix(buf), + boinc_msg_prefix(buf, sizeof(buf)), retval, LOCKFILE_TIMEOUT_PERIOD ); boinc_sleep(LOCKFILE_TIMEOUT_PERIOD); @@ -375,13 +376,13 @@ int boinc_init_options_general(BOINC_OPTIONS& opt) { } if (retval) { fprintf(stderr, "%s Can't acquire lockfile (%d) - exiting\n", - boinc_msg_prefix(buf), + boinc_msg_prefix(buf, sizeof(buf)), retval ); #ifdef _WIN32 - char buf[256]; - windows_error_string(buf, 256); - fprintf(stderr, "%s Error: %s\n", boinc_msg_prefix(buf), buf); + char buf2[256]; + windows_error_string(buf2, 256); + fprintf(stderr, "%s Error: %s\n", boinc_msg_prefix(buf, sizeof(buf)), buf2); #endif // if we can't acquire the lock file there must be // another app instance running in this slot. @@ -400,7 +401,7 @@ int boinc_init_options_general(BOINC_OPTIONS& opt) { if (retval) { fprintf(stderr, "%s Can't set up shared mem: %d. Will run in standalone mode.\n", - boinc_msg_prefix(buf), retval + boinc_msg_prefix(buf, sizeof(buf)), retval ); standalone = true; } @@ -463,7 +464,10 @@ static void send_trickle_up_msg() { int boinc_finish(int status) { char buf[256]; fraction_done = 1; - fprintf(stderr, "%s called boinc_finish\n", boinc_msg_prefix(buf)); + fprintf(stderr, + "%s called boinc_finish\n", + boinc_msg_prefix(buf, sizeof(buf)) + ); boinc_sleep(2.0); // let the timer thread send final messages g_sleep = true; // then disable it @@ -509,11 +513,12 @@ void boinc_exit(int status) { windows_error_string(buf, 256); fprintf(stderr, "%s Can't unlock lockfile (%d): %s\n", - boinc_msg_prefix(buf), retval, buf + boinc_msg_prefix(buf, sizeof(buf)), retval, buf ); #else fprintf(stderr, - "%s Can't unlock lockfile (%d)\n", boinc_msg_prefix(buf), retval + "%s Can't unlock lockfile (%d)\n", + boinc_msg_prefix(buf, sizeof(buf)), retval ); perror("file unlock failed"); #endif @@ -556,7 +561,7 @@ static void exit_from_timer_thread(int status) { #ifdef DEBUG_BOINC_API char buf[256]; fprintf(stderr, "%s exit_from_timer_thread(%d) called\n", - boinc_msg_prefix(buf), status + boinc_msg_prefix(buf, sizeof(buf)), status ); #endif #ifdef _WIN32 @@ -595,7 +600,7 @@ int boinc_parse_init_data_file() { if (!boinc_file_exists(INIT_DATA_FILE)) { fprintf(stderr, "%s Can't open init data file - running in standalone mode\n", - boinc_msg_prefix(buf) + boinc_msg_prefix(buf, sizeof(buf)) ); return ERR_FOPEN; } @@ -605,7 +610,7 @@ int boinc_parse_init_data_file() { if (retval) { fprintf(stderr, "%s Can't parse init data file - running in standalone mode\n", - boinc_msg_prefix(buf) + boinc_msg_prefix(buf, sizeof(buf)) ); return retval; } @@ -723,7 +728,7 @@ static void handle_upload_file_status() { if (!f) { fprintf(stderr, "%s handle_file_upload_status: can't open %s\n", - boinc_msg_prefix(buf), filename.c_str() + boinc_msg_prefix(buf, sizeof(buf)), filename.c_str() ); continue; } @@ -736,7 +741,7 @@ static void handle_upload_file_status() { upload_file_status.push_back(uf); } else { fprintf(stderr, "%s handle_upload_file_status: can't parse %s\n", - boinc_msg_prefix(log_buf), buf + boinc_msg_prefix(log_buf, sizeof(log_buf)), buf ); } } @@ -765,7 +770,7 @@ static void handle_process_control_msg() { #ifdef DEBUG_BOINC_API char log_buf[256] fprintf(stderr, "%s got process control msg %s\n", - boinc_msg_prefix(log_buf), buf + boinc_msg_prefix(log_buf, sizeof(log_buf)), buf ); #endif if (match_tag(buf, "")) { @@ -937,7 +942,8 @@ static void timer_handler() { #ifdef DEBUG_BOINC_API if (in_critical_section) { fprintf(stderr, - "%s: timer_handler(): in critical section\n", boinc_msg_prefix(buf) + "%s: timer_handler(): in critical section\n", + boinc_msg_prefix(buf, sizeof(buf)) ); } #endif @@ -962,7 +968,7 @@ static void timer_handler() { if (interrupt_count % TIMERS_PER_SEC) return; #ifdef DEBUG_BOINC_API - fprintf(stderr, "%s 1 sec elapsed\n", boinc_msg_prefix(buf)); + fprintf(stderr, "%s 1 sec elapsed\n", boinc_msg_prefix(buf, sizeof(buf))); #endif // here it we're at a one-second boundary; do slow stuff @@ -982,7 +988,7 @@ static void timer_handler() { if (heartbeat_giveup_time < interrupt_count) { fprintf(stderr, "%s No heartbeat from core client for 30 sec - exiting\n", - boinc_msg_prefix(buf) + boinc_msg_prefix(buf, sizeof(buf)) ); if (options.direct_process_action) { exit_from_timer_thread(0); @@ -1092,7 +1098,7 @@ int start_timer_thread() { if (!CreateThread(NULL, 0, timer_thread, 0, 0, &timer_thread_id)) { fprintf(stderr, "%s start_timer_thread(): CreateThread() failed, errno %d\n", - boinc_msg_prefix(buf), errno + boinc_msg_prefix(buf, sizeof(buf)), errno ); return errno; } @@ -1110,7 +1116,7 @@ int start_timer_thread() { if (retval) { fprintf(stderr, "%s start_timer_thread(): pthread_create(): %d", - boinc_msg_prefix(buf), retval + boinc_msg_prefix(buf, sizeof(buf)), retval ); return retval; } diff --git a/api/boinc_api.h b/api/boinc_api.h index 3fda1830b3..c677fa3a0c 100644 --- a/api/boinc_api.h +++ b/api/boinc_api.h @@ -125,7 +125,7 @@ extern int boinc_wu_cpu_time(double&); extern double boinc_elapsed_time(); extern int boinc_upload_file(std::string& name); extern int boinc_upload_status(std::string& name); -extern char* boinc_msg_prefix(char*); +extern char* boinc_msg_prefix(char*, int); /////////// API ENDS HERE diff --git a/api/graphics2_unix.cpp b/api/graphics2_unix.cpp index 1492f1a3bd..54ef2556dc 100644 --- a/api/graphics2_unix.cpp +++ b/api/graphics2_unix.cpp @@ -54,7 +54,7 @@ bool fullscreen; void boinc_close_window_and_quit(const char* p) { char buf[256]; - fprintf(stderr, "%s Quitting: %s\n", boinc_msg_prefix(buf), p); + fprintf(stderr, "%s Quitting: %s\n", boinc_msg_prefix(buf, sizeof(buf)), p); exit(0); } diff --git a/api/graphics2_win.cpp b/api/graphics2_win.cpp index c0d01d5c99..fa082310c2 100644 --- a/api/graphics2_win.cpp +++ b/api/graphics2_win.cpp @@ -52,7 +52,7 @@ static bool fullscreen; void boinc_close_window_and_quit(const char* p) { char buf[256]; fprintf(stderr, "%s Close event (%s) detected, shutting down.\n", - boinc_msg_prefix(buf), p + boinc_msg_prefix(buf, sizeof(buf)), p ); window_ready = false; @@ -105,7 +105,7 @@ void SetupPixelFormat(HDC win_dc) { if (!SetPixelFormat(win_dc, nPixelFormat, &pfd)) { fprintf(stderr, "%s ERROR: Couldn't set pixel format for device context (0x%x).\n", - boinc_msg_prefix(buf), GetLastError() + boinc_msg_prefix(buf, sizeof(buf)), GetLastError() ); } } @@ -168,14 +168,14 @@ static void make_window(const char* title) { if (!SetForegroundWindow(window)) { fprintf(stderr, "%s ERROR: SetForegroundWindow() failed (0x%x).\n", - boinc_msg_prefix(buf), GetLastError() + boinc_msg_prefix(buf, sizeof(buf)), GetLastError() ); } if (!GetCursorPos(&mousePos)) { fprintf(stderr, "%s ERROR: GetCursorPos() failed (0x%x).\n", - boinc_msg_prefix(buf), GetLastError() + boinc_msg_prefix(buf, sizeof(buf)), GetLastError() ); } @@ -183,7 +183,7 @@ static void make_window(const char* title) { if (!win_dc) { fprintf(stderr, "%s ERROR: GetDC() failed (0x%x).\n", - boinc_msg_prefix(buf), GetLastError() + boinc_msg_prefix(buf, sizeof(buf)), GetLastError() ); } SetupPixelFormat(win_dc); @@ -192,7 +192,7 @@ static void make_window(const char* title) { if (!gl_dc) { fprintf(stderr, "%s ERROR: wglCreateContext() failed (0x%x).\n", - boinc_msg_prefix(buf), GetLastError() + boinc_msg_prefix(buf, sizeof(buf)), GetLastError() ); ReleaseDC(window, win_dc); return; @@ -201,7 +201,7 @@ static void make_window(const char* title) { if(!wglMakeCurrent(win_dc, gl_dc)) { fprintf(stderr, "%s ERROR: wglMakeCurrent() failed (0x%x).\n", - boinc_msg_prefix(buf), GetLastError() + boinc_msg_prefix(buf, sizeof(buf)), GetLastError() ); ReleaseDC(window, win_dc); wglDeleteContext(gl_dc); @@ -416,13 +416,16 @@ void boinc_graphics_loop(int argc, char** argv, const char* title) { boinc_init_graphics_diagnostics(BOINC_DIAG_DEFAULTS); } - fprintf(stderr, "%s Starting graphics application.\n", boinc_msg_prefix(buf)); + fprintf(stderr, + "%s Starting graphics application.\n", + boinc_msg_prefix(buf, sizeof(buf)) + ); for (int i=1; iunits_done++; fprintf(stderr, "%s thread %d finished %d: %f\n", - boinc_msg_prefix(buf), t->index, i, x + boinc_msg_prefix(buf, sizeof(buf)), t->index, i, x ); } t->id = THREAD_ID_NULL; @@ -160,7 +164,7 @@ int main(int argc, char** argv) { nthreads = atoi(argv[++i]); } else { fprintf(stderr, "%s unrecognized arg: %s\n", - boinc_msg_prefix(buf), argv[i] + boinc_msg_prefix(buf, sizeof(buf)), argv[i] ); } } @@ -181,7 +185,7 @@ int main(int argc, char** argv) { double elapsed_time = dtime()-start_time; fprintf(stderr, "%s All done. Used %d threads. Elapsed time %f\n", - boinc_msg_prefix(buf), nthreads, elapsed_time + boinc_msg_prefix(buf, sizeof(buf)), nthreads, elapsed_time ); boinc_finish(0); } diff --git a/samples/nvcuda/cuda.cpp b/samples/nvcuda/cuda.cpp index 520f3005ca..078af4bd52 100644 --- a/samples/nvcuda/cuda.cpp +++ b/samples/nvcuda/cuda.cpp @@ -44,8 +44,7 @@ bool early_crash = false; bool early_sleep = false; double cpu_time = 20, comp_result; -int main(int argc, char** argv) -{ +int main(int argc, char** argv) { int i, retval, lastInversion=0, checkpointExists=0, dimension=0; double fd; char input_path[512], output_path[512], chkpt_path[512], buf[256]; @@ -68,9 +67,10 @@ int main(int argc, char** argv) retval = boinc_init(); if (retval) { - fprintf(stderr, "%s boinc_init returned %d\n", - boinc_msg_prefix(buf), retval - ); + fprintf(stderr, + "%s boinc_init returned %d\n", + boinc_msg_prefix(buf, sizeof(buf)), retval + ); exit(retval); } @@ -80,9 +80,9 @@ int main(int argc, char** argv) infile = boinc_fopen(input_path, "r"); if (!infile) { fprintf(stderr, - "%s Couldn't find input file, resolved name %s.\n", - boinc_msg_prefix(buf), input_path - ); + "%s Couldn't find input file, resolved name %s.\n", + boinc_msg_prefix(buf, sizeof(buf)), input_path + ); getchar(); exit(-1); } @@ -113,12 +113,14 @@ int main(int argc, char** argv) retval = out.open(output_path, "wb"); if (retval) { - fprintf(stderr, "%s APP: matrix_inversion output open failed:\n", - boinc_msg_prefix(buf) - ); - fprintf(stderr, "%s resolved name %s, retval %d\n", - boinc_msg_prefix(buf), output_path, retval - ); + fprintf(stderr, + "%s APP: matrix_inversion output open failed:\n", + boinc_msg_prefix(buf, sizeof(buf)) + ); + fprintf(stderr, + "%s resolved name %s, retval %d\n", + boinc_msg_prefix(buf, sizeof(buf)), output_path, retval + ); perror("open"); exit(1); } @@ -128,9 +130,10 @@ int main(int argc, char** argv) // shmem = (UC_SHMEM*)boinc_graphics_make_shmem("matrix_inversion", sizeof(UC_SHMEM)); if (!shmem) { - fprintf(stderr, "%s failed to create shared mem segment\n", - boinc_msg_prefix(buf) - ); + fprintf(stderr, + "%s failed to create shared mem segment\n", + boinc_msg_prefix(buf, sizeof(buf)) + ); } update_shmem(); boinc_register_timer_callback(update_shmem); @@ -175,9 +178,10 @@ int main(int argc, char** argv) //we'll need to write the current matrix to the state file. retval = do_checkpoint(out, i, h_idata, dimension); if (retval) { - fprintf(stderr, "%s APP: matrix_inversion checkpoint failed %d\n", - boinc_msg_prefix(buf), retval - ); + fprintf(stderr, + "%s APP: matrix_inversion checkpoint failed %d\n", + boinc_msg_prefix(buf, sizeof(buf)), retval + ); exit(retval); } boinc_checkpoint_completed(); @@ -194,9 +198,10 @@ int main(int argc, char** argv) cudaFreeHost( h_idata ); retval = out.flush(); //force the output file to be closed. if (retval) { - fprintf(stderr, "%s APP: matrix_inversion flush failed %d\n", - boinc_msg_prefix(buf), retval - ); + fprintf(stderr, + "%s APP: matrix_inversion flush failed %d\n", + boinc_msg_prefix(buf, sizeof(buf)), retval + ); exit(1); } @@ -214,9 +219,10 @@ int main(int argc, char** argv) if (boinc_time_to_checkpoint()) { retval = do_checkpoint(out, NUM_ITERATIONS, h_idata, dimension); if (retval) { - fprintf(stderr, "%s APP: maxtrix_inversion checkpoint failed %d\n", - boinc_msg_prefix(buf), retval - ); + fprintf(stderr, + "%s APP: maxtrix_inversion checkpoint failed %d\n", + boinc_msg_prefix(buf, sizeof(buf)), retval + ); exit(1); } boinc_checkpoint_completed(); @@ -381,4 +387,4 @@ void print_to_file(MFILE *out, float *h_odata, int dimension) { } --num_elements; } -} \ No newline at end of file +} diff --git a/samples/nvopencl/nvopencl.cpp b/samples/nvopencl/nvopencl.cpp index 9ce7bd7dec..243172c1b7 100644 --- a/samples/nvopencl/nvopencl.cpp +++ b/samples/nvopencl/nvopencl.cpp @@ -57,8 +57,10 @@ int main(int argc, char * argv[]) { retval = boinc_init(); if (retval) { - fprintf(stderr, "%s boinc_init returned %d\n", - boinc_msg_prefix(buf), retval ); + fprintf(stderr, + "%s boinc_init returned %d\n", + boinc_msg_prefix(buf, sizeof(buf)), retval + ); exit(retval); } @@ -69,7 +71,7 @@ int main(int argc, char * argv[]) { if (!infile) { fprintf(stderr, "%s Couldn't find input file in boinc\\win_build, resolved name %s.\n", - boinc_msg_prefix(buf), input_path + boinc_msg_prefix(buf, sizeof(buf)), input_path ); getchar(); exit(-1); @@ -101,11 +103,13 @@ int main(int argc, char * argv[]) { retval = out.open(output_path, "wb"); if (retval) { - fprintf(stderr, "%s APP: matrix_inversion output open failed:\n", - boinc_msg_prefix(buf) + fprintf(stderr, + "%s APP: matrix_inversion output open failed:\n", + boinc_msg_prefix(buf, sizeof(buf)) ); - fprintf(stderr, "%s resolved name %s, retval %d\n", - boinc_msg_prefix(buf), output_path, retval + fprintf(stderr, + "%s resolved name %s, retval %d\n", + boinc_msg_prefix(buf, sizeof(buf)), output_path, retval ); perror("open"); exit(1); @@ -116,8 +120,9 @@ int main(int argc, char * argv[]) { // shmem = (UC_SHMEM*)boinc_graphics_make_shmem("matrix_inversion", sizeof(UC_SHMEM)); if (!shmem) { - fprintf(stderr, "%s failed to create shared mem segment\n", - boinc_msg_prefix(buf) + fprintf(stderr, + "%s failed to create shared mem segment\n", + boinc_msg_prefix(buf, sizeof(buf)) ); } update_shmem(); @@ -179,8 +184,9 @@ int main(int argc, char * argv[]) { //we'll need to write the current matrix to the state file. retval = do_checkpoint(out, i, input, matrixSize); if (retval) { - fprintf(stderr, "%s APP: matrix_inversion checkpoint failed %d\n", - boinc_msg_prefix(buf), retval + fprintf(stderr, + "%s APP: matrix_inversion checkpoint failed %d\n", + boinc_msg_prefix(buf, sizeof(buf)), retval ); exit(retval); } @@ -197,8 +203,9 @@ int main(int argc, char * argv[]) { retval = out.flush(); //force the output file to be closed. if (retval) { - fprintf(stderr, "%s APP: matrix_inversion flush failed %d\n", - boinc_msg_prefix(buf), retval + fprintf(stderr, + "%s APP: matrix_inversion flush failed %d\n", + boinc_msg_prefix(buf, sizeof(buf)), retval ); exit(1); } @@ -226,8 +233,9 @@ int main(int argc, char * argv[]) { if (boinc_time_to_checkpoint()) { retval = do_checkpoint(out, NUM_ITERATIONS, input, matrixSize); if (retval) { - fprintf(stderr, "%s APP: maxtrix_inversion checkpoint failed %d\n", - boinc_msg_prefix(buf), retval + fprintf(stderr, + "%s APP: maxtrix_inversion checkpoint failed %d\n", + boinc_msg_prefix(buf, sizeof(buf)), retval ); exit(1); } diff --git a/samples/wrapper/wrapper.cpp b/samples/wrapper/wrapper.cpp index 96713a47c8..fdd2db9acb 100644 --- a/samples/wrapper/wrapper.cpp +++ b/samples/wrapper/wrapper.cpp @@ -135,7 +135,7 @@ int TASK::parse(XML_PARSER& xp) { while (!xp.get(tag, sizeof(tag), is_tag)) { if (!is_tag) { fprintf(stderr, "%s TASK::parse(): unexpected text %s\n", - boinc_msg_prefix(buf), tag + boinc_msg_prefix(buf, sizeof(buf)), tag ); continue; } @@ -172,7 +172,10 @@ int parse_job_file() { boinc_resolve_filename(JOB_FILENAME, buf, 1024); FILE* f = boinc_fopen(buf, "r"); if (!f) { - fprintf(stderr, "%s can't open job file %s\n", boinc_msg_prefix(buf2), buf); + fprintf(stderr, + "%s can't open job file %s\n", + boinc_msg_prefix(buf2, sizeof(buf2)), buf + ); return ERR_FOPEN; } mf.init_file(f); @@ -181,8 +184,9 @@ int parse_job_file() { if (!xp.parse_start("job_desc")) return ERR_XML_PARSE; while (!xp.get(tag, sizeof(tag), is_tag)) { if (!is_tag) { - fprintf(stderr, "%s SCHED_CONFIG::parse(): unexpected text %s\n", - boinc_msg_prefix(buf2), tag + fprintf(stderr, + "%s SCHED_CONFIG::parse(): unexpected text %s\n", + boinc_msg_prefix(buf2, sizeof(buf2)), tag ); continue; } @@ -283,7 +287,7 @@ int TASK::run(int argct, char** argvt) { } fprintf(stderr, "%s wrapper: running %s (%s)\n", - boinc_msg_prefix(buf), app_path, command_line.c_str() + boinc_msg_prefix(buf, sizeof(buf)), app_path, command_line.c_str() ); #ifdef _WIN32