From e805ba279177b91f06b09a7094dd550fbd42b59f Mon Sep 17 00:00:00 2001 From: Daniel Hams Date: Wed, 19 Feb 2014 08:21:42 +0000 Subject: [PATCH 1/5] Workaround for clang bug of ambiguous constructor using array indexing []. --- libethereum/AddressState.h | 8 +++++++- libethereum/State.cpp | 16 ++++++++++++++-- libethereum/State.h | 12 ++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/libethereum/AddressState.h b/libethereum/AddressState.h index 1b26bbc3c..da7c9acf2 100644 --- a/libethereum/AddressState.h +++ b/libethereum/AddressState.h @@ -43,7 +43,13 @@ public: AddressState(u256 _balance, u256 _nonce, u256s _memory): m_type(AddressType::Contract), m_balance(_balance), m_nonce(_nonce), m_haveMemory(true) { for (unsigned i = 0; i < _memory.size(); ++i) - m_memory[(u256)i] = _memory[i]; + { + auto mFinder = m_memory.find((u256)i); + if (mFinder == m_memory.end()) + m_memory.emplace((u256)i,_memory[i]); + else + mFinder->second = _memory[i]; + } } void incNonce() { m_nonce++; } diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 8badddcb6..55fc40fe5 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1158,15 +1158,27 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256s break; }*/ case Instruction::MLOAD: + { require(1); - stack.back() = tempMem[stack.back()]; + auto mFinder = tempMem.find(stack.back()); + if (mFinder != tempMem.end()) + stack.back() = mFinder->second; + else + throw BadInstruction(); break; + } case Instruction::MSTORE: + { require(2); - tempMem[stack.back()] = stack[stack.size() - 2]; + auto mFinder = tempMem.find(stack.back()); + if (mFinder == tempMem.end()) + tempMem.emplace(stack.back(), stack[stack.size() - 2]); + else + mFinder->second = stack[stack.size() - 2]; stack.pop_back(); stack.pop_back(); break; + } case Instruction::SLOAD: require(1); stack.back() = store(stack.back()); diff --git a/libethereum/State.h b/libethereum/State.h index af7166eb5..008624573 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -272,7 +272,11 @@ inline std::ostream& operator<<(std::ostream& _out, State const& _s) for (auto const& j: memdb) { _out << std::endl << " [" << j.first << ":" << asHex(j.second) << "]"; - mem[j.first] = RLP(j.second).toInt(); + auto mFinder = mem.find(j.first); + if (mFinder == mem.end()) + mem.emplace(j.first, RLP(j.second).toInt()); + else + mFinder->second = RLP(j.second).toInt(); } _out << std::endl << mem; } @@ -301,7 +305,11 @@ inline std::ostream& operator<<(std::ostream& _out, State const& _s) for (auto const& j: memdb) { _out << std::endl << " [" << j.first << ":" << asHex(j.second) << "]"; - mem[j.first] = RLP(j.second).toInt(); + auto mFinder = mem.find(j.first); + if (mFinder == mem.end()) + mem.emplace(j.first, RLP(j.second).toInt()); + else + mFinder->second = RLP(j.second).toInt(); } _out << std::endl << mem; } From 09677d9f3edfe54729c722573ae0190bcd337a03 Mon Sep 17 00:00:00 2001 From: Daniel Hams Date: Wed, 19 Feb 2014 08:58:46 +0000 Subject: [PATCH 2/5] Bug fix missing parens in log call to Id::name() --- libethereum/Common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libethereum/Common.h b/libethereum/Common.h index bbb319d4e..12028d7a1 100644 --- a/libethereum/Common.h +++ b/libethereum/Common.h @@ -216,7 +216,7 @@ public: char buf[24]; if (strftime(buf, 24, "%X", localtime(&rawTime)) == 0) buf[0] = '\0'; // empty if case strftime fails - sstr << Id::name << " [ " << buf << " | " << *(t_logThreadName.m_name.get()) << (_term ? " ] " : ""); + sstr << Id::name() << " [ " << buf << " | " << *(t_logThreadName.m_name.get()) << (_term ? " ] " : ""); } } ~LogOutputStream() { if (Id::verbosity <= g_logVerbosity) g_logPost(sstr.str(), Id::name()); } From 56359a45aade8d47824cfd443b13373def9e5685 Mon Sep 17 00:00:00 2001 From: Daniel Hams Date: Wed, 19 Feb 2014 09:15:04 +0000 Subject: [PATCH 3/5] Use insert rather than emplace - emplace isn't used elsewhere and I'm not sure windows supports it. --- libethereum/AddressState.h | 2 +- libethereum/State.cpp | 2 +- libethereum/State.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libethereum/AddressState.h b/libethereum/AddressState.h index da7c9acf2..044bb269e 100644 --- a/libethereum/AddressState.h +++ b/libethereum/AddressState.h @@ -46,7 +46,7 @@ public: { auto mFinder = m_memory.find((u256)i); if (mFinder == m_memory.end()) - m_memory.emplace((u256)i,_memory[i]); + m_memory.insert(std::pair((u256)i,_memory[i])); else mFinder->second = _memory[i]; } diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 55fc40fe5..613082f84 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1172,7 +1172,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256s require(2); auto mFinder = tempMem.find(stack.back()); if (mFinder == tempMem.end()) - tempMem.emplace(stack.back(), stack[stack.size() - 2]); + tempMem.insert(std::pair(stack.back(), stack[stack.size() - 2])); else mFinder->second = stack[stack.size() - 2]; stack.pop_back(); diff --git a/libethereum/State.h b/libethereum/State.h index 008624573..5326b73d5 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -274,7 +274,7 @@ inline std::ostream& operator<<(std::ostream& _out, State const& _s) _out << std::endl << " [" << j.first << ":" << asHex(j.second) << "]"; auto mFinder = mem.find(j.first); if (mFinder == mem.end()) - mem.emplace(j.first, RLP(j.second).toInt()); + mem.insert(std::pair(j.first, RLP(j.second).toInt())); else mFinder->second = RLP(j.second).toInt(); } @@ -307,7 +307,7 @@ inline std::ostream& operator<<(std::ostream& _out, State const& _s) _out << std::endl << " [" << j.first << ":" << asHex(j.second) << "]"; auto mFinder = mem.find(j.first); if (mFinder == mem.end()) - mem.emplace(j.first, RLP(j.second).toInt()); + mem.insert(std::pair(j.first, RLP(j.second).toInt())); else mFinder->second = RLP(j.second).toInt(); } From c8982722284c62c514e145af5d28d87cac2e6bd9 Mon Sep 17 00:00:00 2001 From: Daniel Hams Date: Wed, 19 Feb 2014 18:22:02 +0000 Subject: [PATCH 4/5] Fix MLOAD case to set stack.back() to zero and ifdef the clang workarounds for map::operator[]. --- libethereum/AddressState.h | 4 ++++ libethereum/State.cpp | 22 +++++++++++++++++++++- libethereum/State.h | 8 ++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/libethereum/AddressState.h b/libethereum/AddressState.h index 044bb269e..1f514d66a 100644 --- a/libethereum/AddressState.h +++ b/libethereum/AddressState.h @@ -43,6 +43,7 @@ public: AddressState(u256 _balance, u256 _nonce, u256s _memory): m_type(AddressType::Contract), m_balance(_balance), m_nonce(_nonce), m_haveMemory(true) { for (unsigned i = 0; i < _memory.size(); ++i) +#ifdef __clang__ { auto mFinder = m_memory.find((u256)i); if (mFinder == m_memory.end()) @@ -50,6 +51,9 @@ public: else mFinder->second = _memory[i]; } +#else + m_memory[(u256)i] = _memory[i]; +#endif } void incNonce() { m_nonce++; } diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 613082f84..7d4aa4177 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -179,10 +179,14 @@ void State::ensureCached(Address _a, bool _requireMemory, bool _forceCreate) con TrieDB memdb(const_cast(&m_db), it->second.oldRoot()); // promise we won't alter the overlay! :) map& mem = it->second.setHaveMemory(); for (auto const& i: memdb) +#ifdef __clang__ if (mem.find(i.first) == mem.end()) mem.insert(make_pair(i.first, RLP(i.second).toInt())); else mem.at(i.first) = RLP(i.second).toInt(); +#else + mem[i.first] = RLP(i.second).toInt(); +#endif } } @@ -707,10 +711,14 @@ void State::executeBare(Transaction const& _t, Address _sender) m_cache[newAddress] = AddressState(0, 0, AddressType::Contract); auto& mem = m_cache[newAddress].memory(); for (uint i = 0; i < _t.data.size(); ++i) +#ifdef __clang__ if (mem.find(i) == mem.end()) mem.insert(make_pair(i, _t.data[i])); else mem.at(i) = _t.data[i]; +#else + mem[i] = _t.data[i]; +#endif #if ETH_SENDER_PAYS_SETUP subBalance(_sender, _t.value + fee); @@ -752,11 +760,15 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256s { if (_v) { +#ifdef __clang__ auto it = myStore.find(_n); if (it == myStore.end()) myStore.insert(make_pair(_n, _v)); else myStore.at(_n) = _v; +#else + myStore[_n] = _v; +#endif } else myStore.erase(_n); @@ -1160,21 +1172,29 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256s case Instruction::MLOAD: { require(1); +#ifdef __clang__ auto mFinder = tempMem.find(stack.back()); if (mFinder != tempMem.end()) stack.back() = mFinder->second; else - throw BadInstruction(); + stack.back() = 0; +#else + stack.back() = tempMem[stack.back()]; +#endif break; } case Instruction::MSTORE: { require(2); +#ifdef __clang__ auto mFinder = tempMem.find(stack.back()); if (mFinder == tempMem.end()) tempMem.insert(std::pair(stack.back(), stack[stack.size() - 2])); else mFinder->second = stack[stack.size() - 2]; +#else + tempMem[stack.back()] = stack[stack.size() - 2]; +#endif stack.pop_back(); stack.pop_back(); break; diff --git a/libethereum/State.h b/libethereum/State.h index 5326b73d5..2753f5549 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -272,11 +272,15 @@ inline std::ostream& operator<<(std::ostream& _out, State const& _s) for (auto const& j: memdb) { _out << std::endl << " [" << j.first << ":" << asHex(j.second) << "]"; +#ifdef __clang__ auto mFinder = mem.find(j.first); if (mFinder == mem.end()) mem.insert(std::pair(j.first, RLP(j.second).toInt())); else mFinder->second = RLP(j.second).toInt(); +#else + mem[j.first] = RLP(j.second).toInt(); +#endif } _out << std::endl << mem; } @@ -305,11 +309,15 @@ inline std::ostream& operator<<(std::ostream& _out, State const& _s) for (auto const& j: memdb) { _out << std::endl << " [" << j.first << ":" << asHex(j.second) << "]"; +#ifdef __clang__ auto mFinder = mem.find(j.first); if (mFinder == mem.end()) mem.insert(std::pair(j.first, RLP(j.second).toInt())); else mFinder->second = RLP(j.second).toInt(); +#else + mem[j.first] = RLP(j.second).toInt(); +#endif } _out << std::endl << mem; } From 23b8784fd656afb866a78784afafadf05a8b95cd Mon Sep 17 00:00:00 2001 From: Daniel Hams Date: Wed, 19 Feb 2014 18:34:03 +0000 Subject: [PATCH 5/5] Use make_pair in place of std::pair. --- libethereum/State.cpp | 2 +- libethereum/State.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 7d4aa4177..9f2537871 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1189,7 +1189,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256s #ifdef __clang__ auto mFinder = tempMem.find(stack.back()); if (mFinder == tempMem.end()) - tempMem.insert(std::pair(stack.back(), stack[stack.size() - 2])); + tempMem.insert(make_pair(stack.back(), stack[stack.size() - 2])); else mFinder->second = stack[stack.size() - 2]; #else diff --git a/libethereum/State.h b/libethereum/State.h index 2753f5549..23d5fbbc3 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -275,7 +275,7 @@ inline std::ostream& operator<<(std::ostream& _out, State const& _s) #ifdef __clang__ auto mFinder = mem.find(j.first); if (mFinder == mem.end()) - mem.insert(std::pair(j.first, RLP(j.second).toInt())); + mem.insert(std::make_pair(j.first, RLP(j.second).toInt())); else mFinder->second = RLP(j.second).toInt(); #else @@ -312,7 +312,7 @@ inline std::ostream& operator<<(std::ostream& _out, State const& _s) #ifdef __clang__ auto mFinder = mem.find(j.first); if (mFinder == mem.end()) - mem.insert(std::pair(j.first, RLP(j.second).toInt())); + mem.insert(std::make_pair(j.first, RLP(j.second).toInt())); else mFinder->second = RLP(j.second).toInt(); #else