blob: 3ea5b553bc4b3ef3786838ec48a9f0da97315cb4 [file] [log] [blame]
Steven Morelandf23dae22020-10-27 19:34:55 +00001/*
2 * Copyright (C) 2020 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
Steven Moreland62129012021-07-29 12:14:44 -070017#include <android-base/hex.h>
Steven Morelandf23dae22020-10-27 19:34:55 +000018#include <android-base/logging.h>
19#include <binder/Binder.h>
20#include <binder/IBinder.h>
21#include <binder/IPCThreadState.h>
22#include <binder/IServiceManager.h>
23#include <binder/Parcel.h>
24#include <binder/Stability.h>
25#include <gtest/gtest.h>
26
27#include <sys/prctl.h>
28#include <thread>
29
30using namespace android;
31
32const String16 kServerName = String16("binderClearBuf");
33
Steven Morelandf23dae22020-10-27 19:34:55 +000034class FooBar : public BBinder {
35 public:
36 enum {
37 TRANSACTION_REPEAT_STRING = IBinder::FIRST_CALL_TRANSACTION,
38 };
39
40 std::mutex foo;
41 std::string last;
42
43 status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
44 // not checking data, since there is no hook at the time this test is
45 // written to check values there are set to zero. Instead, we only check
46 // the reply parcel.
47
48 switch (code) {
49 case TRANSACTION_REPEAT_STRING: {
50 const char* str = data.readCString();
51 return reply->writeCString(str == nullptr ? "<null>" : str);
52 }
53 }
54 return BBinder::onTransact(code, data, reply, flags);
55 }
56 static std::string RepeatString(const sp<IBinder> binder,
57 const std::string& repeat,
58 std::string* outBuffer) {
59 Parcel data;
60 data.writeCString(repeat.c_str());
61 std::string result;
62 const uint8_t* lastReply;
63 size_t lastReplySize;
64 {
65 Parcel reply;
66 binder->transact(TRANSACTION_REPEAT_STRING, data, &reply, FLAG_CLEAR_BUF);
67 result = reply.readCString();
68 lastReply = reply.data();
69 lastReplySize = reply.dataSize();
70 }
Steven Moreland62129012021-07-29 12:14:44 -070071 *outBuffer = android::base::HexString(lastReply, lastReplySize);
Steven Morelandf23dae22020-10-27 19:34:55 +000072 return result;
73 }
74};
75
76TEST(BinderClearBuf, ClearKernelBuffer) {
Tomasz Wasilczykbb07b982023-10-11 21:25:36 +000077#pragma clang diagnostic push
78#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Steven Morelandf23dae22020-10-27 19:34:55 +000079 sp<IBinder> binder = defaultServiceManager()->getService(kServerName);
Tomasz Wasilczykbb07b982023-10-11 21:25:36 +000080#pragma clang diagnostic pop
Steven Morelandf23dae22020-10-27 19:34:55 +000081 ASSERT_NE(nullptr, binder);
82
83 std::string replyBuffer;
84 std::string result = FooBar::RepeatString(binder, "foo", &replyBuffer);
85 EXPECT_EQ("foo", result);
86
87 // the buffer must have at least some length for the string, but we will
88 // just check it has some length, to avoid assuming anything about the
89 // format
90 EXPECT_GT(replyBuffer.size(), 0);
91
92 for (size_t i = 0; i < replyBuffer.size(); i++) {
93 EXPECT_EQ(replyBuffer[i], '0') << "reply buffer at " << i;
94 }
95}
96
97int main(int argc, char** argv) {
98 ::testing::InitGoogleTest(&argc, argv);
99
100 if (fork() == 0) {
101 prctl(PR_SET_PDEATHSIG, SIGHUP);
102
103 sp<IBinder> server = new FooBar;
104 android::defaultServiceManager()->addService(kServerName, server);
105
106 IPCThreadState::self()->joinThreadPool(true);
107 exit(1); // should not reach
108 }
109
110 // This is not racey. Just giving these services some time to register before we call
111 // getService which sleeps for much longer. One alternative would be to
112 // start a threadpool + use waitForService, but we want to have as few
113 // binder things going on in this test as possible, since we are checking
114 // memory is zero'd which the kernel has a right to change.
115 usleep(100000);
116
117 return RUN_ALL_TESTS();
118}