1 #undef LOG_TAG 2 #define LOG_TAG "gpuservice_unittest" 3 4 #include "gpuservice/GpuService.h" 5 6 #include <gtest/gtest.h> 7 #include <log/log_main.h> 8 9 #include <chrono> 10 #include <thread> 11 12 namespace android { 13 namespace { 14 15 class GpuServiceTest : public testing::Test { 16 public: GpuServiceTest()17 GpuServiceTest() { 18 const ::testing::TestInfo* const test_info = 19 ::testing::UnitTest::GetInstance()->current_test_info(); 20 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); 21 } 22 ~GpuServiceTest()23 ~GpuServiceTest() { 24 const ::testing::TestInfo* const test_info = 25 ::testing::UnitTest::GetInstance()->current_test_info(); 26 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); 27 } 28 29 }; 30 31 32 /* 33 * The behaviour before this test + fixes was UB caused by threads accessing deallocated memory. 34 * 35 * This test creates the service (which initializes the culprit threads), 36 * deallocates it immediately and sleeps. 37 * 38 * GpuService's destructor gets called and joins the threads. 39 * If we haven't crashed by the time the sleep time has elapsed, we're good 40 * Let the test pass. 41 */ TEST_F(GpuServiceTest,onInitializeShouldNotCauseUseAfterFree)42TEST_F(GpuServiceTest, onInitializeShouldNotCauseUseAfterFree) { 43 sp<GpuService> service = new GpuService(); 44 service.clear(); 45 std::this_thread::sleep_for(std::chrono::seconds(3)); 46 47 // If we haven't crashed yet due to threads accessing freed up memory, let the test pass 48 EXPECT_TRUE(true); 49 } 50 51 } // namespace 52 } // namespace android 53