• 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 "containersfastbufferequals_fuzzer.h"
18 
19 #include "ecmascript/containers/containers_buffer.h"
20 #include "ecmascript/containers/containers_private.h"
21 #include "ecmascript/ecma_string-inl.h"
22 #include "ecmascript/ecma_vm.h"
23 #include "ecmascript/global_env.h"
24 #include "ecmascript/js_api/js_api_buffer.h"
25 #include "ecmascript/js_handle.h"
26 #include "ecmascript/object_factory.h"
27 
28 using namespace panda;
29 using namespace panda::ecmascript;
30 using namespace panda::ecmascript::containers;
31 
32 #define MAXBYTELEN sizeof(uint32_t)
33 
34 namespace OHOS {
35 
JSObjectCreate(JSThread * thread)36     JSFunction *JSObjectCreate(JSThread *thread)
37     {
38         EcmaVM *ecmaVM = thread->GetEcmaVM();
39         JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
40         return globalEnv->GetObjectFunction().GetObject<JSFunction>();
41     }
42 
CreateEcmaRuntimeCallInfo(JSThread * thread,uint32_t numArgs)43     EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)
44     {
45         auto factory = thread->GetEcmaVM()->GetFactory();
46         JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread));
47         JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
48         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
49         EcmaRuntimeCallInfo *objCallInfo =
50             EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs);
51         return objCallInfo;
52     }
InitializeFastBufferConstructor(JSThread * thread)53     JSTaggedValue InitializeFastBufferConstructor(JSThread *thread)
54     {
55         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
56         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
57 
58         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
59         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
60         JSHandle<JSTaggedValue> value =
61             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
62 
63         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6);
64         objCallInfo->SetFunction(JSTaggedValue::Undefined());
65         objCallInfo->SetThis(value.GetTaggedValue());
66         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::FastBuffer)));
67         JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
68         return result;
69     }
70 
CreateJSAPIFastBuffer(JSThread * thread,uint32_t length)71     JSHandle<JSAPIFastBuffer> CreateJSAPIFastBuffer(JSThread *thread, uint32_t length)
72     {
73         JSHandle<JSFunction> newTarget(thread, InitializeFastBufferConstructor(thread));
74         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6);
75         objCallInfo->SetFunction(newTarget.GetTaggedValue());
76         objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
77         objCallInfo->SetThis(JSTaggedValue::Undefined());
78         objCallInfo->SetCallArg(0, JSTaggedValue(length));
79 
80         JSTaggedValue result = ContainersBuffer::BufferConstructor(objCallInfo);
81         JSHandle<JSAPIFastBuffer> buffer(thread, result);
82         return buffer;
83     }
84 
ContainersFastBufferIndexOfFuzzTest(const uint8_t * data,size_t size)85     void ContainersFastBufferIndexOfFuzzTest(const uint8_t* data, size_t size)
86     {
87         constexpr size_t STRING_MAX_LENGTH = 100;
88         RuntimeOption option;
89         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
90         EcmaVM *vm = JSNApi::CreateJSVM(option);
91         {
92             JsiFastNativeScope scope(vm);
93             auto thread = vm->GetAssociatedJSThread();
94 
95             const int32_t MaxMenory = 1024;
96             if (size > MaxMenory) {
97                 size = MaxMenory;
98             }
99             FuzzedDataProvider fdp(data, size);
100             ObjectFactory *factory = vm->GetFactory();
101             std::string rdStr = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
102             JSHandle<EcmaString> str = factory->NewFromStdString(rdStr);
103 
104             JSHandle<JSAPIFastBuffer> buf1 = CreateJSAPIFastBuffer(thread, STRING_MAX_LENGTH);
105             JSHandle<JSAPIFastBuffer> buf2 = CreateJSAPIFastBuffer(thread, STRING_MAX_LENGTH);
106             EcmaRuntimeCallInfo *callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6);
107             callInfo1->SetFunction(JSTaggedValue::Undefined());
108             callInfo1->SetThis(buf1.GetTaggedValue());
109             // 0 : means the first parameter
110             callInfo1->SetCallArg(0, JSTaggedValue(*str));
111             [[maybe_unused]] JSTaggedValue ret = ContainersBuffer::Write(callInfo1);
112             EcmaRuntimeCallInfo *callInfo2 = CreateEcmaRuntimeCallInfo(thread, 6);
113             callInfo2->SetFunction(JSTaggedValue::Undefined());
114             callInfo2->SetThis(buf2.GetTaggedValue());
115             // 0 : means the first parameter
116             callInfo2->SetCallArg(0, JSTaggedValue(*str));
117             ret = ContainersBuffer::Write(callInfo2);
118             EcmaRuntimeCallInfo *callInfo3 = CreateEcmaRuntimeCallInfo(thread, 6);
119             callInfo3->SetFunction(JSTaggedValue::Undefined());
120             callInfo3->SetThis(buf1.GetTaggedValue());
121             // 0 : means the first parameter
122             callInfo3->SetCallArg(0, buf2.GetTaggedValue());
123             ret = ContainersBuffer::Equals(callInfo3);
124         }
125         JSNApi::DestroyJSVM(vm);
126     }
127 }
128 
129 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)130 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
131 {
132     // Run your code on data.
133     OHOS::ContainersFastBufferIndexOfFuzzTest(data, size);
134     return 0;
135 }