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 "jsvaluerefisjsprimitive_fuzzer.h"
17#include "ecmascript/ecma_string-inl.h"
18#include "ecmascript/js_primitive_ref.h"
19#include "ecmascript/napi/include/jsnapi.h"
20#include "ecmascript/napi/jsnapi_helper.h"
21#include "ecmascript/object_factory.h"
22
23using namespace panda;
24using namespace panda::ecmascript;
25
26namespace OHOS {
27void IsJSPrimitiveSymbolFuzztest([[maybe_unused]]const uint8_t *data, size_t size)
28{
29    RuntimeOption option;
30    option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
31    EcmaVM *vm = JSNApi::CreateJSVM(option);
32    {
33        JsiFastNativeScope scope(vm);
34        if (size <= 0) {
35            LOG_ECMA(ERROR) << "illegal input!";
36            return;
37        }
38        ObjectFactory *factory = vm->GetFactory();
39        JSHandle<JSTaggedValue> jsTagValue;
40        JSHandle<JSPrimitiveRef> jsPrimitive = factory->NewJSPrimitiveRef(PrimitiveType::PRIMITIVE_SYMBOL, jsTagValue);
41        JSHandle<JSTaggedValue> jsTagPrimitive = JSHandle<JSTaggedValue>::Cast(jsPrimitive);
42        Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(jsTagPrimitive);
43        object->IsJSPrimitiveSymbol(vm);
44    }
45    JSNApi::DestroyJSVM(vm);
46}
47
48void IsJSPrimitiveStringFuzztest([[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            LOG_ECMA(ERROR) << "illegal input!";
57            return;
58        }
59        ObjectFactory *factory = vm->GetFactory();
60        JSHandle<JSTaggedValue> jsTagValue;
61        JSHandle<JSPrimitiveRef> jsPrimitive = factory->NewJSPrimitiveRef(PrimitiveType::PRIMITIVE_SYMBOL, jsTagValue);
62        JSHandle<JSTaggedValue> jsTagPrimitive = JSHandle<JSTaggedValue>::Cast(jsPrimitive);
63        Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(jsTagPrimitive);
64        object->IsJSPrimitiveString(vm);
65    }
66    JSNApi::DestroyJSVM(vm);
67}
68
69void IsJSPrimitiveIntFuzztest([[maybe_unused]]const uint8_t *data, size_t size)
70{
71    RuntimeOption option;
72    option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
73    EcmaVM *vm = JSNApi::CreateJSVM(option);
74    {
75        JsiFastNativeScope scope(vm);
76        if (size <= 0) {
77            LOG_ECMA(ERROR) << "illegal input!";
78            return;
79        }
80        ObjectFactory *factory = vm->GetFactory();
81        JSHandle<JSTaggedValue> jsTagValue;
82        JSHandle<JSPrimitiveRef> jsPrimitive = factory->NewJSPrimitiveRef(PrimitiveType::PRIMITIVE_BIGINT, jsTagValue);
83        JSHandle<JSTaggedValue> jsTagPrimitive = JSHandle<JSTaggedValue>::Cast(jsPrimitive);
84        Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(jsTagPrimitive);
85        object->IsJSPrimitiveInt(vm);
86    }
87    JSNApi::DestroyJSVM(vm);
88}
89}
90
91// Fuzzer entry point.
92extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
93{
94    // Run your code on data.
95    OHOS::IsJSPrimitiveSymbolFuzztest(data, size);
96    OHOS::IsJSPrimitiveStringFuzztest(data, size);
97    OHOS::IsJSPrimitiveIntFuzztest(data, size);
98    return 0;
99}