xref: /petsc/src/sys/error/checkptr.c (revision 718fc40760f39500f2966f934e3e71a26229ec4d)
1 #include <petsc/private/petscimpl.h>
2 #include <petscvalgrind.h>
3 
4 static PetscInt petsc_checkpointer_intensity = 1;
5 
6 /*@
7    PetscCheckPointerSetIntensity - An intense pointer check registers a signal handler and attempts to dereference to
8    confirm whether the address is valid.  An intensity of 0 never uses signal handlers, 1 uses them when not in a "hot"
9    function, and intensity of 2 always uses a signal handler.
10 
11    Not Collective
12 
13    Input Arguments:
14 .  intensity - how much to check pointers for validity
15 
16    Options Database:
17 .  -check_pointer_intensity - intensity (0, 1, or 2)
18 
19    Level: advanced
20 
21 .seealso: PetscCheckPointer(), PetscFunctionBeginHot()
22 @*/
23 PetscErrorCode PetscCheckPointerSetIntensity(PetscInt intensity)
24 {
25 
26   PetscFunctionBegin;
27   switch (intensity) {
28   case 0:
29   case 1:
30   case 2:
31     petsc_checkpointer_intensity = intensity;
32     break;
33   default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Intensity %D not in 0,1,2",intensity);
34   }
35   PetscFunctionReturn(0);
36 }
37 
38 /* ---------------------------------------------------------------------------------------*/
39 
40 #if defined(PETSC_HAVE_SETJMP_H)
41 #include <setjmp.h>
42 PETSC_INTERN jmp_buf PetscSegvJumpBuf;
43 PETSC_INTERN PetscBool PetscSegvJumpBuf_set;
44 
45 /*@C
46      PetscCheckPointer - Returns PETSC_TRUE if a pointer points to accessible data
47 
48    Not Collective
49 
50    Input Parameters:
51 +     ptr - the pointer
52 -     dtype - the type of data the pointer is suppose to point to
53 
54    Level: developer
55 
56 .seealso: PetscCheckPointerSetIntensity()
57 @*/
58 PetscBool PetscCheckPointer(const void *ptr,PetscDataType dtype)
59 {
60 
61   if (PETSC_RUNNING_ON_VALGRIND) return PETSC_TRUE;
62   if (!ptr) return PETSC_FALSE;
63   if (petsc_checkpointer_intensity < 1) return PETSC_TRUE;
64 
65   /* Skip the verbose check if we are inside a hot function. */
66   if (petscstack && petscstack->hotdepth > 0 && petsc_checkpointer_intensity < 2) return PETSC_TRUE;
67 
68   PetscSegvJumpBuf_set = PETSC_TRUE;
69 
70   if (setjmp(PetscSegvJumpBuf)) {
71     /* A segv was triggered in the code below hence we return with an error code */
72     PetscSegvJumpBuf_set = PETSC_FALSE;
73     return PETSC_FALSE;
74   } else {
75     switch (dtype) {
76     case PETSC_INT:{
77       PETSC_UNUSED PetscInt x = (PetscInt)*(volatile PetscInt*)ptr;
78       break;
79     }
80 #if defined(PETSC_USE_COMPLEX)
81     case PETSC_SCALAR:{         /* C++ is seriously dysfunctional with volatile std::complex. */
82 #if defined(PETSC_USE_CXXCOMPLEX)
83       PetscReal xreal = ((volatile PetscReal*)ptr)[0],ximag = ((volatile PetscReal*)ptr)[1];
84       PETSC_UNUSED volatile PetscScalar x = xreal + PETSC_i*ximag;
85 #else
86       PETSC_UNUSED PetscScalar x = *(volatile PetscScalar*)ptr;
87 #endif
88       break;
89     }
90 #endif
91     case PETSC_REAL:{
92       PETSC_UNUSED PetscReal x = *(volatile PetscReal*)ptr;
93       break;
94     }
95     case PETSC_BOOL:{
96       PETSC_UNUSED PetscBool x = *(volatile PetscBool*)ptr;
97       break;
98     }
99     case PETSC_ENUM:{
100       PETSC_UNUSED PetscEnum x = *(volatile PetscEnum*)ptr;
101       break;
102     }
103     case PETSC_CHAR:{
104       PETSC_UNUSED char x = *(volatile char*)ptr;
105       break;
106     }
107     case PETSC_OBJECT:{
108       PETSC_UNUSED volatile PetscClassId classid = ((PetscObject)ptr)->classid;
109       break;
110     }
111     default:;
112     }
113   }
114   PetscSegvJumpBuf_set = PETSC_FALSE;
115   return PETSC_TRUE;
116 }
117 #else
118 PetscBool PetscCheckPointer(const void *ptr,PETSC_UNUSED PetscDataType dtype)
119 {
120   if (!ptr) return PETSC_FALSE;
121   return PETSC_TRUE;
122 }
123 #endif
124