1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <fuzzer/FuzzedDataProvider.h>
18 
19 #include "include/stats_event.h"
20 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)21 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
22     FuzzedDataProvider fdp(data, size);
23 
24     // This only tests the libstatssocket APIs.
25     // Since fuzzing is not supported across processes, it does not fuzz statsd.
26     // See statsd_fuzzer.
27 
28     AStatsEvent* event = AStatsEvent_obtain();
29 
30     AStatsEvent_setAtomId(event, fdp.ConsumeIntegral<int32_t>());
31     // atom-level annotation
32     AStatsEvent_addBoolAnnotation(event, fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeBool());
33 
34     while (fdp.remaining_bytes() > 0) {
35         AStatsEvent_writeInt32(event, fdp.ConsumeIntegral<int32_t>());
36         AStatsEvent_addBoolAnnotation(event, fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeBool());
37         AStatsEvent_addInt32Annotation(event, fdp.ConsumeIntegral<int32_t>(),
38                                        fdp.ConsumeIntegral<int32_t>());
39         AStatsEvent_writeBool(event, fdp.ConsumeBool());
40         AStatsEvent_addBoolAnnotation(event, fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeBool());
41         AStatsEvent_addInt32Annotation(event, fdp.ConsumeIntegral<int32_t>(),
42                                        fdp.ConsumeIntegral<int32_t>());
43         AStatsEvent_writeFloat(event, fdp.ConsumeFloatingPoint<float>());
44         AStatsEvent_addBoolAnnotation(event, fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeBool());
45         AStatsEvent_addInt32Annotation(event, fdp.ConsumeIntegral<int32_t>(),
46                                        fdp.ConsumeIntegral<int32_t>());
47         AStatsEvent_writeInt64(event, fdp.ConsumeIntegral<int64_t>());
48         AStatsEvent_addBoolAnnotation(event, fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeBool());
49         AStatsEvent_addInt32Annotation(event, fdp.ConsumeIntegral<int32_t>(),
50                                        fdp.ConsumeIntegral<int32_t>());
51         AStatsEvent_writeString(event, fdp.ConsumeRandomLengthString().c_str());
52         AStatsEvent_addBoolAnnotation(event, fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeBool());
53         AStatsEvent_addInt32Annotation(event, fdp.ConsumeIntegral<int32_t>(),
54                                        fdp.ConsumeIntegral<int32_t>());
55     }
56 
57     AStatsEvent_write(event);
58     AStatsEvent_release(event);
59     return 0;
60 }
61