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 
17 #pragma once
18 
19 #include <binder/IBinder.h>
20 #include <string>
21 
22 namespace android {
23 
24 class BpBinder;
25 class ProcessState;
26 
27 namespace internal {
28 
29 // WARNING: These APIs are only ever expected to be called by auto-generated code.
30 //     Instead of calling them, you should set the stability of a .aidl interface
31 class Stability final {
32 public:
33     // WARNING: This is only ever expected to be called by auto-generated code. You likely want to
34     // change or modify the stability class of the interface you are using.
35     // This must be called as soon as the binder in question is constructed. No thread safety
36     // is provided.
37     // E.g. stability is according to libbinder compilation unit
38     static void markCompilationUnit(IBinder* binder);
39     // WARNING: This is only ever expected to be called by auto-generated code. You likely want to
40     // change or modify the stability class of the interface you are using.
41     // This must be called as soon as the binder in question is constructed. No thread safety
42     // is provided.
43     // E.g. stability is according to libbinder_ndk or Java SDK AND the interface
44     //     expressed here is guaranteed to be stable for multiple years (Stable AIDL)
45     static void markVintf(IBinder* binder);
46 
47     // WARNING: for debugging only
48     static void debugLogStability(const std::string& tag, const sp<IBinder>& binder);
49 
50     // WARNING: This is only ever expected to be called by auto-generated code or tests.
51     // You likely want to change or modify the stability of the interface you are using.
52     // This must be called as soon as the binder in question is constructed. No thread safety
53     // is provided.
54     // E.g. stability is according to libbinder_ndk or Java SDK AND the interface
55     //     expressed here is guaranteed to be stable for multiple years (Stable AIDL)
56     // If this is called when __ANDROID_VNDK__ is not defined, then it is UB and will likely
57     // break the device during GSI or other tests.
58     static void markVndk(IBinder* binder);
59 
60     // Returns true if the binder needs to be declared in the VINTF manifest or
61     // else false if the binder is local to the current partition.
62     static bool requiresVintfDeclaration(const sp<IBinder>& binder);
63 private:
64     // Parcel needs to read/write stability level in an unstable format.
65     friend ::android::Parcel;
66 
67     // only expose internal APIs inside of libbinder, for checking stability
68     friend ::android::BpBinder;
69 
70     // so that it can mark the context object (only the root object doesn't go
71     // through Parcel)
72     friend ::android::ProcessState;
73 
74     static void tryMarkCompilationUnit(IBinder* binder);
75 
76     enum Level : int32_t {
77         UNDECLARED = 0,
78 
79         VENDOR = 0b000011,
80         SYSTEM = 0b001100,
81         VINTF = 0b111111,
82     };
83 
84 #if defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
85     static constexpr Level kLocalStability = Level::VENDOR;
86 #else
87     static constexpr Level kLocalStability = Level::SYSTEM;
88 #endif
89 
90     // applies stability to binder if stability level is known
91     __attribute__((warn_unused_result))
92     static status_t set(IBinder* binder, int32_t stability, bool log);
93 
94     static Level get(IBinder* binder);
95 
96     static bool check(int32_t provided, Level required);
97 
98     static bool isDeclaredStability(int32_t stability);
99     static std::string stabilityString(int32_t stability);
100 
101     Stability();
102 };
103 
104 }  // namespace internal
105 }  // namespace android
106