Actual source code: checkptr.c
petsc-3.9.4 2018-09-11
1: #include <petsc/private/petscimpl.h>
2: #include <petscvalgrind.h>
4: static PetscInt petsc_checkpointer_intensity = 1;
6: /*@
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.
11: Not Collective
13: Input Arguments:
14: . intensity - how much to check pointers for validity
16: Options Database:
17: . -check_pointer_intensity - intensity (0, 1, or 2)
19: Level: advanced
22: @*/
24: {
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: return(0);
36: }
38: /* ---------------------------------------------------------------------------------------*/
39: #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_HAVE_SIGINFO_T)
40: #include <signal.h>
41: #include <setjmp.h>
42: PETSC_INTERN jmp_buf PetscSegvJumpBuf;
43: PETSC_INTERN void PetscSegv_sigaction(int, siginfo_t*, void *);
45: /*@C
48: Not Collective
50: Input Parameters:
51: + ptr - the pointer
52: - dtype - the type of data the pointer is suppose to point to
54: Level: developer
57: @*/
59: {
60: struct sigaction sa,oldsa;
62: if (PETSC_RUNNING_ON_VALGRIND) return PETSC_TRUE;
63: if (!ptr) return PETSC_FALSE;
64: if (petsc_checkpointer_intensity < 1) return PETSC_TRUE;
66: /* Skip the verbose check if we are inside a hot function. */
67: if (petscstack && petscstack->hotdepth > 0 && petsc_checkpointer_intensity < 2) return PETSC_TRUE;
69: sigemptyset(&sa.sa_mask);
70: sa.sa_sigaction = PetscSegv_sigaction;
71: sa.sa_flags = SA_SIGINFO;
72: sigaction(SIGSEGV, &sa, &oldsa);
74: if (setjmp(PetscSegvJumpBuf)) {
75: /* A segv was triggered in the code below hence we return with an error code */
76: sigaction(SIGSEGV, &oldsa, NULL);/* reset old signal hanlder */
77: return PETSC_FALSE;
78: } else {
79: switch (dtype) {
80: case PETSC_INT:{
81: PETSC_UNUSED PetscInt x = (PetscInt)*(volatile PetscInt*)ptr;
82: break;
83: }
84: #if defined(PETSC_USE_COMPLEX)
85: case PETSC_SCALAR:{ /* C++ is seriously dysfunctional with volatile std::complex. */
86: #if defined(PETSC_USE_CXXCOMPLEX)
87: PetscReal xreal = ((volatile PetscReal*)ptr)[0],ximag = ((volatile PetscReal*)ptr)[1];
88: PETSC_UNUSED volatile PetscScalar x = xreal + PETSC_i*ximag;
89: #else
90: PETSC_UNUSED PetscScalar x = *(volatile PetscScalar*)ptr;
91: #endif
92: break;
93: }
94: #endif
95: case PETSC_REAL:{
96: PETSC_UNUSED PetscReal x = *(volatile PetscReal*)ptr;
97: break;
98: }
99: case PETSC_BOOL:{
100: PETSC_UNUSED PetscBool x = *(volatile PetscBool*)ptr;
101: break;
102: }
103: case PETSC_ENUM:{
104: PETSC_UNUSED PetscEnum x = *(volatile PetscEnum*)ptr;
105: break;
106: }
107: case PETSC_CHAR:{
108: PETSC_UNUSED char x = *(volatile char*)ptr;
109: break;
110: }
111: case PETSC_OBJECT:{
112: PETSC_UNUSED volatile PetscClassId classid = ((PetscObject)ptr)->classid;
113: break;
114: }
115: default:;
116: }
117: }
118: sigaction(SIGSEGV, &oldsa, NULL); /* reset old signal hanlder */
119: return PETSC_TRUE;
120: }
121: #else
123: {
124: if (!ptr) return PETSC_FALSE;
125: return PETSC_TRUE;
126: }
127: #endif