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 "containersfastbufferwrite_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
ContainersFastBufferWriteOfFuzzTest(const uint8_t * data,size_t size)85 void ContainersFastBufferWriteOfFuzzTest(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
101 int32_t offset = fdp.ConsumeIntegral<int32_t>();
102 int32_t number = fdp.ConsumeIntegral<int32_t>();
103 ObjectFactory *factory = vm->GetFactory();
104 std::string rdStr = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
105 JSHandle<EcmaString> str = factory->NewFromStdString(rdStr);
106 JSHandle<JSAPIFastBuffer> buf1 = CreateJSAPIFastBuffer(thread, STRING_MAX_LENGTH);
107
108 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 12);
109 callInfo->SetFunction(JSTaggedValue::Undefined());
110 callInfo->SetThis(buf1.GetTaggedValue());
111 // 0 : means the first parameter
112 callInfo->SetCallArg(0, JSTaggedValue(*str));
113 // 1 : means the second parameter
114 callInfo->SetCallArg(1, JSTaggedValue(offset));
115 // 2 : means the third parameter
116 callInfo->SetCallArg(2, JSTaggedValue(number));
117 // 3 : means the fourth parameter
118 callInfo->SetCallArg(3, JSTaggedValue(*str));
119 [[maybe_unused]] JSTaggedValue ret = ContainersBuffer::Write(callInfo);
120 }
121 JSNApi::DestroyJSVM(vm);
122 }
123 }
124
125 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)126 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
127 {
128 // Run your code on data.
129 OHOS::ContainersFastBufferWriteOfFuzzTest(data, size);
130 return 0;
131 }