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 "getjspandafile_fuzzer.h"
17 #include "ecmascript/ohos/ohos_pkg_args.h"
18
19 using namespace panda;
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::kungfu;
22
23 namespace OHOS {
24
GetJSPandaFileFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)25 void GetJSPandaFileFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
26 {
27 constexpr size_t UINT32_SIZE = sizeof(uint32_t);
28
29 // requires at least 12 bytes to properly extract and interpret three 32-bit unsigned integers (uint32_t)
30 constexpr size_t MIN_SIZE_REQUIRED = UINT32_SIZE * 3;
31 if (size < MIN_SIZE_REQUIRED) {
32 return;
33 }
34
35 RuntimeOption option;
36 JSRuntimeOptions runtimeOptions;
37
38 uint32_t pathLen = 0;
39 size_t maxByteLen = 4;
40 if (size > maxByteLen) {
41 size = maxByteLen;
42 }
43 if (memcpy_s(&pathLen, maxByteLen, data, size) != EOK) {
44 std::cout << "memcpy_s failed!";
45 UNREACHABLE();
46 }
47 data += UINT32_SIZE;
48 size -= UINT32_SIZE;
49 std::string hapPath(data, data + size);
50 data += pathLen;
51 size -= pathLen;
52
53 uint32_t offset = pathLen + MIN_SIZE_REQUIRED;
54 data += UINT32_SIZE;
55 size -= UINT32_SIZE;
56
57 uint32_t fileSize = offset + MIN_SIZE_REQUIRED;
58 data += UINT32_SIZE;
59 size -= UINT32_SIZE;
60
61 // set runtimeOptions
62 runtimeOptions.SetHapPath(hapPath);
63 runtimeOptions.SetHapAbcOffset(offset);
64 runtimeOptions.SetHapAbcSize(fileSize);
65
66 std::shared_ptr<JSPandaFile> pf;
67 OhosPkgArgs pkgArgs;
68
69 pkgArgs.GetJSPandaFile(runtimeOptions, pf);
70 }
71 }
72
73 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76 // Run your code on data.
77 OHOS::GetJSPandaFileFuzzTest(data, size);
78 return 0;
79 }