1 /*
2 * Copyright (c) 2024-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 "abilityframeworksnativejsworker_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "extractor.h"
22 #include "file_mapper.h"
23 #define private public
24 #include "js_worker.h"
25 #undef private
26
27 #include "ability_record.h"
28 #include "securec.h"
29 #include "worker_info.h"
30 #include "js_runtime_common.h"
31
32 using namespace OHOS::AAFwk;
33 using namespace OHOS::AppExecFwk;
34
35 namespace OHOS {
36 namespace {
37 constexpr int INPUT_ZERO = 0;
38 constexpr int INPUT_ONE = 1;
39 constexpr int INPUT_TWO = 2;
40 constexpr int INPUT_THREE = 3;
41 constexpr size_t U32_AT_SIZE = 4;
42 constexpr size_t OFFSET_ZERO = 24;
43 constexpr size_t OFFSET_ONE = 16;
44 constexpr size_t OFFSET_TWO = 8;
45 constexpr uint8_t ENABLE = 2;
46 }
47
GetU32Data(const char * ptr)48 uint32_t GetU32Data(const char* ptr)
49 {
50 // convert fuzz input data to an integer
51 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) |
52 ptr[INPUT_THREE];
53 }
54
GetFuzzAbilityToken()55 sptr<Token> GetFuzzAbilityToken()
56 {
57 sptr<Token> token = nullptr;
58 AbilityRequest abilityRequest;
59 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
60 abilityRequest.abilityInfo.name = "MainAbility";
61 abilityRequest.abilityInfo.type = AbilityType::DATA;
62 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
63 if (abilityRecord) {
64 token = abilityRecord->GetToken();
65 }
66 return token;
67 }
68
DoSomethingInterestingWithMyAPI(const char * data,size_t size)69 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
70 {
71 std::shared_ptr<JsEnv::WorkerInfo> workerInfo = std::make_shared<JsEnv::WorkerInfo>();
72 workerInfo->codePath = panda::panda_file::StringPacProtect("/data/test/codePath");
73 workerInfo->packagePathStr = "/data/test/packagePath";
74 workerInfo->hapPath = panda::panda_file::StringPacProtect("/data/test/hapPath");
75 workerInfo->moduleName = "moduleName";
76 AbilityRuntime::AssetHelper helper = AbilityRuntime::AssetHelper(workerInfo);
77 std::string jsonStr(data, size);
78 uint8_t *buff = nullptr;
79 size_t buffSize;
80 std::unique_ptr<AbilityBase::FileMapper> fileMapper = std::make_unique<AbilityBase::FileMapper>();
81 void* mapper = static_cast<void*>(fileMapper.get());
82 helper.GetSafeData(jsonStr, &buff, &buffSize, &mapper);
83 helper.NormalizedFileName(jsonStr);
84 bool useSecureMem = *data % ENABLE;
85 bool isRestricted = *data % ENABLE;
86 std::vector<uint8_t> content;
87 helper.ReadAmiData(jsonStr, &buff, &buffSize, content, useSecureMem, isRestricted, &mapper);
88 helper.ReadFilePathData(jsonStr, &buff, &buffSize, content, useSecureMem, isRestricted, &mapper);
89 helper.GetAmi(jsonStr, jsonStr);
90 AbilityRuntime::GetContainerId();
91 bool isDebugApp = *data % ENABLE;
92 bool isNativeStart = *data % ENABLE;
93 AbilityRuntime::JsRuntimeCommon::GetInstance().StartDebuggerModule(isDebugApp, isNativeStart);
94 NativeEngine *nativeEngine = nullptr;
95 AbilityRuntime::InitWorkerFunc(nativeEngine);
96 AbilityRuntime::OffWorkerFunc(nativeEngine);
97 int32_t id = static_cast<int32_t>(GetU32Data(data));
98 AbilityRuntime::UpdateContainerScope(id);
99 AbilityRuntime::RestoreContainerScope(id);
100 AbilityRuntime::SetJsFramework();
101 return true;
102 }
103 }
104
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
107 {
108 /* Run your code on data */
109 if (data == nullptr) {
110 return 0;
111 }
112
113 /* Validate the length of size */
114 if (size < OHOS::U32_AT_SIZE) {
115 return 0;
116 }
117
118 char* ch = static_cast<char*>(malloc(size + 1));
119 if (ch == nullptr) {
120 std::cout << "malloc failed." << std::endl;
121 return 0;
122 }
123
124 (void)memset_s(ch, size + 1, 0x00, size + 1);
125 if (memcpy_s(ch, size + 1, data, size) != EOK) {
126 std::cout << "copy failed." << std::endl;
127 free(ch);
128 ch = nullptr;
129 return 0;
130 }
131
132 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
133 free(ch);
134 ch = nullptr;
135 return 0;
136 }
137
138