• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "compilerinterfacestub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include "aot_compiler_service.h"
21 #include "securec.h"
22 #include "system_ability_definition.h"
23 
24 using namespace OHOS::ArkCompiler;
25 
26 namespace OHOS {
27 constexpr int DATA_ZERO = 0;
28 constexpr int DATA_ONE = 1;
29 constexpr int DATA_TWO = 2;
30 constexpr int DATA_THREE = 3;
31 constexpr size_t OFFSET_ONE = 8;
32 constexpr size_t OFFSET_TWO = 16;
33 constexpr size_t OFFSET_THREE = 24;
34 constexpr size_t FOO_MAX_LEN = 1024;
35 constexpr size_t MESSAGE_SIZE = MIN_TRANSACTION_ID + 4; // 4 code methods OnRemoteRequest
36 constexpr size_t U32_AT_SIZE = 4;
37 static AotCompilerService compilerService(AOT_COMPILER_SERVICE_ID, false);
38 
GetU32Data(const char * ptr)39 uint32_t GetU32Data(const char* ptr)
40 {
41     return (ptr[DATA_ZERO] << OFFSET_THREE) | (ptr[DATA_ONE] << OFFSET_TWO) |
42            (ptr[DATA_TWO] << OFFSET_ONE) | (ptr[DATA_THREE]);
43 }
44 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)45 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
46 {
47     uint32_t code = (GetU32Data(data) % MESSAGE_SIZE);
48     MessageParcel datas;
49     std::u16string descriptor = AotCompilerService::GetDescriptor();
50     datas.WriteInterfaceToken(descriptor);
51     datas.WriteBuffer(data, size);
52     datas.RewindRead(0);
53     MessageParcel reply;
54     MessageOption option;
55     compilerService.OnRemoteRequest(code, datas, reply, option);
56     return true;
57 }
58 }
59 
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63     /* Run your code on data */
64     if (data == nullptr) {
65         return 0;
66     }
67 
68     if (size < OHOS::U32_AT_SIZE) {
69         return 0;
70     }
71 
72     /* Validate the length of size */
73     if (size > OHOS::FOO_MAX_LEN) {
74         return 0;
75     }
76 
77     char* dataPtr = static_cast<char*>(malloc(size + 1));
78     if (dataPtr == nullptr) {
79         return 0;
80     }
81 
82     (void)memset_s(dataPtr, size + 1, 0x00, size + 1);
83     if (memcpy_s(dataPtr, size + 1, data, size) != EOK) {
84         free(dataPtr);
85         dataPtr = nullptr;
86         return 0;
87     }
88 
89     OHOS::DoSomethingInterestingWithMyAPI(dataPtr, size);
90     free(dataPtr);
91     dataPtr = nullptr;
92     return 0;
93 }