Copilot commented on code in PR #3516: URL: https://github.com/apache/brpc/pull/3516#discussion_r3951525271
########## 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` (when `server.Start(...)` fails), but the returned pointer is still passed into `RtmpContext` and `ParseRtmpMessage`. If either path dereferences `server`, this becomes a null-deref crash unrelated to the input being fuzzed. Add an early return when `server == nullptr` (similar to the existing `sock` check), or adjust the API usage so `ParseRtmpMessage` is never called with a null server. ########## 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/* Review Comment: This will fail the OSS-Fuzz build if `fuzz_rtmp_seed_corpus/` doesn’t exist or is empty (depending on shell/zip behavior and whether the script runs with `set -e`). Ensure the directory is created and contains at least one file in-repo (common pattern: a minimal valid seed or a placeholder), or guard the zip step so the build remains deterministic. ########## 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: This relies on subtle behavior: after `Start()` then immediate `Stop()/Join()`, later code still uses `server` to drive parsing/handlers. Even if this works today, it’s fragile because `Stop()/Join()` may change internal server state across versions. Prefer either (a) keeping the server running on an ephemeral port for the fuzzer lifetime, or (b) refactoring to construct the minimal handler context needed for parsing without starting/stopping a full `brpc::Server`. -- 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]
