Copilot commented on code in PR #13922:
URL: https://github.com/apache/trafficserver/pull/13922#discussion_r4663474147


##########
tests/fuzzing/fuzz_qpack.cc:
##########
@@ -0,0 +1,326 @@
+/** @file
+
+   fuzzing proxy/http3 QPACK
+
+   @section license License
+
+   Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license agreements.
+   See the NOTICE file distributed with this work for additional information 
regarding copyright
+   ownership.  The ASF licenses this file to you under the Apache License, 
Version 2.0 (the
+   "License"); you may not use this file except in compliance with the 
License.  You may obtain
+   a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software 
distributed under the
+   License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either
+   express or implied. See the License for the specific language governing 
permissions and
+   limitations under the License.
+*/
+
+#include "proxy/http3/QPACK.h"
+#include "proxy/http3/Http3Config.h"
+
+#include "iocore/eventsystem/EventSystem.h"
+#include "proxy/hdrs/HTTP.h"
+#include "records/RecordsConfig.h"
+#include "tscore/Layout.h"
+
+#include <algorithm>
+#include <atomic>
+#include <cctype>
+#include <chrono>
+#include <cstdlib>
+#include <functional>
+#include <string>
+#include <thread>
+#include <vector>
+
+#define kMinInputLength 3
+#define kMaxInputLength 1024
+
+#define TEST_THREADS 1
+
+extern int cmd_disable_pfreelist;
+
+namespace
+{
+// QPACK::decode() hands its result to a Continuation through an event that is
+// scheduled on an event thread.  The continuation therefore has to live for
+// the entire process and must never touch the event payload, which points at
+// per-input objects.
+class QPACKTestCont : public Continuation
+{
+public:
+  QPACKTestCont() : Continuation(new_ProxyMutex()) { 
SET_HANDLER(&QPACKTestCont::on_event); }
+
+  int
+  on_event(int /* event ATS_UNUSED */, void * /* data ATS_UNUSED */)
+  {
+    return EVENT_DONE;
+  }
+};
+
+bool
+DoInitialization()
+{
+  Layout::create();
+  RecProcessInit();
+  LibRecordsConfigInit();
+
+  ink_event_system_init(EVENT_SYSTEM_MODULE_PUBLIC_VERSION);
+  eventProcessor.start(TEST_THREADS);
+
+  // The thread libFuzzer calls in on is not an event thread; register it so
+  // that the header allocators see a valid thread instead of dereferencing a
+  // null one, exactly like src/proxy/http3/test/main_qpack.cc does.
+  Thread *main_thread = new EThread;
+  main_thread->set_specific();
+
+  // Set up the well-known header token tables; without them a recognized
+  // field name decodes to a null pointer with a non-zero length.
+  url_init();
+  mime_init();
+  http_init();
+
+  ts::Http3Config::startup();
+
+  return true;
+}
+
+// Add every "name:value" line of a payload to a HTTPHdr.
+void
+FillHeaders(std::string_view payload, HTTPHdr &hdr)
+{
+  size_t pos = 0;
+  while (pos < payload.size()) {
+    size_t eol  = payload.find('\n', pos);
+    size_t next = (eol == payload.npos) ? payload.size() : eol + 1;
+    auto   line = payload.substr(pos, ((eol == payload.npos) ? payload.size() 
: eol) - pos);
+    pos         = next;
+
+    if (!line.empty() && line.back() == '\r') {
+      line.remove_suffix(1);
+    }
+    auto colon = line.find(':');
+    if (colon == line.npos || colon == 0) {
+      continue;
+    }
+    auto name  = line.substr(0, colon);
+    auto value = line.substr(colon + 1);
+    if (name.empty()) {
+      continue;
+    }
+
+    MIMEField *field = hdr.field_create();
+    field->name_set(hdr.m_heap, hdr.m_mime, name);
+    field->value_set(hdr.m_heap, hdr.m_mime, value);
+    hdr.field_attach(field);
+  }
+}
+
+// QPACK delivers a decode's result on the thread decode() started on, and
+// keeps undelivered results on lists only that thread touches.  Dispatch
+// calls onto a started event thread and block the caller on an atomic, the
+// same way run_on_event_thread() in src/proxy/http3/test/test_QPACK.cc does.
+struct DecodeCallResult {
+  std::atomic<int>  value{0};
+  std::atomic<bool> done{false};
+};
+
+class DecodeCall : public Continuation
+{
+public:
+  DecodeCall(std::function<int()> call, std::shared_ptr<DecodeCallResult> 
result)
+    : Continuation(new_ProxyMutex()), _call(std::move(call)), 
_result(std::move(result))
+  {
+    SET_HANDLER(&DecodeCall::run);
+  }
+
+  int
+  run(int /* event ATS_UNUSED */, Event * /* event ATS_UNUSED */)
+  {
+    _result->value = _call();
+    _result->done  = true;
+    delete this;
+    return 0;
+  }
+
+private:
+  std::function<int()>              _call;
+  std::shared_ptr<DecodeCallResult> _result;
+};
+
+bool
+wait_for_decode_call(std::atomic<bool> &done)
+{
+  for (unsigned i = 0; i < 5000; ++i) {
+    if (done.load()) {
+      return true;
+    }
+    std::this_thread::sleep_for(std::chrono::milliseconds(1));
+  }
+  return done.load();
+}
+
+int
+run_on_event_thread(std::function<int()> call)
+{
+  auto result = std::make_shared<DecodeCallResult>();
+
+  eventProcessor.all_ethreads[0]->schedule_imm(new DecodeCall(std::move(call), 
result));
+  if (!wait_for_decode_call(result->done)) {
+    // The queued call still refers to the caller's locals, so unwinding past
+    // them would let it run on freed stack.
+    std::fprintf(stderr, "event thread stalled\n");
+    std::abort();
+  }
+  return result->value.load();
+}
+
+int
+decode_on_event_thread(QPACK &qpack, uint64_t stream_id, const uint8_t 
*header_block, size_t header_block_len,
+                       HTTPHdr &hdr, Continuation *cont)
+{
+  return run_on_event_thread([&] { return qpack.decode(stream_id, 
header_block, header_block_len, hdr, cont); });
+}
+
+// Withdraw blocked decodes and queued results for @a stream_id on the event
+// thread, so no undelivered event keeps pointing at this input's HTTPHdr
+// after it is destroyed.
+void
+cancel_on_event_thread(QPACK &qpack, uint64_t stream_id)
+{
+  run_on_event_thread([&] { return qpack.cancel(stream_id); });
+}
+
+} // namespace
+
+extern "C" int
+LLVMFuzzerTestOneInput(const uint8_t *input_data, size_t size_data)
+{
+  if (size_data < kMinInputLength || size_data > kMaxInputLength) {
+    return 0;
+  }
+
+  cmd_disable_pfreelist = true;
+
+  static bool          Initialized = DoInitialization();

Review Comment:
   After the first input, this assignment can race with the event thread 
reading the plain global `int` in `THREAD_FREE` 
(`include/iocore/eventsystem/ProxyAllocator.h:110`). `DecodeCall::run()` 
publishes completion before event cleanup finishes, so the next input can write 
the flag while that cleanup reads it; writing the same value still constitutes 
a C++ data race. Set the flag once during static initialization, before 
starting the event processor.



##########
tests/fuzzing/fuzz_qpack.cc:
##########
@@ -0,0 +1,326 @@
+/** @file
+
+   fuzzing proxy/http3 QPACK
+
+   @section license License
+
+   Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license agreements.
+   See the NOTICE file distributed with this work for additional information 
regarding copyright
+   ownership.  The ASF licenses this file to you under the Apache License, 
Version 2.0 (the
+   "License"); you may not use this file except in compliance with the 
License.  You may obtain
+   a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software 
distributed under the
+   License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either
+   express or implied. See the License for the specific language governing 
permissions and
+   limitations under the License.
+*/
+
+#include "proxy/http3/QPACK.h"
+#include "proxy/http3/Http3Config.h"
+
+#include "iocore/eventsystem/EventSystem.h"
+#include "proxy/hdrs/HTTP.h"
+#include "records/RecordsConfig.h"
+#include "tscore/Layout.h"
+
+#include <algorithm>
+#include <atomic>
+#include <cctype>
+#include <chrono>
+#include <cstdlib>
+#include <functional>
+#include <string>
+#include <thread>
+#include <vector>
+
+#define kMinInputLength 3
+#define kMaxInputLength 1024
+
+#define TEST_THREADS 1
+
+extern int cmd_disable_pfreelist;
+
+namespace
+{
+// QPACK::decode() hands its result to a Continuation through an event that is
+// scheduled on an event thread.  The continuation therefore has to live for
+// the entire process and must never touch the event payload, which points at
+// per-input objects.
+class QPACKTestCont : public Continuation
+{
+public:
+  QPACKTestCont() : Continuation(new_ProxyMutex()) { 
SET_HANDLER(&QPACKTestCont::on_event); }
+
+  int
+  on_event(int /* event ATS_UNUSED */, void * /* data ATS_UNUSED */)
+  {
+    return EVENT_DONE;
+  }
+};
+
+bool
+DoInitialization()
+{
+  Layout::create();
+  RecProcessInit();
+  LibRecordsConfigInit();
+
+  ink_event_system_init(EVENT_SYSTEM_MODULE_PUBLIC_VERSION);
+  eventProcessor.start(TEST_THREADS);
+
+  // The thread libFuzzer calls in on is not an event thread; register it so
+  // that the header allocators see a valid thread instead of dereferencing a
+  // null one, exactly like src/proxy/http3/test/main_qpack.cc does.
+  Thread *main_thread = new EThread;
+  main_thread->set_specific();
+
+  // Set up the well-known header token tables; without them a recognized
+  // field name decodes to a null pointer with a non-zero length.
+  url_init();
+  mime_init();
+  http_init();
+
+  ts::Http3Config::startup();
+
+  return true;
+}
+
+// Add every "name:value" line of a payload to a HTTPHdr.
+void
+FillHeaders(std::string_view payload, HTTPHdr &hdr)
+{
+  size_t pos = 0;
+  while (pos < payload.size()) {
+    size_t eol  = payload.find('\n', pos);
+    size_t next = (eol == payload.npos) ? payload.size() : eol + 1;
+    auto   line = payload.substr(pos, ((eol == payload.npos) ? payload.size() 
: eol) - pos);
+    pos         = next;
+
+    if (!line.empty() && line.back() == '\r') {
+      line.remove_suffix(1);
+    }
+    auto colon = line.find(':');
+    if (colon == line.npos || colon == 0) {
+      continue;
+    }
+    auto name  = line.substr(0, colon);
+    auto value = line.substr(colon + 1);
+    if (name.empty()) {
+      continue;
+    }
+
+    MIMEField *field = hdr.field_create();
+    field->name_set(hdr.m_heap, hdr.m_mime, name);
+    field->value_set(hdr.m_heap, hdr.m_mime, value);
+    hdr.field_attach(field);
+  }
+}
+
+// QPACK delivers a decode's result on the thread decode() started on, and
+// keeps undelivered results on lists only that thread touches.  Dispatch
+// calls onto a started event thread and block the caller on an atomic, the
+// same way run_on_event_thread() in src/proxy/http3/test/test_QPACK.cc does.
+struct DecodeCallResult {
+  std::atomic<int>  value{0};
+  std::atomic<bool> done{false};
+};
+
+class DecodeCall : public Continuation
+{
+public:
+  DecodeCall(std::function<int()> call, std::shared_ptr<DecodeCallResult> 
result)
+    : Continuation(new_ProxyMutex()), _call(std::move(call)), 
_result(std::move(result))
+  {
+    SET_HANDLER(&DecodeCall::run);
+  }
+
+  int
+  run(int /* event ATS_UNUSED */, Event * /* event ATS_UNUSED */)
+  {
+    _result->value = _call();
+    _result->done  = true;
+    delete this;
+    return 0;
+  }
+
+private:
+  std::function<int()>              _call;
+  std::shared_ptr<DecodeCallResult> _result;
+};
+
+bool
+wait_for_decode_call(std::atomic<bool> &done)
+{
+  for (unsigned i = 0; i < 5000; ++i) {
+    if (done.load()) {
+      return true;
+    }
+    std::this_thread::sleep_for(std::chrono::milliseconds(1));

Review Comment:
   When the event has not finished by the first check, this forces a 1 ms sleep 
even if it completes immediately afterward. Every raw-decode input makes two 
such handoffs (decode and cancel), so fast inputs typically spend roughly 2 ms 
just polling, restricting throughput to a few hundred executions per second and 
substantially reducing fuzzing coverage. Please use a notification-based timed 
wait instead; combining decode and cancel into one scheduled call would also 
avoid the second handoff.



-- 
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]

Reply via email to