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 "containersfastbuffercompare_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
ContainersFastBufferCompareFuzzTest(const uint8_t * data,size_t size)85 void ContainersFastBufferCompareFuzzTest(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 const int32_t MaxMenory = 1024;
95 if (size > MaxMenory) {
96 size = MaxMenory;
97 }
98 FuzzedDataProvider fdp(data, size);
99 ObjectFactory *factory = vm->GetFactory();
100 int32_t targetStart = fdp.ConsumeIntegral<int32_t>();
101 int32_t targetEnd = fdp.ConsumeIntegral<int32_t>();
102 int32_t sourceStart = fdp.ConsumeIntegral<int32_t>();
103 int32_t sourceEnd = fdp.ConsumeIntegral<int32_t>();
104 std::string rdStr = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
105 JSHandle<EcmaString> str = factory->NewFromStdString(rdStr);
106
107 JSHandle<JSAPIFastBuffer> buf1 = CreateJSAPIFastBuffer(thread, STRING_MAX_LENGTH);
108 JSHandle<JSAPIFastBuffer> buf2 = CreateJSAPIFastBuffer(thread, STRING_MAX_LENGTH);
109
110 EcmaRuntimeCallInfo *callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6);
111 callInfo1->SetFunction(JSTaggedValue::Undefined());
112 callInfo1->SetThis(buf1.GetTaggedValue());
113 // 0 : means the first parameter
114 callInfo1->SetCallArg(0, JSTaggedValue(*str));
115 [[maybe_unused]] JSTaggedValue ret = ContainersBuffer::Write(callInfo1);
116 EcmaRuntimeCallInfo *callInfo2 = CreateEcmaRuntimeCallInfo(thread, 6);
117 callInfo2->SetFunction(JSTaggedValue::Undefined());
118 callInfo2->SetThis(buf2.GetTaggedValue());
119 // 0 : means the first parameter
120 callInfo2->SetCallArg(0, JSTaggedValue(*str));
121 ret = ContainersBuffer::Write(callInfo2);
122 EcmaRuntimeCallInfo *callInfo3 = CreateEcmaRuntimeCallInfo(thread, 14);
123 callInfo3->SetFunction(JSTaggedValue::Undefined());
124 callInfo3->SetThis(buf2.GetTaggedValue());
125 // 0 : means the first parameter
126 callInfo3->SetCallArg(0, buf1.GetTaggedValue());
127 // 1 : means the second parameter
128 callInfo3->SetCallArg(1, JSTaggedValue(targetStart));
129 // 2 : means the third parameter
130 callInfo3->SetCallArg(2, JSTaggedValue(targetEnd));
131 // 3 : means the fourth parameter
132 callInfo3->SetCallArg(3, JSTaggedValue(sourceStart));
133 // 4 : means the fifth parameter
134 callInfo3->SetCallArg(4, JSTaggedValue(sourceEnd));
135 ret = ContainersBuffer::Compare(callInfo3);
136 }
137 JSNApi::DestroyJSVM(vm);
138 }
139 }
140
141 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)142 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
143 {
144 // Run your code on data.
145 OHOS::ContainersFastBufferCompareFuzzTest(data, size);
146 return 0;
147 }