blob: f4467190c1df142958a65ca9ac1f4eacd42259d3 [file] [log] [blame]
Colin Crossb8e20f52016-03-02 17:52:56 -08001/*
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 Crossb8e20f52016-03-02 17:52:56 -080022#include <ScopedDisableMalloc.h>
Colin Crossa83881e2017-06-22 10:50:05 -070023#include <gtest/gtest.h>
Colin Crossb8e20f52016-03-02 17:52:56 -080024
25using namespace std::chrono_literals;
26
Colin Crossa9939e92017-06-21 13:13:00 -070027namespace android {
28
Colin Crossb8e20f52016-03-02 17:52:56 -080029class 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
40TEST_F(DisableMallocTest, reenable) {
Colin Crossa83881e2017-06-22 10:50:05 -070041 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 Crossb8e20f52016-03-02 17:52:56 -080054}
55
56TEST_F(DisableMallocTest, deadlock_allocate) {
Colin Crossa83881e2017-06-22 10:50:05 -070057 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 Crossb8e20f52016-03-02 17:52:56 -080071}
72
73TEST_F(DisableMallocTest, deadlock_new) {
Colin Crossa83881e2017-06-22 10:50:05 -070074 ASSERT_DEATH(
75 {
George Burgess IV180e5e72017-09-12 16:54:53 -070076 // 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 Crossa83881e2017-06-22 10:50:05 -070080 ASSERT_NE(ptr, nullptr);
George Burgess IV180e5e72017-09-12 16:54:53 -070081 ::operator delete(ptr);
Colin Crossa83881e2017-06-22 10:50:05 -070082 {
83 alarm(100ms);
84 ScopedDisableMalloc disable_malloc;
George Burgess IV180e5e72017-09-12 16:54:53 -070085 void* ptr = ::operator new(1);
Colin Crossa83881e2017-06-22 10:50:05 -070086 ASSERT_NE(ptr, nullptr);
George Burgess IV180e5e72017-09-12 16:54:53 -070087 ::operator delete(ptr);
Colin Crossa83881e2017-06-22 10:50:05 -070088 }
89 },
90 "");
Colin Crossb8e20f52016-03-02 17:52:56 -080091}
92
93TEST_F(DisableMallocTest, deadlock_delete) {
Colin Crossa83881e2017-06-22 10:50:05 -070094 ASSERT_DEATH(
95 {
George Burgess IV180e5e72017-09-12 16:54:53 -070096 void* ptr = ::operator new(1);
Colin Crossa83881e2017-06-22 10:50:05 -070097 ASSERT_NE(ptr, nullptr);
98 {
99 alarm(250ms);
100 ScopedDisableMalloc disable_malloc;
George Burgess IV180e5e72017-09-12 16:54:53 -0700101 ::operator delete(ptr);
Colin Crossa83881e2017-06-22 10:50:05 -0700102 }
103 },
104 "");
Colin Crossb8e20f52016-03-02 17:52:56 -0800105}
106
107TEST_F(DisableMallocTest, deadlock_free) {
Colin Crossa83881e2017-06-22 10:50:05 -0700108 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 Crossb8e20f52016-03-02 17:52:56 -0800119}
120
121TEST_F(DisableMallocTest, deadlock_fork) {
122 ASSERT_DEATH({
123 {
124 alarm(100ms);
125 ScopedDisableMalloc disable_malloc;
126 fork();
Colin Crossa83881e2017-06-22 10:50:05 -0700127}
128}, "");
Colin Crossb8e20f52016-03-02 17:52:56 -0800129}
Colin Crossa9939e92017-06-21 13:13:00 -0700130
131} // namespace android