Browse Source

Exception-throwing assert.

cl-refactor
chriseth 10 years ago
parent
commit
79fb35fd69
  1. 111
      libdevcore/Assertions.h
  2. 42
      libdevcore/Common.h
  3. 1
      libdevcrypto/CryptoPP.cpp
  4. 1
      libethereum/BlockChain.cpp
  5. 1
      libethereum/State.cpp
  6. 1
      libevm/VMFactory.cpp
  7. 5
      libevmcore/Assembly.h
  8. 13
      libevmcore/Instruction.h
  9. 1
      libp2p/Host.cpp
  10. 1
      libp2p/Network.cpp
  11. 4
      libp2p/RLPxFrameIO.cpp
  12. 29
      libsolidity/Utils.h
  13. 1
      test/checkRandomStateTest.cpp
  14. 1
      test/checkRandomVMTest.cpp
  15. 1
      test/net.cpp

111
libdevcore/Assertions.h

@ -0,0 +1,111 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file Assertions.h
* @author Christian <c@ethdev.com>
* @date 2015
*
* Assertion handling.
*/
#pragma once
#include "Exceptions.h"
#include "debugbreak.h"
namespace dev
{
#if defined(_MSC_VER)
#define ETH_FUNC __FUNCSIG__
#elif defined(__GNUC__)
#define ETH_FUNC __PRETTY_FUNCTION__
#else
#define ETH_FUNC __func__
#endif
#define asserts(A) ::dev::assertAux(A, #A, __LINE__, __FILE__, ETH_FUNC)
#define assertsEqual(A, B) ::dev::assertEqualAux(A, B, #A, #B, __LINE__, __FILE__, ETH_FUNC)
inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func)
{
bool ret = _a;
if (!ret)
{
std::cerr << "Assertion failed:" << _aStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
#if ETH_DEBUG
debug_break();
#endif
}
return !ret;
}
template<class A, class B>
inline bool assertEqualAux(A const& _a, B const& _b, char const* _aStr, char const* _bStr, unsigned _line, char const* _file, char const* _func)
{
bool ret = _a == _b;
if (!ret)
{
std::cerr << "Assertion failed: " << _aStr << " == " << _bStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
std::cerr << " Fail equality: " << _a << "==" << _b << std::endl;
#if ETH_DEBUG
debug_break();
#endif
}
return !ret;
}
/// Assertion that throws an exception containing the given description if it is not met.
/// Use it as assertThrow(1 == 1, ExceptionType, "Mathematics is wrong.");
/// Do NOT supply an exception object as the second parameter.
#define assertThrow(_condition, _ExceptionType, _description) \
::dev::assertThrowAux<_ExceptionType>(_condition, _description, __LINE__, __FILE__, ETH_FUNC)
using errinfo_comment = boost::error_info<struct tag_comment, std::string>;
template <class _ExceptionType>
inline void assertThrowAux(
bool _condition,
::std::string const& _errorDescription,
unsigned _line,
char const* _file,
char const* _function
)
{
if (!_condition)
::boost::throw_exception(
_ExceptionType() <<
::dev::errinfo_comment(_errorDescription) <<
::boost::throw_function(_function) <<
::boost::throw_file(_file) <<
::boost::throw_line(_line)
);
}
template <class _ExceptionType>
inline void assertThrowAux(
void const* _pointer,
::std::string const& _errorDescription,
unsigned _line,
char const* _file,
char const* _function
)
{
assertThrowAux<_ExceptionType>(_pointer != nullptr, _errorDescription, _line, _file, _function);
}
}

42
libdevcore/Common.h

@ -44,7 +44,6 @@
#pragma warning(pop)
#pragma GCC diagnostic pop
#include "vector_ref.h"
#include "debugbreak.h"
// CryptoPP defines byte in the global namespace, so must we.
using byte = uint8_t;
@ -135,45 +134,4 @@ private:
std::function<void(void)> m_f;
};
// Assertions...
#if defined(_MSC_VER)
#define ETH_FUNC __FUNCSIG__
#elif defined(__GNUC__)
#define ETH_FUNC __PRETTY_FUNCTION__
#else
#define ETH_FUNC __func__
#endif
#define asserts(A) ::dev::assertAux(A, #A, __LINE__, __FILE__, ETH_FUNC)
#define assertsEqual(A, B) ::dev::assertEqualAux(A, B, #A, #B, __LINE__, __FILE__, ETH_FUNC)
inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func)
{
bool ret = _a;
if (!ret)
{
std::cerr << "Assertion failed:" << _aStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
#if ETH_DEBUG
debug_break();
#endif
}
return !ret;
}
template<class A, class B>
inline bool assertEqualAux(A const& _a, B const& _b, char const* _aStr, char const* _bStr, unsigned _line, char const* _file, char const* _func)
{
bool ret = _a == _b;
if (!ret)
{
std::cerr << "Assertion failed: " << _aStr << " == " << _bStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
std::cerr << " Fail equality: " << _a << "==" << _b << std::endl;
#if ETH_DEBUG
debug_break();
#endif
}
return !ret;
}
}

1
libdevcrypto/CryptoPP.cpp

@ -20,6 +20,7 @@
*/
#include <libdevcore/Guards.h>
#include <libdevcore/Assertions.h>
#include "ECDHE.h"
#include "CryptoPP.h"

1
libethereum/BlockChain.cpp

@ -26,6 +26,7 @@
#include <boost/filesystem.hpp>
#include <test/JsonSpiritHeaders.h>
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/RLP.h>
#include <libdevcore/StructuredLogger.h>
#include <libdevcrypto/FileSystem.h>

1
libethereum/State.cpp

@ -27,6 +27,7 @@
#include <boost/timer.hpp>
#include <secp256k1/secp256k1.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/StructuredLogger.h>
#include <libevmcore/Instruction.h>
#include <libethcore/Exceptions.h>

1
libevm/VMFactory.cpp

@ -16,6 +16,7 @@
*/
#include "VMFactory.h"
#include <libdevcore/Assertions.h>
#include "VM.h"
#if ETH_EVMJIT

5
libevmcore/Assembly.h

@ -24,6 +24,7 @@
#include <iostream>
#include <sstream>
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libevmcore/SourceLocation.h>
#include <libevmcore/Instruction.h>
#include "Exceptions.h"
@ -48,8 +49,8 @@ public:
AssemblyItem(Instruction _i): m_type(Operation), m_data((byte)_i) {}
AssemblyItem(AssemblyItemType _type, u256 _data = 0): m_type(_type), m_data(_data) {}
AssemblyItem tag() const { if (asserts(m_type == PushTag || m_type == Tag)) BOOST_THROW_EXCEPTION(Exception()); return AssemblyItem(Tag, m_data); }
AssemblyItem pushTag() const { if (asserts(m_type == PushTag || m_type == Tag)) BOOST_THROW_EXCEPTION(Exception()); return AssemblyItem(PushTag, m_data); }
AssemblyItem tag() const { assertThrow(m_type == PushTag || m_type == Tag, Exception, ""); return AssemblyItem(Tag, m_data); }
AssemblyItem pushTag() const { assertThrow(m_type == PushTag || m_type == Tag, Exception, ""); return AssemblyItem(PushTag, m_data); }
AssemblyItemType type() const { return m_type; }
u256 data() const { return m_data; }

13
libevmcore/Instruction.h

@ -22,6 +22,7 @@
#pragma once
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libevmcore/Exceptions.h>
namespace dev
@ -194,32 +195,28 @@ inline unsigned getSwapNumber(Instruction _inst)
/// @returns the PUSH<_number> instruction
inline Instruction pushInstruction(unsigned _number)
{
if (asserts(1 <= _number && _number <= 32))
BOOST_THROW_EXCEPTION(InvalidOpcode() << errinfo_comment("Invalid PUSH instruction requested."));
assertThrow(1 <= _number && _number <= 32, InvalidOpcode, "Invalid PUSH instruction requested.");
return Instruction(unsigned(Instruction::PUSH1) + _number - 1);
}
/// @returns the DUP<_number> instruction
inline Instruction dupInstruction(unsigned _number)
{
if (asserts(1 <= _number && _number <= 16))
BOOST_THROW_EXCEPTION(InvalidOpcode() << errinfo_comment("Invalid DUP instruction requested."));
assertThrow(1 <= _number && _number <= 16, InvalidOpcode, "Invalid DUP instruction requested.");
return Instruction(unsigned(Instruction::DUP1) + _number - 1);
}
/// @returns the SWAP<_number> instruction
inline Instruction swapInstruction(unsigned _number)
{
if (asserts(1 <= _number && _number <= 16))
BOOST_THROW_EXCEPTION(InvalidOpcode() << errinfo_comment("Invalid SWAP instruction requested."));
assertThrow(1 <= _number && _number <= 16, InvalidOpcode, "Invalid SWAP instruction requested.");
return Instruction(unsigned(Instruction::SWAP1) + _number - 1);
}
/// @returns the LOG<_number> instruction
inline Instruction logInstruction(unsigned _number)
{
if (asserts(_number <= 4))
BOOST_THROW_EXCEPTION(InvalidOpcode() << errinfo_comment("Invalid LOG instruction requested."));
assertThrow(_number <= 4, InvalidOpcode, "Invalid LOG instruction requested.");
return Instruction(unsigned(Instruction::LOG0) + _number);
}

1
libp2p/Host.cpp

@ -27,6 +27,7 @@
#include <memory>
#include <boost/algorithm/string.hpp>
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/StructuredLogger.h>
#include <libethcore/Exceptions.h>

1
libp2p/Network.cpp

@ -29,6 +29,7 @@
#include <boost/algorithm/string.hpp>
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/CommonIO.h>
#include <libethcore/Exceptions.h>
#include "Common.h"

4
libp2p/RLPxFrameIO.cpp

@ -19,11 +19,13 @@
* @date 2015
*/
#include "RLPxFrameIO.h"
#include <libdevcore/Assertions.h>
#include "Host.h"
#include "Session.h"
#include "Peer.h"
#include "RLPxHandshake.h"
#include "RLPxFrameIO.h"
using namespace std;
using namespace dev;
using namespace dev::p2p;

29
libsolidity/Utils.h

@ -22,34 +22,9 @@
#pragma once
#include <string>
#include <libsolidity/Exceptions.h>
namespace dev
{
namespace solidity
{
#include <libdevcore/Assertions.h>
/// Assertion that throws an InternalCompilerError containing the given description if it is not met.
#define solAssert(CONDITION, DESCRIPTION) \
::dev::solidity::solAssertAux(CONDITION, DESCRIPTION, __LINE__, __FILE__, ETH_FUNC)
inline void solAssertAux(bool _condition, std::string const& _errorDescription, unsigned _line,
char const* _file, char const* _function)
{
if (!_condition)
::boost::throw_exception( InternalCompilerError()
<< errinfo_comment(_errorDescription)
<< ::boost::throw_function(_function)
<< ::boost::throw_file(_file)
<< ::boost::throw_line(_line));
}
inline void solAssertAux(void const* _pointer, std::string const& _errorDescription, unsigned _line,
char const* _file, char const* _function)
{
solAssertAux(_pointer != nullptr, _errorDescription, _line, _file, _function);
}
assertThrow(CONDITION, ::dev::solidity::InternalCompilerError, DESCRIPTION)
}
}

1
test/checkRandomStateTest.cpp

@ -21,6 +21,7 @@
*/
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/Exceptions.h>
#include <libdevcore/Log.h>
#include <libevm/VMFactory.h>

1
test/checkRandomVMTest.cpp

@ -21,6 +21,7 @@
*/
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/Exceptions.h>
#include <libdevcore/Log.h>
#include <libevm/VMFactory.h>

1
test/net.cpp

@ -22,6 +22,7 @@
#include <boost/test/unit_test.hpp>
#include <libdevcore/Worker.h>
#include <libdevcore/Assertions.h>
#include <libdevcrypto/Common.h>
#include <libp2p/UDP.h>
#include <libp2p/NodeTable.h>

Loading…
Cancel
Save