You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "JosiahWI (via GitHub)" <gi...@apache.org> on 2026/10/12 22:17:06 UTC

[PR] Call `Continuation` handlers with strong data type [trafficserver]

JosiahWI opened a new pull request, #13830:
URL: https://github.com/apache/trafficserver/pull/13830

   In this patch, the type of `ContinuationHandler` changes from a function pointer type to a struct containing a pointer to the handler and a pointer to an internal thunk that invokes the handler with the correct data type. This is a backwards-incompatible ABI change, exposed to plugins via `TSCont`.
   
   Additionally, this patch removes the constraint that `Continuation` must be listed as the first base class in multiple inheritance. As far as I can tell, that constraint was never required by the semantics, but was likely added because of a false positive GCC warning.
   
   This patch _does not_ provide a way for the event system to pass a strongly typed argument. The handler object accepts a `void*` as before, but casts it to the correct type before invoking the handler function.
   
   The new design has some similarities to the standard C++ function `std::mem_fn`. The return type of `std::mem_fn` is unspecified, so we cannot use that type here. The design also borrows functional programming terminology: a `thunk` is a deferred computation. In this context, the thunk is the deferred handler invocation.
   
   The previous code contained two bugs which are addressed in this patch:
   
   1) Calling a method through a different method type is is undefined behavior
   
   2) Comparing method pointers through the wrong type is undefined behavior
   
   TLDR: this patch makes it possible to use `int f(int, ExactType)` as an event handler without undefined behavior.


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Call `Continuation` handlers with strong data type [trafficserver]

Posted by "cmcfarlen (via GitHub)" <gi...@apache.org>.
cmcfarlen commented on PR #13830:
URL: https://github.com/apache/trafficserver/pull/13830#issuecomment-6146068116

   This PR adds a second dispatch function call to every event.  It has a cost in both size and runtime dispatch(extra function call).  I agree with Masaori that this could be done statically without either cost. I'm not convinced a change here is necessary, since the type of the second argument is also dependent on the event passed (some handlers will cast it to something else depending on the event anyway) and I don't think there is any combination of types that will cause the thunk template function to produce different code.
   
   The old code comments acknowledge the potential for UB if used outside of the intent, but if every use only passes a handler that is a member of a subclass of Continuation, its gonna be fine.


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Call `Continuation` handlers with strong data type [trafficserver]

Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on PR #13830:
URL: https://github.com/apache/trafficserver/pull/13830#issuecomment-6144799742

   I ruled out the trampoline alternative because of the code folding optimization, which I did not understand at the time. The implementation in this patch carries the same code folding risk. I think the trampoline version is both simpler and less wasteful with memory. I think I will try it.


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Call `Continuation` handlers with strong data type [trafficserver]

Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on PR #13830:
URL: https://github.com/apache/trafficserver/pull/13830#issuecomment-6138843115

   Thanks for tackling this. Fixing the UB in handler dispatch and comparison is worthwhile. Two things:
   
   **1. The QUIC handlers still go through a mismatched member-pointer type**
   
   These calls still cast to `NetVConnHandler`, i.e. `int (UnixNetVConnection::*)(int, void *)`:
   
   - `src/iocore/net/QUICNetVConnection.cc`: lines 81, 253, 356
   - `src/iocore/net/OpenSSLQUICNetVConnection.cc`: lines 102, 313, 418
   
   ```cpp
   SET_HANDLER((NetVConnHandler)&QUICNetVConnection::acceptEvent);
   ```
   
   **2. An alternative: a per-handler trampoline**
   
   You may have already considered this, but here's an alternative. Every in-tree `SET_HANDLER` argument is a compile-time constant `&X::y`. That means the handler can be a non-type template argument, and we only need to store a single function pointer:
   
   ```cpp
   using ContinuationHandler = int (*)(Continuation *, int, void *);
   
   template <typename> struct handler_traits;
   template <typename D, typename T> struct handler_traits<int (D::*)(int, T *)> {
     using cls  = D;
     using data = T;
   };
   
   template <auto M>
   int
   trampoline(Continuation *c, int event, void *data)
   {
     using Tr = handler_traits<decltype(M)>;
     return (static_cast<typename Tr::cls *>(c)->*M)(event, static_cast<typename Tr::data *>(data));
   }
   
   #define SET_HANDLER(_h) (this->handler = &trampoline<_h>)
   // comparison: handler == &trampoline<&CacheVC::dead>
   ```
   
   What this gets us:
   
   - Like this PR, it lets `int f(int, ExactType *)` be used directly as a handler: the trampoline reads `ExactType` from the member pointer's type, so no stored thunk is needed.
   - No `reinterpret_cast` anywhere. The only conversions are `static_cast<D *>` on the object and `static_cast<T *>` on the payload.
   - A mismatched cast like the QUIC ones above can't be a template argument (it's a `reinterpret_cast`), so it becomes a compile error instead of silent UB.
   - The handler shrinks to 8 bytes. Today it's 16; with this PR it's 24, and that cost lands on every `Continuation`.
   - Dispatch is one indirect call. The member call inside the trampoline goes to a known function, so it can be inlined, which avoids the virtual-bit check and offset adjustment of a member-pointer call.
   - Virtual handlers still dispatch dynamically. `Continuation` doesn't need to be the first base.
   
   The trade-offs:
   
   - Handlers must be compile-time constants. Copying, as in `save_handler = handler`, still works.
   - `==` compares function addresses. This has the same cross-DSO address-identity caveat as comparing the thunk pointers in this PR, and aggressive identical code folding (`--icf=all`) could merge trampolines that have the same body.
   - Comparison sites (`handler == &CacheVC::dead`, etc.), `SET_CONTINUATION_HANDLER` and `PUSH_HANDLER` would need to spell `&trampoline<...>`, likely behind a macro.
   


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Call `Continuation` handlers with strong data type [trafficserver]

Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on PR #13830:
URL: https://github.com/apache/trafficserver/pull/13830#issuecomment-6146319911

   @cmcfarlen The UB is not a latent risk caused by AI misuse. Dereferencing a function pointer cast to a different function pointer type is UB regardless. Every call to a Continuation handler is UB, even if the data argument has the correct effective type.


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Call `Continuation` handlers with strong data type [trafficserver]

Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on PR #13830:
URL: https://github.com/apache/trafficserver/pull/13830#issuecomment-6317147460

   [approve ci]


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Call `Continuation` handlers with strong data type [trafficserver]

Posted by "bryancall (via GitHub)" <gi...@apache.org>.
bryancall commented on code in PR #13830:
URL: https://github.com/apache/trafficserver/pull/13830#discussion_r4332852721


##########
include/iocore/eventsystem/Continuation.h:
##########
@@ -82,70 +79,135 @@ extern EThread *this_event_thread();
 */
 #define CONTINUATION_CONT 1
 
+class force_VFPT_to_top
+{
+public:
+  virtual ~force_VFPT_to_top() {}
+};
+
 /**
-  Pointer-to-member type of an event handler, as stored in
-  @c Continuation::handler.
+  A copyable binding to an event-handler member function of
+  @c Continuation or a class derived from it.
 
-  The handler receives the event code and payload of each event
-  dispatched to the Continuation, and returns a Processor-specific
-  status.
+  Bind a handler of type @c int(D::*)(int, T*), then invoke it on a
+  @c Continuation pointer with a @c void* payload, which is converted to
+  @c T* before the call. One ContinuationHandler can thus hold, over
+  time, handlers whose classes and payload types differ. Copy one to save
+  a Continuation's current handler and assign it back to restore it.
 
-  @note A handler declared in a class derived from @c Continuation MUST be
-        converted to this type with @c static_cast, which applies the
-        base-class offset.
+  A default-constructed ContinuationHandler is null: it holds no handler.
 
-  @note Invoking a converted handler is undefined behavior unless the
-        Continuation's dynamic type is, or derives from, the class that
-        declared the handler.
+  @par Thread Safety
+  Concurrent invocations and other reads are safe. Binding a handler to
+  a ContinuationHandler or assigning to it MUST NOT run concurrently
+  with any other access to it.
 */
-using ContinuationHandler = int (Continuation::*)(int, void *);
-
-template <class C, typename T>
-constexpr ContinuationHandler
-continuation_handler_void_ptr(int (C::*fp)(int, T *))
+class ContinuationHandler
 {
-  auto fp2 = reinterpret_cast<int (C::*)(int, void *)>(fp);
-
-  // We keep this a static_cast for added static type analysis from the
-  // compiler. If a compiler warning is generated for the following line of
-  // code of the type "-Werror=shift-negative-value", then this may be an issue
-  // with multiple inheritance of the C templated type. Make sure that for type
-  // C the Continuation parent is listed first (either directly or indirectly
-  // via the inheritance tree) before any other parent in the multiple class
-  // hierarchy of C.
-  return static_cast<ContinuationHandler>(fp2);
-}
+public:
+  /**
+    Binds @p M as the handler, or clears the handler if @p M is
+    @c nullptr.
 
-/**
-  Overload for a literal @c nullptr, from which the converting overload
-  cannot deduce a handler type.
+    @tparam M @c nullptr, or a non-null pointer to a member function of
+              type @c int(D::*)(int, T*), where @c D is @c Continuation
+              or a class derived from it.
 
-  @return A null @c ContinuationHandler.
+    @post If @p M is not @c nullptr, invoking this ContinuationHandler
+          calls @p M.
 
-  @par Thread Safety
-    Thread-safe.
-*/
-constexpr ContinuationHandler
-continuation_handler_void_ptr(std::nullptr_t)
-{
-#undef X
-#if !defined(__GNUC__)
-#define X 1
-#else
-#define X (__GNUC__ > 7)
-#endif
-#if X
-  static_assert(!static_cast<ContinuationHandler>(nullptr));
-#endif
-#undef X
+    @par Thread Safety
+    Not thread-safe.
+  */
+  template <auto M>
+  void
+  set()
+  {
+    if constexpr (std::is_null_pointer_v<decltype(M)>) {
+      this->handler = nullptr;
+    } else {
+      this->handler = &invoke_handler<M>;
+    }
+  }
 
-  return static_cast<ContinuationHandler>(nullptr);
-}
+  /**
+    Tests whether this ContinuationHandler holds the handler @p M.
 
-class force_VFPT_to_top
-{
-public:
-  virtual ~force_VFPT_to_top() {}
+    @tparam M @c nullptr, or a pointer to a member function of type
+              @c int(D::*)(int, T*), where @c D is @c Continuation or
+              derives from it.
+
+    @return If @p M is @c nullptr, whether this ContinuationHandler is
+            null; otherwise, whether it holds @p M.
+
+    @note @c &B::f and @c &D::f are distinct handlers even when
+          @c D::f overrides @c B::f.
+
+    @par Thread Safety
+    Safe to call concurrently with other reads of this
+    ContinuationHandler, but not with @c set or an assignment to it.
+  */
+  template <auto M>
+  bool
+  is() const
+  {
+    if constexpr (std::is_null_pointer_v<decltype(M)>) {
+      return nullptr == this->handler;
+    } else {
+      return &invoke_handler<M> == this->handler;
+    }
+  }
+
+  /**
+    Invokes the bound handler on @p c with @p event and @p data.
+
+    @param[in] b     The object to invoke the handler on.

Review Comment:
   Nit: the parameter is `c`, so this should be `@param[in] c`.



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org