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 "printservice_fuzzer.h" 17 18#include "print_constant.h" 19#include "print_log.h" 20#include "print_service_ability.h" 21 22namespace OHOS { 23namespace Print { 24 constexpr size_t FOO_MAX_LEN = 1024; 25 constexpr size_t U32_AT_SIZE = 4; 26 const std::u16string PRINT_SERVICE_INTERFACE_TOKEN = u"OHOS.Print.IPrintService"; 27 28 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 29 { 30 for (uint32_t code = CMD_START_PRINT; code <= CMD_NOTIFY_PRINT_SERVICE; ++code) { 31 MessageParcel datas; 32 datas.WriteInterfaceToken(PRINT_SERVICE_INTERFACE_TOKEN); 33 datas.WriteBuffer(data, size); 34 datas.RewindRead(0); 35 MessageParcel reply; 36 MessageOption option; 37 PrintServiceAbility::GetInstance()->OnRemoteRequest(code, datas, reply, option); 38 } 39 return true; 40 } 41} 42} 43 44/* Fuzzer entry point */ 45extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 46{ 47 /* Run your code on data */ 48 if (data == nullptr) { 49 return 0; 50 } 51 52 if (size < OHOS::Print::U32_AT_SIZE) { 53 return 0; 54 } 55 56 /* Validate the length of size */ 57 if (size == 0 || size > OHOS::Print::FOO_MAX_LEN) { 58 return 0; 59 } 60 61 char* ch = (char *)malloc(size + 1); 62 if (ch == nullptr) { 63 return 0; 64 } 65 66 (void)memset_s(ch, size + 1, 0x00, size + 1); 67 if (memcpy_s(ch, size + 1, data, size) != EOK) { 68 free(ch); 69 ch = nullptr; 70 return 0; 71 } 72 73 OHOS::Print::DoSomethingInterestingWithMyAPI(ch, size); 74 free(ch); 75 ch = nullptr; 76 return 0; 77} 78 79