Merge branch 'master' into fuzz-no

pull/35196/head
Craig Tiller 1 year ago
commit 28c95606f9
  1. 57
      src/core/lib/channel/channel_args.h
  2. 31
      test/core/channel/channel_args_test.cc
  3. 5
      test/distrib/cpp/run_distrib_test_cmake_for_dll.bat

@ -183,13 +183,27 @@ struct ChannelArgTypeTraits<T,
}; };
}; };
// Determine if the pointer for a channel arg name should be const or not
template <typename T, typename SfinaeVoid = void>
struct ChannelArgPointerShouldBeConst {
static constexpr bool kValue = false;
};
template <typename T>
struct ChannelArgPointerShouldBeConst<
T, absl::void_t<decltype(T::ChannelArgUseConstPtr())>> {
static constexpr bool kValue = T::ChannelArgUseConstPtr();
};
// GetObject support for shared_ptr and RefCountedPtr // GetObject support for shared_ptr and RefCountedPtr
template <typename T, typename Ignored = void /* for SFINAE */> template <typename T, typename Ignored = void /* for SFINAE */>
struct GetObjectImpl; struct GetObjectImpl;
// std::shared_ptr implementation // std::shared_ptr implementation
template <typename T> template <typename T>
struct GetObjectImpl< struct GetObjectImpl<
T, absl::enable_if_t<SupportedSharedPtrType<T>::value, void>> { T, absl::enable_if_t<!ChannelArgPointerShouldBeConst<T>::kValue &&
SupportedSharedPtrType<T>::value,
void>> {
using Result = T*; using Result = T*;
using ReffedResult = std::shared_ptr<T>; using ReffedResult = std::shared_ptr<T>;
using StoredType = std::shared_ptr<T>*; using StoredType = std::shared_ptr<T>*;
@ -210,7 +224,9 @@ struct GetObjectImpl<
// RefCountedPtr // RefCountedPtr
template <typename T> template <typename T>
struct GetObjectImpl< struct GetObjectImpl<
T, absl::enable_if_t<!SupportedSharedPtrType<T>::value, void>> { T, absl::enable_if_t<!ChannelArgPointerShouldBeConst<T>::kValue &&
!SupportedSharedPtrType<T>::value,
void>> {
using Result = T*; using Result = T*;
using ReffedResult = RefCountedPtr<T>; using ReffedResult = RefCountedPtr<T>;
using StoredType = Result; using StoredType = Result;
@ -226,6 +242,26 @@ struct GetObjectImpl<
}; };
}; };
template <typename T>
struct GetObjectImpl<
T, absl::enable_if_t<ChannelArgPointerShouldBeConst<T>::kValue &&
!SupportedSharedPtrType<T>::value,
void>> {
using Result = const T*;
using ReffedResult = RefCountedPtr<const T>;
using StoredType = Result;
static Result Get(StoredType p) { return p; };
static ReffedResult GetReffed(StoredType p) {
if (p == nullptr) return nullptr;
return p->Ref();
};
static ReffedResult GetReffed(StoredType p, const DebugLocation& location,
const char* reason) {
if (p == nullptr) return nullptr;
return p->Ref(location, reason);
};
};
// Provide the canonical name for a type's channel arg key // Provide the canonical name for a type's channel arg key
template <typename T> template <typename T>
struct ChannelArgNameTraits { struct ChannelArgNameTraits {
@ -242,6 +278,7 @@ struct ChannelArgNameTraits<grpc_event_engine::experimental::EventEngine> {
return GRPC_INTERNAL_ARG_EVENT_ENGINE; return GRPC_INTERNAL_ARG_EVENT_ENGINE;
} }
}; };
class ChannelArgs { class ChannelArgs {
public: public:
class Pointer { class Pointer {
@ -381,6 +418,7 @@ class ChannelArgs {
GRPC_MUST_USE_RESULT auto Set(absl::string_view name, GRPC_MUST_USE_RESULT auto Set(absl::string_view name,
RefCountedPtr<T> value) const RefCountedPtr<T> value) const
-> absl::enable_if_t< -> absl::enable_if_t<
!ChannelArgPointerShouldBeConst<T>::kValue &&
std::is_same<const grpc_arg_pointer_vtable*, std::is_same<const grpc_arg_pointer_vtable*,
decltype(ChannelArgTypeTraits< decltype(ChannelArgTypeTraits<
absl::remove_cvref_t<T>>::VTable())>::value, absl::remove_cvref_t<T>>::VTable())>::value,
@ -390,6 +428,19 @@ class ChannelArgs {
ChannelArgTypeTraits<absl::remove_cvref_t<T>>::VTable())); ChannelArgTypeTraits<absl::remove_cvref_t<T>>::VTable()));
} }
template <typename T> template <typename T>
GRPC_MUST_USE_RESULT auto Set(absl::string_view name,
RefCountedPtr<const T> value) const
-> absl::enable_if_t<
ChannelArgPointerShouldBeConst<T>::kValue &&
std::is_same<const grpc_arg_pointer_vtable*,
decltype(ChannelArgTypeTraits<
absl::remove_cvref_t<T>>::VTable())>::value,
ChannelArgs> {
return Set(
name, Pointer(const_cast<T*>(value.release()),
ChannelArgTypeTraits<absl::remove_cvref_t<T>>::VTable()));
}
template <typename T>
GRPC_MUST_USE_RESULT absl::enable_if_t< GRPC_MUST_USE_RESULT absl::enable_if_t<
std::is_same< std::is_same<
const grpc_arg_pointer_vtable*, const grpc_arg_pointer_vtable*,
@ -426,6 +477,8 @@ class ChannelArgs {
absl::optional<int> GetInt(absl::string_view name) const; absl::optional<int> GetInt(absl::string_view name) const;
absl::optional<absl::string_view> GetString(absl::string_view name) const; absl::optional<absl::string_view> GetString(absl::string_view name) const;
absl::optional<std::string> GetOwnedString(absl::string_view name) const; absl::optional<std::string> GetOwnedString(absl::string_view name) const;
// WARNING: this is broken if `name` represents something that was stored as a
// RefCounted<const T> - we will discard the const-ness.
void* GetVoidPointer(absl::string_view name) const; void* GetVoidPointer(absl::string_view name) const;
template <typename T> template <typename T>
typename GetObjectImpl<T>::StoredType GetPointer( typename GetObjectImpl<T>::StoredType GetPointer(

@ -209,6 +209,37 @@ TEST(ChannelArgsTest, GetNonOwningEventEngine) {
ASSERT_EQ(p.use_count(), 2); ASSERT_EQ(p.use_count(), 2);
} }
struct MutableValue : public RefCounted<MutableValue> {
static constexpr absl::string_view ChannelArgName() {
return "grpc.test.mutable_value";
}
static int ChannelArgsCompare(const MutableValue* a, const MutableValue* b) {
return a->i - b->i;
}
int i = 42;
};
struct ConstValue : public RefCounted<ConstValue> {
static constexpr absl::string_view ChannelArgName() {
return "grpc.test.const_value";
}
static constexpr bool ChannelArgUseConstPtr() { return true; };
static int ChannelArgsCompare(const ConstValue* a, const ConstValue* b) {
return a->i - b->i;
}
int i = 42;
};
TEST(ChannelArgsTest, SetObjectRespectsMutabilityConstraints) {
auto m = MakeRefCounted<MutableValue>();
auto c = MakeRefCounted<const ConstValue>();
auto args = ChannelArgs().SetObject(m).SetObject(c);
RefCountedPtr<MutableValue> m1 = args.GetObjectRef<MutableValue>();
RefCountedPtr<const ConstValue> c1 = args.GetObjectRef<ConstValue>();
EXPECT_EQ(m1.get(), m.get());
EXPECT_EQ(c1.get(), c.get());
}
} // namespace grpc_core } // namespace grpc_core
TEST(GrpcChannelArgsTest, Create) { TEST(GrpcChannelArgsTest, Create) {

@ -78,6 +78,11 @@ popd
@rem folders, like the following command trying to imitate. @rem folders, like the following command trying to imitate.
git submodule foreach bash -c "cd $toplevel; rm -rf $name" git submodule foreach bash -c "cd $toplevel; rm -rf $name"
@rem TODO(dawidcha): Remove this once this DLL test can pass {
echo Skipped!
exit /b 0
@rem TODO(dawidcha): Remove this once this DLL test can pass }
@rem Install gRPC @rem Install gRPC
@rem NOTE(jtattermusch): The -DProtobuf_USE_STATIC_LIBS=ON is necessary on cmake3.16+ @rem NOTE(jtattermusch): The -DProtobuf_USE_STATIC_LIBS=ON is necessary on cmake3.16+
@rem since by default "find_package(Protobuf ...)" uses the cmake's builtin @rem since by default "find_package(Protobuf ...)" uses the cmake's builtin

Loading…
Cancel
Save