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 {
27 constexpr size_t BYTE_LEN = 8;
28 constexpr size_t MAP_MAX_LEN = 1024;
29 constexpr size_t VEC_MAX_LEN = 2048;
30
GetI16Data(const char * ptr,size_t index)31 int16_t GetI16Data(const char* ptr, size_t index)
32 {
33 return (ptr[index] << BYTE_LEN) | (ptr[index - 1]);
34 }
35
DoSomethingInterestingWithMyAPI(const char * data,size_t size)36 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
37 {
38 std::string argsKey(data, size);
39 std::string argsValue = "fuzz test";
40
41 std::unordered_map<std::string, std::string> argsMap;
42 for (size_t i = size % MAP_MAX_LEN; i > 0; --i) {
43 argsMap.emplace(argsKey, argsValue);
44 }
45 std::vector<int16_t> sigData;
46 for (size_t j = size % VEC_MAX_LEN; j > 0; --j) {
47 sigData.emplace_back(GetI16Data(data, j));
48 }
49 AotCompilerImpl::GetInstance().EcmascriptAotCompiler(argsMap, sigData);
50 return true;
51 }
52 }
53
54 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
56 {
57 /* Run your code on data */
58 const char* dataPtr = reinterpret_cast<const char*>(data);
59
60 OHOS::DoSomethingInterestingWithMyAPI(dataPtr, size);
61 dataPtr = nullptr;
62 return 0;
63 }