From d99f9334a4c4429783c7e323e0c8ac840d3f0fb9 Mon Sep 17 00:00:00 2001 From: Timothy Banks Date: Tue, 21 Jul 2020 14:33:52 -0400 Subject: [PATCH] Explicitly catch bad_alloc exceptions --- libraries/chain/apply_context.cpp | 4 ++++ libraries/chain/controller.cpp | 20 ++++++++++++++++ libraries/fc/src/network/ntp.cpp | 8 +++++++ libraries/fc/src/network/udt_socket.cpp | 8 +++++++ libraries/fc/src/rpc/cli.cpp | 8 +++++++ libraries/fc/src/rpc/http_api.cpp | 24 +++++++++++++++++++ libraries/fc/src/rpc/websocket_api.cpp | 16 +++++++++++++ libraries/rodeos/rodeos.cpp | 4 ++++ .../faucet_testnet_plugin.cpp | 4 ++++ .../http_client_plugin/http_client_plugin.cpp | 8 +++++++ plugins/net_plugin/net_plugin.cpp | 17 +++++++++++-- plugins/producer_plugin/producer_plugin.cpp | 8 +++++++ .../trace_api_plugin/utils/compress_cmd.cpp | 4 ++++ .../txn_test_gen_plugin.cpp | 8 +++++++ programs/cleos/main.cpp | 6 ++++- programs/nodeos-sectl/main.cpp | 10 +++++++- 16 files changed, 153 insertions(+), 4 deletions(-) diff --git a/libraries/chain/apply_context.cpp b/libraries/chain/apply_context.cpp index f2a8be2219f..a3841e34705 100644 --- a/libraries/chain/apply_context.cpp +++ b/libraries/chain/apply_context.cpp @@ -141,6 +141,10 @@ void apply_context::exec_one() } else { act_digest = digest_type::hash(*act); } + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch( const fc::exception& e ) { handle_exception(e); } catch ( const std::exception& e ) { diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index 924913835e2..1a6c7e4b19b 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -1219,6 +1219,10 @@ struct controller_impl { return trace; } catch( const objective_block_validation_exception& ) { throw; + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch( const fc::exception& e ) { handle_exception(e); } catch ( const std::exception& e ) { @@ -1391,6 +1395,10 @@ struct controller_impl { return trace; } catch( const objective_block_validation_exception& ) { throw; + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch( const fc::exception& e ) { handle_exception(e); } catch ( const std::exception& e) { @@ -1604,6 +1612,10 @@ struct controller_impl { return trace; } catch( const objective_block_validation_exception& ) { throw; + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch (const fc::exception& e) { handle_exception(e); } catch (const std::exception& e) { @@ -2051,6 +2063,10 @@ struct controller_impl { commit_block(false); return; + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch ( const fc::exception& e ) { edump((e.to_detail_string())); abort_block(); @@ -2237,6 +2253,10 @@ struct controller_impl { : controller::block_status::complete, trx_lookup ); fork_db.mark_valid( *ritr ); head = *ritr; + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch (const fc::exception& e) { elog("exception thrown while switching forks ${e}", ("e", e.to_detail_string())); except = std::current_exception(); diff --git a/libraries/fc/src/network/ntp.cpp b/libraries/fc/src/network/ntp.cpp index e8163f9459c..d04dacb1a67 100644 --- a/libraries/fc/src/network/ntp.cpp +++ b/libraries/fc/src/network/ntp.cpp @@ -100,6 +100,14 @@ namespace fc { throw; } + catch ( const std::bad_alloc& ) + { + throw; + } + catch ( const boost::interprocess::bad_alloc& ) + { + throw; + } // this could fail to resolve but we want to go on to other hosts.. catch ( const fc::exception& e ) { diff --git a/libraries/fc/src/network/udt_socket.cpp b/libraries/fc/src/network/udt_socket.cpp index f2e15730a4c..3806ee78707 100644 --- a/libraries/fc/src/network/udt_socket.cpp +++ b/libraries/fc/src/network/udt_socket.cpp @@ -159,6 +159,10 @@ namespace fc { { try { close(); + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch ( const fc::exception& e ) { wlog( "${e}", ("e", e.to_detail_string() ) ); } catch ( const std::exception& e ) { @@ -338,6 +342,10 @@ namespace fc { { try { close(); + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch ( const fc::exception& e ) { wlog( "${e}", ("e", e.to_detail_string() ) ); } catch ( const std::exception& e ) { diff --git a/libraries/fc/src/rpc/cli.cpp b/libraries/fc/src/rpc/cli.cpp index 858e89e43e3..64fafbe1857 100644 --- a/libraries/fc/src/rpc/cli.cpp +++ b/libraries/fc/src/rpc/cli.cpp @@ -118,6 +118,14 @@ void cli::run() else std::cout << itr->second( result, args ) << "\n"; } + catch ( const std::bad_alloc& ) + { + throw; + } + catch ( const boost::interprocess::bad_alloc& ) + { + throw; + } catch ( const fc::exception& e ) { std::cout << e.to_detail_string() << "\n"; diff --git a/libraries/fc/src/rpc/http_api.cpp b/libraries/fc/src/rpc/http_api.cpp index d20e4528cd9..41b009f267f 100644 --- a/libraries/fc/src/rpc/http_api.cpp +++ b/libraries/fc/src/rpc/http_api.cpp @@ -115,6 +115,14 @@ void http_api_connection::on_request( const fc::http::request& req, const fc::ht resp_body = fc::json::to_string( fc::rpc::response( *call.id, result ) ); resp_status = http::reply::OK; } + catch ( const std::bad_alloc& ) + { + throw; + } + catch ( const boost::interprocess::bad_alloc& ) + { + throw; + } catch ( const fc::exception& e ) { handle_error_inner(e); @@ -130,6 +138,14 @@ void http_api_connection::on_request( const fc::http::request& req, const fc::ht resp_body = ""; } } + catch ( const std::bad_alloc& ) + { + throw; + } + catch ( const boost::interprocess::bad_alloc& ) + { + throw; + } catch ( const fc::exception& e ) { handle_error(e); @@ -145,6 +161,14 @@ void http_api_connection::on_request( const fc::http::request& req, const fc::ht resp.set_length( resp_body.length() ); resp.write( resp_body.c_str(), resp_body.length() ); } + catch ( const std::bad_alloc& ) + { + throw; + } + catch ( const boost::interprocess::bad_alloc& ) + { + throw; + } catch( const fc::exception& e ) { wdump((e.to_detail_string())); diff --git a/libraries/fc/src/rpc/websocket_api.cpp b/libraries/fc/src/rpc/websocket_api.cpp index 432a90c3a79..026c822c742 100644 --- a/libraries/fc/src/rpc/websocket_api.cpp +++ b/libraries/fc/src/rpc/websocket_api.cpp @@ -123,6 +123,14 @@ std::string websocket_api_connection::on_message( return reply; } } + catch ( const std::bad_alloc& ) + { + throw; + } + catch ( const boost::interprocess::bad_alloc& ) + { + throw; + } catch ( const fc::exception& e ) { optexcept = handle_error_inner(e); @@ -147,6 +155,14 @@ std::string websocket_api_connection::on_message( _rpc_state.handle_reply( reply ); } } + catch ( const std::bad_alloc& ) + { + throw; + } + catch ( const boost::interprocess::bad_alloc& ) + { + throw; + } catch ( const fc::exception& e ) { return handle_error(e); diff --git a/libraries/rodeos/rodeos.cpp b/libraries/rodeos/rodeos.cpp index 4249f38a381..c9b443e67db 100644 --- a/libraries/rodeos/rodeos.cpp +++ b/libraries/rodeos/rodeos.cpp @@ -282,6 +282,10 @@ void rodeos_filter::process(rodeos_db_snapshot& snapshot, const ship_protocol::g } catch (...) { try { throw; + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch( const fc::exception& e ) { elog( "fc::exception processing filter wasm: ${e}", ("e", e.to_detail_string()) ); } catch( const std::exception& e ) { diff --git a/plugins/faucet_testnet_plugin/faucet_testnet_plugin.cpp b/plugins/faucet_testnet_plugin/faucet_testnet_plugin.cpp index 5c7022bae84..a9617dbc6fc 100644 --- a/plugins/faucet_testnet_plugin/faucet_testnet_plugin.cpp +++ b/plugins/faucet_testnet_plugin/faucet_testnet_plugin.cpp @@ -315,6 +315,10 @@ void faucet_testnet_plugin::plugin_startup() { void faucet_testnet_plugin::plugin_shutdown() { try { my->_timer.cancel(); + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch(const fc::exception& e) { edump((e.to_detail_string())); } catch (const std::exception& e){ diff --git a/plugins/http_client_plugin/http_client_plugin.cpp b/plugins/http_client_plugin/http_client_plugin.cpp index be8a01fb372..fa052d7ebf8 100644 --- a/plugins/http_client_plugin/http_client_plugin.cpp +++ b/plugins/http_client_plugin/http_client_plugin.cpp @@ -33,6 +33,10 @@ void http_client_plugin::plugin_initialize(const variables_map& options) { EOS_ASSERT( boost::algorithm::starts_with( pem_str, "-----BEGIN CERTIFICATE-----\n" ), chain::invalid_http_client_root_cert, "File does not appear to be a PEM encoded certificate" ); + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch ( const fc::exception& e ) { elog( "Failed to read PEM ${f} : ${e}", ("f", root_pem)( "e", e.to_detail_string())); } catch ( const std::exception& e ) { @@ -42,6 +46,10 @@ void http_client_plugin::plugin_initialize(const variables_map& options) { try { my->add_cert( pem_str ); + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch ( const fc::exception& e ) { elog( "Failed to read PEM : ${e} \n${pem}\n", ("pem", pem_str)( "e", e.to_detail_string())); } catch ( const std::exception& e ) { diff --git a/plugins/net_plugin/net_plugin.cpp b/plugins/net_plugin/net_plugin.cpp index a605d45030c..f9ed51b7394 100644 --- a/plugins/net_plugin/net_plugin.cpp +++ b/plugins/net_plugin/net_plugin.cpp @@ -1144,6 +1144,10 @@ namespace eosio { c->enqueue_sync_block(); c->do_queue_write(); + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch( const fc::exception& ex ) { fc_elog( logger, "Exception in do_queue_write to ${p} ${s}", ("p", c->peer_name())( "s", ex.to_string() ) ); } catch( const std::exception& ex ) { @@ -2475,8 +2479,17 @@ namespace eosio { } close_connection = true; } - } - catch(const fc::exception &ex) { + } + catch ( const std::bad_alloc& ) + { + throw; + } + catch ( const boost::interprocess::bad_alloc& ) + { + throw; + } + catch(const fc::exception &ex) + { fc_elog( logger, "Exception in handling read data ${s}", ("s",ex.to_string()) ); close_connection = true; } diff --git a/plugins/producer_plugin/producer_plugin.cpp b/plugins/producer_plugin/producer_plugin.cpp index 97e70873785..548019d77fc 100644 --- a/plugins/producer_plugin/producer_plugin.cpp +++ b/plugins/producer_plugin/producer_plugin.cpp @@ -913,6 +913,10 @@ void producer_plugin::plugin_startup() void producer_plugin::plugin_shutdown() { try { my->_timer.cancel(); + } catch ( const std::bad_alloc& ) { + chain_plugin::handle_bad_alloc(); + } catch ( const boost::interprocess::bad_alloc& ) { + chain_plugin::handle_bad_alloc(); } catch(const fc::exception& e) { edump((e.to_detail_string())); } catch(const std::exception& e) { @@ -1472,6 +1476,10 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block() { bool drop_features_to_activate = false; try { chain.validate_protocol_features( _protocol_features_to_activate ); + } catch ( const std::bad_alloc& ) { + chain_plugin::handle_bad_alloc(); + } catch ( const boost::interprocess::bad_alloc& ) { + chain_plugin::handle_bad_alloc(); } catch( const fc::exception& e ) { wlog( "protocol features to activate are no longer all valid: ${details}", ("details",e.to_detail_string()) ); diff --git a/plugins/trace_api_plugin/utils/compress_cmd.cpp b/plugins/trace_api_plugin/utils/compress_cmd.cpp index b2303a20c43..b235ec6e40c 100644 --- a/plugins/trace_api_plugin/utils/compress_cmd.cpp +++ b/plugins/trace_api_plugin/utils/compress_cmd.cpp @@ -118,6 +118,10 @@ namespace { } catch (const bpo::error& e) { std::cerr << "Error: " << e.what() << "\n\n"; print_help_text(std::cerr, vis_desc); + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch (const fc::exception& e) { std::cerr << "Error: " << e.to_detail_string() << "\n"; } catch (const std::exception& e) { diff --git a/plugins/txn_test_gen_plugin/txn_test_gen_plugin.cpp b/plugins/txn_test_gen_plugin/txn_test_gen_plugin.cpp index 0054ae02544..5ebe12be940 100644 --- a/plugins/txn_test_gen_plugin/txn_test_gen_plugin.cpp +++ b/plugins/txn_test_gen_plugin/txn_test_gen_plugin.cpp @@ -255,6 +255,10 @@ struct txn_test_gen_plugin_impl { trx.sign(txn_test_receiver_C_priv_key, chainid); trxs.emplace_back(std::move(trx)); } + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch (const fc::exception& e) { next(e.dynamic_copy_exception()); return; @@ -382,6 +386,10 @@ struct txn_test_gen_plugin_impl { trxs.emplace_back(std::move(trx)); } } + } catch ( const std::bad_alloc& ) { + throw; + } catch ( const boost::interprocess::bad_alloc& ) { + throw; } catch ( const fc::exception& e ) { next(e.dynamic_copy_exception()); } catch (const std::exception& e) { diff --git a/programs/cleos/main.cpp b/programs/cleos/main.cpp index 6c5946d3dc7..2fd65fc55d1 100644 --- a/programs/cleos/main.cpp +++ b/programs/cleos/main.cpp @@ -4153,8 +4153,12 @@ int main( int argc, char** argv ) { elog("connect error: ${e}", ("e", e.to_detail_string())); } return 1; + } catch ( const std::bad_alloc& ) { + elog("bad alloc"); + } catch( const boost::interprocess::bad_alloc& ) { + elog("bad alloc"); } catch (const fc::exception& e) { - return handle_error(e); + return handle_error(e); } catch (const std::exception& e) { return handle_error(fc::std_exception_wrapper::from_current_exception(e)); } diff --git a/programs/nodeos-sectl/main.cpp b/programs/nodeos-sectl/main.cpp index 28483760c6d..ac261503dcd 100644 --- a/programs/nodeos-sectl/main.cpp +++ b/programs/nodeos-sectl/main.cpp @@ -36,7 +36,15 @@ int main(int argc, char** argv) { try { bpo::store(bpo::parse_command_line(argc, argv, cli), varmap); bpo::notify(varmap); - } + } + catch ( const std::bad_alloc& ) { + elog("bad alloc"); + return BAD_ALLOC; + } + catch ( const boost::interprocess::bad_alloc& ) { + elog("bad alloc"); + return BAD_ALLOC; + } catch(const fc::exception& e) { elog("${e}", ("e", e.to_detail_string())); return 1;