| Steven Moreland | f23dae2 | 2020-10-27 19:34:55 +0000 | [diff] [blame] | 1 | /* | 
|  | 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 Moreland | f39d2ae | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 17 | #include <android-base/hex.h> | 
| Steven Moreland | f23dae2 | 2020-10-27 19:34:55 +0000 | [diff] [blame] | 18 | #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 |  | 
|  | 30 | using namespace android; | 
|  | 31 |  | 
|  | 32 | const String16 kServerName = String16("binderClearBuf"); | 
|  | 33 |  | 
| Steven Moreland | f23dae2 | 2020-10-27 19:34:55 +0000 | [diff] [blame] | 34 | class 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 Moreland | f39d2ae | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 71 | *outBuffer = android::base::HexString(lastReply, lastReplySize); | 
| Steven Moreland | f23dae2 | 2020-10-27 19:34:55 +0000 | [diff] [blame] | 72 | return result; | 
|  | 73 | } | 
|  | 74 | }; | 
|  | 75 |  | 
|  | 76 | TEST(BinderClearBuf, ClearKernelBuffer) { | 
|  | 77 | sp<IBinder> binder = defaultServiceManager()->getService(kServerName); | 
|  | 78 | ASSERT_NE(nullptr, binder); | 
|  | 79 |  | 
|  | 80 | std::string replyBuffer; | 
|  | 81 | std::string result = FooBar::RepeatString(binder, "foo", &replyBuffer); | 
|  | 82 | EXPECT_EQ("foo", result); | 
|  | 83 |  | 
|  | 84 | // the buffer must have at least some length for the string, but we will | 
|  | 85 | // just check it has some length, to avoid assuming anything about the | 
|  | 86 | // format | 
|  | 87 | EXPECT_GT(replyBuffer.size(), 0); | 
|  | 88 |  | 
|  | 89 | for (size_t i = 0; i < replyBuffer.size(); i++) { | 
|  | 90 | EXPECT_EQ(replyBuffer[i], '0') << "reply buffer at " << i; | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | int main(int argc, char** argv) { | 
|  | 95 | ::testing::InitGoogleTest(&argc, argv); | 
|  | 96 |  | 
|  | 97 | if (fork() == 0) { | 
|  | 98 | prctl(PR_SET_PDEATHSIG, SIGHUP); | 
|  | 99 |  | 
|  | 100 | sp<IBinder> server = new FooBar; | 
|  | 101 | android::defaultServiceManager()->addService(kServerName, server); | 
|  | 102 |  | 
|  | 103 | IPCThreadState::self()->joinThreadPool(true); | 
|  | 104 | exit(1);  // should not reach | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | // This is not racey. Just giving these services some time to register before we call | 
|  | 108 | // getService which sleeps for much longer. One alternative would be to | 
|  | 109 | // start a threadpool + use waitForService, but we want to have as few | 
|  | 110 | // binder things going on in this test as possible, since we are checking | 
|  | 111 | // memory is zero'd which the kernel has a right to change. | 
|  | 112 | usleep(100000); | 
|  | 113 |  | 
|  | 114 | return RUN_ALL_TESTS(); | 
|  | 115 | } |