blob: ed8bb7fc93a40ed9f397cb2423e2c312d5705426 [file] [log] [blame]
Alec Mourib2089a32022-01-15 00:34:20 +00001/*
2 * Copyright 2021 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
18#include <FuzzableDataspaces.h>
19#include <binder/IServiceManager.h>
20#include <fuzzer/FuzzedDataProvider.h>
21#include <ui/DisplayStatInfo.h>
22#include "surfaceflinger_fuzzers_utils.h"
23
24namespace android::fuzz {
25
26static constexpr LatchUnsignaledConfig kLatchUnsignaledConfig[] = {
Ady Abraham9dada822022-02-03 10:26:59 -080027 LatchUnsignaledConfig::AutoSingleLayer,
Alec Mourib2089a32022-01-15 00:34:20 +000028 LatchUnsignaledConfig::Disabled,
29};
30
Alec Mourib2089a32022-01-15 00:34:20 +000031static constexpr BnSurfaceComposer::ISurfaceComposerTag kSurfaceComposerTags[]{
32 BnSurfaceComposer::BOOT_FINISHED,
33 BnSurfaceComposer::CREATE_CONNECTION,
34 BnSurfaceComposer::GET_STATIC_DISPLAY_INFO,
35 BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
36 BnSurfaceComposer::CREATE_DISPLAY,
37 BnSurfaceComposer::DESTROY_DISPLAY,
38 BnSurfaceComposer::GET_PHYSICAL_DISPLAY_TOKEN,
39 BnSurfaceComposer::SET_TRANSACTION_STATE,
40 BnSurfaceComposer::AUTHENTICATE_SURFACE,
41 BnSurfaceComposer::GET_SUPPORTED_FRAME_TIMESTAMPS,
42 BnSurfaceComposer::GET_DISPLAY_MODES,
43 BnSurfaceComposer::GET_ACTIVE_DISPLAY_MODE,
44 BnSurfaceComposer::GET_DISPLAY_STATE,
45 BnSurfaceComposer::CAPTURE_DISPLAY,
46 BnSurfaceComposer::CAPTURE_LAYERS,
47 BnSurfaceComposer::CLEAR_ANIMATION_FRAME_STATS,
48 BnSurfaceComposer::GET_ANIMATION_FRAME_STATS,
49 BnSurfaceComposer::SET_POWER_MODE,
50 BnSurfaceComposer::GET_DISPLAY_STATS,
51 BnSurfaceComposer::GET_HDR_CAPABILITIES,
52 BnSurfaceComposer::GET_DISPLAY_COLOR_MODES,
53 BnSurfaceComposer::GET_ACTIVE_COLOR_MODE,
54 BnSurfaceComposer::SET_ACTIVE_COLOR_MODE,
55 BnSurfaceComposer::ENABLE_VSYNC_INJECTIONS,
56 BnSurfaceComposer::INJECT_VSYNC,
57 BnSurfaceComposer::GET_LAYER_DEBUG_INFO,
58 BnSurfaceComposer::GET_COMPOSITION_PREFERENCE,
59 BnSurfaceComposer::GET_COLOR_MANAGEMENT,
60 BnSurfaceComposer::GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES,
61 BnSurfaceComposer::SET_DISPLAY_CONTENT_SAMPLING_ENABLED,
62 BnSurfaceComposer::GET_DISPLAYED_CONTENT_SAMPLE,
63 BnSurfaceComposer::GET_PROTECTED_CONTENT_SUPPORT,
64 BnSurfaceComposer::IS_WIDE_COLOR_DISPLAY,
65 BnSurfaceComposer::GET_DISPLAY_NATIVE_PRIMARIES,
66 BnSurfaceComposer::GET_PHYSICAL_DISPLAY_IDS,
67 BnSurfaceComposer::ADD_REGION_SAMPLING_LISTENER,
68 BnSurfaceComposer::REMOVE_REGION_SAMPLING_LISTENER,
69 BnSurfaceComposer::SET_DESIRED_DISPLAY_MODE_SPECS,
70 BnSurfaceComposer::GET_DESIRED_DISPLAY_MODE_SPECS,
71 BnSurfaceComposer::GET_DISPLAY_BRIGHTNESS_SUPPORT,
72 BnSurfaceComposer::SET_DISPLAY_BRIGHTNESS,
73 BnSurfaceComposer::CAPTURE_DISPLAY_BY_ID,
74 BnSurfaceComposer::NOTIFY_POWER_BOOST,
75 BnSurfaceComposer::SET_GLOBAL_SHADOW_SETTINGS,
76 BnSurfaceComposer::GET_AUTO_LOW_LATENCY_MODE_SUPPORT,
77 BnSurfaceComposer::SET_AUTO_LOW_LATENCY_MODE,
78 BnSurfaceComposer::GET_GAME_CONTENT_TYPE_SUPPORT,
79 BnSurfaceComposer::SET_GAME_CONTENT_TYPE,
80 BnSurfaceComposer::SET_FRAME_RATE,
81 BnSurfaceComposer::ACQUIRE_FRAME_RATE_FLEXIBILITY_TOKEN,
82 BnSurfaceComposer::SET_FRAME_TIMELINE_INFO,
83 BnSurfaceComposer::ADD_TRANSACTION_TRACE_LISTENER,
84 BnSurfaceComposer::GET_GPU_CONTEXT_PRIORITY,
85 BnSurfaceComposer::GET_MAX_ACQUIRED_BUFFER_COUNT,
86 BnSurfaceComposer::GET_DYNAMIC_DISPLAY_INFO,
87 BnSurfaceComposer::ADD_FPS_LISTENER,
88 BnSurfaceComposer::REMOVE_FPS_LISTENER,
89 BnSurfaceComposer::OVERRIDE_HDR_TYPES,
90 BnSurfaceComposer::ADD_HDR_LAYER_INFO_LISTENER,
91 BnSurfaceComposer::REMOVE_HDR_LAYER_INFO_LISTENER,
92 BnSurfaceComposer::ON_PULL_ATOM,
93 BnSurfaceComposer::ADD_TUNNEL_MODE_ENABLED_LISTENER,
94 BnSurfaceComposer::REMOVE_TUNNEL_MODE_ENABLED_LISTENER,
95 BnSurfaceComposer::ADD_WINDOW_INFOS_LISTENER,
96 BnSurfaceComposer::REMOVE_WINDOW_INFOS_LISTENER,
97};
98
99static constexpr uint32_t kMinCode = 1000;
100static constexpr uint32_t kMaxCode = 1050;
101
102class SurfaceFlingerFuzzer {
103public:
104 SurfaceFlingerFuzzer(const uint8_t *data, size_t size) : mFdp(data, size) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700105 mFlinger = sp<SurfaceFlinger>::fromExisting(mTestableFlinger.flinger());
Alec Mourib2089a32022-01-15 00:34:20 +0000106 };
107 void process(const uint8_t *data, size_t size);
108
109private:
110 void setUp();
111 void invokeFlinger();
112 void setTransactionState();
113 void setInternalDisplayPrimaries();
114 void setDisplayStateLocked();
115 void onTransact(const uint8_t *data, size_t size);
116
117 FuzzedDataProvider mFdp;
118 TestableSurfaceFlinger mTestableFlinger;
119 sp<SurfaceFlinger> mFlinger = nullptr;
120};
121
122void SurfaceFlingerFuzzer::invokeFlinger() {
123 mFlinger->setSchedFifo(mFdp.ConsumeBool());
124 mFlinger->setSchedAttr(mFdp.ConsumeBool());
125 mFlinger->getServiceName();
Lloyd Pique30db6402023-06-26 18:56:51 +0000126 mFlinger->hasSyncFramework = mFdp.ConsumeBool();
127 mFlinger->dispSyncPresentTimeOffset = mFdp.ConsumeIntegral<int64_t>();
128 mFlinger->useHwcForRgbToYuv = mFdp.ConsumeBool();
129 mFlinger->maxFrameBufferAcquiredBuffers = mFdp.ConsumeIntegral<int64_t>();
130 mFlinger->maxGraphicsWidth = mFdp.ConsumeIntegral<uint32_t>();
131 mFlinger->maxGraphicsHeight = mFdp.ConsumeIntegral<uint32_t>();
132 mTestableFlinger.mutableSupportsWideColor() = mFdp.ConsumeBool();
133 mFlinger->useContextPriority = mFdp.ConsumeBool();
Alec Mourib2089a32022-01-15 00:34:20 +0000134
Lloyd Pique30db6402023-06-26 18:56:51 +0000135 mFlinger->defaultCompositionDataspace = mFdp.PickValueInArray(kDataspaces);
136 mFlinger->defaultCompositionPixelFormat = mFdp.PickValueInArray(kPixelFormats);
137 mFlinger->wideColorGamutCompositionDataspace = mFdp.PickValueInArray(kDataspaces);
138 mFlinger->wideColorGamutCompositionPixelFormat = mFdp.PickValueInArray(kPixelFormats);
Alec Mourib2089a32022-01-15 00:34:20 +0000139
140 mFlinger->enableLatchUnsignaledConfig = mFdp.PickValueInArray(kLatchUnsignaledConfig);
141
Dominik Laskowskidd5827a2022-03-17 12:44:23 -0700142 using FrameHint = SurfaceFlinger::FrameHint;
143 mFlinger->scheduleComposite(mFdp.ConsumeBool() ? FrameHint::kActive : FrameHint::kNone);
Alec Mourib2089a32022-01-15 00:34:20 +0000144 mFlinger->scheduleRepaint();
145 mFlinger->scheduleSample();
146
Alec Mourib2089a32022-01-15 00:34:20 +0000147 sp<IBinder> handle = defaultServiceManager()->checkService(
148 String16(mFdp.ConsumeRandomLengthString().c_str()));
Vishnu Nair07e2a482022-10-18 19:18:16 +0000149 LayerHandle::getLayer(handle);
Alec Mourib2089a32022-01-15 00:34:20 +0000150 mFlinger->disableExpensiveRendering();
151}
152
153void SurfaceFlingerFuzzer::setInternalDisplayPrimaries() {
Lloyd Pique30db6402023-06-26 18:56:51 +0000154 ui::DisplayPrimaries primaries;
Alec Mourib2089a32022-01-15 00:34:20 +0000155 primaries.red.X = mFdp.ConsumeFloatingPoint<float>();
156 primaries.red.Y = mFdp.ConsumeFloatingPoint<float>();
157 primaries.red.Z = mFdp.ConsumeFloatingPoint<float>();
158 primaries.green.X = mFdp.ConsumeFloatingPoint<float>();
159 primaries.green.Y = mFdp.ConsumeFloatingPoint<float>();
160 primaries.green.Z = mFdp.ConsumeFloatingPoint<float>();
161 primaries.blue.X = mFdp.ConsumeFloatingPoint<float>();
162 primaries.blue.Y = mFdp.ConsumeFloatingPoint<float>();
163 primaries.blue.Z = mFdp.ConsumeFloatingPoint<float>();
164 primaries.white.X = mFdp.ConsumeFloatingPoint<float>();
165 primaries.white.Y = mFdp.ConsumeFloatingPoint<float>();
166 primaries.white.Z = mFdp.ConsumeFloatingPoint<float>();
Lloyd Pique30db6402023-06-26 18:56:51 +0000167 mTestableFlinger.setInternalDisplayPrimaries(primaries);
Alec Mourib2089a32022-01-15 00:34:20 +0000168}
169
170void SurfaceFlingerFuzzer::setTransactionState() {
171 Vector<ComposerState> states;
172 Vector<DisplayState> displays;
173 ComposerState composerState;
174 composerState.state.what = layer_state_t::eLayerChanged;
175 composerState.state.surface = nullptr;
176 states.add(composerState);
177 uint32_t flags = mFdp.ConsumeIntegral<uint32_t>();
178 const sp<IBinder> applyToken = nullptr;
179 int64_t desiredPresentTime = mFdp.ConsumeIntegral<int64_t>();
180 bool isAutoTimestamp = mFdp.ConsumeBool();
181 bool hasListenerCallbacks = mFdp.ConsumeBool();
182 std::vector<ListenerCallbacks> listenerCallbacks{};
183 uint64_t transactionId = mFdp.ConsumeIntegral<uint64_t>();
Pablo Gamito23780be2023-04-18 08:30:00 +0000184 std::vector<uint64_t> mergedTransactionIds{};
Alec Mourib2089a32022-01-15 00:34:20 +0000185
186 mTestableFlinger.setTransactionState(FrameTimelineInfo{}, states, displays, flags, applyToken,
187 InputWindowCommands{}, desiredPresentTime, isAutoTimestamp,
Pablo Gamito23780be2023-04-18 08:30:00 +0000188 {}, hasListenerCallbacks, listenerCallbacks, transactionId,
189 mergedTransactionIds);
Alec Mourib2089a32022-01-15 00:34:20 +0000190}
191
192void SurfaceFlingerFuzzer::setDisplayStateLocked() {
193 DisplayState state{};
194 mTestableFlinger.setDisplayStateLocked(state);
195}
196
197void SurfaceFlingerFuzzer::onTransact(const uint8_t *data, size_t size) {
198 Parcel fuzzedData, reply;
199 fuzzedData.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
200 fuzzedData.setData(data, size);
201 fuzzedData.setDataPosition(0);
202 uint32_t code = mFdp.ConsumeBool() ? mFdp.PickValueInArray(kSurfaceComposerTags)
203 : mFdp.ConsumeIntegralInRange<uint32_t>(kMinCode, kMaxCode);
204 mTestableFlinger.onTransact(code, fuzzedData, &reply, 0);
205}
206
207void SurfaceFlingerFuzzer::setUp() {
208 mTestableFlinger.setupScheduler(std::make_unique<android::mock::VsyncController>(),
209 std::make_unique<android::mock::VSyncTracker>(),
210 std::make_unique<android::mock::EventThread>(),
211 std::make_unique<android::mock::EventThread>());
212
213 mTestableFlinger.setupTimeStats(std::make_unique<android::mock::TimeStats>());
214
215 std::unique_ptr<android::renderengine::RenderEngine> renderEngine =
216 std::make_unique<android::renderengine::mock::RenderEngine>();
217 mTestableFlinger.setupRenderEngine(std::move(renderEngine));
218 mTestableFlinger.setupComposer(std::make_unique<android::Hwc2::mock::Composer>());
219}
220
221void SurfaceFlingerFuzzer::process(const uint8_t *data, size_t size) {
222 setUp();
223
224 invokeFlinger();
225
226 mTestableFlinger.fuzzSurfaceFlinger(data, size);
227
228 mTestableFlinger.setCreateBufferQueueFunction(
229 surfaceflinger::test::Factory::CreateBufferQueueFunction());
230 mTestableFlinger.setCreateNativeWindowSurface(
231 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction());
232
233 setInternalDisplayPrimaries();
234
235 mTestableFlinger.enableHalVirtualDisplays(mFdp.ConsumeBool());
236
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700237 FTL_FAKE_GUARD(kMainThreadContext,
238 mTestableFlinger.commitTransactionsLocked(mFdp.ConsumeIntegral<uint32_t>()));
Alec Mourib2089a32022-01-15 00:34:20 +0000239
240 mTestableFlinger.notifyPowerBoost(mFdp.ConsumeIntegral<int32_t>());
241
242 setDisplayStateLocked();
243
244 setTransactionState();
kunal raie31f06a2022-10-18 12:18:18 +0530245 mTestableFlinger.flushTransactionQueues();
Alec Mourib2089a32022-01-15 00:34:20 +0000246
247 onTransact(data, size);
248}
249
250extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
251 android::fuzz::SurfaceFlingerFuzzer surfaceFlingerFuzzer(data, size);
252 surfaceFlingerFuzzer.process(data, size);
253 return 0;
254}
255
256} // namespace android::fuzz