• 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 "containersfastbuffercopy_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             int32_t targetStart = 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             EcmaRuntimeCallInfo *callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6);
110             callInfo1->SetFunction(JSTaggedValue::Undefined());
111             callInfo1->SetThis(buf1.GetTaggedValue());
112             // 0 : means the first parameter
113             callInfo1->SetCallArg(0, JSTaggedValue(*str));
114             [[maybe_unused]] JSTaggedValue ret = ContainersBuffer::Write(callInfo1);
115             EcmaRuntimeCallInfo *callInfo2 = CreateEcmaRuntimeCallInfo(thread, 14);
116             callInfo2->SetFunction(JSTaggedValue::Undefined());
117             callInfo2->SetThis(buf1.GetTaggedValue());
118             // 0 : means the first parameter
119             callInfo2->SetCallArg(0, buf2.GetTaggedValue());
120             // 1 : means the second parameter
121             callInfo2->SetCallArg(1, JSTaggedValue(targetStart));
122             // 2 : means the third parameter
123             callInfo2->SetCallArg(2, JSTaggedValue(sourceStart));
124             // 3 : means the fourth parameter
125             callInfo2->SetCallArg(3, JSTaggedValue(sourceEnd));
126             ret = ContainersBuffer::Copy(callInfo2);
127         }
128         JSNApi::DestroyJSVM(vm);
129     }
130 }
131 
132 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)133 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
134 {
135     // Run your code on data.
136     OHOS::ContainersFastBufferIndexOfFuzzTest(data, size);
137     return 0;
138 }