Files
resourceschedule_soc_perf/test/fuzztest/socperf_fuzzer/socperf_fuzzer.cpp
T
wangxiaoding 2ff2fc7a2d fix: govern soc_perf inner_api.
Signed-off-by: wangxiaoding <wangxiaoding2@huawei.com>
2023-01-04 20:22:05 +08:00

89 lines
2.5 KiB
C++

/*
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "socperf_fuzzer.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
#include "i_socperf_service.h"
#include "socperf_log.h"
namespace OHOS {
namespace SOCPERF {
constexpr int32_t MIN_LEN = 4;
std::mutex mutexLock;
sptr<IRemoteObject> remoteObj = nullptr;
bool DoInit()
{
std::lock_guard<std::mutex> lock(mutexLock);
if (remoteObj) {
return true;
}
auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (!samgr) {
return false;
}
remoteObj = samgr->GetSystemAbility(SOC_PERF_SERVICE_SA_ID);
if (!remoteObj) {
return false;
}
return true;
}
int32_t onRemoteRequest(uint32_t code, MessageParcel& data)
{
if (!DoInit()) {
return -1;
}
MessageParcel reply;
MessageOption option;
return remoteObj->SendRequest(code, data, reply, option);
}
bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
{
if (size <= MIN_LEN) {
return false;
}
MessageParcel dataMessageParcel;
if (!dataMessageParcel.WriteInterfaceToken(IRemoteStub<ISocPerfService>::GetDescriptor())) {
return false;
}
uint32_t code = *(reinterpret_cast<const uint32_t*>(data));
size -= sizeof(uint32_t);
dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size);
dataMessageParcel.RewindRead(0);
SOC_PERF_LOGD("_______________");
onRemoteRequest(code, dataMessageParcel);
return true;
}
} // namespace SOCPERF
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
/* Run your code on data */
OHOS::SOCPERF::DoSomethingInterestingWithMyAPI(data, size);
return 0;
}