Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include <sys/time.h> |
| 18 | |
| 19 | #include <chrono> |
| 20 | #include <functional> |
| 21 | |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 22 | #include <ScopedDisableMalloc.h> |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 24 | |
| 25 | using namespace std::chrono_literals; |
| 26 | |
Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 29 | class DisableMallocTest : public ::testing::Test { |
| 30 | protected: |
| 31 | void alarm(std::chrono::microseconds us) { |
| 32 | std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(us); |
| 33 | itimerval t = itimerval(); |
| 34 | t.it_value.tv_sec = s.count(); |
| 35 | t.it_value.tv_usec = (us - s).count(); |
| 36 | setitimer(ITIMER_REAL, &t, NULL); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | TEST_F(DisableMallocTest, reenable) { |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 41 | ASSERT_EXIT( |
| 42 | { |
| 43 | alarm(100ms); |
| 44 | void* ptr1 = malloc(128); |
| 45 | ASSERT_NE(ptr1, nullptr); |
| 46 | free(ptr1); |
| 47 | { ScopedDisableMalloc disable_malloc; } |
| 48 | void* ptr2 = malloc(128); |
| 49 | ASSERT_NE(ptr2, nullptr); |
| 50 | free(ptr2); |
| 51 | _exit(1); |
| 52 | }, |
| 53 | ::testing::ExitedWithCode(1), ""); |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TEST_F(DisableMallocTest, deadlock_allocate) { |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 57 | ASSERT_DEATH( |
| 58 | { |
| 59 | void* ptr = malloc(128); |
| 60 | ASSERT_NE(ptr, nullptr); |
| 61 | free(ptr); |
| 62 | { |
| 63 | alarm(100ms); |
| 64 | ScopedDisableMalloc disable_malloc; |
| 65 | void* ptr = malloc(128); |
| 66 | ASSERT_NE(ptr, nullptr); |
| 67 | free(ptr); |
| 68 | } |
| 69 | }, |
| 70 | ""); |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST_F(DisableMallocTest, deadlock_new) { |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 74 | ASSERT_DEATH( |
| 75 | { |
George Burgess IV | 180e5e7 | 2017-09-12 16:54:53 -0700 | [diff] [blame] | 76 | // C++ allows `new Foo` to be replaced with a stack allocation or merged |
| 77 | // with future `new Foo` expressions, provided certain conditions are |
| 78 | // met [expr.new/10]. None of this applies to `operator new(size_t)`. |
| 79 | void* ptr = ::operator new(1); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 80 | ASSERT_NE(ptr, nullptr); |
George Burgess IV | 180e5e7 | 2017-09-12 16:54:53 -0700 | [diff] [blame] | 81 | ::operator delete(ptr); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 82 | { |
| 83 | alarm(100ms); |
| 84 | ScopedDisableMalloc disable_malloc; |
George Burgess IV | 180e5e7 | 2017-09-12 16:54:53 -0700 | [diff] [blame] | 85 | void* ptr = ::operator new(1); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 86 | ASSERT_NE(ptr, nullptr); |
George Burgess IV | 180e5e7 | 2017-09-12 16:54:53 -0700 | [diff] [blame] | 87 | ::operator delete(ptr); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 88 | } |
| 89 | }, |
| 90 | ""); |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST_F(DisableMallocTest, deadlock_delete) { |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 94 | ASSERT_DEATH( |
| 95 | { |
George Burgess IV | 180e5e7 | 2017-09-12 16:54:53 -0700 | [diff] [blame] | 96 | void* ptr = ::operator new(1); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 97 | ASSERT_NE(ptr, nullptr); |
| 98 | { |
| 99 | alarm(250ms); |
| 100 | ScopedDisableMalloc disable_malloc; |
George Burgess IV | 180e5e7 | 2017-09-12 16:54:53 -0700 | [diff] [blame] | 101 | ::operator delete(ptr); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 102 | } |
| 103 | }, |
| 104 | ""); |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST_F(DisableMallocTest, deadlock_free) { |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 108 | ASSERT_DEATH( |
| 109 | { |
| 110 | void* ptr = malloc(128); |
| 111 | ASSERT_NE(ptr, nullptr); |
| 112 | { |
| 113 | alarm(100ms); |
| 114 | ScopedDisableMalloc disable_malloc; |
| 115 | free(ptr); |
| 116 | } |
| 117 | }, |
| 118 | ""); |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TEST_F(DisableMallocTest, deadlock_fork) { |
| 122 | ASSERT_DEATH({ |
| 123 | { |
| 124 | alarm(100ms); |
| 125 | ScopedDisableMalloc disable_malloc; |
| 126 | fork(); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 127 | } |
| 128 | }, ""); |
Colin Cross | b8e20f5 | 2016-03-02 17:52:56 -0800 | [diff] [blame] | 129 | } |
Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 130 | |
| 131 | } // namespace android |