1 /*
2  * Copyright (C) 2018 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 
18 #include <atomic>
19 #include <memory>
20 
21 #include <jni.h>
22 #include <signal.h>
23 #include <stdint.h>
24 #include <sys/mman.h>
25 
26 #include "base/globals.h"
27 #include "base/mem_map.h"
28 #include "fault_handler.h"
29 
30 namespace art {
31 
32 class TestFaultHandler final : public FaultHandler {
33  public:
TestFaultHandler(FaultManager * manager)34   explicit TestFaultHandler(FaultManager* manager)
35       : FaultHandler(manager),
36         map_error_(),
37         target_map_(MemMap::MapAnonymous("test-305-mmap",
38                                          /* addr */ nullptr,
39                                          /* byte_count */ kPageSize,
40                                          /* prot */ PROT_NONE,
41                                          /* low_4gb */ false,
42                                          /* reuse */ false,
43                                          /* reservation */ nullptr,
44                                          /* error_msg */ &map_error_,
45                                          /* use_ashmem */ false)),
46         was_hit_(false) {
47     CHECK(target_map_.IsValid()) << "Unable to create segfault target address " << map_error_;
48     manager_->AddHandler(this, /*in_generated_code*/false);
49   }
50 
~TestFaultHandler()51   virtual ~TestFaultHandler() {
52     manager_->RemoveHandler(this);
53   }
54 
Action(int sig,siginfo_t * siginfo,void * context ATTRIBUTE_UNUSED)55   bool Action(int sig, siginfo_t* siginfo, void* context ATTRIBUTE_UNUSED) override {
56     CHECK_EQ(sig, SIGSEGV);
57     CHECK_EQ(reinterpret_cast<uint32_t*>(siginfo->si_addr),
58              GetTargetPointer()) << "Segfault on unexpected address!";
59     CHECK(!was_hit_) << "Recursive signal!";
60     was_hit_ = true;
61 
62     LOG(INFO) << "SEGV Caught. mprotecting map.";
63     CHECK(target_map_.Protect(PROT_READ | PROT_WRITE)) << "Failed to mprotect R/W";
64     LOG(INFO) << "Setting value to be read.";
65     *GetTargetPointer() = kDataValue;
66     LOG(INFO) << "Changing prot to be read-only.";
67     CHECK(target_map_.Protect(PROT_READ)) << "Failed to mprotect R-only";
68     return true;
69   }
70 
CauseSegfault()71   void CauseSegfault() {
72     CHECK_EQ(target_map_.GetProtect(), PROT_NONE);
73 
74     // This will segfault. The handler should deal with it though and we will get a value out of it.
75     uint32_t data = *GetTargetPointer();
76 
77     // Prevent re-ordering around the *GetTargetPointer by the compiler
78     std::atomic_signal_fence(std::memory_order_seq_cst);
79 
80     CHECK(was_hit_);
81     CHECK_EQ(data, kDataValue) << "Unexpected read value from mmap";
82     CHECK_EQ(target_map_.GetProtect(), PROT_READ);
83     LOG(INFO) << "Success!";
84   }
85 
86  private:
GetTargetPointer()87   uint32_t* GetTargetPointer() {
88     return reinterpret_cast<uint32_t*>(target_map_.Begin() + 8);
89   }
90 
91   static constexpr uint32_t kDataValue = 0xDEADBEEF;
92 
93   std::string map_error_;
94   MemMap target_map_;
95   bool was_hit_;
96 };
97 
Java_Main_runFaultHandlerTest(JNIEnv *,jclass)98 extern "C" JNIEXPORT void JNICALL Java_Main_runFaultHandlerTest(JNIEnv*, jclass) {
99   std::unique_ptr<TestFaultHandler> handler(new TestFaultHandler(&fault_manager));
100   handler->CauseSegfault();
101 }
102 
103 }  // namespace art
104