1 /*
2 * Copyright (c) 2022 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 <fuzzer/FuzzedDataProvider.h>
17 #include "snapshotserializebuiltins_fuzzer.h"
18
19 #include "ecmascript/log_wrapper.h"
20 #include "ecmascript/snapshot/mem/snapshot.h"
21
22 using namespace panda;
23 using namespace panda::ecmascript;
24
25 namespace OHOS {
SnapshotSerializeBuiltinsFuzzTest(const uint8_t * data,size_t size)26 void SnapshotSerializeBuiltinsFuzzTest(const uint8_t* data, size_t size)
27 {
28 // remove builtins.snapshot file first if exist
29 const CString fileName = "builtins.snapshot";
30 std::remove(fileName.c_str());
31 // generate builtins.snapshot file
32 FuzzedDataProvider fdp(data, size);
33 const int arkProp1 = fdp.ConsumeIntegral<int>();
34 RuntimeOption option1;
35 option1.SetLogLevel(common::LOG_LEVEL::ERROR);
36 option1.SetArkProperties(ArkProperties::ENABLE_SNAPSHOT_SERIALIZE | arkProp1);
37 // create vm and generate builtins.snapshot file
38 EcmaVM *vm1 = JSNApi::CreateJSVM(option1);
39 if (size <= 0 || data == nullptr) {
40 return;
41 }
42 JSNApi::DestroyJSVM(vm1);
43
44 const int arkProp2 = fdp.ConsumeIntegral<int>();
45 RuntimeOption option2;
46 option2.SetLogLevel(common::LOG_LEVEL::ERROR);
47 option2.SetArkProperties(ArkProperties::ENABLE_SNAPSHOT_DESERIALIZE | arkProp2);
48 // create vm by deserialize builtins.snapshot file
49 EcmaVM *vm2 = JSNApi::CreateJSVM(option2);
50 JSNApi::DestroyJSVM(vm2);
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 OHOS::SnapshotSerializeBuiltinsFuzzTest(data, size);
59 return 0;
60 }