Skip to content

Commit d582191

Browse files
committed
MPI_T: add code to check obj_handle arg
Three MPI_T methods - for pvar, cvar, events, can bind one of these objects to an arbitrary MPI object. There was verbiage added to the MPI 4.1 standard which states its erroneous to bind one of these 'T' objects to an invalid MPI object. This PR adds code to make an effort at checking the validity of the arbitrary MPI object argument. We don't bother with events since only a stub implementation is currently present. Related to issue #12185. Signed-off-by: Howard Pritchard <[email protected]>
1 parent a4a7e25 commit d582191

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

ompi/mpi/tool/cvar_handle_alloc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* reserved.
1010
* Copyright (c) 2021 Amazon.com, Inc. or its affiliates. All Rights
1111
* reserved.
12+
* Copyright (c) 2025 Triad National Security, LLC. All rights
13+
* reserved.
1214
* $COPYRIGHT$
1315
*
1416
* Additional copyrights may follow
@@ -41,6 +43,12 @@ int MPI_T_cvar_handle_alloc (int cvar_index, void *obj_handle,
4143
return MPI_T_ERR_INVALID;
4244
}
4345

46+
if (NULL != obj_handle) {
47+
if (ompit_obj_invalid(obj_handle)) {
48+
return MPI_T_ERR_INVALID_HANDLE;
49+
}
50+
}
51+
4452
ompi_mpit_lock ();
4553

4654
*handle = NULL;

ompi/mpi/tool/cvar_read.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ int MPI_T_cvar_read (MPI_T_cvar_handle handle, void *buf)
3737
return MPI_T_ERR_NOT_INITIALIZED;
3838
}
3939

40-
if (MPI_PARAM_CHECK && NULL == buf) {
41-
return MPI_T_ERR_INVALID;
40+
if (MPI_PARAM_CHECK) {
41+
if ((NULL == buf) || (NULL == handle)) {
42+
return MPI_T_ERR_INVALID;
43+
}
4244
}
4345

4446
ompi_mpit_lock ();

ompi/mpi/tool/mpit-internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Copyright (c) 2011 UT-Battelle, LLC. All rights reserved.
66
* Copyright (c) 2017 IBM Corporation. All rights reserved.
77
* Copyright (c) 2018 Cisco Systems, Inc. All rights reserved
8+
* Copyright (c) 2025 Triad National Security, LLC. All rights
9+
* reserved.
810
* $COPYRIGHT$
911
*
1012
* Additional copyrights may follow
@@ -22,8 +24,12 @@
2224
#include "ompi/include/ompi_config.h"
2325
#include "ompi/runtime/params.h"
2426
#include "ompi/communicator/communicator.h"
27+
#include "ompi/win/win.h"
2528
#include "ompi/constants.h"
2629
#include "ompi/datatype/ompi_datatype.h"
30+
#include "ompi/file/file.h"
31+
#include "ompi/op/op.h"
32+
#include "ompi/message/message.h"
2733

2834
#include "mpi.h"
2935

@@ -42,6 +48,7 @@ extern volatile uint32_t ompi_mpit_init_count;
4248

4349
int ompit_var_type_to_datatype (mca_base_var_type_t type, MPI_Datatype *datatype);
4450
int ompit_opal_to_mpit_error (int rc);
51+
bool ompit_obj_invalid(void *obj_handle);
4552

4653
static inline int mpit_is_initialized (void)
4754
{

ompi/mpi/tool/mpit_common.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* Copyright (c) 2020 The University of Tennessee and The University
99
* of Tennessee Research Foundation. All rights
1010
* reserved.
11+
* Copyright (c) 2025 Triad National Security, LLC. All rights
12+
* reserved.
1113
* $COPYRIGHT$
1214
*
1315
* Additional copyrights may follow
@@ -87,3 +89,118 @@ int ompit_opal_to_mpit_error (int rc)
8789
return MPI_T_ERR_INVALID;
8890
}
8991
}
92+
93+
/*
94+
* Check whether a MPI object is valid or not.
95+
* If invalid return true, otherwise false.
96+
*/
97+
bool ompit_obj_invalid(void *obj_handle)
98+
{
99+
bool ret = true; /* by default return obj is invalid */
100+
opal_object_t *opal_obj = NULL;
101+
opal_class_t *opal_class;
102+
const char *obj_name = NULL;
103+
104+
if (NULL == obj_handle) {
105+
goto fn_exit;
106+
}
107+
108+
/* we are actually evaulating a pointer to an OMPI MPI opaque handle */
109+
opal_obj = *(opal_object_t **)obj_handle;
110+
111+
/*
112+
* this should have already been checked by the caller but we do it here too
113+
*/
114+
if (NULL == opal_obj) {
115+
goto fn_exit;
116+
}
117+
118+
opal_class = opal_obj->obj_class;
119+
if (NULL == opal_class) {
120+
goto fn_exit;
121+
}
122+
123+
obj_name = opal_class->cls_name;
124+
if (NULL == obj_name) {
125+
goto fn_exit;
126+
}
127+
128+
/*
129+
* An MPIT C or P var or event can in principle be bound to any of these MPI object types
130+
* This code is meant to address, at least in part, the text in section 15.3.2 in the MPI 4.1
131+
* standard concerning binding one of the T things to an MPI object.
132+
*/
133+
if (0 == strncmp(obj_name, "ompi_communicator_t", strlen("ompi_communicator_t"))) {
134+
ompi_communicator_t *comm = (ompi_communicator_t *)obj_handle;
135+
ret = ompi_comm_invalid(comm);
136+
goto fn_exit;
137+
}
138+
139+
if (0 == strncmp(obj_name, "ompi_win_t", strlen("ompi_win_t"))) {
140+
ompi_win_t *win = (ompi_win_t *)obj_handle;
141+
ret = ompi_win_invalid(win);
142+
goto fn_exit;
143+
}
144+
145+
if (0 == strncmp(obj_name, "ompi_file_t", strlen("ompi_file_t"))) {
146+
ompi_file_t *file = (ompi_file_t *)obj_handle;
147+
ret = ompi_file_invalid(file);
148+
goto fn_exit;
149+
}
150+
151+
if (0 == strncmp(obj_name, "ompi_instance_t", strlen("ompi_instance_t"))) {
152+
ompi_instance_t *instance = (ompi_instance_t *)obj_handle;
153+
ret = ompi_instance_invalid(instance);
154+
goto fn_exit;
155+
}
156+
157+
/*
158+
* following object types don't seem to have robust validity checks so just
159+
* do a smoke test for use of NULL objects.
160+
*/
161+
if (0 == strncmp(obj_name, "ompi_info_t", strlen("ompi_info_t"))) {
162+
ompi_info_t *info = (ompi_info_t *)obj_handle;
163+
ret = (MPI_INFO_NULL == info) ? true : false;
164+
goto fn_exit;
165+
}
166+
167+
if (0 == strncmp(obj_name, "ompi_datatype_t", strlen("ompi_datatype_t"))) {
168+
ompi_datatype_t *datatype = (ompi_datatype_t *)obj_handle;
169+
ret = (MPI_DATATYPE_NULL == datatype) ? true : false;
170+
goto fn_exit;
171+
}
172+
173+
if (0 == strncmp(obj_name, "ompi_request_t", strlen("ompi_request_t"))) {
174+
ompi_request_t *request = (ompi_request_t *)obj_handle;
175+
ret = (MPI_REQUEST_NULL == request) ? true : false;
176+
goto fn_exit;
177+
}
178+
179+
if (0 == strncmp(obj_name, "ompi_errhandler_t", strlen("ompi_errhandler_t"))) {
180+
ompi_errhandler_t *errhandler = (ompi_errhandler_t *)obj_handle;
181+
ret = (MPI_ERRHANDLER_NULL == errhandler) ? true : false;
182+
goto fn_exit;
183+
}
184+
185+
if (0 == strncmp(obj_name, "ompi_opt_t", strlen("ompi_op_t"))) {
186+
ompi_op_t *op = (ompi_op_t *)obj_handle;
187+
ret = (MPI_OP_NULL == op) ? true : false;
188+
goto fn_exit;
189+
}
190+
191+
if (0 == strncmp(obj_name, "ompi_message_t", strlen("ompi_message_t"))) {
192+
ompi_message_t *message = (ompi_message_t *)obj_handle;
193+
ret = (MPI_MESSAGE_NULL == message) ? true : false;
194+
goto fn_exit;
195+
}
196+
197+
if (0 == strncmp(obj_name, "ompi_group_t", strlen("ompi_group_t"))) {
198+
ompi_group_t *group = (ompi_group_t *)obj_handle;
199+
ret = (MPI_GROUP_NULL == group) ? true : false;
200+
goto fn_exit;
201+
}
202+
203+
fn_exit:
204+
return ret;
205+
}
206+

ompi/mpi/tool/pvar_handle_alloc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* reserved.
1010
* Copyright (c) 2021 Amazon.com, Inc. or its affiliates. All Rights
1111
* reserved.
12+
* Copyright (c) 2025 Triad National Security, LLC. All rights
13+
* reserved.
1214
* $COPYRIGHT$
1315
*
1416
* Additional copyrights may follow
@@ -37,6 +39,12 @@ int MPI_T_pvar_handle_alloc(MPI_T_pvar_session session, int pvar_index,
3739
return MPI_T_ERR_NOT_INITIALIZED;
3840
}
3941

42+
if (NULL != obj_handle) {
43+
if (ompit_obj_invalid(obj_handle)) {
44+
return MPI_T_ERR_INVALID_HANDLE;
45+
}
46+
}
47+
4048
ompi_mpit_lock ();
4149

4250
do {

0 commit comments

Comments
 (0)