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 <fuzzer/FuzzedDataProvider.h>
17 #include "ecmascript/ecma_string-inl.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_handle.h"
20 #include "ecmascript/js_collator.h"
21 #include "ecmascript/napi/include/jsnapi.h"
22 #include "ecmascript/napi/jsnapi_helper.h"
23 #include "publicapicollatorrefget_fuzzer.h"
24
25 using namespace panda;
26 using namespace panda::ecmascript;
27
28 namespace OHOS {
GetCompareFunctionFuzzTest(const uint8_t * data,size_t size)29 void GetCompareFunctionFuzzTest(const uint8_t *data, size_t size)
30 {
31 RuntimeOption option;
32 option.SetLogLevel(common::LOG_LEVEL::ERROR);
33 EcmaVM *vm = JSNApi::CreateJSVM(option);
34 {
35 JsiFastNativeScope scope(vm);
36 if (size <= 0) {
37 LOG_ECMA(ERROR) << "illegal input!";
38 return;
39 }
40 JSThread *thread = vm->GetJSThread();
41 JSHandle<GlobalEnv> env = vm->GetGlobalEnv();
42 ObjectFactory *factory = vm->GetFactory();
43 JSHandle<JSTaggedValue> ctor = env->GetCollatorFunction();
44 JSHandle<JSCollator> collator =
45 JSHandle<JSCollator>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
46 FuzzedDataProvider fdp(data, size);
47 std::string str = fdp.PickValueInArray({
48 "en-US", "en", "fr", "es", "de", "pt", "it", "ca",
49 "de-AT", "fi", "id", "id-ID", "ms", "nl", "pl", "ro",
50 "sl", "sv", "sw", "vi", "en-DE", "en-GB",
51 });
52 JSHandle<JSTaggedValue> localeStr(factory->NewFromStdString(str));
53 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
54 JSHandle<JSCollator> initCollator =
55 JSCollator::InitializeCollator(thread, collator, localeStr, undefinedHandle);
56 JSHandle<JSTaggedValue> collatorTagHandleVal = JSHandle<JSTaggedValue>::Cast(initCollator);
57 Local<CollatorRef> object = JSNApiHelper::ToLocal<CollatorRef>(collatorTagHandleVal);
58 object->GetCompareFunction(vm);
59 }
60 JSNApi::DestroyJSVM(vm);
61 return;
62 }
63 }
64
65 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
67 {
68 // Run your code on data.
69 OHOS::GetCompareFunctionFuzzTest(data, size);
70 return 0;
71 }