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 "aotcompilerargsprepare_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include "aot_compiler_impl.h"
21 #include "securec.h"
22 #include "system_ability_definition.h"
23
24 using namespace OHOS::ArkCompiler;
25
26 namespace OHOS {
DoSomethingInterestingWithMyAPI(const char* data, size_t size)27 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
28 {
29 std::unordered_map<std::string, std::string> argsMap;
30 std::vector<int16_t> sigData;
31 size_t offset = 0;
32 // check if there is enough data
33 if (offset + sizeof(int16_t) > size) {
34 return false;
35 }
36 uint8_t numberOfArgs = static_cast<uint8_t>(data[offset]);
37
38 // parse argsMap
39 offset += sizeof(uint8_t);
40 for (uint8_t i = 0; i < numberOfArgs && offset < size; ++i) {
41 // read the key length
42 if (offset + sizeof(int16_t) > size) {
43 break;
44 }
45 uint8_t keyLength = static_cast<uint8_t>(data[offset]);
46 offset += sizeof(uint8_t);
47 // read the key
48 if (offset + keyLength > size) {
49 break;
50 }
51 std::string key(&data[offset], keyLength);
52 offset += keyLength;
53 // read the value length
54 if (offset + sizeof(uint8_t) > size) {
55 break;
56 }
57 uint8_t valueLength = static_cast<uint8_t>(data[offset]);
58 offset += sizeof(uint8_t);
59 // read the value
60 if (offset + valueLength > size) {
61 break;
62 }
63 std::string value(&data[offset], valueLength);
64 offset += valueLength;
65 argsMap.emplace(std::move(key), std::move(value));
66 }
67
68 // parse sigData
69 while (offset + sizeof(int16_t) <= size) {
70 int16_t signalValue;
71 std::copy_n(&data[offset], sizeof(int16_t), &signalValue);
72 sigData.push_back(signalValue);
73 offset += sizeof(int16_t);
74 }
75 AotCompilerImpl::GetInstance().EcmascriptAotCompiler(argsMap, sigData);
76 return true;
77 }
78 }
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 /* Run your code on data */
84 const char* dataPtr = reinterpret_cast<const char*>(data);
85 OHOS::DoSomethingInterestingWithMyAPI(dataPtr, size);
86 return 0;
87 }