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 "jsvaluerefisjs_fuzzer.h"
18 #include "common_components/base/utf_helper.h"
19 #include "ecmascript/ecma_string-inl.h"
20 #include "ecmascript/js_array.h"
21 #include "ecmascript/js_primitive_ref.h"
22 #include "ecmascript/js_typed_array.h"
23 #include "ecmascript/napi/include/jsnapi.h"
24 #include "ecmascript/napi/jsnapi_helper.h"
25
26 using namespace panda;
27 using namespace panda::ecmascript;
28 using namespace common::utf_helper;
29
30 namespace OHOS {
JSValueRefIsJSArrayFuzzTest(const uint8_t * data,size_t size)31 void JSValueRefIsJSArrayFuzzTest(const uint8_t *data, size_t size)
32 {
33 RuntimeOption option;
34 option.SetLogLevel(common::LOG_LEVEL::ERROR);
35 EcmaVM *vm = JSNApi::CreateJSVM(option);
36 {
37 JsiFastNativeScope scope(vm);
38 if (size <= 0) {
39 return;
40 }
41 FuzzedDataProvider fdp(data, size);
42 const int len = fdp.ConsumeIntegralInRange<int>(0, 1024);
43 JSHandle<JSTaggedValue> jsArrayTag = JSArray::ArrayCreate(vm->GetJSThread(), JSTaggedNumber(len));
44 Local<JSValueRef> jsArray = JSNApiHelper::ToLocal<JSTypedArray>(jsArrayTag);
45 jsArray->IsJSArray(vm);
46 }
47 JSNApi::DestroyJSVM(vm);
48 return;
49 }
50
JSValueRefIsJSPrimitiveNumberFuzzTest(const uint8_t * data,size_t size)51 void JSValueRefIsJSPrimitiveNumberFuzzTest(const uint8_t *data, size_t size)
52 {
53 RuntimeOption option;
54 option.SetLogLevel(common::LOG_LEVEL::ERROR);
55 EcmaVM *vm = JSNApi::CreateJSVM(option);
56 {
57 JsiFastNativeScope scope(vm);
58 if (size <= 0) {
59 return;
60 }
61 ObjectFactory *factory = vm->GetFactory();
62 JSHandle<JSTaggedValue> jstagvalue;
63 FuzzedDataProvider fdp(data, size);
64 auto type = fdp.PickValueInArray({
65 PrimitiveType::PRIMITIVE_BOOLEAN,
66 PrimitiveType::PRIMITIVE_NUMBER,
67 PrimitiveType::PRIMITIVE_SYMBOL,
68 PrimitiveType::PRIMITIVE_BIGINT,
69 });
70 JSHandle<JSPrimitiveRef> jsprimitive = factory->NewJSPrimitiveRef(type, jstagvalue);
71 JSHandle<JSTaggedValue> jspri = JSHandle<JSTaggedValue>::Cast(jsprimitive);
72 Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(jspri);
73 object->IsJSPrimitiveNumber(vm);
74 }
75 JSNApi::DestroyJSVM(vm);
76 return;
77 }
78 }
79
80 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82 {
83 // Run your code on data.
84 OHOS::JSValueRefIsJSArrayFuzzTest(data, size);
85 OHOS::JSValueRefIsJSPrimitiveNumberFuzzTest(data, size);
86 return 0;
87 }