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 "jsvaluerefiscorrect_fuzzer.h"
17 #include "ecmascript/base/utf_helper.h"
18 #include "ecmascript/ecma_string-inl.h"
19 #include "ecmascript/napi/include/jsnapi.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::base::utf_helper;
24
25 namespace OHOS {
JSValueRefIsFalseFuzzTest(const uint8_t * data,size_t size)26 void JSValueRefIsFalseFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
27 {
28 RuntimeOption option;
29 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
30 EcmaVM *vm = JSNApi::CreateJSVM(option);
31 if (size <= 0) {
32 return;
33 }
34 Local<JSValueRef> object = JSValueRef::False(vm);
35 object->IsFalse();
36 JSNApi::DestroyJSVM(vm);
37 return;
38 }
39
JSValueRefIsTrueFuzzTest(const uint8_t * data,size_t size)40 void JSValueRefIsTrueFuzzTest(const uint8_t *data, size_t size)
41 {
42 RuntimeOption option;
43 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
44 EcmaVM *vm = JSNApi::CreateJSVM(option);
45 if (data == nullptr || size <= 0) {
46 LOG_ECMA(ERROR) << "illegal input!";
47 return;
48 }
49 int value;
50 size = size > sizeof(int) ? sizeof(int) : size;
51 if (memcpy_s(&value, sizeof(int), data, size) != EOK) {
52 LOG_ECMA(ERROR) << "memcpy_s failed !";
53 UNREACHABLE();
54 }
55 Local<JSValueRef> object = IntegerRef::New(vm, value);
56 object->IsTrue();
57 JSNApi::DestroyJSVM(vm);
58 return;
59 }
60
JSValueRefIsHoleFuzzTest(const uint8_t * data,size_t size)61 void JSValueRefIsHoleFuzzTest(const uint8_t *data, size_t size)
62 {
63 RuntimeOption option;
64 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
65 EcmaVM *vm = JSNApi::CreateJSVM(option);
66 if (data == nullptr || size <= 0) {
67 LOG_ECMA(ERROR) << "illegal input!";
68 return;
69 }
70 int value;
71 size = size > sizeof(int) ? sizeof(int) : size;
72 if (memcpy_s(&value, sizeof(int), data, size) != EOK) {
73 LOG_ECMA(ERROR) << "memcpy_s failed!";
74 UNREACHABLE();
75 }
76 Local<JSValueRef> object = IntegerRef::New(vm, value);
77 object->IsHole();
78 JSNApi::DestroyJSVM(vm);
79 return;
80 }
81
JSValueRefIsUndefinedFuzzTest(const uint8_t * data,size_t size)82 void JSValueRefIsUndefinedFuzzTest(const uint8_t *data, size_t size)
83 {
84 RuntimeOption option;
85 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
86 EcmaVM *vm = JSNApi::CreateJSVM(option);
87 if (data == nullptr || size <= 0) {
88 LOG_ECMA(ERROR) << "illegal input!";
89 return;
90 }
91 Local<JSValueRef> tag = StringRef::NewFromUtf8(vm, (char *)data, (int)size);
92 tag->IsUndefined();
93 JSNApi::DestroyJSVM(vm);
94 return;
95 }
96 }
97
98 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
100 {
101 // Run your code on data.
102 OHOS::JSValueRefIsFalseFuzzTest(data, size);
103 OHOS::JSValueRefIsTrueFuzzTest(data, size);
104 OHOS::JSValueRefIsHoleFuzzTest(data, size);
105 OHOS::JSValueRefIsUndefinedFuzzTest(data, size);
106 return 0;
107 }