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(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 = *reinterpret_cast<const uint32_t*>(data);
39 data += UINT32_SIZE;
40 size -= UINT32_SIZE;
41
42 std::string hapPath(reinterpret_cast<const char*>(data), pathLen);
43 data += pathLen;
44 size -= pathLen;
45
46 uint32_t offset = *reinterpret_cast<const uint32_t*>(data);
47 data += UINT32_SIZE;
48 size -= UINT32_SIZE;
49
50 uint32_t fileSize = *reinterpret_cast<const uint32_t*>(data);
51 data += UINT32_SIZE;
52 size -= UINT32_SIZE;
53
54 // set runtimeOptions
55 runtimeOptions.SetHapPath(hapPath);
56 runtimeOptions.SetHapAbcOffset(offset);
57 runtimeOptions.SetHapAbcSize(fileSize);
58
59 std::shared_ptr<JSPandaFile> pf;
60 OhosPkgArgs pkgArgs;
61
62 pkgArgs.GetJSPandaFile(runtimeOptions, pf, 0);
63 }
64 }
65
66 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
68 {
69 // Run your code on data.
70 OHOS::GetJSPandaFileFuzzTest(data, size);
71 return 0;
72 }