blob: a5a716d45865a35b8198a016b60b3edce7898481 [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) {
106 mFlinger = mTestableFlinger.flinger();
107 };
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>();
133 mFlinger->hasWideColorDisplay = 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
143 mFlinger->scheduleComposite(mFdp.ConsumeBool()
144 ? scheduler::ISchedulerCallback::FrameHint::kActive
145 : scheduler::ISchedulerCallback::FrameHint::kNone);
146
147 mFlinger->scheduleRepaint();
148 mFlinger->scheduleSample();
149
150 uint32_t texture = mFlinger->getNewTexture();
151 mFlinger->deleteTextureAsync(texture);
152
153 sp<IBinder> handle = defaultServiceManager()->checkService(
154 String16(mFdp.ConsumeRandomLengthString().c_str()));
155 mFlinger->fromHandle(handle);
156 mFlinger->windowInfosReported();
157 mFlinger->disableExpensiveRendering();
158}
159
160void SurfaceFlingerFuzzer::setInternalDisplayPrimaries() {
161 ui::DisplayPrimaries primaries;
162 primaries.red.X = mFdp.ConsumeFloatingPoint<float>();
163 primaries.red.Y = mFdp.ConsumeFloatingPoint<float>();
164 primaries.red.Z = mFdp.ConsumeFloatingPoint<float>();
165 primaries.green.X = mFdp.ConsumeFloatingPoint<float>();
166 primaries.green.Y = mFdp.ConsumeFloatingPoint<float>();
167 primaries.green.Z = mFdp.ConsumeFloatingPoint<float>();
168 primaries.blue.X = mFdp.ConsumeFloatingPoint<float>();
169 primaries.blue.Y = mFdp.ConsumeFloatingPoint<float>();
170 primaries.blue.Z = mFdp.ConsumeFloatingPoint<float>();
171 primaries.white.X = mFdp.ConsumeFloatingPoint<float>();
172 primaries.white.Y = mFdp.ConsumeFloatingPoint<float>();
173 primaries.white.Z = mFdp.ConsumeFloatingPoint<float>();
174 mTestableFlinger.setInternalDisplayPrimaries(primaries);
175}
176
177void SurfaceFlingerFuzzer::setTransactionState() {
178 Vector<ComposerState> states;
179 Vector<DisplayState> displays;
180 ComposerState composerState;
181 composerState.state.what = layer_state_t::eLayerChanged;
182 composerState.state.surface = nullptr;
183 states.add(composerState);
184 uint32_t flags = mFdp.ConsumeIntegral<uint32_t>();
185 const sp<IBinder> applyToken = nullptr;
186 int64_t desiredPresentTime = mFdp.ConsumeIntegral<int64_t>();
187 bool isAutoTimestamp = mFdp.ConsumeBool();
188 bool hasListenerCallbacks = mFdp.ConsumeBool();
189 std::vector<ListenerCallbacks> listenerCallbacks{};
190 uint64_t transactionId = mFdp.ConsumeIntegral<uint64_t>();
191
192 mTestableFlinger.setTransactionState(FrameTimelineInfo{}, states, displays, flags, applyToken,
193 InputWindowCommands{}, desiredPresentTime, isAutoTimestamp,
194 {}, hasListenerCallbacks, listenerCallbacks,
195 transactionId);
196}
197
198void SurfaceFlingerFuzzer::setDisplayStateLocked() {
199 DisplayState state{};
200 mTestableFlinger.setDisplayStateLocked(state);
201}
202
203void SurfaceFlingerFuzzer::onTransact(const uint8_t *data, size_t size) {
204 Parcel fuzzedData, reply;
205 fuzzedData.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
206 fuzzedData.setData(data, size);
207 fuzzedData.setDataPosition(0);
208 uint32_t code = mFdp.ConsumeBool() ? mFdp.PickValueInArray(kSurfaceComposerTags)
209 : mFdp.ConsumeIntegralInRange<uint32_t>(kMinCode, kMaxCode);
210 mTestableFlinger.onTransact(code, fuzzedData, &reply, 0);
211}
212
213void SurfaceFlingerFuzzer::setUp() {
214 mTestableFlinger.setupScheduler(std::make_unique<android::mock::VsyncController>(),
215 std::make_unique<android::mock::VSyncTracker>(),
216 std::make_unique<android::mock::EventThread>(),
217 std::make_unique<android::mock::EventThread>());
218
219 mTestableFlinger.setupTimeStats(std::make_unique<android::mock::TimeStats>());
220
221 std::unique_ptr<android::renderengine::RenderEngine> renderEngine =
222 std::make_unique<android::renderengine::mock::RenderEngine>();
223 mTestableFlinger.setupRenderEngine(std::move(renderEngine));
224 mTestableFlinger.setupComposer(std::make_unique<android::Hwc2::mock::Composer>());
225}
226
227void SurfaceFlingerFuzzer::process(const uint8_t *data, size_t size) {
228 setUp();
229
230 invokeFlinger();
231
232 mTestableFlinger.fuzzSurfaceFlinger(data, size);
233
234 mTestableFlinger.setCreateBufferQueueFunction(
235 surfaceflinger::test::Factory::CreateBufferQueueFunction());
236 mTestableFlinger.setCreateNativeWindowSurface(
237 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction());
238
239 setInternalDisplayPrimaries();
240
241 mTestableFlinger.enableHalVirtualDisplays(mFdp.ConsumeBool());
242
243 mTestableFlinger.commitTransactionsLocked(mFdp.ConsumeIntegral<uint32_t>());
244
245 mTestableFlinger.notifyPowerBoost(mFdp.ConsumeIntegral<int32_t>());
246
247 setDisplayStateLocked();
248
249 setTransactionState();
250 mTestableFlinger.flushTransactionQueues();
251
252 onTransact(data, size);
253}
254
255extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
256 android::fuzz::SurfaceFlingerFuzzer surfaceFlingerFuzzer(data, size);
257 surfaceFlingerFuzzer.process(data, size);
258 return 0;
259}
260
261} // namespace android::fuzz