Copilot commented on code in PR #3516:
URL: https://github.com/apache/brpc/pull/3516#discussion_r3958353325


##########
test/fuzzing/oss-fuzz.sh:
##########
@@ -53,4 +53,5 @@ zip $OUT/fuzz_redis_seed_corpus.zip fuzz_redis_seed_corpus/*
 zip $OUT/fuzz_http_seed_corpus.zip  fuzz_http_seed_corpus/*
 zip $OUT/fuzz_butil_seed_corpus.zip fuzz_butil_seed_corpus/*
 zip $OUT/fuzz_hpack_seed_corpus.zip fuzz_hpack_seed_corpus/*
+zip $OUT/fuzz_rtmp_seed_corpus.zip  fuzz_rtmp_seed_corpus/*

Review Comment:
   This zip step will fail the OSS-Fuzz build if `fuzz_rtmp_seed_corpus/` 
doesn't exist or is empty (shell glob can expand to a literal and `zip` exits 
non-zero). Add a guard (e.g., check directory existence and that it contains 
files) or ensure an initial seed corpus directory/files are always present in 
the repo.



##########
test/fuzzing/fuzz_rtmp.cpp:
##########
@@ -0,0 +1,81 @@
+// 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 "brpc/policy/rtmp_protocol.h"
+#include "brpc/rtmp.h"
+#include "brpc/server.h"
+#include "butil/iobuf.h"
+#include "fuzz_common.h"
+
+namespace {
+
+constexpr size_t kMinInputLength = 5;
+constexpr size_t kMaxInputLength = 4096;
+
+class FuzzRtmpService : public brpc::RtmpService {
+public:
+    brpc::RtmpServerStream* NewStream(const brpc::RtmpConnectRequest&) 
override {
+        return nullptr;
+    }
+};
+
+brpc::Server* get_fuzz_server() {
+    static brpc::Server* started = []() -> brpc::Server* {
+        static FuzzRtmpService rtmp_service;
+        static brpc::Server server;
+        brpc::ServerOptions options;
+        options.rtmp_service = &rtmp_service;
+        options.has_builtin_services = false;
+        options.num_threads = 0;
+        if (server.Start(0, &options) != 0) {
+            // Fall back to a context with no service
+            LOG(ERROR) << "Fail to start fuzz RTMP server, "
+                          "server-side command handlers will be skipped";
+            return nullptr;
+        }
+
+        // Stop again straight away. _options -- and so rtmp_service -- is not
+        // touched by Stop()/Join()
+        server.Stop(0);
+        server.Join();
+        return &server;
+    }();
+    return started;
+}
+
+}  // namespace
+
+extern "C" int
+LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+    if (size < kMinInputLength || size > kMaxInputLength){

Review Comment:
   Minor formatting: add a space before `{` to match typical C++ style used 
elsewhere (e.g., `... kMaxInputLength) {`).



##########
test/fuzzing/fuzz_rtmp.cpp:
##########
@@ -0,0 +1,81 @@
+// 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 "brpc/policy/rtmp_protocol.h"
+#include "brpc/rtmp.h"
+#include "brpc/server.h"
+#include "butil/iobuf.h"
+#include "fuzz_common.h"
+
+namespace {
+
+constexpr size_t kMinInputLength = 5;
+constexpr size_t kMaxInputLength = 4096;
+
+class FuzzRtmpService : public brpc::RtmpService {
+public:
+    brpc::RtmpServerStream* NewStream(const brpc::RtmpConnectRequest&) 
override {
+        return nullptr;
+    }
+};
+
+brpc::Server* get_fuzz_server() {
+    static brpc::Server* started = []() -> brpc::Server* {
+        static FuzzRtmpService rtmp_service;
+        static brpc::Server server;
+        brpc::ServerOptions options;
+        options.rtmp_service = &rtmp_service;
+        options.has_builtin_services = false;
+        options.num_threads = 0;
+        if (server.Start(0, &options) != 0) {
+            // Fall back to a context with no service
+            LOG(ERROR) << "Fail to start fuzz RTMP server, "
+                          "server-side command handlers will be skipped";
+            return nullptr;
+        }
+
+        // Stop again straight away. _options -- and so rtmp_service -- is not
+        // touched by Stop()/Join()
+        server.Stop(0);
+        server.Join();

Review Comment:
   The fuzzer relies on the assumption that `Stop()/Join()` do not affect the 
server’s stored options/services, then returns a pointer to a stopped server. 
This is a fragile, implementation-dependent contract and makes the setup hard 
to reason about. Prefer keeping the server running for the lifetime of the 
process (start once, don’t stop), or refactor so 
`ParseRtmpMessage`/`RtmpContext` can be provided the needed service/handler 
state without starting/stopping a `brpc::Server`.



##########
test/fuzzing/fuzz_rtmp.cpp:
##########
@@ -0,0 +1,81 @@
+// 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 "brpc/policy/rtmp_protocol.h"
+#include "brpc/rtmp.h"
+#include "brpc/server.h"
+#include "butil/iobuf.h"
+#include "fuzz_common.h"
+
+namespace {
+
+constexpr size_t kMinInputLength = 5;
+constexpr size_t kMaxInputLength = 4096;
+
+class FuzzRtmpService : public brpc::RtmpService {
+public:
+    brpc::RtmpServerStream* NewStream(const brpc::RtmpConnectRequest&) 
override {
+        return nullptr;
+    }
+};
+
+brpc::Server* get_fuzz_server() {
+    static brpc::Server* started = []() -> brpc::Server* {
+        static FuzzRtmpService rtmp_service;
+        static brpc::Server server;
+        brpc::ServerOptions options;
+        options.rtmp_service = &rtmp_service;
+        options.has_builtin_services = false;
+        options.num_threads = 0;
+        if (server.Start(0, &options) != 0) {
+            // Fall back to a context with no service
+            LOG(ERROR) << "Fail to start fuzz RTMP server, "
+                          "server-side command handlers will be skipped";
+            return nullptr;
+        }
+
+        // Stop again straight away. _options -- and so rtmp_service -- is not
+        // touched by Stop()/Join()
+        server.Stop(0);
+        server.Join();
+        return &server;
+    }();
+    return started;
+}
+
+}  // namespace
+
+extern "C" int
+LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+    if (size < kMinInputLength || size > kMaxInputLength){
+        return 0;
+    }
+
+    brpc::Socket* sock = get_fuzz_socket();
+    if (sock == nullptr) {
+        return 0;
+    }
+    brpc::Server* server = get_fuzz_server();
+
+    butil::IOBuf buf;
+    buf.append(data, size);
+
+    sock->reset_parsing_context(new brpc::policy::RtmpContext(nullptr, 
server));
+    brpc::policy::ParseRtmpMessage(&buf, sock, false, server);

Review Comment:
   `get_fuzz_server()` can return `nullptr` (see the `Start()` failure path), 
but the returned `server` is still passed into `RtmpContext` and 
`ParseRtmpMessage`. Add a clear fallback/guard here (e.g., early-return, or 
call these APIs in a way that guarantees they won’t dereference a null server) 
to avoid a potential null dereference during fuzzing.



##########
test/fuzzing/fuzz_rtmp.cpp:
##########
@@ -0,0 +1,81 @@
+// 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 "brpc/policy/rtmp_protocol.h"
+#include "brpc/rtmp.h"
+#include "brpc/server.h"
+#include "butil/iobuf.h"
+#include "fuzz_common.h"
+
+namespace {
+
+constexpr size_t kMinInputLength = 5;
+constexpr size_t kMaxInputLength = 4096;
+
+class FuzzRtmpService : public brpc::RtmpService {
+public:
+    brpc::RtmpServerStream* NewStream(const brpc::RtmpConnectRequest&) 
override {
+        return nullptr;
+    }
+};
+
+brpc::Server* get_fuzz_server() {
+    static brpc::Server* started = []() -> brpc::Server* {
+        static FuzzRtmpService rtmp_service;
+        static brpc::Server server;
+        brpc::ServerOptions options;
+        options.rtmp_service = &rtmp_service;
+        options.has_builtin_services = false;
+        options.num_threads = 0;
+        if (server.Start(0, &options) != 0) {
+            // Fall back to a context with no service
+            LOG(ERROR) << "Fail to start fuzz RTMP server, "
+                          "server-side command handlers will be skipped";
+            return nullptr;
+        }
+
+        // Stop again straight away. _options -- and so rtmp_service -- is not
+        // touched by Stop()/Join()
+        server.Stop(0);
+        server.Join();
+        return &server;
+    }();
+    return started;
+}
+
+}  // namespace
+
+extern "C" int
+LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+    if (size < kMinInputLength || size > kMaxInputLength){
+        return 0;
+    }
+
+    brpc::Socket* sock = get_fuzz_socket();
+    if (sock == nullptr) {
+        return 0;
+    }
+    brpc::Server* server = get_fuzz_server();

Review Comment:
   `get_fuzz_server()` can return `nullptr` (see the `Start()` failure path), 
but the returned `server` is still passed into `RtmpContext` and 
`ParseRtmpMessage`. Add a clear fallback/guard here (e.g., early-return, or 
call these APIs in a way that guarantees they won’t dereference a null server) 
to avoid a potential null dereference during fuzzing.



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

Reply via email to