• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "playprimitiveeffectenhanced_fuzzer.h"
17 
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "securec.h"
21 #include "token_setproc.h"
22 
23 #include "vibrator_agent.h"
24 
25 namespace OHOS {
26 namespace Sensors {
27 using namespace Security::AccessToken;
28 using Security::AccessToken::AccessTokenID;
29 
30 namespace {
31 constexpr size_t DATA_MIN_SIZE = 28;
32 constexpr char END_CHAR = '\0';
33 constexpr size_t LEN = 20;
34 } // namespace
35 
36 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)37 size_t GetObject(const uint8_t *data, size_t size, T &object)
38 {
39     size_t objectSize = sizeof(object);
40     if (objectSize > size) {
41         return 0;
42     }
43     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
44     if (ret != EOK) {
45         return 0;
46     }
47     return objectSize;
48 }
49 
SetUpTestCase()50 void SetUpTestCase()
51 {
52     const char **perms = new (std::nothrow) const char *[1];
53     if (perms == nullptr) {
54         return;
55     }
56     perms[0] = "ohos.permission.VIBRATE";
57     TokenInfoParams infoInstance = {
58         .dcapsNum = 0,
59         .permsNum = 1,
60         .aclsNum = 0,
61         .dcaps = nullptr,
62         .perms = perms,
63         .acls = nullptr,
64         .processName = "PlayPrimitiveEffectEnhancedFuzzTest",
65         .aplStr = "system_core",
66     };
67     uint64_t tokenId = GetAccessTokenId(&infoInstance);
68     SetSelfTokenID(tokenId);
69     AccessTokenKit::ReloadNativeTokenInfo();
70     delete[] perms;
71 }
72 
PlayPrimitiveEffectEnhancedFuzzTest(const uint8_t * data,size_t size)73 void PlayPrimitiveEffectEnhancedFuzzTest(const uint8_t *data, size_t size)
74 {
75     SetUpTestCase();
76     if (data == nullptr || size < DATA_MIN_SIZE) {
77         return;
78     }
79     VibratorIdentifier identifier;
80     size_t startPos = 0;
81     char effectId[LEN + 1];
82     effectId[LEN] = END_CHAR;
83     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.deviceId);
84     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.vibratorId);
85     for (size_t i = 0; i < LEN; ++i) {
86         startPos += GetObject<char>(data + startPos, size - startPos, effectId[i]);
87     }
88     int32_t intensity { 0 };
89     OHOS::Sensors::PlayPrimitiveEffectEnhanced(identifier, effectId, intensity);
90 }
91 } // namespace Sensors
92 } // namespace OHOS
93 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
95 {
96     OHOS::Sensors::PlayPrimitiveEffectEnhancedFuzzTest(data, size);
97     return 0;
98 }