@@ -255,16 +255,25 @@ std::pair<bool, bytes> blake2Compression(bytesConstRef _in)
255255evmc_result execute (evmc_vm*, const evmc_host_interface*, evmc_host_context*,
256256 enum evmc_revision _rev, const evmc_message* _msg, const uint8_t *, size_t ) noexcept
257257{
258- static std::array<std::pair<Pricer, Executor>, 9 > const precompiles = {
259- {{ecrecoverPrice, ecrecover}, {sha256Price, sha256}, {ripemd160Price, ripemd160},
260- {identityPrice, identity}, {modexpPrice, modexp},
261- {alt_bn128_G1_addPrice, alt_bn128_G1_add}, {alt_bn128_G1_mulPrice, alt_bn128_G1_mul},
262- {alt_bn128_pairing_productPrice, alt_bn128_pairing_product},
263- {blake2CompressionPrice, blake2Compression}}};
264-
265- // TODO check range
266-
267- auto const precompile = precompiles[_msg->destination .bytes [sizeof (_msg->destination ) - 1 ] - 1 ];
258+ static constexpr std::pair<Pricer, Executor> c_precompiles[] = {{ecrecoverPrice, ecrecover},
259+ {sha256Price, sha256}, {ripemd160Price, ripemd160}, {identityPrice, identity},
260+ {modexpPrice, modexp}, {alt_bn128_G1_addPrice, alt_bn128_G1_add},
261+ {alt_bn128_G1_mulPrice, alt_bn128_G1_mul},
262+ {alt_bn128_pairing_productPrice, alt_bn128_pairing_product},
263+ {blake2CompressionPrice, blake2Compression}};
264+ static constexpr size_t c_precompilesSize = sizeof (c_precompiles) / sizeof (c_precompiles[0 ]);
265+ static_assert (c_precompilesSize < 0xFF , " Assuming no more than 256 precompiles" );
266+ static constexpr evmc::address c_maxPrecompileAddress =
267+ evmc_address{{0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , c_precompilesSize}};
268+
269+ // check that address is within the range of defined precompiles
270+ auto const destination = evmc::address{_msg->destination };
271+ if (destination == evmc::address{} || c_maxPrecompileAddress < destination)
272+ return evmc::make_result (EVMC_REJECTED, 0 , nullptr , 0 );
273+
274+ // convert address to array index
275+ auto const precompileAddressLSB = _msg->destination .bytes [sizeof (_msg->destination ) - 1 ];
276+ auto const precompile = c_precompiles[precompileAddressLSB - 1 ];
268277
269278 bytesConstRef input{_msg->input_data , _msg->input_size };
270279
0 commit comments