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 "jsvaluerefisjs_fuzzer.h"
17 #include "ecmascript/base/utf_helper.h"
18 #include "ecmascript/ecma_string-inl.h"
19 #include "ecmascript/js_array.h"
20 #include "ecmascript/js_primitive_ref.h"
21 #include "ecmascript/js_typed_array.h"
22 #include "ecmascript/napi/include/jsnapi.h"
23 #include "ecmascript/napi/jsnapi_helper.h"
24 
25 using namespace panda;
26 using namespace panda::ecmascript;
27 using namespace panda::ecmascript::base::utf_helper;
28 
29 namespace OHOS {
JSValueRefIsJSArrayFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)30 void JSValueRefIsJSArrayFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
31 {
32     RuntimeOption option;
33     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
34     EcmaVM *vm = JSNApi::CreateJSVM(option);
35     {
36         JsiFastNativeScope scope(vm);
37         if (size <= 0) {
38             return;
39         }
40         JSHandle<JSTaggedValue> jsArrayTag = JSArray::ArrayCreate(vm->GetJSThread(), JSTaggedNumber(0));
41         Local<JSValueRef> jsArray = JSNApiHelper::ToLocal<JSTypedArray>(jsArrayTag);
42         jsArray->IsJSArray(vm);
43     }
44     JSNApi::DestroyJSVM(vm);
45     return;
46 }
47 
JSValueRefIsJSPrimitiveNumberFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)48 void JSValueRefIsJSPrimitiveNumberFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
49 {
50     RuntimeOption option;
51     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
52     EcmaVM *vm = JSNApi::CreateJSVM(option);
53     {
54         JsiFastNativeScope scope(vm);
55         if (size <= 0) {
56             return;
57         }
58         ObjectFactory *factory = vm->GetFactory();
59         JSHandle<JSTaggedValue> jstagvalue;
60         JSHandle<JSPrimitiveRef> jsprimitive = factory->NewJSPrimitiveRef(PrimitiveType::PRIMITIVE_NUMBER, jstagvalue);
61         JSHandle<JSTaggedValue> jspri = JSHandle<JSTaggedValue>::Cast(jsprimitive);
62         Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(jspri);
63         object->IsJSPrimitiveNumber(vm);
64     }
65     JSNApi::DestroyJSVM(vm);
66     return;
67 }
68 }
69 
70 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72 {
73     // Run your code on data.
74     OHOS::JSValueRefIsJSArrayFuzzTest(data, size);
75     OHOS::JSValueRefIsJSPrimitiveNumberFuzzTest(data, size);
76     return 0;
77 }