Replace some unsafe API calls with safe alternatives on Windows, prepare code for similar actions on other platforms - wrap unsafe API calls into macros.

llvm-svn: 233915
This commit is contained in:
Andrey Churbanov 2015-04-02 13:27:08 +00:00
parent 1362ae750f
commit 74bf17b8ff
22 changed files with 163 additions and 96 deletions

View File

@ -139,8 +139,8 @@ private:
data = new char[length = _length];
}
*(unsigned*)data = length;
memcpy(data + sizeof(unsigned), _data + sizeof(unsigned),
length - sizeof(unsigned));
KMP_MEMCPY(data + sizeof(unsigned), _data + sizeof(unsigned),
length - sizeof(unsigned));
makeDirectory();
}
public:
@ -192,7 +192,7 @@ public:
size_t l = str.size();
if(l > 8) {
directory.insert(make_pair(str, p - data));
memcpy(p, str.c_str(), l);
KMP_MEMCPY(p, str.c_str(), l);
p[l] = 0;
p += l + 1;
}
@ -211,7 +211,7 @@ public:
if(str.size() <= 8) {
// encoded directly
((char*)&r)[7] = 0;
strncpy((char*)&r, str.c_str(), 8);
KMP_STRNCPY_S((char*)&r, sizeof(r), str.c_str(), 8);
return r;
} else {
// represented as index into table

View File

@ -41,13 +41,13 @@ __kmp_affinity_print_mask(char *buf, int buf_len, kmp_affin_mask_t *mask)
}
}
if (i == KMP_CPU_SETSIZE) {
sprintf(scan, "{<empty>}");
KMP_SNPRINTF(scan, buf_len, "{<empty>}");
while (*scan != '\0') scan++;
KMP_ASSERT(scan <= end);
return buf;
}
sprintf(scan, "{%ld", (long)i);
KMP_SNPRINTF(scan, buf_len, "{%ld", (long)i);
while (*scan != '\0') scan++;
i++;
for (; i < KMP_CPU_SETSIZE; i++) {
@ -64,14 +64,14 @@ __kmp_affinity_print_mask(char *buf, int buf_len, kmp_affin_mask_t *mask)
if (end - scan < 15) {
break;
}
sprintf(scan, ",%-ld", (long)i);
KMP_SNPRINTF(scan, buf_len, ",%-ld", (long)i);
while (*scan != '\0') scan++;
}
if (i < KMP_CPU_SETSIZE) {
sprintf(scan, ",...");
KMP_SNPRINTF(scan, buf_len, ",...");
while (*scan != '\0') scan++;
}
sprintf(scan, "}");
KMP_SNPRINTF(scan, buf_len, "}");
while (*scan != '\0') scan++;
KMP_ASSERT(scan <= end);
return buf;
@ -1849,7 +1849,7 @@ __kmp_affinity_create_cpuinfo_map(AddrUnsPair **address2os, int *line,
// FIXME - this will match "node_<n> <garbage>"
//
unsigned level;
if (sscanf(buf, "node_%d id", &level) == 1) {
if (KMP_SSCANF(buf, "node_%d id", &level) == 1) {
if (nodeIdIndex + level >= maxIndex) {
maxIndex = nodeIdIndex + level;
}
@ -1968,17 +1968,17 @@ __kmp_affinity_create_cpuinfo_map(AddrUnsPair **address2os, int *line,
CHECK_LINE;
char *p = strchr(buf + sizeof(s1) - 1, ':');
unsigned val;
if ((p == NULL) || (sscanf(p + 1, "%u\n", &val) != 1)) goto no_val;
if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1)) goto no_val;
if (threadInfo[num_avail][osIdIndex] != UINT_MAX) goto dup_field;
threadInfo[num_avail][osIdIndex] = val;
#if KMP_OS_LINUX && USE_SYSFS_INFO
char path[256];
snprintf(path, sizeof(path),
KMP_SNPRINTF(path, sizeof(path),
"/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
threadInfo[num_avail][osIdIndex]);
__kmp_read_from_file(path, "%u", &threadInfo[num_avail][pkgIdIndex]);
snprintf(path, sizeof(path),
KMP_SNPRINTF(path, sizeof(path),
"/sys/devices/system/cpu/cpu%u/topology/core_id",
threadInfo[num_avail][osIdIndex]);
__kmp_read_from_file(path, "%u", &threadInfo[num_avail][coreIdIndex]);
@ -1990,7 +1990,7 @@ __kmp_affinity_create_cpuinfo_map(AddrUnsPair **address2os, int *line,
CHECK_LINE;
char *p = strchr(buf + sizeof(s2) - 1, ':');
unsigned val;
if ((p == NULL) || (sscanf(p + 1, "%u\n", &val) != 1)) goto no_val;
if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1)) goto no_val;
if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX) goto dup_field;
threadInfo[num_avail][pkgIdIndex] = val;
continue;
@ -2000,7 +2000,7 @@ __kmp_affinity_create_cpuinfo_map(AddrUnsPair **address2os, int *line,
CHECK_LINE;
char *p = strchr(buf + sizeof(s3) - 1, ':');
unsigned val;
if ((p == NULL) || (sscanf(p + 1, "%u\n", &val) != 1)) goto no_val;
if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1)) goto no_val;
if (threadInfo[num_avail][coreIdIndex] != UINT_MAX) goto dup_field;
threadInfo[num_avail][coreIdIndex] = val;
continue;
@ -2011,17 +2011,17 @@ __kmp_affinity_create_cpuinfo_map(AddrUnsPair **address2os, int *line,
CHECK_LINE;
char *p = strchr(buf + sizeof(s4) - 1, ':');
unsigned val;
if ((p == NULL) || (sscanf(p + 1, "%u\n", &val) != 1)) goto no_val;
if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1)) goto no_val;
if (threadInfo[num_avail][threadIdIndex] != UINT_MAX) goto dup_field;
threadInfo[num_avail][threadIdIndex] = val;
continue;
}
unsigned level;
if (sscanf(buf, "node_%d id", &level) == 1) {
if (KMP_SSCANF(buf, "node_%d id", &level) == 1) {
CHECK_LINE;
char *p = strchr(buf + sizeof(s4) - 1, ':');
unsigned val;
if ((p == NULL) || (sscanf(p + 1, "%u\n", &val) != 1)) goto no_val;
if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1)) goto no_val;
KMP_ASSERT(nodeIdIndex + level <= maxIndex);
if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX) goto dup_field;
threadInfo[num_avail][nodeIdIndex + level] = val;
@ -2591,7 +2591,7 @@ __kmp_create_masks(unsigned *maxIndex, unsigned *numUnique,
unsigned leader = 0;
Address *leaderAddr = &(address2os[0].first);
kmp_affin_mask_t *sum
= (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
= (kmp_affin_mask_t *)KMP_ALLOCA(__kmp_affin_mask_size);
KMP_CPU_ZERO(sum);
KMP_CPU_SET(address2os[0].second, sum);
for (i = 1; i < numAddrs; i++) {
@ -2913,7 +2913,7 @@ __kmp_affinity_process_proclist(kmp_affin_mask_t **out_masks,
}
*out_masks
= (kmp_affin_mask_t *)__kmp_allocate(nextNewMask * __kmp_affin_mask_size);
memcpy(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
KMP_MEMCPY(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
__kmp_free(sumMask);
KMP_INTERNAL_FREE(newMasks);
}
@ -3319,7 +3319,7 @@ __kmp_affinity_process_placelist(kmp_affin_mask_t **out_masks,
}
*out_masks
= (kmp_affin_mask_t *)__kmp_allocate(nextNewMask * __kmp_affin_mask_size);
memcpy(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
KMP_MEMCPY(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
__kmp_free(tempMask);
KMP_INTERNAL_FREE(newMasks);
}
@ -4398,7 +4398,7 @@ void __kmp_balanced_affinity( int tid, int nthreads )
KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
"Illegal set affinity operation when not capable");
kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
kmp_affin_mask_t *mask = (kmp_affin_mask_t *)KMP_ALLOCA(__kmp_affin_mask_size);
KMP_CPU_ZERO(mask);
// Granularity == thread
@ -4421,7 +4421,7 @@ void __kmp_balanced_affinity( int tid, int nthreads )
__kmp_set_system_affinity( mask, TRUE );
} else { // Non-uniform topology
kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
kmp_affin_mask_t *mask = (kmp_affin_mask_t *)KMP_ALLOCA(__kmp_affin_mask_size);
KMP_CPU_ZERO(mask);
// Number of hyper threads per core in HT machine

View File

@ -785,7 +785,7 @@ bgetr( kmp_info_t *th, void *buf, bufsize size)
KMP_DEBUG_ASSERT(osize > 0);
(void) memcpy((char *) nbuf, (char *) buf, /* Copy the data */
(void) KMP_MEMCPY((char *) nbuf, (char *) buf, /* Copy the data */
(size_t) ((size < osize) ? size : osize));
brel( th, buf );
@ -1178,7 +1178,7 @@ bufdump( kmp_info_t *th, void *buf )
}
for (i = 0; i < l; i++) {
(void) sprintf(bhex + i * 3, "%02X ", bdump[i]);
(void) KMP_SNPRINTF(bhex + i * 3, sizeof(bhex), "%02X ", bdump[i]);
if (bdump[i] > 0x20 && bdump[i] < 0x7F)
bascii[ i ] = bdump[ i ];
else

View File

@ -199,7 +199,7 @@ __kmpc_ok_to_fork(ident_t *loc)
return __kmp_par_range < 0;
}
}
if (sscanf(semi3 + 1, "%d", &line_no) == 1) {
if (KMP_SSCANF(semi3 + 1, "%d", &line_no) == 1) {
if ((line_no >= __kmp_par_range_lb) && (line_no <= __kmp_par_range_ub)) {
return __kmp_par_range > 0;
}
@ -1396,7 +1396,7 @@ void
kmpc_set_defaults( char const * str )
{
// __kmp_aux_set_defaults initializes the library if needed
__kmp_aux_set_defaults( str, strlen( str ) );
__kmp_aux_set_defaults( str, KMP_STRLEN( str ) );
}
int

View File

@ -102,12 +102,12 @@ __kmp_env_get( char const * name ) {
#if KMP_OS_UNIX
char const * value = getenv( name );
if ( value != NULL ) {
size_t len = strlen( value ) + 1;
size_t len = KMP_STRLEN( value ) + 1;
result = (char *) KMP_INTERNAL_MALLOC( len );
if ( result == NULL ) {
KMP_FATAL( MemoryAllocFailed );
}; // if
strncpy( result, value, len );
KMP_STRNCPY_S( result, len, value, len );
}; // if
#elif KMP_OS_WINDOWS
/*
@ -393,19 +393,19 @@ ___kmp_env_blk_parse_windows(
int len; // Length of variable.
count = 0;
var = env; // The first variable starts and beginning of environment block.
len = strlen( var );
len = KMP_STRLEN( var );
while ( len != 0 ) {
++ count;
size = size + len + 1;
var = var + len + 1; // Move pointer to the beginning of the next variable.
len = strlen( var );
len = KMP_STRLEN( var );
}; // while
size = size + 1; // Total size of env block, including terminating zero byte.
}
// Copy original block to bulk, we will modify bulk, not original block.
bulk = (char *) allocate( size );
memcpy( bulk, env, size );
KMP_MEMCPY_S( bulk, size, env, size );
// Allocate vars array.
vars = (kmp_env_var_t *) allocate( count * sizeof( kmp_env_var_t ) );
@ -415,7 +415,7 @@ ___kmp_env_blk_parse_windows(
int len; // Length of variable.
count = 0;
var = bulk;
len = strlen( var );
len = KMP_STRLEN( var );
while ( len != 0 ) {
// Save variable in vars array.
__kmp_str_split( var, '=', & name, & value );
@ -424,7 +424,7 @@ ___kmp_env_blk_parse_windows(
++ count;
// Get the next var.
var = var + len + 1;
len = strlen( var );
len = KMP_STRLEN( var );
}; // while
}
@ -462,7 +462,7 @@ ___kmp_env_blk_parse_unix(
count = 0;
size = 0;
while ( env[ count ] != NULL ) {
size += strlen( env[ count ] ) + 1;
size += KMP_STRLEN( env[ count ] ) + 1;
++ count;
}; // while
}
@ -481,8 +481,8 @@ ___kmp_env_blk_parse_unix(
var = bulk;
for ( i = 0; i < count; ++ i ) {
// Copy variable to bulk.
len = strlen( env[ i ] );
memcpy( var, env[ i ], len + 1 );
len = KMP_STRLEN( env[ i ] );
KMP_MEMCPY_S( var, size, env[ i ], len + 1 );
// Save found variable in vars array.
__kmp_str_split( var, '=', & name, & value );
vars[ i ].name = name;

View File

@ -1046,7 +1046,7 @@ FTN_SET_DEFAULTS( char const * str
{
#ifndef KMP_STUB
#ifdef PASS_ARGS_BY_VALUE
int len = (int)strlen( str );
int len = (int)KMP_STRLEN( str );
#endif
__kmp_aux_set_defaults( str, len );
#endif

View File

@ -699,7 +699,7 @@ xexpand(KMP_API_NAME_GOMP_TASK)(void (*func)(void *), void *data, void (*copy_fu
(*copy_func)(task->shareds, data);
}
else {
memcpy(task->shareds, data, arg_size);
KMP_MEMCPY(task->shareds, data, arg_size);
}
}

View File

@ -864,7 +864,7 @@ __kmp_msg_error_code(
msg.type = kmp_mt_syserr;
msg.num = code;
msg.str = sys_error( code );
msg.len = strlen( msg.str );
msg.len = KMP_STRLEN( msg.str );
return msg;
} // __kmp_msg_error_code
@ -880,7 +880,7 @@ __kmp_msg_error_mesg(
msg.type = kmp_mt_syserr;
msg.num = 0;
msg.str = __kmp_str_format( "%s", mesg );
msg.len = strlen( msg.str );
msg.len = KMP_STRLEN( msg.str );
return msg;
} // __kmp_msg_error_mesg

View File

@ -1,5 +1,5 @@
/*
* kmp_io.c -- RTL IO
* KMP_IO.c -- RTL IO
*/
@ -169,9 +169,9 @@ __kmp_vprintf( enum kmp_io __kmp_io, char const * format, va_list ap )
int chars = 0;
#ifdef KMP_DEBUG_PIDS
chars = sprintf( db, "pid=%d: ", (kmp_int32)getpid() );
chars = KMP_SNPRINTF( db, __kmp_debug_buf_chars, "pid=%d: ", (kmp_int32)getpid() );
#endif
chars += vsprintf( db, format, ap );
chars += KMP_VSNPRINTF( db, __kmp_debug_buf_chars, format, ap );
if ( chars + 1 > __kmp_debug_buf_chars ) {
if ( chars + 1 > __kmp_debug_buf_warn_chars ) {

View File

@ -1130,9 +1130,9 @@ __kmp_set_ticket_lock_flags( kmp_ticket_lock_t *lck, kmp_lock_flags_t flags )
#define TRACE_BUF_ELE 1024
static char traces[TRACE_BUF_ELE][128] = { 0 }
static int tc = 0;
#define TRACE_LOCK(X,Y) sprintf( traces[tc++ % TRACE_BUF_ELE], "t%d at %s\n", X, Y );
#define TRACE_LOCK_T(X,Y,Z) sprintf( traces[tc++ % TRACE_BUF_ELE], "t%d at %s%d\n", X,Y,Z );
#define TRACE_LOCK_HT(X,Y,Z,Q) sprintf( traces[tc++ % TRACE_BUF_ELE], "t%d at %s %d,%d\n", X, Y, Z, Q );
#define TRACE_LOCK(X,Y) KMP_SNPRINTF( traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s\n", X, Y );
#define TRACE_LOCK_T(X,Y,Z) KMP_SNPRINTF( traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s%d\n", X,Y,Z );
#define TRACE_LOCK_HT(X,Y,Z,Q) KMP_SNPRINTF( traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s %d,%d\n", X, Y, Z, Q );
static void
__kmp_dump_queuing_lock( kmp_info_t *this_thr, kmp_int32 gtid,
@ -2035,9 +2035,9 @@ FILE * __kmp_open_stats_file()
if (strcmp (__kmp_speculative_statsfile, "-") == 0)
return stdout;
size_t buffLen = strlen( __kmp_speculative_statsfile ) + 20;
size_t buffLen = KMP_STRLEN( __kmp_speculative_statsfile ) + 20;
char buffer[buffLen];
snprintf (&buffer[0], buffLen, __kmp_speculative_statsfile,
KMP_SNPRINTF (&buffer[0], buffLen, __kmp_speculative_statsfile,
(kmp_int32)getpid());
FILE * result = fopen(&buffer[0], "w");
@ -3147,7 +3147,7 @@ __kmp_insert_indirect_lock(kmp_indirect_lock_t *lck)
kmp_lock_index_t size = __kmp_indirect_lock_table_size;
kmp_indirect_lock_t **old_table = __kmp_indirect_lock_table;
__kmp_indirect_lock_table = (kmp_indirect_lock_t **)__kmp_allocate(2*next*sizeof(kmp_indirect_lock_t *));
memcpy(__kmp_indirect_lock_table, old_table, next*sizeof(kmp_indirect_lock_t *));
KMP_MEMCPY(__kmp_indirect_lock_table, old_table, next*sizeof(kmp_indirect_lock_t *));
__kmp_free(old_table);
__kmp_indirect_lock_table_size = 2*next;
}
@ -3826,7 +3826,7 @@ __kmp_lock_table_insert( kmp_user_lock_p lck )
size = __kmp_user_lock_table.allocated * 2;
}
table = (kmp_user_lock_p *)__kmp_allocate( sizeof( kmp_user_lock_p ) * size );
memcpy( table + 1, __kmp_user_lock_table.table + 1, sizeof( kmp_user_lock_p ) * ( __kmp_user_lock_table.used - 1 ) );
KMP_MEMCPY( table + 1, __kmp_user_lock_table.table + 1, sizeof( kmp_user_lock_p ) * ( __kmp_user_lock_table.used - 1 ) );
table[ 0 ] = (kmp_user_lock_p)__kmp_user_lock_table.table;
// We cannot free the previos table now, sinse it may be in use by other
// threads. So save the pointer to the previous table in in the first element of the

View File

@ -850,3 +850,6 @@ enum kmp_warnings_level {
#endif // __cplusplus
#endif /* KMP_OS_H */
// Safe C API
#include "kmp_safe_c_api.h"

View File

@ -325,7 +325,7 @@ __kmp_print_storage_map_gtid( int gtid, void *p1, void *p2, size_t size, char co
va_list ap;
va_start( ap, format);
sprintf( buffer, "OMP storage map: %p %p%8lu %s\n", p1, p2, (unsigned long) size, format );
KMP_SNPRINTF( buffer, sizeof(buffer), "OMP storage map: %p %p%8lu %s\n", p1, p2, (unsigned long) size, format );
__kmp_acquire_bootstrap_lock( & __kmp_stdio_lock );
__kmp_vprintf( kmp_err, buffer, ap );
#if KMP_PRINT_DATA_PLACEMENT
@ -387,7 +387,7 @@ __kmp_warn( char const * format, ... )
va_start( ap, format );
snprintf( buffer, sizeof(buffer) , "OMP warning: %s\n", format );
KMP_SNPRINTF( buffer, sizeof(buffer) , "OMP warning: %s\n", format );
__kmp_acquire_bootstrap_lock( & __kmp_stdio_lock );
__kmp_vprintf( kmp_err, buffer, ap );
__kmp_release_bootstrap_lock( & __kmp_stdio_lock );
@ -1458,7 +1458,7 @@ __kmp_fork_call(
if ( __kmp_stkpadding > 0 && __kmp_root[gtid] != NULL ) {
/* Some systems prefer the stack for the root thread(s) to start with */
/* some gap from the parent stack to prevent false sharing. */
void *dummy = alloca(__kmp_stkpadding);
void *dummy = KMP_ALLOCA(__kmp_stkpadding);
/* These 2 lines below are so this does not get optimized out */
if ( __kmp_stkpadding > KMP_MAX_STKPADDING )
__kmp_stkpadding += (short)((kmp_int64)dummy);
@ -1604,7 +1604,7 @@ __kmp_fork_call(
#if KMP_OS_LINUX && ( KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
void * args[ argc ];
#else
void * * args = (void**) alloca( argc * sizeof( void * ) );
void * * args = (void**) KMP_ALLOCA( argc * sizeof( void * ) );
#endif /* KMP_OS_LINUX && ( KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) */
__kmp_release_bootstrap_lock( &__kmp_forkjoin_lock );
@ -2745,7 +2745,7 @@ __kmp_reallocate_team_arrays(kmp_team_t *team, int max_nth) {
#endif
__kmp_allocate_team_arrays(team, max_nth);
memcpy(team->t.t_threads, oldThreads, team->t.t_nproc * sizeof (kmp_info_t*));
KMP_MEMCPY(team->t.t_threads, oldThreads, team->t.t_nproc * sizeof (kmp_info_t*));
__kmp_free(oldThreads);
}
@ -3257,8 +3257,8 @@ __kmp_expand_threads(int nWish, int nNeed) {
} while(newCapacity < minimumRequiredCapacity);
newThreads = (kmp_info_t**) __kmp_allocate((sizeof(kmp_info_t*) + sizeof(kmp_root_t*)) * newCapacity + CACHE_LINE);
newRoot = (kmp_root_t**) ((char*)newThreads + sizeof(kmp_info_t*) * newCapacity );
memcpy(newThreads, __kmp_threads, __kmp_threads_capacity * sizeof(kmp_info_t*));
memcpy(newRoot, __kmp_root, __kmp_threads_capacity * sizeof(kmp_root_t*));
KMP_MEMCPY(newThreads, __kmp_threads, __kmp_threads_capacity * sizeof(kmp_info_t*));
KMP_MEMCPY(newRoot, __kmp_root, __kmp_threads_capacity * sizeof(kmp_root_t*));
memset(newThreads + __kmp_threads_capacity, 0,
(newCapacity - __kmp_threads_capacity) * sizeof(kmp_info_t*));
memset(newRoot + __kmp_threads_capacity, 0,
@ -5783,8 +5783,8 @@ __kmp_register_library_startup(
if ( tail != NULL ) {
long * flag_addr = 0;
long flag_val = 0;
sscanf( flag_addr_str, "%p", & flag_addr );
sscanf( flag_val_str, "%lx", & flag_val );
KMP_SSCANF( flag_addr_str, "%p", & flag_addr );
KMP_SSCANF( flag_val_str, "%lx", & flag_val );
if ( flag_addr != 0 && flag_val != 0 && strcmp( file_name, "" ) != 0 ) {
// First, check whether environment-encoded address is mapped into addr space.
// If so, dereference it to see if it still has the right value.

View File

@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.txt for details.
//
//===----------------------------------------------------------------------===//
#ifndef KMP_SAFE_C_API_H
#define KMP_SAFE_C_API_H
//
// Replacement for banned C API
//
// Not every unsafe call listed here is handled now, but keeping everything
// in one place should be handy for future maintenance.
#if KMP_OS_WINDOWS
# define RSIZE_MAX_STR ( 4UL << 10 ) // 4KB
// _malloca was suggested, but it is not a drop-in replacement for _alloca
// TODO: test performance and replace with _alloca (as below)
# define KMP_ALLOCA alloca
//# define KMP_ALLOCA _alloca
# define KMP_MEMCPY_S memcpy_s
# define KMP_SNPRINTF sprintf_s
# define KMP_SSCANF sscanf_s
# define KMP_STRCPY_S strcpy_s
# define KMP_STRNCPY_S strncpy_s
// Use this only when buffer size is unknown
# define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
# define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR)
// Use this only when buffer size is unknown
# define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt)
// _TRUNCATE insures buffer size > max string to print.
# define KMP_VSNPRINTF(dst, cnt, fmt, arg) vsnprintf_s(dst, cnt, _TRUNCATE, fmt, arg)
#else // KMP_OS_WINDOWS
// For now, these macros use the existing API.
# define KMP_ALLOCA alloca
# define KMP_MEMCPY_S(dst, bsz, src, cnt) memcpy(dst, src, cnt)
# define KMP_SNPRINTF snprintf
# define KMP_SSCANF sscanf
# define KMP_STRCPY_S(dst, bsz, src) strcpy(dst, src)
# define KMP_STRNCPY_S(dst, bsz, src, cnt) strncpy(dst, src, cnt)
# define KMP_VSNPRINTF vsnprintf
# define KMP_STRNCPY strncpy
# define KMP_STRLEN strlen
# define KMP_MEMCPY memcpy
#endif // KMP_OS_WINDOWS
#endif // KMP_SAFE_C_API_H

View File

@ -37,7 +37,7 @@ __kmp_convert_to_double( char const * s )
{
double result;
if ( sscanf( s, "%lf", &result ) < 1 ) {
if ( KMP_SSCANF( s, "%lf", &result ) < 1 ) {
result = 0.0;
}
@ -171,7 +171,7 @@ __kmp_convert_to_seconds( char const * data )
if (data == NULL) return (0);
value = 0;
mult = '\0';
nvalues = sscanf (data, "%d%c%c", &value, &mult, &extra);
nvalues = KMP_SSCANF (data, "%d%c%c", &value, &mult, &extra);
if (nvalues < 1) return (0);
if (nvalues == 1) mult = '\0';
if (nvalues == 3) return (-1);
@ -221,7 +221,7 @@ __kmp_convert_to_milliseconds( char const * data )
if ( __kmp_str_match( "infinit", -1, data)) return (INT_MAX);
value = (double) 0.0;
mult = '\0';
nvalues = sscanf (data, "%lf%c%c", &value, &mult, &extra);
nvalues = KMP_SSCANF (data, "%lf%c%c", &value, &mult, &extra);
if (nvalues < 1) return (-1);
if (nvalues == 1) mult = '\0';
if (nvalues == 3) return (-1);
@ -272,7 +272,7 @@ __kmp_convert_to_nanoseconds( // R: Time in nanoseconds, or ~0 in case o
if ( str == NULL || str[ 0 ] == 0 ) { // No string or empty string.
return 0; // Default value.
}; // if
rc = sscanf( str, "%lf%c%c", &value, &unit, &extra );
rc = KMP_SSCANF( str, "%lf%c%c", &value, &unit, &extra );
switch ( rc ) {
case 0: { // Value is not parsed.
return ~ 0;
@ -545,9 +545,9 @@ __kmp_stg_parse_par_range(
int * out_lb,
int * out_ub
) {
size_t len = strlen( value + 1 );
size_t len = KMP_STRLEN( value + 1 );
par_range_to_print = (char *) KMP_INTERNAL_MALLOC( len +1 );
strncpy( par_range_to_print, value, len + 1);
KMP_STRNCPY_S( par_range_to_print, len + 1, value, len + 1);
__kmp_par_range = +1;
__kmp_par_range_lb = 0;
__kmp_par_range_ub = INT_MAX;
@ -585,7 +585,7 @@ __kmp_stg_parse_par_range(
if (( ! __kmp_strcasecmp_with_sentinel( "range", value, '=' ))
|| ( ! __kmp_strcasecmp_with_sentinel( "incl_range", value, '=' ))) {
value = strchr( value, '=' ) + 1;
if ( sscanf( value, "%d:%d", out_lb, out_ub ) != 2 ) {
if ( KMP_SSCANF( value, "%d:%d", out_lb, out_ub ) != 2 ) {
goto par_range_error;
}
*out_range = +1;
@ -597,7 +597,7 @@ __kmp_stg_parse_par_range(
}
if ( ! __kmp_strcasecmp_with_sentinel( "excl_range", value, '=' )) {
value = strchr( value, '=' ) + 1;
if ( sscanf( value, "%d:%d", out_lb, out_ub) != 2 ) {
if ( KMP_SSCANF( value, "%d:%d", out_lb, out_ub) != 2 ) {
goto par_range_error;
}
*out_range = -1;
@ -1972,7 +1972,7 @@ __kmp_parse_affinity_proc_id_list( const char *var, const char *env,
{
int len = next - env;
char *retlist = (char *)__kmp_allocate((len + 1) * sizeof(char));
memcpy(retlist, env, len * sizeof(char));
KMP_MEMCPY_S(retlist, (len+1)*sizeof(char), env, len * sizeof(char));
retlist[len] = '\0';
*proclist = retlist;
}
@ -2833,7 +2833,7 @@ __kmp_parse_place_list( const char *var, const char *env, char **place_list )
{
int len = scan - env;
char *retlist = (char *)__kmp_allocate((len + 1) * sizeof(char));
memcpy(retlist, env, len * sizeof(char));
KMP_MEMCPY_S(retlist, (len+1)*sizeof(char), env, len * sizeof(char));
retlist[len] = '\0';
*place_list = retlist;
}
@ -3507,7 +3507,7 @@ static void
__kmp_stg_parse_schedule( char const * name, char const * value, void * data ) {
if ( value != NULL ) {
size_t length = strlen( value );
size_t length = KMP_STRLEN( value );
if ( length > INT_MAX ) {
KMP_WARNING( LongValue, name );
} else {
@ -3581,7 +3581,7 @@ __kmp_stg_parse_omp_schedule( char const * name, char const * value, void * data
{
size_t length;
if( value ) {
length = strlen( value );
length = KMP_STRLEN( value );
if( length ) {
char *comma = (char *) strchr( value, ',' );
if( value[ length - 1 ] == '"' || value[ length -1 ] == '\'')
@ -5315,7 +5315,7 @@ __kmp_env_print() {
char const * name = block.vars[ i ].name;
char const * value = block.vars[ i ].value;
if (
( strlen( name ) > 4 && strncmp( name, "KMP_", 4 ) == 0 )
( KMP_STRLEN( name ) > 4 && strncmp( name, "KMP_", 4 ) == 0 )
|| strncmp( name, "OMP_", 4 ) == 0
#ifdef KMP_GOMP_COMPAT
|| strncmp( name, "GOMP_", 5 ) == 0

View File

@ -54,7 +54,7 @@ double tsc_tick_count::tick_time()
for (;*start == ' '; start++)
;
char * end = brand + strlen(brand) - 3;
char * end = brand + KMP_STRLEN(brand) - 3;
uint64_t multiplier;
if (*end == 'M') multiplier = 1000LL*1000LL;

View File

@ -112,7 +112,7 @@ __kmp_str_buf_reserve(
if ( buffer->str == NULL ) {
KMP_FATAL( MemoryAllocFailed );
}; // if
memcpy( buffer->str, buffer->bulk, buffer->used + 1 );
KMP_MEMCPY_S( buffer->str, buffer->size, buffer->bulk, buffer->used + 1 );
} else {
buffer->str = (char *) KMP_INTERNAL_REALLOC( buffer->str, buffer->size );
if ( buffer->str == NULL ) {
@ -142,7 +142,7 @@ __kmp_str_buf_detach(
if ( buffer->str == NULL ) {
KMP_FATAL( MemoryAllocFailed );
}; // if
memcpy( buffer->str, buffer->bulk, buffer->used + 1 );
KMP_MEMCPY_S( buffer->str, buffer->size, buffer->bulk, buffer->used + 1 );
}; // if
} // __kmp_str_buf_detach
@ -173,7 +173,7 @@ __kmp_str_buf_cat(
KMP_DEBUG_ASSERT( str != NULL );
KMP_DEBUG_ASSERT( len >= 0 );
__kmp_str_buf_reserve( buffer, buffer->used + len + 1 );
memcpy( buffer->str + buffer->used, str, len );
KMP_MEMCPY( buffer->str + buffer->used, str, len );
buffer->str[ buffer->used + len ] = 0;
buffer->used += len;
KMP_STR_BUF_INVARIANT( buffer );
@ -211,7 +211,7 @@ __kmp_str_buf_vprint(
__va_copy( _args, args ); // Make copy of args.
#define args _args // Substitute args with its copy, _args.
#endif // KMP_OS_WINDOWS
rc = vsnprintf( buffer->str + buffer->used, free, format, args );
rc = KMP_VSNPRINTF( buffer->str + buffer->used, free, format, args );
#if ! KMP_OS_WINDOWS
#undef args // Remove substitution.
va_end( _args );
@ -502,7 +502,7 @@ __kmp_str_format( // Allocated string.
// Try to format string.
va_start( args, format );
rc = vsnprintf( buffer, size, format, args );
rc = KMP_VSNPRINTF( buffer, size, format, args );
va_end( args );
// No errors, string has been formatted.

View File

@ -27,7 +27,6 @@
#if KMP_OS_WINDOWS
# define strdup _strdup
# define snprintf _snprintf
#endif
/* some macros to replace ctype.h functions */

View File

@ -1978,8 +1978,9 @@ __kmp_realloc_task_threads_data( kmp_info_t *thread, kmp_task_team_t *task_team
new_data = (kmp_thread_data_t *)
__kmp_allocate( nthreads * sizeof(kmp_thread_data_t) );
// copy old data to new data
memcpy( (void *) new_data, (void *) old_data,
maxthreads * sizeof(kmp_taskdata_t *) );
KMP_MEMCPY_S( (void *) new_data, nthreads * sizeof(kmp_thread_data_t),
(void *) old_data,
maxthreads * sizeof(kmp_taskdata_t *) );
#ifdef BUILD_TIED_TASK_STACK
// GEH: Figure out if this is the right thing to do

View File

@ -114,7 +114,7 @@ __kmp_init_common_data( void *pc_addr, size_t pc_size )
for (i = pc_size; i > 0; --i) {
if (*p++ != '\0') {
d->data = __kmp_allocate( pc_size );
memcpy( d->data, pc_addr, pc_size );
KMP_MEMCPY( d->data, pc_addr, pc_size );
break;
}
}
@ -137,7 +137,7 @@ __kmp_copy_common_data( void *pc_addr, struct private_data *d )
if (d->data == 0)
memset( & addr[ offset ], '\0', d->size );
else
memcpy( & addr[ offset ], d->data, d->size );
KMP_MEMCPY( & addr[ offset ], d->data, d->size );
offset += d->size;
}
}

View File

@ -314,12 +314,12 @@ __kmp_expand_host_name( char *buffer, size_t size )
DWORD s = size;
if (! GetComputerNameA( buffer, & s ))
strcpy( buffer, unknown );
KMP_STRCPY_S( buffer, size, unknown );
}
#else
buffer[size - 2] = 0;
if (gethostname( buffer, size ) || buffer[size - 2] != 0)
strcpy( buffer, unknown );
KMP_STRCPY_S( buffer, size, unknown );
#endif
}
@ -374,7 +374,7 @@ __kmp_expand_file_name( char *result, size_t rlen, char *pattern )
case 'h':
{
__kmp_expand_host_name( buffer, sizeof( buffer ) );
strncpy( pos, buffer, end - pos + 1);
KMP_STRNCPY( pos, buffer, end - pos + 1);
if(*end == 0) {
while ( *pos )
++pos;
@ -386,7 +386,7 @@ __kmp_expand_file_name( char *result, size_t rlen, char *pattern )
case 'P':
case 'p':
{
snp_result = snprintf( pos, end - pos + 1, "%0*d", cpu_width, __kmp_dflt_team_nth );
snp_result = KMP_SNPRINTF( pos, end - pos + 1, "%0*d", cpu_width, __kmp_dflt_team_nth );
if(snp_result >= 0 && snp_result <= end - pos) {
while ( *pos )
++pos;
@ -399,7 +399,7 @@ __kmp_expand_file_name( char *result, size_t rlen, char *pattern )
case 'i':
{
pid_t id = getpid();
snp_result = snprintf( pos, end - pos + 1, "%0*d", width, id );
snp_result = KMP_SNPRINTF( pos, end - pos + 1, "%0*d", width, id );
if(snp_result >= 0 && snp_result <= end - pos) {
while ( *pos )
++pos;

View File

@ -101,7 +101,7 @@ static kmp_mutex_align_t __kmp_wait_mx;
static void
__kmp_print_cond( char *buffer, kmp_cond_align_t *cond )
{
sprintf( buffer, "(cond (lock (%ld, %d)), (descr (%p)))",
KMP_SNPRINTF( buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
cond->c_cond.__c_waiting );
}
@ -227,7 +227,7 @@ __kmp_affinity_bind_thread( int which )
KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
"Illegal set affinity operation when not capable");
kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
kmp_affin_mask_t *mask = (kmp_affin_mask_t *)KMP_ALLOCA(__kmp_affin_mask_size);
KMP_CPU_ZERO(mask);
KMP_CPU_SET(which, mask);
__kmp_set_system_affinity(mask, TRUE);
@ -732,7 +732,7 @@ __kmp_launch_worker( void *thr )
#if KMP_OS_LINUX || KMP_OS_FREEBSD
if ( __kmp_stkoffset > 0 && gtid > 0 ) {
padding = alloca( gtid * __kmp_stkoffset );
padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
}
#endif
@ -2300,7 +2300,7 @@ __kmp_is_address_mapped( void * addr ) {
if ( rc == EOF ) {
break;
}; // if
KMP_ASSERT( rc == 3 && strlen( perms ) == 4 ); // Make sure all fields are read.
KMP_ASSERT( rc == 3 && KMP_STRLEN( perms ) == 4 ); // Make sure all fields are read.
// Ending address is not included in the region, but beginning is.
if ( ( addr >= beginning ) && ( addr < ending ) ) {
@ -2475,7 +2475,7 @@ __kmp_get_load_balance( int max )
// Construct task_path.
task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
__kmp_str_buf_cat( & task_path, proc_entry->d_name, strlen( proc_entry->d_name ) );
__kmp_str_buf_cat( & task_path, proc_entry->d_name, KMP_STRLEN( proc_entry->d_name ) );
__kmp_str_buf_cat( & task_path, "/task", 5 );
task_dir = opendir( task_path.str );
@ -2510,7 +2510,7 @@ __kmp_get_load_balance( int max )
// __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, task_entry->d_name );
// but seriae of __kmp_str_buf_cat works a bit faster.
stat_path.used = stat_path_fixed_len; // Reset stat path to its fixed part.
__kmp_str_buf_cat( & stat_path, task_entry->d_name, strlen( task_entry->d_name ) );
__kmp_str_buf_cat( & stat_path, task_entry->d_name, KMP_STRLEN( task_entry->d_name ) );
__kmp_str_buf_cat( & stat_path, "/stat", 5 );
// Note: Low-level API (open/read/close) is used. High-level API

View File

@ -1190,7 +1190,7 @@ __kmp_launch_worker( void *arg )
#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
if ( __kmp_stkoffset > 0 && gtid > 0 ) {
padding = _alloca( gtid * __kmp_stkoffset );
padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
}
KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );