File: exec_mon_example.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (218 lines) | stat: -rw-r--r-- 5,663 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//  (C) Copyright Gennadiy Rozental 2003-2014.
//  Distributed under the Boost Software License, Version 1.0.
//  (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/libs/test for the library home page.

#include <boost/test/prg_exec_monitor.hpp>
#include <boost/test/execution_monitor.hpp>
#include <boost/test/utils/basic_cstring/io.hpp>

#include <iostream>

struct my_exception1
{
    explicit    my_exception1( int res_code ) : m_res_code( res_code ) {}

    int         m_res_code;
};

struct my_exception2
{
    explicit    my_exception2( int res_code ) : m_res_code( res_code ) {}

    int         m_res_code;
};

namespace {

class dangerous_call {
public:
    dangerous_call( int argc ) : m_argc( argc ) {}

    int operator()()
    {
        // here we perform some operation under monitoring that could throw my_exception
        if( m_argc < 2 )
            throw my_exception1( 23 );
        if( m_argc > 3 )
            throw my_exception2( 45 );
        else if( m_argc > 2 )
            throw "too many args";

        return 1;
    }

private:
    // Data members
    int     m_argc;
};

void translate_my_exception1( my_exception1 const& ex )
{
    std::cout << "Caught my_exception1(" << ex.m_res_code << ")"<< std::endl;
}

void translate_my_exception2( my_exception2 const& ex )
{
    std::cout << "Caught my_exception2(" << ex.m_res_code << ")"<< std::endl;
}

int generate_fpe()
{
    double d = 0.0;

    d = 1/d;

    return 0;
}

int generate_fpe2()
{
    double d = 1e158;

    d = d*d;

    return 0;
}

int generate_fpe3()
{
    double d = 1.1e-308;

    d = 1/d;

    return 0;
}

int generate_int_div_0()
{
    int i = 0;

    return 1/i;
}

#if (defined(__clang__) && __clang_major__ >= 6) || (defined(__GNUC__) && __GNUC__ >= 8)
__attribute__((no_sanitize("null")))
#endif
int generate_sigfault()
{
    int* p = 0;

    return *p;
}


} // local_namespace

int
cpp_main( int argc , char *[] )
{
    ::boost::execution_monitor ex_mon;

    ///////////////////////////////////////////////////////////////

    ex_mon.register_exception_translator<my_exception1>( &translate_my_exception1, "except1" );
    ex_mon.register_exception_translator<my_exception2>( &translate_my_exception2, "except2" );

    try {
        ex_mon.execute( dangerous_call( argc ) );
        std::cout << "Should reach this line " << __LINE__ << std::endl;
    }
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }

    ///////////////////////////////////////////////////////////////

    ex_mon.erase_exception_translator( "except2" );

    try {
        ex_mon.execute( dangerous_call( 5 ) );
        std::cout << "Should not reach this line " << __LINE__ << std::endl;
    }
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }

    ///////////////////////////////////////////////////////////////

    ex_mon.erase_exception_translator<my_exception1>();

    try {
        ex_mon.execute( dangerous_call( 1 ) );
        std::cout << "Should not reach this line " << __LINE__ << std::endl;
    }
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }

    ///////////////////////////////////////////////////////////////

// we are currently not able to silence those errors below with UBSAN under clang
// this seems to come from the way clang handles floating point exceptions + UB.
#if !(defined(HAS_UBSAN) && (HAS_UBSAN==1) && defined(__clang__))

    ex_mon.p_detect_fp_exceptions.value = boost::fpe::BOOST_FPE_DIVBYZERO;
    ex_mon.p_catch_system_errors.value = false;

    try {
        ex_mon.execute( &generate_fpe );
        std::cout << "Should not reach this line " << __LINE__ << std::endl;
    }
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }

    ///////////////////////////////////////////////////////////////

    ex_mon.p_detect_fp_exceptions.value = boost::fpe::BOOST_FPE_ALL;

    try {
        ex_mon.execute( &generate_fpe2 );
        std::cout << "Should not reach this line " << __LINE__ << std::endl;
    }
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }

    try {
        ex_mon.execute( &generate_fpe3 );
        std::cout << "Should not reach this line " << __LINE__ << std::endl;
    }
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }

    ///////////////////////////////////////////////////////////////

    ex_mon.p_detect_fp_exceptions.value = boost::fpe::BOOST_FPE_OFF;
    ex_mon.p_catch_system_errors.value = true;

    try {
        ex_mon.execute( &generate_int_div_0 );
        std::cout << "Should not reach this line " << __LINE__ << std::endl;
    }
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }

    ///////////////////////////////////////////////////////////////

    ex_mon.p_detect_fp_exceptions.value = boost::fpe::BOOST_FPE_OFF;
    ex_mon.p_catch_system_errors.value = true;

    try {
        ex_mon.execute( &generate_sigfault );
        std::cout << "Should not reach this line " << __LINE__ << std::endl;
    }
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }
#endif // UBSAN issue

    return 0;
}

// EOF