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 "bigintrefgetwordsarraysize_fuzzer.h"
17 #include "ecmascript/ecma_string-inl.h"
18 #include "ecmascript/napi/include/jsnapi.h"
19 #include "ecmascript/log_wrapper.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23 #define MAXBYTELEN sizeof(uint64_t)
24 namespace OHOS {
BigIntRefGetWordsArraySize(const uint8_t * data,size_t size)25 void BigIntRefGetWordsArraySize(const uint8_t *data, size_t size)
26 {
27 RuntimeOption option;
28 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
29 EcmaVM *vm = JSNApi::CreateJSVM(option);
30 if (data == nullptr || size <= 0) {
31 LOG_ECMA(ERROR) << "illegal input!";
32 return;
33 }
34 if (size > MAXBYTELEN) {
35 size = MAXBYTELEN;
36 }
37 bool sign = false;
38 const size_t uint64BytesNum = 8;
39 size_t wordsNum = size / uint64BytesNum;
40 size_t hasRemain = size % uint64BytesNum;
41 if (hasRemain) {
42 wordsNum++;
43 }
44 uint64_t *words = new uint64_t[wordsNum]();
45 if (memcpy_s(words, MAXBYTELEN, data, size) != EOK) {
46 LOG_ECMA(ERROR) << "memcpy_s failed!";
47 }
48 Local<JSValueRef> bigWords = BigIntRef::CreateBigWords(vm, sign, wordsNum, words);
49 Local<BigIntRef> bigWordsRef(bigWords);
50 bigWordsRef->GetWordsArraySize(vm);
51 delete[] words;
52 words = nullptr;
53 JSNApi::DestroyJSVM(vm);
54 }
55 }
56
57 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
59 {
60 // Run your code on data.
61 OHOS::BigIntRefGetWordsArraySize(data, size);
62 return 0;
63 }