gemini-code-assist[bot] commented on code in PR #451:
URL: https://github.com/apache/tvm-ffi/pull/451#discussion_r2810003566
##########
include/tvm/ffi/any.h:
##########
@@ -623,13 +624,76 @@ struct AnyHash {
return details::StableHashCombine(src.data_.type_index,
details::StableHashBytes(src_str->data, src_str->size));
} else {
+ if (src.data_.type_index >= TypeIndex::kTVMFFIStaticObjectBegin) {
+ static const TVMFFITypeAttrColumn* custom_hash_column =
GetAnyHashTypeAttrColumn();
+ if (custom_hash_column != nullptr &&
+ static_cast<size_t>(src.data_.type_index) <
custom_hash_column->size) {
+ const TVMFFIAny& custom_any_hash =
custom_hash_column->data[src.data_.type_index];
+ if (custom_any_hash.type_index != TypeIndex::kTVMFFINone) {
+ return details::StableHashCombine(src.data_.type_index,
+
CallCustomAnyHash(custom_any_hash, src));
Review Comment:

The custom hash function is invoked here. If the custom hash function (which
could be a Python function via FFI) throws an exception, it will propagate out
of `AnyHash::operator()`. This is problematic because hash functions used with
`std::unordered_map` (which `tvm::Map` is based on) are not allowed to throw
exceptions. Doing so results in undefined behavior.
A similar issue exists for `AnyEqual`.
To fix this, you should wrap the call to `CallCustomAnyHash` in a
`try-catch` block and handle the exception appropriately. Given that an error
during hashing is a severe problem, logging a fatal error and aborting is the
safest option.
##########
include/tvm/ffi/any.h:
##########
@@ -657,6 +721,16 @@ struct AnyEqual {
details::AnyUnsafe::CopyFromAnyViewAfterCheck<const
details::BytesObjBase*>(rhs);
return Bytes::memequal(lhs_str->data, rhs_str->data, lhs_str->size,
rhs_str->size);
}
+ if (lhs.data_.type_index >= TypeIndex::kTVMFFIStaticObjectBegin) {
+ static const TVMFFITypeAttrColumn* custom_equal_column =
GetAnyEqualTypeAttrColumn();
+ if (custom_equal_column != nullptr &&
+ static_cast<size_t>(lhs.data_.type_index) <
custom_equal_column->size) {
+ const TVMFFIAny& custom_any_equal =
custom_equal_column->data[lhs.data_.type_index];
+ if (custom_any_equal.type_index != TypeIndex::kTVMFFINone) {
+ return CallCustomAnyEqual(custom_any_equal, lhs, rhs);
Review Comment:

The custom equality function is invoked here. If this function throws an
exception, it will propagate out of `AnyEqual::operator()`. The equality
function for `std::unordered_map` is not allowed to throw, and doing so leads
to undefined behavior.
To prevent this, the call to `CallCustomAnyEqual` should be wrapped in a
`try-catch` block. If an exception is caught, it should be handled, for
instance by logging a fatal error and aborting.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]