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