• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #853

19 Dec 2022 01:01AM UTC coverage: 86.287% (+0.4%) from 85.912%
#853

push

StellarBot
Merge #6109

6109: Modernize serialization module r=hkaiser a=hkaiser

- flyby separate serialization of Boost types

working towards https://github.com/STEllAR-GROUP/hpx/issues/5497

Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

53 of 53 new or added lines in 6 files covered. (100.0%)

173939 of 201582 relevant lines covered (86.29%)

1931657.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.9
/libs/core/runtime_local/src/serialize_exception.cpp
1
//  Copyright (c) 2007-2020 Hartmut Kaiser
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/assert.hpp>
8
#include <hpx/modules/errors.hpp>
9
#include <hpx/runtime_local/custom_exception_info.hpp>
10
#include <hpx/runtime_local/detail/serialize_exception.hpp>
11
#include <hpx/serialization/serialize.hpp>
12

13
#if ASIO_HAS_BOOST_THROW_EXCEPTION != 0
14
#include <boost/exception/diagnostic_information.hpp>
15
#include <boost/exception/exception.hpp>
16
#endif
17

18
#include <cstddef>
19
#include <cstdint>
20
#include <exception>
21
#include <stdexcept>
22
#include <string>
23
#include <system_error>
24
#include <typeinfo>
25

26
///////////////////////////////////////////////////////////////////////////////
27
namespace hpx::runtime_local::detail {
28

29
    ///////////////////////////////////////////////////////////////////////////
30
    // TODO: This is not scalable, and painful to update.
31
    void save_custom_exception(hpx::serialization::output_archive& ar,
14,202✔
32
        std::exception_ptr const& ep, unsigned int /* version */)
33
    {
34
        hpx::util::exception_type type =
14,202✔
35
            hpx::util::exception_type::unknown_exception;
36
        std::string what;
14,202✔
37
        hpx::error err_value = hpx::error::success;
14,202✔
38
        std::string err_message;
14,202✔
39

40
        std::uint32_t throw_locality_ = 0;
14,202✔
41
        std::string throw_hostname_;
14,202✔
42
        std::int64_t throw_pid_ = -1;
14,202✔
43
        std::size_t throw_shepherd_ = 0;
14,202✔
44
        std::size_t throw_thread_id_ = 0;
14,202✔
45
        std::string throw_thread_name_;
14,202✔
46
        std::string throw_function_;
14,202✔
47
        std::string throw_file_;
14,202✔
48
        std::string throw_back_trace_;
14,202✔
49
        long throw_line_ = 0;
14,202✔
50
        std::string throw_env_;
14,202✔
51
        std::string throw_config_;
14,202✔
52
        std::string throw_state_;
14,202✔
53
        std::string throw_auxinfo_;
14,202✔
54

55
        // retrieve information related to exception_info
56
        try
57
        {
58
            std::rethrow_exception(ep);
14,202✔
59
        }
14,202✔
60
        catch (exception_info const& xi)
61
        {
62
            std::string const* function = xi.get<hpx::detail::throw_function>();
×
63
            if (function)
×
64
                throw_function_ = *function;
×
65

66
            std::string const* file = xi.get<hpx::detail::throw_file>();
×
67
            if (file)
×
68
                throw_file_ = *file;
×
69

70
            long const* line = xi.get<hpx::detail::throw_line>();
×
71
            if (line)
×
72
                throw_line_ = *line;
×
73

74
            std::uint32_t const* locality =
×
75
                xi.get<hpx::detail::throw_locality>();
×
76
            if (locality)
×
77
                throw_locality_ = *locality;
×
78

79
            std::string const* hostname_ =
×
80
                xi.get<hpx::detail::throw_hostname>();
×
81
            if (hostname_)
×
82
                throw_hostname_ = *hostname_;
×
83

84
            std::int64_t const* pid_ = xi.get<hpx::detail::throw_pid>();
×
85
            if (pid_)
×
86
                throw_pid_ = *pid_;
×
87

88
            std::size_t const* shepherd = xi.get<hpx::detail::throw_shepherd>();
×
89
            if (shepherd)
×
90
                throw_shepherd_ = *shepherd;
×
91

92
            std::size_t const* thread_id =
×
93
                xi.get<hpx::detail::throw_thread_id>();
×
94
            if (thread_id)
×
95
                throw_thread_id_ = *thread_id;
×
96

97
            std::string const* thread_name =
×
98
                xi.get<hpx::detail::throw_thread_name>();
×
99
            if (thread_name)
×
100
                throw_thread_name_ = *thread_name;
×
101

102
            std::string const* back_trace =
×
103
                xi.get<hpx::detail::throw_stacktrace>();
×
104
            if (back_trace)
×
105
                throw_back_trace_ = *back_trace;
×
106

107
            std::string const* env_ = xi.get<hpx::detail::throw_env>();
×
108
            if (env_)
×
109
                throw_env_ = *env_;
×
110

111
            std::string const* config_ = xi.get<hpx::detail::throw_config>();
×
112
            if (config_)
×
113
                throw_config_ = *config_;
×
114

115
            std::string const* state_ = xi.get<hpx::detail::throw_state>();
×
116
            if (state_)
×
117
                throw_state_ = *state_;
×
118

119
            std::string const* auxinfo_ = xi.get<hpx::detail::throw_auxinfo>();
×
120
            if (auxinfo_)
×
121
                throw_auxinfo_ = *auxinfo_;
×
122
        }
×
123
        catch (...)
124
        {
125
            // do nothing
126
        }
14,202✔
127

128
        // figure out concrete underlying exception type
129
        try
130
        {
131
            std::rethrow_exception(ep);
14,202✔
132
        }
14,202✔
133
        catch (hpx::thread_interrupted const&)
134
        {
135
            type = hpx::util::exception_type::hpx_thread_interrupted_exception;
×
136
            what = "hpx::thread_interrupted";
×
137
            err_value = hpx::error::thread_cancelled;
×
138
        }
×
139
        catch (hpx::exception const& e)
140
        {
141
            type = hpx::util::exception_type::hpx_exception;
×
142
            what = e.what();
×
143
            err_value = e.get_error();
×
144
        }
×
145
        catch (std::system_error const& e)
146
        {
147
            type = hpx::util::exception_type::std_system_error;
×
148
            what = e.what();
×
149
            err_value = static_cast<hpx::error>(e.code().value());
×
150
            err_message = e.code().message();
×
151
        }
×
152
        catch (std::runtime_error const& e)
153
        {
154
            type = hpx::util::exception_type::std_runtime_error;
14,200✔
155
            what = e.what();
14,200✔
156
        }
14,200✔
157
        catch (std::invalid_argument const& e)
158
        {
159
            type = hpx::util::exception_type::std_invalid_argument;
×
160
            what = e.what();
×
161
        }
14,200✔
162
        catch (std::out_of_range const& e)
163
        {
164
            type = hpx::util::exception_type::std_out_of_range;
×
165
            what = e.what();
×
166
        }
×
167
        catch (std::logic_error const& e)
168
        {
169
            type = hpx::util::exception_type::std_logic_error;
×
170
            what = e.what();
×
171
        }
×
172
        catch (std::bad_alloc const& e)
173
        {
174
            type = hpx::util::exception_type::std_bad_alloc;
×
175
            what = e.what();
×
176
        }
×
177
        catch (std::bad_cast const& e)
178
        {
179
            type = hpx::util::exception_type::std_bad_cast;
×
180
            what = e.what();
×
181
        }
×
182
        catch (std::bad_typeid const& e)
183
        {
184
            type = hpx::util::exception_type::std_bad_typeid;
×
185
            what = e.what();
×
186
        }
×
187
        catch (std::bad_exception const& e)
188
        {
189
            type = hpx::util::exception_type::std_bad_exception;
×
190
            what = e.what();
×
191
        }
×
192
        catch (std::exception const& e)
193
        {
194
            type = hpx::util::exception_type::std_exception;
2✔
195
            what = e.what();
2✔
196
        }
2✔
197
#if ASIO_HAS_BOOST_THROW_EXCEPTION != 0
198
        catch (boost::exception const& e)
199
        {
200
            type = hpx::util::boost_exception;
201
            what = boost::diagnostic_information(e);
202
        }
203
#endif
204
        catch (...)
205
        {
206
            type = hpx::util::exception_type::unknown_exception;
×
207
            what = "unknown exception";
×
208
        }
2✔
209

210
        // clang-format off
211
        ar & type & what & throw_function_ & throw_file_ & throw_line_ &
14,202✔
212
            throw_locality_ & throw_hostname_ & throw_pid_ & throw_shepherd_ &
14,202✔
213
            throw_thread_id_ & throw_thread_name_ & throw_back_trace_ &
14,202✔
214
            throw_env_ & throw_config_ & throw_state_ & throw_auxinfo_;
14,202✔
215
        // clang-format on
216

217
        if (hpx::util::exception_type::hpx_exception == type)
14,202✔
218
        {
219
            // clang-format off
220
            ar << static_cast<int>(err_value);
×
221
            // clang-format on
222
        }
×
223
        else if (hpx::util::exception_type::std_system_error == type)
14,202✔
224
        {
225
            // clang-format off
226
            ar << static_cast<int>(err_value) << err_message;
×
227
            // clang-format on
228
        }
×
229
    }
42,606✔
230

231
    ///////////////////////////////////////////////////////////////////////////
232
    // TODO: This is not scalable, and painful to update.
233
    void load_custom_exception(hpx::serialization::input_archive& ar,
7,101✔
234
        std::exception_ptr& e, unsigned int /*version*/)
235
    {
236
        hpx::util::exception_type type =
7,101✔
237
            hpx::util::exception_type::unknown_exception;
238
        std::string what;
7,101✔
239
        hpx::error err_value = hpx::error::success;
7,101✔
240
        std::string err_message;
7,101✔
241

242
        std::uint32_t throw_locality_ = 0;
7,101✔
243
        std::string throw_hostname_;
7,101✔
244
        std::int64_t throw_pid_ = -1;
7,101✔
245
        std::size_t throw_shepherd_ = 0;
7,101✔
246
        std::size_t throw_thread_id_ = 0;
7,101✔
247
        std::string throw_thread_name_;
7,101✔
248
        std::string throw_function_;
7,101✔
249
        std::string throw_file_;
7,101✔
250
        std::string throw_back_trace_;
7,101✔
251
        int throw_line_ = 0;
7,101✔
252
        std::string throw_env_;
7,101✔
253
        std::string throw_config_;
7,101✔
254
        std::string throw_state_;
7,101✔
255
        std::string throw_auxinfo_;
7,101✔
256

257
        // clang-format off
258
        ar & type & what & throw_function_ & throw_file_ & throw_line_ &
7,101✔
259
            throw_locality_ & throw_hostname_ & throw_pid_ & throw_shepherd_ &
7,101✔
260
            throw_thread_id_ & throw_thread_name_ & throw_back_trace_ &
7,101✔
261
            throw_env_ & throw_config_ & throw_state_ & throw_auxinfo_;
7,101✔
262
        // clang-format on
263

264
        if (hpx::util::exception_type::hpx_exception == type)
7,101✔
265
        {
266
            // clang-format off
267
            int error_code = 0;
×
268
            ar >> error_code;
×
269
            err_value = static_cast<hpx::error>(error_code);
×
270
            // clang-format on
271
        }
×
272
        else if (hpx::util::exception_type::std_system_error == type)
7,101✔
273
        {
274
            // clang-format off
275
            int error_code = 0;
×
276
            ar >> error_code >> err_message;
×
277
            err_value = static_cast<hpx::error>(error_code);
×
278
            // clang-format on
279
        }
×
280

281
        switch (type)
7,101✔
282
        {
1✔
283
        default:
284
        case hpx::util::exception_type::std_exception:
285
            [[fallthrough]];
286

287
        case hpx::util::exception_type::unknown_exception:
288
            e = hpx::detail::construct_exception(
1✔
289
                hpx::detail::std_exception(what),
1✔
290
                hpx::detail::construct_exception_info(throw_function_,
1✔
291
                    throw_file_, throw_line_, throw_back_trace_,
1✔
292
                    throw_locality_, throw_hostname_, throw_pid_,
1✔
293
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
1✔
294
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
295
            break;
1✔
296

297
        // standard exceptions
298
        case hpx::util::exception_type::std_runtime_error:
299
            e = hpx::detail::construct_exception(std::runtime_error(what),
14,200✔
300
                hpx::detail::construct_exception_info(throw_function_,
7,100✔
301
                    throw_file_, throw_line_, throw_back_trace_,
7,100✔
302
                    throw_locality_, throw_hostname_, throw_pid_,
7,100✔
303
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
7,100✔
304
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
305
            break;
7,100✔
306

307
        case hpx::util::exception_type::std_invalid_argument:
308
            e = hpx::detail::construct_exception(std::invalid_argument(what),
×
309
                hpx::detail::construct_exception_info(throw_function_,
×
310
                    throw_file_, throw_line_, throw_back_trace_,
×
311
                    throw_locality_, throw_hostname_, throw_pid_,
×
312
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
313
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
314
            break;
×
315

316
        case hpx::util::exception_type::std_out_of_range:
317
            e = hpx::detail::construct_exception(std::out_of_range(what),
×
318
                hpx::detail::construct_exception_info(throw_function_,
×
319
                    throw_file_, throw_line_, throw_back_trace_,
×
320
                    throw_locality_, throw_hostname_, throw_pid_,
×
321
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
322
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
323
            break;
×
324

325
        case hpx::util::exception_type::std_logic_error:
326
            e = hpx::detail::construct_exception(std::logic_error(what),
×
327
                hpx::detail::construct_exception_info(throw_function_,
×
328
                    throw_file_, throw_line_, throw_back_trace_,
×
329
                    throw_locality_, throw_hostname_, throw_pid_,
×
330
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
331
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
332
            break;
×
333

334
        case hpx::util::exception_type::std_bad_alloc:
335
            e = hpx::detail::construct_exception(hpx::detail::bad_alloc(what),
×
336
                hpx::detail::construct_exception_info(throw_function_,
×
337
                    throw_file_, throw_line_, throw_back_trace_,
×
338
                    throw_locality_, throw_hostname_, throw_pid_,
×
339
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
340
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
341
            break;
×
342

343
        case hpx::util::exception_type::std_bad_cast:
344
            e = hpx::detail::construct_exception(hpx::detail::bad_cast(what),
×
345
                hpx::detail::construct_exception_info(throw_function_,
×
346
                    throw_file_, throw_line_, throw_back_trace_,
×
347
                    throw_locality_, throw_hostname_, throw_pid_,
×
348
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
349
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
350
            break;
×
351

352
        case hpx::util::exception_type::std_bad_typeid:
353
            e = hpx::detail::construct_exception(hpx::detail::bad_typeid(what),
×
354
                hpx::detail::construct_exception_info(throw_function_,
×
355
                    throw_file_, throw_line_, throw_back_trace_,
×
356
                    throw_locality_, throw_hostname_, throw_pid_,
×
357
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
358
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
359
            break;
×
360
        case hpx::util::exception_type::std_bad_exception:
361
            e = hpx::detail::construct_exception(
×
362
                hpx::detail::bad_exception(what),
×
363
                hpx::detail::construct_exception_info(throw_function_,
×
364
                    throw_file_, throw_line_, throw_back_trace_,
×
365
                    throw_locality_, throw_hostname_, throw_pid_,
×
366
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
367
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
368
            break;
×
369

370
#if ASIO_HAS_BOOST_THROW_EXCEPTION != 0
371
        // boost exceptions
372
        case hpx::util::boost_exception:
373
            HPX_ASSERT(false);    // shouldn't happen
374
            break;
375
#endif
376

377
        // boost::system::system_error
378
        case hpx::util::exception_type::boost_system_error:
379
            [[fallthrough]];
380

381
        // std::system_error
382
        case hpx::util::exception_type::std_system_error:
383
            e = hpx::detail::construct_exception(
×
384
                std::system_error(static_cast<int>(err_value),
×
385
                    std::system_category(), err_message),
×
386
                hpx::detail::construct_exception_info(throw_function_,
×
387
                    throw_file_, throw_line_, throw_back_trace_,
×
388
                    throw_locality_, throw_hostname_, throw_pid_,
×
389
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
390
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
391
            break;
×
392

393
        // hpx::exception
394
        case hpx::util::exception_type::hpx_exception:
395
            e = hpx::detail::construct_exception(
×
396
                hpx::exception(err_value, what, hpx::throwmode::rethrow),
×
397
                hpx::detail::construct_exception_info(throw_function_,
×
398
                    throw_file_, throw_line_, throw_back_trace_,
×
399
                    throw_locality_, throw_hostname_, throw_pid_,
×
400
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
401
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
402
            break;
×
403

404
        // hpx::thread_interrupted
405
        case hpx::util::exception_type::hpx_thread_interrupted_exception:
406
            e = hpx::detail::construct_lightweight_exception(
×
407
                hpx::thread_interrupted());
×
408
            break;
×
409
        }
410
    }
7,101✔
411
}    // namespace hpx::runtime_local::detail
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc