Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -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 | #ifndef LIBMEMUNREACHABLE_SCOPED_DISABLE_MALLOC_H_ |
| 18 | #define LIBMEMUNREACHABLE_SCOPED_DISABLE_MALLOC_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | |
| 22 | #include "android-base/macros.h" |
| 23 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 24 | #include "ScopedAlarm.h" |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 25 | #include "bionic.h" |
| 26 | #include "log.h" |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 27 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 28 | class DisableMallocGuard { |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 29 | public: |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 30 | DisableMallocGuard() : disabled_(false) {} |
| 31 | ~DisableMallocGuard() { Enable(); } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 32 | |
| 33 | void Disable() { |
| 34 | if (!disabled_) { |
| 35 | malloc_disable(); |
| 36 | disabled_ = true; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void Enable() { |
| 41 | if (disabled_) { |
| 42 | malloc_enable(); |
| 43 | disabled_ = false; |
| 44 | } |
| 45 | } |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 46 | |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 47 | private: |
| 48 | DISALLOW_COPY_AND_ASSIGN(DisableMallocGuard); |
| 49 | bool disabled_; |
| 50 | }; |
| 51 | |
| 52 | // Any calls to malloc or free from this thread will deadlock as long as this |
| 53 | // object is in scope. Calls to malloc from other threads may succeed (for |
| 54 | // example if the allocation is satisfied out of the thread's tcache), or may |
| 55 | // block until the object is destroyed. |
| 56 | // |
| 57 | // Don't call fork() while malloc is disabled, it needs the same locks held |
| 58 | // here. |
| 59 | class ScopedDisableMalloc { |
| 60 | public: |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 61 | ScopedDisableMalloc() { disable_malloc_.Disable(); } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 62 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 63 | ~ScopedDisableMalloc() { disable_malloc_.Enable(); } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | DISALLOW_COPY_AND_ASSIGN(ScopedDisableMalloc); |
| 67 | DisableMallocGuard disable_malloc_; |
| 68 | }; |
| 69 | |
| 70 | class ScopedDisableMallocTimeout { |
| 71 | public: |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 72 | explicit ScopedDisableMallocTimeout( |
| 73 | std::chrono::milliseconds timeout = std::chrono::milliseconds(2000)) |
| 74 | : timeout_(timeout), timed_out_(false), disable_malloc_() { |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 75 | Disable(); |
| 76 | } |
| 77 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 78 | ~ScopedDisableMallocTimeout() { Enable(); } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 79 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 80 | bool timed_out() { return timed_out_; } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 81 | |
| 82 | void Enable() { |
| 83 | disable_malloc_.Enable(); |
| 84 | alarm_ = nullptr; |
| 85 | } |
| 86 | |
| 87 | void Disable() { |
| 88 | // set up the alarm before disabling malloc so unique_ptr can be used |
| 89 | alarm_ = std::make_unique<ScopedAlarm>(timeout_, [&]() { |
| 90 | disable_malloc_.Enable(); |
| 91 | timed_out_ = true; |
| 92 | }); |
| 93 | |
| 94 | disable_malloc_.Disable(); |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | DISALLOW_COPY_AND_ASSIGN(ScopedDisableMallocTimeout); |
| 99 | std::chrono::milliseconds timeout_; |
| 100 | bool timed_out_; |
| 101 | std::unique_ptr<ScopedAlarm> alarm_; |
| 102 | DisableMallocGuard disable_malloc_; |
| 103 | }; |
| 104 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 105 | #endif // LIBMEMUNREACHABLE_SCOPED_DISABLE_MALLOC_H_ |