blob: cb6dc4fa614192857724bac2bf74b5405c256b64 [file] [log] [blame]
Stephen Nusko95545132019-07-09 15:00:54 +01001/*
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
22namespace perfetto {
23namespace socket_fuzz {
24// If we're building on Android and starting the daemons ourselves,
25// create the sockets in a world-writable location.
26#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
27 PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
28#define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer"
29#else
30#define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket()
31#endif
32
33class FakeEventListener : public base::UnixSocket::EventListener {
34 public:
35 FakeEventListener(const uint8_t* data,
36 size_t size,
37 std::function<void()> data_sent_callback)
38 : data_(data), size_(size), data_sent_(data_sent_callback) {}
39
40 void OnNewIncomingConnection(base::UnixSocket*,
41 std::unique_ptr<base::UnixSocket>) override {
42 PERFETTO_CHECK(false);
43 }
44
45 void OnConnect(base::UnixSocket* self, bool connected) override {
46 PERFETTO_CHECK(connected && self->is_connected());
47 PERFETTO_CHECK(self->Send(data_, size_, self->fd(),
48 base::UnixSocket::BlockingMode::kBlocking));
49 data_sent_();
50 }
51
52 void OnDisconnect(base::UnixSocket*) override { PERFETTO_CHECK(false); }
53 void OnDataAvailable(base::UnixSocket*) override { PERFETTO_CHECK(false); }
54
55 private:
56 const uint8_t* data_;
57 const size_t size_;
58 std::function<void()> data_sent_;
59};
60
61int FuzzSharedMemory(const uint8_t* data, size_t size);
62
63int FuzzSharedMemory(const uint8_t* data, size_t size) {
64 if (!data)
65 return 0;
66 base::TestTaskRunner task_runner;
67
68 TestHelper helper(&task_runner);
69 helper.StartServiceIfRequired();
70
71 FakeEventListener fake_event_listener(
72 data, size, task_runner.CreateCheckpoint("data_sent"));
73
74 std::unique_ptr<base::UnixSocket> sock = base::UnixSocket::Connect(
75 helper.GetProducerSocketName(), &fake_event_listener, &task_runner);
76
77 task_runner.RunUntilCheckpoint("data_sent");
78 return 0;
79}
80} // namespace socket_fuzz
81} // namespace perfetto
82
83extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
84
85extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
86 return perfetto::socket_fuzz::FuzzSharedMemory(data, size);
87}