1/* 2 * Copyright (c) 2024 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 "getwantsender_fuzzer.h" 17 18#include <cstddef> 19#include <cstdint> 20 21#include "ability_record.h" 22#include "parcel.h" 23#include "sender_info.h" 24#include "want_agent_client.h" 25#include "want_sender_info.h" 26 27using namespace OHOS::AAFwk; 28using namespace OHOS::AppExecFwk; 29 30namespace OHOS { 31namespace { 32constexpr size_t FOO_MAX_LEN = 1024; 33constexpr size_t U32_AT_SIZE = 4; 34} 35sptr<Token> GetFuzzAbilityToken() 36{ 37 sptr<Token> token = nullptr; 38 39 AbilityRequest abilityRequest; 40 abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; 41 abilityRequest.abilityInfo.name = "MainAbility"; 42 abilityRequest.abilityInfo.type = AbilityType::DATA; 43 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); 44 if (abilityRecord) { 45 token = abilityRecord->GetToken(); 46 } 47 48 return token; 49} 50 51bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 52{ 53 // get token 54 sptr<Token> token = GetFuzzAbilityToken(); 55 if (!token) { 56 std::cout << "Get ability token failed." << std::endl; 57 return false; 58 } 59 60 // fuzz for wantSenderInfo 61 Parcel wantSenderInfoParcel; 62 WantSenderInfo* wantSenderInfo = nullptr; 63 sptr<IWantSender> sender = nullptr; 64 if (wantSenderInfoParcel.WriteBuffer(data, size)) { 65 wantSenderInfo = WantSenderInfo::Unmarshalling(wantSenderInfoParcel); 66 if (wantSenderInfo) { 67 WantAgentClient::GetInstance().GetWantSender(*wantSenderInfo, token, sender); 68 } 69 } 70 71 // fuzz for senderInfo 72 Parcel senderInfoParcel; 73 SenderInfo* senderInfo = nullptr; 74 if (senderInfoParcel.WriteBuffer(data, size)) { 75 senderInfo = SenderInfo::Unmarshalling(senderInfoParcel); 76 if (sender && senderInfo) { 77 WantAgentClient::GetInstance().SendWantSender(sender, *senderInfo); 78 } 79 } 80 81 if (senderInfo) { 82 delete senderInfo; 83 senderInfo = nullptr; 84 } 85 if (wantSenderInfo) { 86 delete wantSenderInfo; 87 wantSenderInfo = nullptr; 88 } 89 90 return true; 91} 92} 93 94/* Fuzzer entry point */ 95extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 96{ 97 /* Run your code on data */ 98 if (data == nullptr) { 99 std::cout << "invalid data" << std::endl; 100 return 0; 101 } 102 103 /* Validate the length of size */ 104 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { 105 return 0; 106 } 107 108 char* ch = (char*)malloc(size + 1); 109 if (ch == nullptr) { 110 std::cout << "malloc failed." << std::endl; 111 return 0; 112 } 113 114 (void)memset_s(ch, size + 1, 0x00, size + 1); 115 if (memcpy_s(ch, size, data, size) != EOK) { 116 std::cout << "copy failed." << std::endl; 117 free(ch); 118 ch = nullptr; 119 return 0; 120 } 121 122 OHOS::DoSomethingInterestingWithMyAPI(ch, size); 123 free(ch); 124 ch = nullptr; 125 return 0; 126} 127 128