Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include <sys/socket.h> |
| 9 | #include <sys/types.h> |
| 10 | |
| 11 | #include <base/bind.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 12 | #include <base/check.h> |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 13 | #include <base/files/file_util.h> |
| 14 | #include <base/logging.h> |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 15 | #include <base/run_loop.h> |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 16 | #include <base/task/single_thread_task_executor.h> |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 17 | #include <base/test/test_mock_time_task_runner.h> |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 18 | #include <brillo/message_loops/base_message_loop.h> |
| 19 | #include <libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h> |
| 20 | |
| 21 | #include "bindings/worker_common.pb.h" |
| 22 | #include "system-proxy/protobuf_util.h" |
| 23 | #include "system-proxy/server_proxy.h" |
| 24 | |
| 25 | namespace { |
| 26 | void NullClosure() {} |
| 27 | } // namespace |
| 28 | |
| 29 | struct Environment { |
| 30 | Environment() { |
hscham | e671130 | 2020-11-20 17:08:14 +0900 | [diff] [blame] | 31 | logging::SetMinLogLevel(logging::LOGGING_FATAL); // Disable logging. |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 32 | } |
| 33 | }; |
| 34 | |
| 35 | // ServerProxy implementation that receives input from a given file descriptor, |
| 36 | // instead of the default standard input file descriptor (STDIN_FILENO). |
| 37 | class FakeServerProxy : public system_proxy::ServerProxy { |
| 38 | public: |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 39 | FakeServerProxy(base::ScopedFD stdin_fd, base::OnceClosure quit_task) |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 40 | : system_proxy::ServerProxy(base::BindOnce(&NullClosure)), |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 41 | stdin_fd_(std::move(stdin_fd)), |
| 42 | quit_task_(std::move(quit_task)) {} |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 43 | FakeServerProxy(const FakeServerProxy&) = delete; |
| 44 | FakeServerProxy& operator=(const FakeServerProxy&) = delete; |
| 45 | ~FakeServerProxy() override = default; |
| 46 | |
| 47 | int GetStdinPipe() override { return stdin_fd_.get(); } |
| 48 | |
| 49 | private: |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 50 | void HandleStdinReadable() override { |
| 51 | system_proxy::ServerProxy::HandleStdinReadable(); |
| 52 | std::move(quit_task_).Run(); |
| 53 | } |
| 54 | |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 55 | base::ScopedFD stdin_fd_; |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 56 | base::OnceClosure quit_task_; |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 59 | DEFINE_PROTO_FUZZER(const system_proxy::worker::WorkerConfigs& configs) { |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 60 | static Environment env; |
| 61 | |
| 62 | // Mock main task runner |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 63 | base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO); |
| 64 | brillo::BaseMessageLoop brillo_loop(task_executor.task_runner()); |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 65 | brillo_loop.SetAsCurrent(); |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 66 | base::RunLoop run_loop; |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 67 | |
| 68 | int fds[2]; |
| 69 | CHECK(base::CreateLocalNonBlockingPipe(fds)); |
| 70 | base::ScopedFD stdin_read_fd(fds[0]); |
| 71 | base::ScopedFD stdin_write_fd(fds[1]); |
| 72 | |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 73 | auto server = std::make_unique<FakeServerProxy>(std::move(stdin_read_fd), |
| 74 | run_loop.QuitClosure()); |
| 75 | server->Init(); |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 76 | // Send the config to the worker's stdin input. |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 77 | system_proxy::WriteProtobuf(stdin_write_fd.get(), configs); |
| 78 | |
| 79 | run_loop.Run(); |
Andreea Costinas | 6506315 | 2020-04-27 12:03:18 +0200 | [diff] [blame] | 80 | } |