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 "jsvaluerefisplainarray_fuzzer.h"
17#include "ecmascript/containers/containers_list.h"
18#include "ecmascript/containers/containers_private.h"
19#include "ecmascript/ecma_string-inl.h"
20#include "ecmascript/ecma_vm.h"
21#include "ecmascript/global_env.h"
22#include "ecmascript/js_handle.h"
23#include "ecmascript/js_tagged_value.h"
24#include "ecmascript/napi/include/jsnapi.h"
25#include "ecmascript/js_thread.h"
26#include "ecmascript/js_global_object.h"
27#include "ecmascript/napi/jsnapi_helper.h"
28#include "ecmascript/linked_hash_table.h"
29#include "ecmascript/ecma_runtime_call_info.h"
30#include "ecmascript/containers/containers_hashset.h"
31#include "ecmascript/common.h"
32#include "ecmascript/frames.h"
33#include "ecmascript/tagged_list.h"
34#include "ecmascript/linked_hash_table.h"
35#include "ecmascript/js_api/js_api_list.h"
36#include "ecmascript/js_api/js_api_linked_list.h"
37#include "ecmascript/js_api/js_api_linked_list_iterator.h"
38#include "ecmascript/js_map_iterator.h"
39#include "ecmascript/containers/containers_arraylist.h"
40#include "ecmascript/js_api/js_api_arraylist.h"
41#include "ecmascript/builtins/builtins_function.h"
42#include "ecmascript/builtins/builtins.h"
43#include "ecmascript/ecma_global_storage.h"
44#include "ecmascript/js_bigint.h"
45#include "ecmascript/js_runtime_options.h"
46#include "ecmascript/object_factory.h"
47#include "ecmascript/tagged_array.h"
48#include "ecmascript/js_generator_object.h"
49#include "ecmascript/js_string_iterator.h"
50#include "ecmascript/js_date_time_format.h"
51#include "ecmascript/js_tagged_number.h"
52#include "ecmascript/js_api/js_api_hashmap.h"
53#include "ecmascript/builtins/builtins_regexp.h"
54#include "ecmascript/js_regexp.h"
55#include "ecmascript/tagged_hash_array.h"
56#include "ecmascript/containers/containers_lightweightmap.h"
57#include "ecmascript/containers/containers_lightweightset.h"
58#include "ecmascript/js_api/js_api_plain_array.h"
59
60using namespace panda;
61using namespace panda::test;
62using namespace panda::ecmascript;
63using namespace panda::ecmascript::containers;
64
65namespace OHOS {
66EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength)
67{
68    const uint8_t testDecodedSize = 2;
69    int32_t numActualArgs = argvLength / testDecodedSize + 1;
70    JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
71
72    size_t frameSize = 0;
73    if (thread->IsAsmInterpreter()) {
74        frameSize = InterpretedEntryFrame::NumOfMembers() + numActualArgs;
75    } else {
76        frameSize = InterpretedFrame::NumOfMembers() + numActualArgs;
77    }
78    JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
79    for (int i = numActualArgs; i > 0; i--) {
80        newSp[i - 1] = JSTaggedValue::Undefined().GetRawData();
81    }
82    EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp - 2);
83    *(--newSp) = numActualArgs;
84    *(--newSp) = panda::ecmascript::ToUintPtr(thread);
85    ecmaRuntimeCallInfo->SetNewTarget(newTgt);
86    return ecmaRuntimeCallInfo;
87}
88
89static JSTaggedType *SetupFrame(JSThread *thread, EcmaRuntimeCallInfo *info)
90{
91    JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
92    size_t frameSize = 0;
93    const int num = 2;
94    // 2 means thread and numArgs
95    if (thread->IsAsmInterpreter()) {
96        frameSize = InterpretedEntryFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
97    } else {
98        frameSize = InterpretedFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
99    }
100    JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
101
102    InterpretedEntryFrame *state = reinterpret_cast<InterpretedEntryFrame *>(newSp) - 1;
103    state->base.type = ecmascript::FrameType::INTERPRETER_ENTRY_FRAME;
104    state->base.prev = sp;
105    state->pc = nullptr;
106    thread->SetCurrentSPFrame(newSp);
107    return sp;
108}
109
110void TearDownFrame(JSThread *thread, JSTaggedType *prev)
111{
112    thread->SetCurrentSPFrame(prev);
113}
114
115void JSValueRefIsPlainArrayFuzzTest([[maybe_unused]] const uint8_t *data, size_t size)
116{
117    RuntimeOption option;
118    option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
119    EcmaVM *vm = JSNApi::CreateJSVM(option);
120    {
121        JsiFastNativeScope scope(vm);
122        if (size <= 0) {
123            LOG_ECMA(ERROR) << "Parameter out of range..";
124            return;
125        }
126        auto thread = vm->GetAssociatedJSThread();
127        ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
128        JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
129        JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
130        JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
131        JSHandle<JSTaggedValue> value =
132            JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
133        auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
134        objCallInfo->SetFunction(JSTaggedValue::Undefined());
135        objCallInfo->SetThis(value.GetTaggedValue());
136        objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::PlainArray)));
137        [[maybe_unused]] auto prev = SetupFrame(thread, objCallInfo);
138        JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
139        TearDownFrame(thread, prev);
140        JSHandle<JSTaggedValue> constructor(thread, result);
141        JSHandle<JSAPIPlainArray> plainArray(
142            factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
143        constexpr int nodeNumbers = 8;
144        JSHandle<JSTaggedValue> keyArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(nodeNumbers));
145        JSHandle<JSTaggedValue> valueArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(nodeNumbers));
146        plainArray->SetKeys(thread, keyArray);
147        plainArray->SetValues(thread, valueArray);
148        JSHandle<JSTaggedValue> plainarraytag = JSHandle<JSTaggedValue>::Cast(plainArray);
149        Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(plainarraytag);
150        object->IsPlainArray(vm);
151    }
152    JSNApi::DestroyJSVM(vm);
153}
154}
155
156// Fuzzer entry point.
157extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
158{
159    // Run your code on data.
160    OHOS::JSValueRefIsPlainArrayFuzzTest(data, size);
161    return 0;
162}