1 /*
2  * Copyright (C) 2019 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 #define FUZZ_LOG_TAG "binder_ndk"
17 
18 #include "binder_ndk.h"
19 
20 #include <android/binder_parcel_utils.h>
21 
22 #include "util.h"
23 
24 // TODO(b/142061461): parent class
25 class SomeParcelable {
26 public:
readFromParcel(const AParcel * parcel)27     binder_status_t readFromParcel(const AParcel* parcel) {
28         return AParcel_readInt32(parcel, &mValue);
29     }
30 
31 private:
32     int32_t mValue = 0;
33 };
34 
35 #define PARCEL_READ(T, FUN)                                              \
36     [](const NdkParcelAdapter& p, uint8_t /*data*/) {                    \
37         FUZZ_LOG() << "about to read " #T " using " #FUN " with status"; \
38         T t{};                                                           \
39         binder_status_t status = FUN(p.aParcel(), &t);                   \
40         FUZZ_LOG() << #T " status: " << status /* << " value: " << t*/;  \
41     }
42 
43 // clang-format off
44 std::vector<ParcelRead<NdkParcelAdapter>> BINDER_NDK_PARCEL_READ_FUNCTIONS{
45         // methods from binder_parcel.h
__anon1d87ec770102() 46         [](const NdkParcelAdapter& p, uint8_t pos) {
47             FUZZ_LOG() << "about to set data position to " << pos;
48             binder_status_t status = AParcel_setDataPosition(p.aParcel(), pos);
49             FUZZ_LOG() << "set data position: " << status;
50         },
__anon1d87ec770202() 51         [](const NdkParcelAdapter& p, uint8_t /*data*/) {
52             FUZZ_LOG() << "about to read status header";
53             ndk::ScopedAStatus t;
54             binder_status_t status = AParcel_readStatusHeader(p.aParcel(), t.getR());
55             FUZZ_LOG() << "read status header: " << status;
56         },
57         PARCEL_READ(int32_t, AParcel_readInt32),
58         PARCEL_READ(uint32_t, AParcel_readUint32),
59         PARCEL_READ(int64_t, AParcel_readInt64),
60         PARCEL_READ(uint64_t, AParcel_readUint64),
61         PARCEL_READ(float, AParcel_readFloat),
62         PARCEL_READ(double, AParcel_readDouble),
63         PARCEL_READ(bool, AParcel_readBool),
64         PARCEL_READ(char16_t, AParcel_readChar),
65         PARCEL_READ(int8_t, AParcel_readByte),
66 
67         // methods from binder_parcel_utils.h
68         PARCEL_READ(ndk::SpAIBinder, ndk::AParcel_readNullableStrongBinder),
69         PARCEL_READ(ndk::SpAIBinder, ndk::AParcel_readRequiredStrongBinder),
70         PARCEL_READ(ndk::ScopedFileDescriptor, ndk::AParcel_readNullableParcelFileDescriptor),
71         PARCEL_READ(ndk::ScopedFileDescriptor, ndk::AParcel_readRequiredParcelFileDescriptor),
72         PARCEL_READ(std::string, ndk::AParcel_readString),
73         PARCEL_READ(std::optional<std::string>, ndk::AParcel_readString),
74         // TODO(b/131868573): can force process to allocate arbitrary amount of
75         // memory
76         // PARCEL_READ(std::vector<std::string>, ndk::AParcel_readVector),
77         // PARCEL_READ(std::optional<std::vector<std::optional<std::string>>>,
78         // ndk::AParcel_readVector), PARCEL_READ(std::vector<SomeParcelable>,
79         // ndk::AParcel_readVector), PARCEL_READ(std::vector<int32_t>, ndk::AParcel_readVector),
80         // PARCEL_READ(std::optional<std::vector<int32_t>>, ndk::AParcel_readVector),
81         // PARCEL_READ(std::vector<uint32_t>, ndk::AParcel_readVector),
82         // PARCEL_READ(std::optional<std::vector<uint32_t>>, ndk::AParcel_readVector),
83         // PARCEL_READ(std::vector<int64_t>, ndk::AParcel_readVector),
84         // PARCEL_READ(std::optional<std::vector<int64_t>>, ndk::AParcel_readVector),
85         // PARCEL_READ(std::vector<uint64_t>, ndk::AParcel_readVector),
86         // PARCEL_READ(std::optional<std::vector<uint64_t>>, ndk::AParcel_readVector),
87         // PARCEL_READ(std::vector<float>, ndk::AParcel_readVector),
88         // PARCEL_READ(std::optional<std::vector<float>>, ndk::AParcel_readVector),
89         // PARCEL_READ(std::vector<double>, ndk::AParcel_readVector),
90         // PARCEL_READ(std::optional<std::vector<double>>, ndk::AParcel_readVector),
91         // PARCEL_READ(std::vector<bool>, ndk::AParcel_readVector),
92         // PARCEL_READ(std::optional<std::vector<bool>>, ndk::AParcel_readVector),
93         // PARCEL_READ(std::vector<char16_t>, ndk::AParcel_readVector),
94         // PARCEL_READ(std::optional<std::vector<char16_t>>, ndk::AParcel_readVector),
95         // PARCEL_READ(std::vector<int32_t>, ndk::AParcel_resizeVector),
96         // PARCEL_READ(std::optional<std::vector<int32_t>>, ndk::AParcel_resizeVector),
97 };
98 // clang-format on
99