Repository navigation
Description
evmc::bytes32 derives from evmc_bytes32. The C API has two arrays of 32-byte values, evmc_tx_context::blob_hashes and the emit_log topics, both typed const evmc_bytes32*. evmone uses both in ways that are undefined behavior in C++. The code works only because the two types have the same size and layout.
BLOBHASH indexes an evmc::bytes32 array through const evmc_bytes32*
The state host fills evmc_tx_context::blob_hashes from a std::vector<evmc::bytes32> (host.cpp#L371, transaction.hpp#L77), and BLOBHASH indexes it (instructions.hpp#L606):
.blob_hashes = m_tx.blob_hashes.data(),
intx::be::load<uint256>(tx.blob_hashes[static_cast<size_t>(index)])
Converting data() to const evmc_bytes32* gives a pointer to the base class subobject of element 0. That subobject is not an array element, so pointer arithmetic treats it as an array of one element ([expr.add]/4 and its footnote). Every index ≥ 1 is therefore undefined. C++20 named this case in a note to [expr.add]/6: "In particular, a pointer to a base class cannot be used for pointer arithmetic when the array contains objects of a derived class type."
The unit tests blobhash_two and blobhash_invalid_hash_version do the same with std::array<evmc::bytes32, 2> (L60-L64, L88-L92).
The emit_log trampoline downcasts the topics
evmc::internal::emit_log converts the C array back with static_cast<const bytes32*>(topics) (evmc.hpp#L838). [expr.static.cast]/12 defines the result only "if the prvalue of type “pointer to cv1 B” points to a B that is actually a base class subobject of an object of type D". Otherwise the behavior is undefined.
evmone's LOG passes a std::array<evmc::bytes32, NumTopics> (instructions.hpp#L1068). So the cast is defined when evmone is the VM, and undefined for a VM that passes evmc_bytes32 objects.
Fix direction
An array can be indexed only through a pointer to its own element type, so both sides of the API have to use the same element type. That is either evmc_bytes32 on both sides, or one C++ type on both sides with no C struct in between. Because evmc::bytes32 is derived, its implicit conversion to const evmc_bytes32* hides the mismatch. A C++ type that does not derive from the C struct makes it a compile error.
With the current EVMC API:
- BLOBHASH: the state host and the unit tests have to provide an array of
evmc_bytes32objects. - LOG: evmone has to build its topics as
evmc_bytes32, andevmc::HostInterface::emit_loghas to takeconst evmc_bytes32*. Changing only the interface would move the undefined indexing into the host.
Checked and defined
The C++ types that derive from C structs are evmc::address, evmc::bytes32, evmc::Result and evmone::VM. Their other conversions are defined:
- Upcasts:
&addrandrelease(this). - Copies:
evmc_result{*this};- the
addressandbytes32converting constructors, which the host trampolines use for the single-valueconst evmc_address*andconst evmc_bytes32*parameters.
evmone::VMdowncasts:static_cast<evmone::VM*>of anevmc_vmcreated byevmc_create_evmone(), invm.cpp,baseline_execution.cpp,test/bench/helpers.hppandtest/unittests/tracing_test.cpp.- Host context: the
Host↔evmc_host_context*round trip.
Activity