1bc03f14fSopenharmony_ci/* 2bc03f14fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License. 5bc03f14fSopenharmony_ci * You may obtain a copy of the License at 6bc03f14fSopenharmony_ci * 7bc03f14fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bc03f14fSopenharmony_ci * 9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and 13bc03f14fSopenharmony_ci * limitations under the License. 14bc03f14fSopenharmony_ci */ 15bc03f14fSopenharmony_ci 16bc03f14fSopenharmony_ci#include "ffrtutils_fuzzer.h" 17bc03f14fSopenharmony_ci 18bc03f14fSopenharmony_ci#include <vector> 19bc03f14fSopenharmony_ci#include "ffrt_utils.h" 20bc03f14fSopenharmony_ci 21bc03f14fSopenharmony_cinamespace OHOS { 22bc03f14fSopenharmony_ciusing namespace OHOS::MiscServices; 23bc03f14fSopenharmony_ciusing namespace std; 24bc03f14fSopenharmony_ci 25bc03f14fSopenharmony_citemplate<class T> 26bc03f14fSopenharmony_ciT TypeCast(const uint8_t *data, int *pos = nullptr) 27bc03f14fSopenharmony_ci{ 28bc03f14fSopenharmony_ci if (pos) { 29bc03f14fSopenharmony_ci *pos += sizeof(T); 30bc03f14fSopenharmony_ci } 31bc03f14fSopenharmony_ci return *(reinterpret_cast<const T*>(data)); 32bc03f14fSopenharmony_ci} 33bc03f14fSopenharmony_ci 34bc03f14fSopenharmony_cibool FuzzFfrtUtilsSubmitQueueTasks(const uint8_t *rawData, size_t size) 35bc03f14fSopenharmony_ci{ 36bc03f14fSopenharmony_ci if (rawData == nullptr || size < sizeof(int32_t) + sizeof(int32_t)) { 37bc03f14fSopenharmony_ci return true; 38bc03f14fSopenharmony_ci } 39bc03f14fSopenharmony_ci int pos = 0; 40bc03f14fSopenharmony_ci int x = TypeCast<int32_t>(rawData, &pos); 41bc03f14fSopenharmony_ci int y = TypeCast<int32_t>(rawData + pos); 42bc03f14fSopenharmony_ci FFRTTask task = [&] { 43bc03f14fSopenharmony_ci x /= 2; 44bc03f14fSopenharmony_ci }; 45bc03f14fSopenharmony_ci FFRTTask task1 = [&] { 46bc03f14fSopenharmony_ci y /= 2; 47bc03f14fSopenharmony_ci }; 48bc03f14fSopenharmony_ci static uint32_t thresHold = 1; 49bc03f14fSopenharmony_ci if (thresHold <= 1) { 50bc03f14fSopenharmony_ci std::vector<FFRTTask> tasks; 51bc03f14fSopenharmony_ci tasks.push_back(task); 52bc03f14fSopenharmony_ci tasks.push_back(task1); 53bc03f14fSopenharmony_ci FFRTQueue serialQueue("pasteboard_queue"); 54bc03f14fSopenharmony_ci FFRTUtils::SubmitQueueTasks(tasks, serialQueue); 55bc03f14fSopenharmony_ci thresHold++; 56bc03f14fSopenharmony_ci } 57bc03f14fSopenharmony_ci return true; 58bc03f14fSopenharmony_ci} 59bc03f14fSopenharmony_ci 60bc03f14fSopenharmony_cibool FuzzFfrtUtilsSubmitDelayTask(const uint8_t *rawData, size_t size) 61bc03f14fSopenharmony_ci{ 62bc03f14fSopenharmony_ci if (rawData == nullptr || size < sizeof(int32_t)) { 63bc03f14fSopenharmony_ci return true; 64bc03f14fSopenharmony_ci } 65bc03f14fSopenharmony_ci static uint32_t thresHold = 1; 66bc03f14fSopenharmony_ci if (thresHold <= 1) { 67bc03f14fSopenharmony_ci int y = TypeCast<int32_t>(rawData); 68bc03f14fSopenharmony_ci FFRTTask task = [&] { 69bc03f14fSopenharmony_ci y /= 2; 70bc03f14fSopenharmony_ci }; 71bc03f14fSopenharmony_ci uint32_t delayMs = 5; // delay 5Ms 72bc03f14fSopenharmony_ci FFRTQueue serQueue("delaytask_queue"); 73bc03f14fSopenharmony_ci FFRTUtils::SubmitDelayTask(task, delayMs, serQueue); 74bc03f14fSopenharmony_ci thresHold++; 75bc03f14fSopenharmony_ci } 76bc03f14fSopenharmony_ci return true; 77bc03f14fSopenharmony_ci} 78bc03f14fSopenharmony_ci 79bc03f14fSopenharmony_cibool FuzzUtilsSubmitDelayTask(const uint8_t *rawData, size_t size) 80bc03f14fSopenharmony_ci{ 81bc03f14fSopenharmony_ci if (rawData == nullptr || size < sizeof(int32_t)) { 82bc03f14fSopenharmony_ci return true; 83bc03f14fSopenharmony_ci } 84bc03f14fSopenharmony_ci int z = TypeCast<int32_t>(rawData, nullptr); 85bc03f14fSopenharmony_ci FFRTTask task = [&] { 86bc03f14fSopenharmony_ci z /= 2; 87bc03f14fSopenharmony_ci }; 88bc03f14fSopenharmony_ci uint32_t delayMs = 5; // delay 5Ms 89bc03f14fSopenharmony_ci static uint32_t thresHold = 1; 90bc03f14fSopenharmony_ci if (thresHold <= 1) { 91bc03f14fSopenharmony_ci shared_ptr<FFRTQueue> queue = make_shared<FFRTQueue>("delaytask_queue_ptr"); 92bc03f14fSopenharmony_ci FFRTUtils::SubmitDelayTask(task, delayMs, queue); 93bc03f14fSopenharmony_ci thresHold++; 94bc03f14fSopenharmony_ci } 95bc03f14fSopenharmony_ci return true; 96bc03f14fSopenharmony_ci} 97bc03f14fSopenharmony_ci 98bc03f14fSopenharmony_cibool FuzzUtilsSubmitTimeoutTask(const uint8_t *rawData, size_t size) 99bc03f14fSopenharmony_ci{ 100bc03f14fSopenharmony_ci if (rawData == nullptr || size < sizeof(int32_t)) { 101bc03f14fSopenharmony_ci return true; 102bc03f14fSopenharmony_ci } 103bc03f14fSopenharmony_ci int m = TypeCast<int32_t>(rawData, nullptr); 104bc03f14fSopenharmony_ci FFRTTask task = [&] { 105bc03f14fSopenharmony_ci m /= 2; 106bc03f14fSopenharmony_ci }; 107bc03f14fSopenharmony_ci static uint32_t thresHold = 1; 108bc03f14fSopenharmony_ci if (thresHold <= 1) { 109bc03f14fSopenharmony_ci uint32_t timeout = 10; // timeout 10Ms 110bc03f14fSopenharmony_ci FFRTUtils::SubmitTimeoutTask(task, timeout); 111bc03f14fSopenharmony_ci thresHold++; 112bc03f14fSopenharmony_ci } 113bc03f14fSopenharmony_ci return true; 114bc03f14fSopenharmony_ci} 115bc03f14fSopenharmony_ci 116bc03f14fSopenharmony_cibool FuzzFfRTTimerSingleSetAndCancel(const uint8_t *rawData, size_t size) 117bc03f14fSopenharmony_ci{ 118bc03f14fSopenharmony_ci if (rawData == nullptr || size < sizeof(int32_t)) { 119bc03f14fSopenharmony_ci return true; 120bc03f14fSopenharmony_ci } 121bc03f14fSopenharmony_ci int pos = 0; 122bc03f14fSopenharmony_ci int m = TypeCast<int32_t>(rawData, &pos); 123bc03f14fSopenharmony_ci FFRTTask task = [&] { 124bc03f14fSopenharmony_ci m /= 2; 125bc03f14fSopenharmony_ci }; 126bc03f14fSopenharmony_ci static uint32_t thresHold = 1; 127bc03f14fSopenharmony_ci if (thresHold <= 1) { 128bc03f14fSopenharmony_ci string timerId = "TimerSingleSetAndCancelId"; 129bc03f14fSopenharmony_ci FFRTTimer ffrtTimer; 130bc03f14fSopenharmony_ci ffrtTimer.SetTimer(timerId, task); 131bc03f14fSopenharmony_ci ffrtTimer.CancelTimer(timerId); 132bc03f14fSopenharmony_ci thresHold++; 133bc03f14fSopenharmony_ci } 134bc03f14fSopenharmony_ci return true; 135bc03f14fSopenharmony_ci} 136bc03f14fSopenharmony_ci 137bc03f14fSopenharmony_cibool FuzzFfRTTimerMultiSetAndCancel(const uint8_t *rawData, size_t size) 138bc03f14fSopenharmony_ci{ 139bc03f14fSopenharmony_ci int n = 0; 140bc03f14fSopenharmony_ci FFRTTask task = [&] { 141bc03f14fSopenharmony_ci n = 2; 142bc03f14fSopenharmony_ci }; 143bc03f14fSopenharmony_ci FFRTTask task1 = [&] { 144bc03f14fSopenharmony_ci n = 5; 145bc03f14fSopenharmony_ci }; 146bc03f14fSopenharmony_ci int len = size >> 1; 147bc03f14fSopenharmony_ci string timerId1 = "TimerMultiSetAndCancel1"; 148bc03f14fSopenharmony_ci string timerId2 = "TimerMultiSetAndCancel2"; 149bc03f14fSopenharmony_ci static uint32_t thresHold = 1; 150bc03f14fSopenharmony_ci if (thresHold <= 1) { 151bc03f14fSopenharmony_ci FFRTTimer ffrtTimer("fuzz_test_timername"); 152bc03f14fSopenharmony_ci uint32_t delayMs = 5; // delay 5Ms 153bc03f14fSopenharmony_ci ffrtTimer.SetTimer(timerId1, task, delayMs); 154bc03f14fSopenharmony_ci ffrtTimer.SetTimer(timerId2, task1, delayMs); 155bc03f14fSopenharmony_ci ffrtTimer.CancelAllTimer(); 156bc03f14fSopenharmony_ci thresHold++; 157bc03f14fSopenharmony_ci } 158bc03f14fSopenharmony_ci return true; 159bc03f14fSopenharmony_ci} 160bc03f14fSopenharmony_ci} // namespace OHOS 161bc03f14fSopenharmony_ci/* Fuzzer entry point */ 162bc03f14fSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 163bc03f14fSopenharmony_ci{ 164bc03f14fSopenharmony_ci /* Run your code on data */ 165bc03f14fSopenharmony_ci OHOS::FuzzFfrtUtilsSubmitQueueTasks(data, size); 166bc03f14fSopenharmony_ci OHOS::FuzzFfrtUtilsSubmitDelayTask(data, size); 167bc03f14fSopenharmony_ci OHOS::FuzzUtilsSubmitDelayTask(data, size); 168bc03f14fSopenharmony_ci OHOS::FuzzUtilsSubmitTimeoutTask(data, size); 169bc03f14fSopenharmony_ci OHOS::FuzzFfRTTimerSingleSetAndCancel(data, size); 170bc03f14fSopenharmony_ci OHOS::FuzzFfRTTimerMultiSetAndCancel(data, size); 171bc03f14fSopenharmony_ci return 0; 172bc03f14fSopenharmony_ci} 173