13fc297bdSopenharmony_ci/* 23fc297bdSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 33fc297bdSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43fc297bdSopenharmony_ci * you may not use this file except in compliance with the License. 53fc297bdSopenharmony_ci * You may obtain a copy of the License at 63fc297bdSopenharmony_ci * 73fc297bdSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83fc297bdSopenharmony_ci * 93fc297bdSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103fc297bdSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113fc297bdSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123fc297bdSopenharmony_ci * See the License for the specific language governing permissions and 133fc297bdSopenharmony_ci * limitations under the License. 143fc297bdSopenharmony_ci */ 153fc297bdSopenharmony_ci 163fc297bdSopenharmony_ci#include "socperf_fuzzer.h" 173fc297bdSopenharmony_ci 183fc297bdSopenharmony_ci#include "iservice_registry.h" 193fc297bdSopenharmony_ci#include "system_ability_definition.h" 203fc297bdSopenharmony_ci 213fc297bdSopenharmony_ci#include "i_socperf_service.h" 223fc297bdSopenharmony_ci#include "socperf_fuzz_mock.h" 233fc297bdSopenharmony_ci#include "socperf_log.h" 243fc297bdSopenharmony_ci 253fc297bdSopenharmony_cinamespace OHOS { 263fc297bdSopenharmony_cinamespace SOCPERF { 273fc297bdSopenharmony_ci constexpr int32_t MIN_LEN = 4; 283fc297bdSopenharmony_ci std::mutex mutexLock; 293fc297bdSopenharmony_ci sptr<IRemoteObject> remoteObj = nullptr; 303fc297bdSopenharmony_ci 313fc297bdSopenharmony_ci bool DoInit() 323fc297bdSopenharmony_ci { 333fc297bdSopenharmony_ci std::lock_guard<std::mutex> lock(mutexLock); 343fc297bdSopenharmony_ci if (remoteObj) { 353fc297bdSopenharmony_ci return true; 363fc297bdSopenharmony_ci } 373fc297bdSopenharmony_ci auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 383fc297bdSopenharmony_ci if (!samgr) { 393fc297bdSopenharmony_ci return false; 403fc297bdSopenharmony_ci } 413fc297bdSopenharmony_ci remoteObj = samgr->GetSystemAbility(SOC_PERF_SERVICE_SA_ID); 423fc297bdSopenharmony_ci if (!remoteObj) { 433fc297bdSopenharmony_ci return false; 443fc297bdSopenharmony_ci } 453fc297bdSopenharmony_ci return true; 463fc297bdSopenharmony_ci } 473fc297bdSopenharmony_ci 483fc297bdSopenharmony_ci int32_t onRemoteRequest(uint32_t code, MessageParcel& data) 493fc297bdSopenharmony_ci { 503fc297bdSopenharmony_ci if (!DoInit()) { 513fc297bdSopenharmony_ci return -1; 523fc297bdSopenharmony_ci } 533fc297bdSopenharmony_ci MessageParcel reply; 543fc297bdSopenharmony_ci MessageOption option; 553fc297bdSopenharmony_ci return remoteObj->SendRequest(code, data, reply, option); 563fc297bdSopenharmony_ci } 573fc297bdSopenharmony_ci 583fc297bdSopenharmony_ci bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) 593fc297bdSopenharmony_ci { 603fc297bdSopenharmony_ci if (size <= MIN_LEN) { 613fc297bdSopenharmony_ci return false; 623fc297bdSopenharmony_ci } 633fc297bdSopenharmony_ci 643fc297bdSopenharmony_ci MessageParcel dataMessageParcel; 653fc297bdSopenharmony_ci if (!dataMessageParcel.WriteInterfaceToken(IRemoteStub<ISocPerfService>::GetDescriptor())) { 663fc297bdSopenharmony_ci return false; 673fc297bdSopenharmony_ci } 683fc297bdSopenharmony_ci 693fc297bdSopenharmony_ci uint32_t code = *(reinterpret_cast<const uint32_t*>(data)); 703fc297bdSopenharmony_ci size -= sizeof(uint32_t); 713fc297bdSopenharmony_ci 723fc297bdSopenharmony_ci dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size); 733fc297bdSopenharmony_ci dataMessageParcel.RewindRead(0); 743fc297bdSopenharmony_ci 753fc297bdSopenharmony_ci onRemoteRequest(code, dataMessageParcel); 763fc297bdSopenharmony_ci return true; 773fc297bdSopenharmony_ci } 783fc297bdSopenharmony_ci} // namespace SOCPERF 793fc297bdSopenharmony_ci} // namespace OHOS 803fc297bdSopenharmony_ci 813fc297bdSopenharmony_ci/* Fuzzer entry point */ 823fc297bdSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 833fc297bdSopenharmony_ci{ 843fc297bdSopenharmony_ci /* Run your code on data */ 853fc297bdSopenharmony_ci OHOS::SOCPERF::MockProcess(); 863fc297bdSopenharmony_ci OHOS::SOCPERF::DoSomethingInterestingWithMyAPI(data, size); 873fc297bdSopenharmony_ci return 0; 883fc297bdSopenharmony_ci} 893fc297bdSopenharmony_ci 90