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