1 /*
2 * Copyright (c) 2023 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 "ecmascript/ecma_vm.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/js_weak_container.h"
19 #include "ecmascript/linked_hash_table.h"
20 #include "ecmascript/log_wrapper.h"
21 #include "ecmascript/napi/jsnapi_helper.h"
22 #include "ecmascript/napi/include/jsnapi.h"
23 #include "maprefgetsetkeyvaluesize_fuzzer.h"
24
25 using namespace panda;
26 using namespace panda::ecmascript;
27 namespace OHOS {
Int32GetSizeFuzzerTest(const uint8_t *data, size_t size)28 void Int32GetSizeFuzzerTest(const uint8_t *data, size_t size)
29 {
30 RuntimeOption option;
31 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
32 EcmaVM *vm_ = JSNApi::CreateJSVM(option);
33 if (data == nullptr || size <= 0) {
34 LOG_ECMA(ERROR) << "illegal input!";
35 return;
36 }
37 char *value = new char[size]();
38 if (memcpy_s(value, size, data, size) != EOK) {
39 LOG_ECMA(ERROR) << "memcpy_s failed!";
40 }
41 Local<MapRef> object = MapRef::New(vm_);
42 Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, value, (int32_t)size);
43 Local<JSValueRef> objectvalue = StringRef::NewFromUtf8(vm_, value, (int32_t)size);
44 object->Set(vm_, key, objectvalue);
45 object->GetSize(vm_);
46 object->GetTotalElements(vm_);
47 object->Get(vm_, key);
48 object->GetKey(vm_, 0);
49 object->GetValue(vm_, 0);
50 JSNApi::DestroyJSVM(vm_);
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::Int32GetSizeFuzzerTest(data, size);
59 return 0;
60 }