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 "publicapilocalregexpref_fuzzer.h"
18 #include "ecmascript/base/string_helper.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_regexp.h"
21 #include "ecmascript/regexp/regexp_parser.h"
22 #include "ecmascript/napi/include/jsnapi.h"
23 #include "ecmascript/napi/jsnapi_helper.h"
24
25 using namespace panda;
26 using namespace panda::ecmascript;
27
28 namespace OHOS {
LocalRegExpAllFuzzTest(const uint8_t * data,size_t size)29 void LocalRegExpAllFuzzTest(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 FuzzedDataProvider fdp(data, size);
41 const uint32_t flags = fdp.ConsumeIntegralInRange<uint32_t>(0, (1 << RegExpParser::FLAG_NUM) - 1);
42 const uint32_t len = fdp.ConsumeIntegralInRange<uint32_t>(0, 1024);
43 JSThread *thread = vm->GetJSThread();
44 ObjectFactory *factory = vm->GetFactory();
45 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
46 JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
47 JSHandle<JSHClass> jSRegExpClass = factory->NewEcmaHClass(JSRegExp::SIZE, JSType::JS_REG_EXP, proto);
48 JSHandle<JSRegExp> jSRegExp = JSHandle<JSRegExp>::Cast(factory->NewJSObject(jSRegExpClass));
49 jSRegExp->SetByteCodeBuffer(thread, JSTaggedValue::Undefined());
50 jSRegExp->SetOriginalSource(thread, JSTaggedValue::Undefined());
51 jSRegExp->SetGroupName(thread, JSTaggedValue::Undefined());
52 jSRegExp->SetOriginalFlags(thread, JSTaggedValue(flags));
53 jSRegExp->SetLength(len);
54 JSHandle<JSTaggedValue> jsregtag = JSHandle<JSTaggedValue>::Cast(jSRegExp);
55 Local<RegExpRef> object = JSNApiHelper::ToLocal<RegExpRef>(jsregtag);
56 object->GetOriginalSource(vm);
57 object->GetOriginalFlags(vm);
58 object->IsGlobal(vm);
59 object->IsIgnoreCase(vm);
60 object->IsMultiline(vm);
61 object->IsDotAll(vm);
62 object->IsUtf16(vm);
63 object->IsStick(vm);
64 }
65 JSNApi::DestroyJSVM(vm);
66 }
67 }
68
69 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
71 {
72 // Run your code on data.
73 OHOS::LocalRegExpAllFuzzTest(data, size);
74 return 0;
75 }