Stephen Nusko | 9554513 | 2019-07-09 15:00:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "perfetto/base/build_config.h" |
| 18 | #include "perfetto/ext/base/unix_socket.h" |
| 19 | #include "src/base/test/test_task_runner.h" |
| 20 | #include "test/test_helper.h" |
| 21 | |
| 22 | namespace perfetto { |
| 23 | namespace socket_fuzz { |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame^] | 24 | namespace { |
Stephen Nusko | 9554513 | 2019-07-09 15:00:54 +0100 | [diff] [blame] | 25 | |
| 26 | class FakeEventListener : public base::UnixSocket::EventListener { |
| 27 | public: |
| 28 | FakeEventListener(const uint8_t* data, |
| 29 | size_t size, |
| 30 | std::function<void()> data_sent_callback) |
| 31 | : data_(data), size_(size), data_sent_(data_sent_callback) {} |
| 32 | |
| 33 | void OnNewIncomingConnection(base::UnixSocket*, |
| 34 | std::unique_ptr<base::UnixSocket>) override { |
| 35 | PERFETTO_CHECK(false); |
| 36 | } |
| 37 | |
| 38 | void OnConnect(base::UnixSocket* self, bool connected) override { |
| 39 | PERFETTO_CHECK(connected && self->is_connected()); |
| 40 | PERFETTO_CHECK(self->Send(data_, size_, self->fd(), |
| 41 | base::UnixSocket::BlockingMode::kBlocking)); |
| 42 | data_sent_(); |
| 43 | } |
| 44 | |
| 45 | void OnDisconnect(base::UnixSocket*) override { PERFETTO_CHECK(false); } |
| 46 | void OnDataAvailable(base::UnixSocket*) override { PERFETTO_CHECK(false); } |
| 47 | |
| 48 | private: |
| 49 | const uint8_t* data_; |
| 50 | const size_t size_; |
| 51 | std::function<void()> data_sent_; |
| 52 | }; |
| 53 | |
Stephen Nusko | 9554513 | 2019-07-09 15:00:54 +0100 | [diff] [blame] | 54 | int FuzzSharedMemory(const uint8_t* data, size_t size) { |
| 55 | if (!data) |
| 56 | return 0; |
| 57 | base::TestTaskRunner task_runner; |
| 58 | |
| 59 | TestHelper helper(&task_runner); |
| 60 | helper.StartServiceIfRequired(); |
| 61 | |
| 62 | FakeEventListener fake_event_listener( |
| 63 | data, size, task_runner.CreateCheckpoint("data_sent")); |
| 64 | |
| 65 | std::unique_ptr<base::UnixSocket> sock = base::UnixSocket::Connect( |
| 66 | helper.GetProducerSocketName(), &fake_event_listener, &task_runner); |
| 67 | |
| 68 | task_runner.RunUntilCheckpoint("data_sent"); |
| 69 | return 0; |
| 70 | } |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame^] | 71 | } // namespace |
Stephen Nusko | 9554513 | 2019-07-09 15:00:54 +0100 | [diff] [blame] | 72 | } // namespace socket_fuzz |
| 73 | } // namespace perfetto |
| 74 | |
| 75 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); |
| 76 | |
| 77 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 78 | return perfetto::socket_fuzz::FuzzSharedMemory(data, size); |
| 79 | } |