• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "snapshotserializehugeobject_fuzzer.h"
18 
19 #include "ecmascript/log_wrapper.h"
20 #include "ecmascript/object_factory.h"
21 #include "ecmascript/snapshot/mem/snapshot.h"
22 
23 using namespace panda;
24 using namespace panda::ecmascript;
25 
26 namespace OHOS {
SnapshotSerializeHugeObjectFuzzTest(const uint8_t * data,size_t size)27     void SnapshotSerializeHugeObjectFuzzTest(const uint8_t* data, size_t size)
28     {
29         RuntimeOption option;
30         option.SetLogLevel(common::LOG_LEVEL::ERROR);
31         EcmaVM *vm = JSNApi::CreateJSVM(option);
32         {
33             JsiFastNativeScope scope(vm);
34             if (size <= 0) {
35                 return;
36             }
37             auto factory = vm->GetFactory();
38             const uint32_t hugeSize = 256 * 1024;
39             FuzzedDataProvider fdp(data, size);
40             const uint32_t len1 = fdp.ConsumeIntegralInRange<uint32_t>(hugeSize, hugeSize * 2);
41             JSHandle<TaggedArray> array = factory->NewTaggedArray(len1); // new huge object tagged array
42             const bool anotherArray = fdp.ConsumeBool();
43             if (anotherArray) {
44                 const uint32_t len2 = fdp.ConsumeIntegralInRange<uint32_t>(hugeSize, hugeSize * 2);
45                 JSHandle<TaggedArray> array1 = factory->NewTaggedArray(len2);
46                 array->Set(vm->GetAssociatedJSThread(), 0, array1.GetTaggedValue());
47             }
48             const CString fileName = "snapshot";
49             Snapshot snapshotSerialize(vm);
50             // serialize
51             snapshotSerialize.Serialize(*array, nullptr, fileName);
52             // deserialize
53             Snapshot snapshotDeserialize(vm);
54             snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName);
55             // remove snapshot file if exist
56             std::remove(fileName.c_str());
57         }
58         JSNApi::DestroyJSVM(vm);
59     }
60 }
61 
62 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
64 {
65     // Run your code on data.
66     OHOS::SnapshotSerializeHugeObjectFuzzTest(data, size);
67     return 0;
68 }