1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "abilityappmgrrenderstateobservermanager_fuzzer.h" 17eace7efcSopenharmony_ci#include "ability_record.h" 18eace7efcSopenharmony_ci 19eace7efcSopenharmony_ci#include <cstddef> 20eace7efcSopenharmony_ci#include <cstdint> 21eace7efcSopenharmony_ci 22eace7efcSopenharmony_ci#define private public 23eace7efcSopenharmony_ci#include "render_state_observer_manager.h" 24eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h" 25eace7efcSopenharmony_ci#include "iremote_object.h" 26eace7efcSopenharmony_ci#undef private 27eace7efcSopenharmony_ci#include "render_state_observer_proxy.h" 28eace7efcSopenharmony_ci#include "render_state_observer_stub.h" 29eace7efcSopenharmony_ci 30eace7efcSopenharmony_ci#include "securec.h" 31eace7efcSopenharmony_ci 32eace7efcSopenharmony_ci 33eace7efcSopenharmony_ciusing namespace OHOS::AAFwk; 34eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk; 35eace7efcSopenharmony_ci 36eace7efcSopenharmony_cinamespace OHOS { 37eace7efcSopenharmony_cinamespace { 38eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0; 39eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1; 40eace7efcSopenharmony_ciconstexpr int INPUT_TWO = 2; 41eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3; 42eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024; 43eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 44eace7efcSopenharmony_ciconstexpr size_t OFFSET_ZERO = 24; 45eace7efcSopenharmony_ciconstexpr size_t OFFSET_ONE = 16; 46eace7efcSopenharmony_ciconstexpr size_t OFFSET_TWO = 8; 47eace7efcSopenharmony_ciconstexpr uint8_t ENABLE = 2; 48eace7efcSopenharmony_ci} 49eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr) 50eace7efcSopenharmony_ci{ 51eace7efcSopenharmony_ci // convert fuzz input data to an integer 52eace7efcSopenharmony_ci return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) | 53eace7efcSopenharmony_ci ptr[INPUT_THREE]; 54eace7efcSopenharmony_ci} 55eace7efcSopenharmony_ci 56eace7efcSopenharmony_cisptr<Token> GetFuzzAbilityToken() 57eace7efcSopenharmony_ci{ 58eace7efcSopenharmony_ci sptr<Token> token = nullptr; 59eace7efcSopenharmony_ci AbilityRequest abilityRequest; 60eace7efcSopenharmony_ci abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; 61eace7efcSopenharmony_ci abilityRequest.abilityInfo.name = "MainAbility"; 62eace7efcSopenharmony_ci abilityRequest.abilityInfo.type = AbilityType::DATA; 63eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); 64eace7efcSopenharmony_ci if (abilityRecord) { 65eace7efcSopenharmony_ci token = abilityRecord->GetToken(); 66eace7efcSopenharmony_ci } 67eace7efcSopenharmony_ci return token; 68eace7efcSopenharmony_ci} 69eace7efcSopenharmony_ci 70eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 71eace7efcSopenharmony_ci{ 72eace7efcSopenharmony_ci std::shared_ptr<RenderStateObserverManager> manager = std::make_shared<RenderStateObserverManager>(); 73eace7efcSopenharmony_ci manager->Init(); 74eace7efcSopenharmony_ci sptr<IRenderStateObserver> observer; 75eace7efcSopenharmony_ci manager->RegisterRenderStateObserver(observer); 76eace7efcSopenharmony_ci manager->Init(); 77eace7efcSopenharmony_ci sptr<IRenderStateObserver> observers; 78eace7efcSopenharmony_ci manager->UnregisterRenderStateObserver(observers); 79eace7efcSopenharmony_ci manager->Init(); 80eace7efcSopenharmony_ci std::shared_ptr<RenderRecord> renderRecord; 81eace7efcSopenharmony_ci int32_t state = static_cast<int32_t>(GetU32Data(data)); 82eace7efcSopenharmony_ci manager->OnRenderStateChanged(renderRecord, state); 83eace7efcSopenharmony_ci return true; 84eace7efcSopenharmony_ci} 85eace7efcSopenharmony_ci} 86eace7efcSopenharmony_ci 87eace7efcSopenharmony_ci/* Fuzzer entry point */ 88eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 89eace7efcSopenharmony_ci{ 90eace7efcSopenharmony_ci /* Run your code on data */ 91eace7efcSopenharmony_ci if (data == nullptr) { 92eace7efcSopenharmony_ci std::cout << "invalid data" << std::endl; 93eace7efcSopenharmony_ci return 0; 94eace7efcSopenharmony_ci } 95eace7efcSopenharmony_ci 96eace7efcSopenharmony_ci /* Validate the length of size */ 97eace7efcSopenharmony_ci if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { 98eace7efcSopenharmony_ci return 0; 99eace7efcSopenharmony_ci } 100eace7efcSopenharmony_ci 101eace7efcSopenharmony_ci char* ch = (char*)malloc(size + 1); 102eace7efcSopenharmony_ci if (ch == nullptr) { 103eace7efcSopenharmony_ci std::cout << "malloc failed." << std::endl; 104eace7efcSopenharmony_ci return 0; 105eace7efcSopenharmony_ci } 106eace7efcSopenharmony_ci 107eace7efcSopenharmony_ci (void)memset_s(ch, size + 1, 0x00, size + 1); 108eace7efcSopenharmony_ci if (memcpy_s(ch, size + 1, data, size) != EOK) { 109eace7efcSopenharmony_ci std::cout << "copy failed." << std::endl; 110eace7efcSopenharmony_ci free(ch); 111eace7efcSopenharmony_ci ch = nullptr; 112eace7efcSopenharmony_ci return 0; 113eace7efcSopenharmony_ci } 114eace7efcSopenharmony_ci 115eace7efcSopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(ch, size); 116eace7efcSopenharmony_ci free(ch); 117eace7efcSopenharmony_ci ch = nullptr; 118eace7efcSopenharmony_ci return 0; 119eace7efcSopenharmony_ci} 120eace7efcSopenharmony_ci 121