1 /*
2 * Copyright (c) 2025 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 "stringtableconcatstringintern_fuzzer.h"
17 #include "ecmascript/base/string_helper.h"
18 #include "ecmascript/ecma_string-inl.h"
19 #include "ecmascript/ecma_string_table_optimization-inl.h"
20 #include "common_components/base/utf_helper.h"
21 #include "ecmascript/napi/include/jsnapi.h"
22 #include "ecmascript/napi/jsnapi_helper.h"
23 #include <thread>
24 #include <vector>
25
26 using namespace panda;
27 using namespace panda::ecmascript;
28 using namespace common::utf_helper;
29
30 namespace OHOS {
31 constexpr size_t MIN_BYTE_SIZE = 4;
32 constexpr size_t MAX_RANDOM = 50;
33
StringTableConcatStringInternFuzzTest(const uint8_t * data,size_t size)34 void StringTableConcatStringInternFuzzTest(const uint8_t* data, size_t size)
35 {
36 if (data == nullptr || size < MIN_BYTE_SIZE) {
37 LOG_ECMA(ERROR) << "illegal input!";
38 return;
39 }
40 RuntimeOption option;
41 option.SetLogLevel(common::LOG_LEVEL::ERROR);
42 EcmaVM *vm = JSNApi::CreateJSVM(option);
43 ObjectFactory* factory = vm->GetFactory();
44 EcmaStringTable* table = vm->GetEcmaStringTable();
45
46 size_t str1Size = std::min<size_t>(MAX_RANDOM, size / MIN_BYTE_SIZE);
47 std::string prefix(reinterpret_cast<const char*>(data), str1Size);
48 JSHandle<EcmaString> str1 = factory->NewFromStdString(prefix);
49
50 size_t str2Size = std::min<size_t>(MAX_RANDOM, size / MIN_BYTE_SIZE);
51 std::string suffix(reinterpret_cast<const char*>(data + str1Size), str2Size);
52 JSHandle<EcmaString> str2 = factory->NewFromStdString(suffix);
53 table->GetOrInternString(vm, str1, str2);
54
55 JSNApi::DestroyJSVM(vm);
56 }
57 }
58
59 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
61 {
62 // Run your code on data.
63 OHOS::StringTableConcatStringInternFuzzTest(data, size);
64 return 0;
65 }