• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "hdidevice_fuzzer.h"
17 
18 #include <securec.h>
19 
20 #include "hdi_device.h"
21 #include "hdi_backend.h"
22 #include "surface.h"
23 using namespace OHOS::Rosen;
24 
25 namespace OHOS {
26     namespace {
27         constexpr int DEFAULT_FENCE = 100;
28         const uint8_t* data_ = nullptr;
29         size_t size_ = 0;
30         size_t pos;
31     }
32 
33     /*
34     * describe: get data from outside untrusted data(data_) which size is according to sizeof(T)
35     * tips: only support basic type
36     */
37     template<class T>
GetData()38     T GetData()
39     {
40         T object {};
41         size_t objectSize = sizeof(object);
42         if (data_ == nullptr || objectSize > size_ - pos) {
43             return object;
44         }
45         errno_t ret = memcpy_s(&object, objectSize, data_ + pos, objectSize);
46         if (ret != EOK) {
47             return {};
48         }
49         pos += objectSize;
50         return object;
51     }
52 
HdiDeviceFuzzTest3()53     void HdiDeviceFuzzTest3()
54     {
55         // get data
56         uint32_t screenId = GetData<uint32_t>();
57         uint32_t layerId = GetData<uint32_t>();
58         GraphicHDRMetaData metaData = GetData<GraphicHDRMetaData>();
59         uint8_t metaData2 = GetData<uint8_t>();
60         GraphicHDRMetadataKey key = GetData<GraphicHDRMetadataKey>();
61         GraphicPresentTimestamp timestamp = GetData<GraphicPresentTimestamp>();
62         uint32_t maskInfo = GetData<uint32_t>();
63 
64         // test
65         HdiDevice *device = HdiDevice::GetInstance();
66         std::vector<GraphicHDRMetaData> metaDatas = {metaData};
67         device->SetLayerMetaData(screenId, layerId, metaDatas);
68         std::vector<uint8_t> metaDatas2 = {metaData2};
69         device->SetLayerMetaDataSet(screenId, layerId, key, metaDatas2);
70         device->SetLayerTunnelHandle(screenId, layerId, nullptr);
71         device->GetPresentTimestamp(screenId, layerId, timestamp);
72         device->SetLayerMaskInfo(screenId, layerId, maskInfo);
73     }
74 
HdiDeviceFuzzTest2()75     void HdiDeviceFuzzTest2()
76     {
77         // get data
78         GraphicColorGamut gamut = GetData<GraphicColorGamut>();
79         uint32_t screenId = GetData<uint32_t>();
80         uint32_t layerId = GetData<uint32_t>();
81         int32_t fenceFd = GetData<int32_t>() % 32768; // maximum fd of linux is 32768
82         // fd 0,1,2 represent stdin, stdout and stderr respectively, they should not be closed.
83         fenceFd = ((fenceFd >= 0 && fenceFd <= 2) ? DEFAULT_FENCE : fenceFd);
84         sptr<SyncFence> fence = new SyncFence(fenceFd);
85         GraphicLayerAlpha alpha = GetData<GraphicLayerAlpha>();
86         GraphicIRect layerRect = GetData<GraphicIRect>();
87         GraphicTransformType ttype = GetData<GraphicTransformType>();
88         GraphicIRect visible = GetData<GraphicIRect>();
89         std::vector<GraphicIRect> visibles = { visible };
90         GraphicIRect dirty = GetData<GraphicIRect>();
91         std::vector<GraphicIRect> dirtyRegions = { dirty };
92         GraphicCompositionType ctype = GetData<GraphicCompositionType>();
93         GraphicBlendType btype = GetData<GraphicBlendType>();
94         GraphicIRect crop = GetData<GraphicIRect>();
95         uint32_t zorder = GetData<uint32_t>();
96         bool isPreMulti = GetData<bool>();
97         float matrixElement = GetData<float>();
98         std::vector<float> matrix = { matrixElement };
99         GraphicColorDataSpace colorSpace = GetData<GraphicColorDataSpace>();
100 
101         uint32_t deletingIndex = GetData<uint32_t>();
102         std::vector<uint32_t> deletingList = { deletingIndex };
103 
104         // test
105         HdiDevice *device = HdiDevice::GetInstance();
106         device->GetScreenColorGamut(screenId, gamut);
107         device->SetLayerAlpha(screenId, layerId, alpha);
108         device->SetLayerSize(screenId, layerId, layerRect);
109         device->SetTransformMode(screenId, layerId, ttype);
110         device->SetLayerVisibleRegion(screenId, layerId, visibles);
111         device->SetLayerDirtyRegion(screenId, layerId, dirtyRegions);
112         GraphicLayerBuffer layerBuffer = {nullptr, INVALID_BUFFER_CACHE_INDEX, fence, deletingList};
113         device->SetLayerBuffer(screenId, layerId, layerBuffer);
114         device->SetLayerCompositionType(screenId, layerId, ctype);
115         device->SetLayerBlendType(screenId, layerId, btype);
116         device->SetLayerCrop(screenId, layerId, crop);
117         device->SetLayerZorder(screenId, layerId, zorder);
118         device->SetLayerPreMulti(screenId, layerId, isPreMulti);
119         device->SetLayerColorTransform(screenId, layerId, matrix);
120         device->SetLayerColorDataSpace(screenId, layerId, colorSpace);
121         device->GetLayerColorDataSpace(screenId, layerId, colorSpace);
122 
123         HdiDeviceFuzzTest3();
124     }
125 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)126     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
127     {
128         if (data == nullptr) {
129             return false;
130         }
131 
132         // initialize
133         data_ = data;
134         size_ = size;
135         pos = 0;
136 
137         // get data
138         uint32_t screenId = GetData<uint32_t>();
139         bool needFlush = GetData<bool>();
140         int32_t fenceFd = GetData<int32_t>() % 32768; // maximum fd of linux is 32768
141         // fd 0,1,2 represent stdin, stdout and stderr respectively, they should not be closed.
142         if (fenceFd >= 0 && fenceFd <= 2) {
143             fenceFd = DEFAULT_FENCE;
144         }
145         sptr<SyncFence> fence = new SyncFence(fenceFd);
146         uint32_t cacheIndex = GetData<uint32_t>();
147         GraphicIRect damageRect = GetData<GraphicIRect>();
148         std::vector<GraphicIRect> damageRects = { damageRect };
149         GraphicGamutMap gamutMap = GetData<GraphicGamutMap>();
150 
151         uint32_t formatCount = GetData<uint32_t>();
152         GraphicHDRFormat formats = GetData<GraphicHDRFormat>();
153         float maxLum = GetData<float>();
154         float maxAverageLum = GetData<float>();
155         float minLum = GetData<float>();
156 
157         // test
158         HdiDevice *device = HdiDevice::GetInstance();
159         device->PrepareScreenLayers(screenId, needFlush);
160         std::vector<uint32_t> layersId;
161         std::vector<int32_t> types;
162         device->GetScreenCompChange(screenId, layersId, types);
163         device->SetScreenClientBuffer(screenId, nullptr, cacheIndex, fence);
164         device->SetScreenClientDamage(screenId, damageRects);
165         std::vector<uint32_t> layers;
166         std::vector<sptr<SyncFence>> fences;
167         device->GetScreenReleaseFence(screenId, layers, fences);
168         std::vector<GraphicColorGamut> gamuts;
169         device->GetScreenSupportedColorGamuts(screenId, gamuts);
170         device->SetScreenGamutMap(screenId, gamutMap);
171         GraphicHDRCapability info = {
172             .formatCount = formatCount,
173             .maxLum = maxLum,
174             .maxAverageLum = maxAverageLum,
175             .minLum = minLum,
176         };
177         info.formats.push_back(formats);
178         device->GetHDRCapabilityInfos(screenId, info);
179         std::vector<GraphicHDRMetadataKey> keys;
180         device->GetSupportedMetaDataKey(screenId, keys);
181         device->Commit(screenId, fence);
182 
183         HdiDeviceFuzzTest2();
184 
185         return true;
186     }
187 }
188 
189 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)190 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
191 {
192     /* Run your code on data */
193     OHOS::DoSomethingInterestingWithMyAPI(data, size);
194     return 0;
195 }
196 
197