Blame src/include/mpir_pointers.h

Packit Service c5cf8c
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit Service c5cf8c
/*
Packit Service c5cf8c
 *  (C) 2001 by Argonne National Laboratory.
Packit Service c5cf8c
 *      See COPYRIGHT in top-level directory.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 */
Packit Service c5cf8c
Packit Service c5cf8c
#ifndef MPIR_POINTERS_H_INCLUDED
Packit Service c5cf8c
#define MPIR_POINTERS_H_INCLUDED
Packit Service c5cf8c
Packit Service c5cf8c
#include "mpi.h"
Packit Service c5cf8c
#include "mpichconf.h"
Packit Service c5cf8c
#include "mpichconfconst.h"
Packit Service c5cf8c
#include "mpir_assert.h"
Packit Service c5cf8c
#include "mpl.h"
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
/* This test is lame.  Should eventually include cookie test
Packit Service c5cf8c
   and in-range addresses */
Packit Service c5cf8c
#define MPIR_Valid_ptr_class(kind,ptr,errclass,err) \
Packit Service c5cf8c
    do {                                                                \
Packit Service c5cf8c
        if (!(ptr)) {                                                   \
Packit Service c5cf8c
            err = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, errclass, \
Packit Service c5cf8c
                                       "**nullptrtype", "**nullptrtype %s", #kind); \
Packit Service c5cf8c
            /* Explicitly tell Coverity that errclass != MPI_SUCCESS => err != MPI_SUCCESS */ \
Packit Service c5cf8c
            MPIR_Assert((errclass) == MPI_SUCCESS || ((err) != MPI_SUCCESS)); \
Packit Service c5cf8c
        }                                                               \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
Packit Service c5cf8c
#define MPIR_Info_valid_ptr(ptr,err) MPIR_Valid_ptr_class(Info,ptr,MPI_ERR_INFO,err)
Packit Service c5cf8c
/* Check not only for a null pointer but for an invalid communicator,
Packit Service c5cf8c
   such as one that has been freed.  Let's try the ref_count as the test
Packit Service c5cf8c
   for now */
Packit Service c5cf8c
/* ticket #1441: check (refcount<=0) to cover the case of 0, an "over-free" of
Packit Service c5cf8c
 * -1 or similar, and the 0xecec... case when --enable-g=mem is used */
Packit Service c5cf8c
#define MPIR_Comm_valid_ptr(ptr,err,ignore_rev) {     \
Packit Service c5cf8c
     MPIR_Valid_ptr_class(Comm,ptr,MPI_ERR_COMM,err); \
Packit Service c5cf8c
     if ((ptr) && MPIR_Object_get_ref(ptr) <= 0) {    \
Packit Service c5cf8c
         MPIR_ERR_SET(err,MPI_ERR_COMM,"**comm");     \
Packit Service c5cf8c
         ptr = 0;                                     \
Packit Service c5cf8c
     } else if ((ptr) && (ptr)->revoked && !(ignore_rev)) {        \
Packit Service c5cf8c
         MPIR_ERR_SET(err,MPIX_ERR_REVOKED,"**comm"); \
Packit Service c5cf8c
     }                                                \
Packit Service c5cf8c
}
Packit Service c5cf8c
#define MPIR_Win_valid_ptr(ptr,err) MPIR_Valid_ptr_class(Win,ptr,MPI_ERR_WIN,err)
Packit Service c5cf8c
#define MPIR_Group_valid_ptr(ptr,err) MPIR_Valid_ptr_class(Group,ptr,MPI_ERR_GROUP,err)
Packit Service c5cf8c
#define MPIR_Op_valid_ptr(ptr,err) MPIR_Valid_ptr_class(Op,ptr,MPI_ERR_OP,err)
Packit Service c5cf8c
#define MPIR_Errhandler_valid_ptr(ptr,err) MPIR_Valid_ptr_class(Errhandler,ptr,MPI_ERR_ARG,err)
Packit Service c5cf8c
#define MPIR_Request_valid_ptr(ptr,err) MPIR_Valid_ptr_class(Request,ptr,MPI_ERR_REQUEST,err)
Packit Service c5cf8c
#define MPII_Keyval_valid_ptr(ptr,err) MPIR_Valid_ptr_class(Keyval,ptr,MPI_ERR_KEYVAL,err)
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
/* Assigns (src_) to (dst_), checking that (src_) fits in (dst_) without
Packit Service c5cf8c
 * truncation.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * When fiddling with this macro, please keep C's overly complicated integer
Packit Service c5cf8c
 * promotion/truncation/conversion rules in mind.  A discussion of these issues
Packit Service c5cf8c
 * can be found in Chapter 5 of "Secure Coding in C and C++" by Robert Seacord.
Packit Service c5cf8c
 */
Packit Service c5cf8c
#define MPIR_Assign_trunc(dst_,src_,dst_type_)                                         \
Packit Service c5cf8c
    do {                                                                               \
Packit Service c5cf8c
        /* will catch some of the cases if the expr_inttype macros aren't available */ \
Packit Service c5cf8c
        MPIR_Assert((src_) == (dst_type_)(src_));                                      \
Packit Service c5cf8c
        dst_ = (dst_type_)(src_);                                                      \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
Packit Service c5cf8c
/*
Packit Service c5cf8c
 * Ensure an MPI_Aint value fits into a signed int.
Packit Service c5cf8c
 * Useful for detecting overflow when MPI_Aint is larger than an int.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * \param[in]  aint  Variable of type MPI_Aint
Packit Service c5cf8c
 */
Packit Service c5cf8c
#define MPIR_Ensure_Aint_fits_in_int(aint) \
Packit Service c5cf8c
  MPIR_Assert((aint) == (MPI_Aint)(int)(aint));
Packit Service c5cf8c
Packit Service c5cf8c
/*
Packit Service c5cf8c
 * Ensure an MPI_Aint value fits into a pointer.
Packit Service c5cf8c
 * Useful for detecting overflow when MPI_Aint is larger than a pointer.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * \param[in]  aint  Variable of type MPI_Aint
Packit Service c5cf8c
 */
Packit Service c5cf8c
#ifndef SIZEOF_PTR_IS_AINT
Packit Service c5cf8c
#define MPIR_Ensure_Aint_fits_in_pointer(aint) \
Packit Service c5cf8c
  MPIR_Assert((aint) == (MPI_Aint)(uintptr_t) MPIR_AINT_CAST_TO_VOID_PTR(aint));
Packit Service c5cf8c
#else
Packit Service c5cf8c
#define MPIR_Ensure_Aint_fits_in_pointer(aint) do {} while (0)
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
#endif /* MPIR_POINTERS_H_INCLUDED */