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