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