diff --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h --- a/openmp/runtime/src/kmp.h +++ b/openmp/runtime/src/kmp.h @@ -3078,6 +3078,11 @@ return __kmp_threads[gtid]->th.th_team; } +static inline void __kmp_assert_valid_gtid(kmp_int32 gtid) { + if (UNLIKELY(gtid < 0 || gtid >= __kmp_threads_capacity)) + KMP_FATAL(ThreadIdentInvalid); +} + /* ------------------------------------------------------------------------- */ extern kmp_global_t __kmp_global; /* global status */ diff --git a/openmp/runtime/src/kmp_csupport.cpp b/openmp/runtime/src/kmp_csupport.cpp --- a/openmp/runtime/src/kmp_csupport.cpp +++ b/openmp/runtime/src/kmp_csupport.cpp @@ -231,13 +231,12 @@ kmp_int32 num_threads) { KA_TRACE(20, ("__kmpc_push_num_threads: enter T#%d num_threads=%d\n", global_tid, num_threads)); - + __kmp_assert_valid_gtid(global_tid); __kmp_push_num_threads(loc, global_tid, num_threads); } void __kmpc_pop_num_threads(ident_t *loc, kmp_int32 global_tid) { KA_TRACE(20, ("__kmpc_pop_num_threads: enter\n")); - /* the num_threads are automatically popped */ } @@ -245,7 +244,7 @@ kmp_int32 proc_bind) { KA_TRACE(20, ("__kmpc_push_proc_bind: enter T#%d proc_bind=%d\n", global_tid, proc_bind)); - + __kmp_assert_valid_gtid(global_tid); __kmp_push_proc_bind(loc, global_tid, (kmp_proc_bind_t)proc_bind); } @@ -353,7 +352,7 @@ KA_TRACE(20, ("__kmpc_push_num_teams: enter T#%d num_teams=%d num_threads=%d\n", global_tid, num_teams, num_threads)); - + __kmp_assert_valid_gtid(global_tid); __kmp_push_num_teams(loc, global_tid, num_teams, num_threads); } @@ -474,9 +473,10 @@ when the condition is false. */ void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 global_tid) { -// The implementation is now in kmp_runtime.cpp so that it can share static -// functions with kmp_fork_call since the tasks to be done are similar in -// each case. + // The implementation is now in kmp_runtime.cpp so that it can share static + // functions with kmp_fork_call since the tasks to be done are similar in + // each case. + __kmp_assert_valid_gtid(global_tid); #if OMPT_SUPPORT OMPT_STORE_RETURN_ADDRESS(global_tid); #endif @@ -504,6 +504,7 @@ return; // Not autopar code + __kmp_assert_valid_gtid(global_tid); if (!TCR_4(__kmp_init_parallel)) __kmp_parallel_initialize(); @@ -713,6 +714,7 @@ void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid) { KMP_COUNT_BLOCK(OMP_BARRIER); KC_TRACE(10, ("__kmpc_barrier: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); if (!TCR_4(__kmp_init_parallel)) __kmp_parallel_initialize(); @@ -762,6 +764,7 @@ int status = 0; KC_TRACE(10, ("__kmpc_master: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); if (!TCR_4(__kmp_init_parallel)) __kmp_parallel_initialize(); @@ -816,7 +819,7 @@ */ void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid) { KC_TRACE(10, ("__kmpc_end_master: called T#%d\n", global_tid)); - + __kmp_assert_valid_gtid(global_tid); KMP_DEBUG_ASSERT(KMP_MASTER_GTID(global_tid)); KMP_POP_PARTITIONED_TIMER(); @@ -833,9 +836,6 @@ #endif if (__kmp_env_consistency_check) { - if (global_tid < 0) - KMP_WARNING(ThreadIdentInvalid); - if (KMP_MASTER_GTID(global_tid)) __kmp_pop_sync(global_tid, ct_master, loc); } @@ -854,6 +854,7 @@ KMP_DEBUG_ASSERT(__kmp_init_serial); KC_TRACE(10, ("__kmpc_ordered: called T#%d\n", gtid)); + __kmp_assert_valid_gtid(gtid); if (!TCR_4(__kmp_init_parallel)) __kmp_parallel_initialize(); @@ -925,6 +926,7 @@ kmp_info_t *th; KC_TRACE(10, ("__kmpc_end_ordered: called T#%d\n", gtid)); + __kmp_assert_valid_gtid(gtid); #if USE_ITT_BUILD __kmp_itt_ordered_end(gtid); @@ -1147,7 +1149,7 @@ /*! @ingroup WORK_SHARING @param loc source location information. -@param global_tid global thread number . +@param global_tid global thread number. @param crit identity of the critical section. This could be a pointer to a lock associated with the critical section, or some other suitably unique value. @@ -1170,6 +1172,7 @@ kmp_user_lock_p lck; KC_TRACE(10, ("__kmpc_critical: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); // TODO: add THR_OVHD_STATE @@ -1392,6 +1395,7 @@ #endif KC_TRACE(10, ("__kmpc_critical: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); kmp_dyna_lock_t *lk = (kmp_dyna_lock_t *)crit; // Check if it is initialized. @@ -1607,8 +1611,8 @@ */ kmp_int32 __kmpc_barrier_master(ident_t *loc, kmp_int32 global_tid) { int status; - KC_TRACE(10, ("__kmpc_barrier_master: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); if (!TCR_4(__kmp_init_parallel)) __kmp_parallel_initialize(); @@ -1651,7 +1655,7 @@ */ void __kmpc_end_barrier_master(ident_t *loc, kmp_int32 global_tid) { KC_TRACE(10, ("__kmpc_end_barrier_master: called T#%d\n", global_tid)); - + __kmp_assert_valid_gtid(global_tid); __kmp_end_split_barrier(bs_plain_barrier, global_tid); } @@ -1667,8 +1671,8 @@ */ kmp_int32 __kmpc_barrier_master_nowait(ident_t *loc, kmp_int32 global_tid) { kmp_int32 ret; - KC_TRACE(10, ("__kmpc_barrier_master_nowait: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); if (!TCR_4(__kmp_init_parallel)) __kmp_parallel_initialize(); @@ -1706,14 +1710,9 @@ if (__kmp_env_consistency_check) { /* there's no __kmpc_end_master called; so the (stats) */ /* actions of __kmpc_end_master are done here */ - - if (global_tid < 0) { - KMP_WARNING(ThreadIdentInvalid); - } if (ret) { /* only one thread should do the pop since only */ /* one did the push (see __kmpc_master()) */ - __kmp_pop_sync(global_tid, ct_master, loc); } } @@ -1734,6 +1733,7 @@ */ kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid) { + __kmp_assert_valid_gtid(global_tid); kmp_int32 rc = __kmp_enter_single(global_tid, loc, TRUE); if (rc) { @@ -1786,6 +1786,7 @@ by the `single` construct. */ void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid) { + __kmp_assert_valid_gtid(global_tid); __kmp_exit_single(global_tid); KMP_POP_PARTITIONED_TIMER(); @@ -2065,8 +2066,8 @@ void *cpy_data, void (*cpy_func)(void *, void *), kmp_int32 didit) { void **data_ptr; - KC_TRACE(10, ("__kmpc_copyprivate: called T#%d\n", gtid)); + __kmp_assert_valid_gtid(gtid); KMP_MB(); @@ -3382,6 +3383,7 @@ kmp_team_t *team; int teams_swapped = 0, task_state; KA_TRACE(10, ("__kmpc_reduce_nowait() enter: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); // why do we need this initialization here at all? // Reduction clause can not be used as a stand-alone directive. @@ -3535,6 +3537,7 @@ PACKED_REDUCTION_METHOD_T packed_reduction_method; KA_TRACE(10, ("__kmpc_end_reduce_nowait() enter: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); packed_reduction_method = __KMP_GET_REDUCTION_METHOD(global_tid); @@ -3609,6 +3612,7 @@ int teams_swapped = 0, task_state; KA_TRACE(10, ("__kmpc_reduce() enter: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); // why do we need this initialization here at all? // Reduction clause can not be a stand-alone directive. @@ -3727,6 +3731,7 @@ int teams_swapped = 0, task_state; KA_TRACE(10, ("__kmpc_end_reduce() enter: called T#%d\n", global_tid)); + __kmp_assert_valid_gtid(global_tid); th = __kmp_thread_from_gtid(global_tid); teams_swapped = __kmp_swap_teams_for_teams_reduction(th, &team, &task_state); @@ -3883,6 +3888,7 @@ */ void __kmpc_doacross_init(ident_t *loc, int gtid, int num_dims, const struct kmp_dim *dims) { + __kmp_assert_valid_gtid(gtid); int j, idx; kmp_int64 last, trace_count; kmp_info_t *th = __kmp_threads[gtid]; @@ -4002,6 +4008,7 @@ } void __kmpc_doacross_wait(ident_t *loc, int gtid, const kmp_int64 *vec) { + __kmp_assert_valid_gtid(gtid); kmp_int32 shft, num_dims, i; kmp_uint32 flag; kmp_int64 iter_number; // iteration number of "collapsed" loop nest @@ -4112,6 +4119,7 @@ } void __kmpc_doacross_post(ident_t *loc, int gtid, const kmp_int64 *vec) { + __kmp_assert_valid_gtid(gtid); kmp_int32 shft, num_dims, i; kmp_uint32 flag; kmp_int64 iter_number; // iteration number of "collapsed" loop nest @@ -4183,6 +4191,7 @@ } void __kmpc_doacross_fini(ident_t *loc, int gtid) { + __kmp_assert_valid_gtid(gtid); kmp_int32 num_done; kmp_info_t *th = __kmp_threads[gtid]; kmp_team_t *team = th->th.th_team; diff --git a/openmp/runtime/src/kmp_dispatch.cpp b/openmp/runtime/src/kmp_dispatch.cpp --- a/openmp/runtime/src/kmp_dispatch.cpp +++ b/openmp/runtime/src/kmp_dispatch.cpp @@ -773,6 +773,7 @@ sizeof(dispatch_private_info)); KMP_BUILD_ASSERT(sizeof(dispatch_shared_info_template) == sizeof(dispatch_shared_info)); + __kmp_assert_valid_gtid(gtid); if (!TCR_4(__kmp_init_parallel)) __kmp_parallel_initialize(); @@ -997,6 +998,7 @@ template static void __kmp_dispatch_finish(int gtid, ident_t *loc) { typedef typename traits_t::signed_t ST; + __kmp_assert_valid_gtid(gtid); kmp_info_t *th = __kmp_threads[gtid]; KD_TRACE(100, ("__kmp_dispatch_finish: T#%d called\n", gtid)); @@ -1060,6 +1062,7 @@ template static void __kmp_dispatch_finish_chunk(int gtid, ident_t *loc) { typedef typename traits_t::signed_t ST; + __kmp_assert_valid_gtid(gtid); kmp_info_t *th = __kmp_threads[gtid]; KD_TRACE(100, ("__kmp_dispatch_finish_chunk: T#%d called\n", gtid)); @@ -1900,6 +1903,7 @@ int status; dispatch_private_info_template *pr; + __kmp_assert_valid_gtid(gtid); kmp_info_t *th = __kmp_threads[gtid]; kmp_team_t *team = th->th.th_team; @@ -2192,6 +2196,7 @@ __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrIllegal, ct_pdo, loc); } } + __kmp_assert_valid_gtid(gtid); th = __kmp_threads[gtid]; team = th->th.th_team; KMP_DEBUG_ASSERT(th->th.th_teams_microtask); // we are in the teams construct diff --git a/openmp/runtime/src/kmp_error.cpp b/openmp/runtime/src/kmp_error.cpp --- a/openmp/runtime/src/kmp_error.cpp +++ b/openmp/runtime/src/kmp_error.cpp @@ -415,9 +415,6 @@ __kmp_error_construct2(kmp_i18n_msg_CnsExpectedEnd, ct, ident, &p->stack_data[tos]); } - if (gtid < 0) { - __kmp_check_null_func(); - } KE_TRACE(100, (POP_MSG(p))); p->s_top = p->stack_data[tos].prev; p->stack_data[tos].type = ct_none; diff --git a/openmp/runtime/src/kmp_sched.cpp b/openmp/runtime/src/kmp_sched.cpp --- a/openmp/runtime/src/kmp_sched.cpp +++ b/openmp/runtime/src/kmp_sched.cpp @@ -85,6 +85,7 @@ kmp_uint32 nth; UT trip_count; kmp_team_t *team; + __kmp_assert_valid_gtid(gtid); kmp_info_t *th = __kmp_threads[gtid]; #if OMPT_SUPPORT && OMPT_OPTIONAL @@ -438,6 +439,7 @@ KMP_DEBUG_ASSERT(plastiter && plower && pupper && pupperDist && pstride); KE_TRACE(10, ("__kmpc_dist_for_static_init called (%d)\n", gtid)); + __kmp_assert_valid_gtid(gtid); #ifdef KMP_DEBUG { char *buff; @@ -681,6 +683,7 @@ KMP_DEBUG_ASSERT(p_last && p_lb && p_ub && p_st); KE_TRACE(10, ("__kmp_team_static_init called (%d)\n", gtid)); + __kmp_assert_valid_gtid(gtid); #ifdef KMP_DEBUG { char *buff; diff --git a/openmp/runtime/src/kmp_taskdeps.cpp b/openmp/runtime/src/kmp_taskdeps.cpp --- a/openmp/runtime/src/kmp_taskdeps.cpp +++ b/openmp/runtime/src/kmp_taskdeps.cpp @@ -514,7 +514,7 @@ kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task); KA_TRACE(10, ("__kmpc_omp_task_with_deps(enter): T#%d loc=%p task=%p\n", gtid, loc_ref, new_taskdata)); - + __kmp_assert_valid_gtid(gtid); kmp_info_t *thread = __kmp_threads[gtid]; kmp_taskdata_t *current_task = thread->th.th_current_task; @@ -677,7 +677,7 @@ gtid, loc_ref)); return; } - + __kmp_assert_valid_gtid(gtid); kmp_info_t *thread = __kmp_threads[gtid]; kmp_taskdata_t *current_task = thread->th.th_current_task; diff --git a/openmp/runtime/src/kmp_tasking.cpp b/openmp/runtime/src/kmp_tasking.cpp --- a/openmp/runtime/src/kmp_tasking.cpp +++ b/openmp/runtime/src/kmp_tasking.cpp @@ -984,6 +984,7 @@ kmp_task_t *task) { KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n", gtid, loc_ref, KMP_TASK_TO_TASKDATA(task))); + __kmp_assert_valid_gtid(gtid); // this routine will provide task to resume __kmp_task_finish(gtid, task, NULL); @@ -1381,10 +1382,9 @@ kmp_routine_entry_t task_entry) { kmp_task_t *retval; kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags; - + __kmp_assert_valid_gtid(gtid); input_flags->native = FALSE; // __kmp_task_alloc() sets up all other runtime flags - KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) " "sizeof_task=%ld sizeof_shared=%ld entry=%p\n", gtid, loc_ref, input_flags->tiedness ? "tied " : "untied", @@ -1716,6 +1716,7 @@ #endif KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref, new_taskdata)); + __kmp_assert_valid_gtid(gtid); #if OMPT_SUPPORT kmp_taskdata_t *parent = NULL; @@ -1824,6 +1825,7 @@ KMP_SET_THREAD_STATE_BLOCK(TASKWAIT); KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref)); + __kmp_assert_valid_gtid(gtid); if (__kmp_tasking_mode != tskm_immediate_exec) { thread = __kmp_threads[gtid]; @@ -1953,6 +1955,7 @@ KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n", gtid, loc_ref, end_part)); + __kmp_assert_valid_gtid(gtid); if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) { thread = __kmp_threads[gtid]; @@ -2108,6 +2111,7 @@ template void *__kmp_task_reduction_init(int gtid, int num, T *data) { + __kmp_assert_valid_gtid(gtid); kmp_info_t *thread = __kmp_threads[gtid]; kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup; kmp_int32 nth = thread->th.th_team_nproc; @@ -2223,6 +2227,7 @@ Get thread-specific location of data item */ void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) { + __kmp_assert_valid_gtid(gtid); kmp_info_t *thread = __kmp_threads[gtid]; kmp_int32 nth = thread->th.th_team_nproc; if (nth == 1) @@ -2328,6 +2333,7 @@ template void *__kmp_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, T *data) { + __kmp_assert_valid_gtid(gtid); kmp_info_t *thr = __kmp_threads[gtid]; kmp_int32 nth = thr->th.th_team_nproc; __kmpc_taskgroup(loc, gtid); // form new taskgroup first @@ -2423,6 +2429,7 @@ // __kmpc_taskgroup: Start a new taskgroup void __kmpc_taskgroup(ident_t *loc, int gtid) { + __kmp_assert_valid_gtid(gtid); kmp_info_t *thread = __kmp_threads[gtid]; kmp_taskdata_t *taskdata = thread->th.th_current_task; kmp_taskgroup_t *tg_new = @@ -2455,6 +2462,7 @@ // __kmpc_end_taskgroup: Wait until all tasks generated by the current task // and its descendants are complete void __kmpc_end_taskgroup(ident_t *loc, int gtid) { + __kmp_assert_valid_gtid(gtid); kmp_info_t *thread = __kmp_threads[gtid]; kmp_taskdata_t *taskdata = thread->th.th_current_task; kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup; @@ -3807,7 +3815,7 @@ KA_TRACE( 10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n", gtid, taskdata)); - + __kmp_assert_valid_gtid(gtid); KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); __kmp_first_top_half_finish_proxy(taskdata); @@ -4439,7 +4447,7 @@ int sched, kmp_uint64 grainsize, void *task_dup) { kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); KMP_DEBUG_ASSERT(task != NULL); - + __kmp_assert_valid_gtid(gtid); if (nogroup == 0) { #if OMPT_SUPPORT && OMPT_OPTIONAL OMPT_STORE_RETURN_ADDRESS(gtid);