1501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown/*
2501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * Copyright (C) 2011 The Android Open Source Project
3501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown *
4501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
5501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * you may not use this file except in compliance with the License.
6501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * You may obtain a copy of the License at
7501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown *
8501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
9501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown *
10501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * Unless required by applicable law or agreed to in writing, software
11501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
12501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * See the License for the specific language governing permissions and
14501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown * limitations under the License.
15501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown */
16501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
17501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#define LOG_TAG "Corkscrew"
18501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown//#define LOG_NDEBUG 0
19501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
20501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include "ptrace-arch.h"
21501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <corkscrew/ptrace.h>
22501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
23501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <errno.h>
24420a7fa82d0fba6516d76d6024728de010b1cb8dElliott Hughes#include <stdlib.h>
25501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <sys/ptrace.h>
26501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <cutils/log.h>
27501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
28501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownstatic const uint32_t ELF_MAGIC = 0x464C457f; // "ELF\0177"
29501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
30501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#ifndef PAGE_SIZE
31501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#define PAGE_SIZE 4096
32501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
33501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
34501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#ifndef PAGE_MASK
35501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#define PAGE_MASK (~(PAGE_SIZE - 1))
36501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
37501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
38f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownvoid init_memory(memory_t* memory, const map_info_t* map_info_list) {
39f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    memory->tid = -1;
40f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    memory->map_info_list = map_info_list;
41f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown}
42f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
43f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownvoid init_memory_ptrace(memory_t* memory, pid_t tid) {
44f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    memory->tid = tid;
45f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    memory->map_info_list = NULL;
46f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown}
47f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
48f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownbool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value) {
49f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    ALOGV("try_get_word: reading word at 0x%08x", ptr);
50501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (ptr & 3) {
5169f4cd7f5add7a7c7f5915e5292aab7eb2a42e9fSteve Block        ALOGV("try_get_word: invalid pointer 0x%08x", ptr);
52f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        *out_value = 0xffffffffL;
53501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        return false;
54501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
55f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    if (memory->tid < 0) {
56f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        if (!is_readable_map(memory->map_info_list, ptr)) {
57f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            ALOGV("try_get_word: pointer 0x%08x not in a readable map", ptr);
58f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            *out_value = 0xffffffffL;
59f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            return false;
60501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
61501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        *out_value = *(uint32_t*)ptr;
62501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        return true;
63501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    } else {
64501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        // ptrace() returns -1 and sets errno when the operation fails.
65501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        // To disambiguate -1 from a valid result, we clear errno beforehand.
66501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        errno = 0;
67f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        *out_value = ptrace(PTRACE_PEEKTEXT, memory->tid, (void*)ptr, NULL);
68501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        if (*out_value == 0xffffffffL && errno) {
69f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            ALOGV("try_get_word: invalid pointer 0x%08x reading from tid %d, "
70f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown                    "ptrace() errno=%d", ptr, memory->tid, errno);
71501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            return false;
72501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
73501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        return true;
74501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
75501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
76501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
77f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownbool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value) {
78f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    memory_t memory;
79f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    init_memory_ptrace(&memory, tid);
80f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    return try_get_word(&memory, ptr, out_value);
81f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown}
82f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
83501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownstatic void load_ptrace_map_info_data(pid_t pid, map_info_t* mi) {
84f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    if (mi->is_executable && mi->is_readable) {
85501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        uint32_t elf_magic;
86f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        if (try_get_word_ptrace(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) {
87501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            map_info_data_t* data = (map_info_data_t*)calloc(1, sizeof(map_info_data_t));
88501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            if (data) {
89501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                mi->data = data;
90501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                if (mi->name[0]) {
91501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                    data->symbol_table = load_symbol_table(mi->name);
92501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                }
93501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#ifdef CORKSCREW_HAVE_ARCH
94501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                load_ptrace_map_info_data_arch(pid, mi, data);
95501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
96501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            }
97501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
98501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
99501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
100501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
101501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownptrace_context_t* load_ptrace_context(pid_t pid) {
102501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    ptrace_context_t* context =
103501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            (ptrace_context_t*)calloc(1, sizeof(ptrace_context_t));
104501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (context) {
105501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        context->map_info_list = load_map_info_list(pid);
106501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) {
107501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            load_ptrace_map_info_data(pid, mi);
108501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
109501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
110501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return context;
111501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
112501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
113501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownstatic void free_ptrace_map_info_data(map_info_t* mi) {
114501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    map_info_data_t* data = (map_info_data_t*)mi->data;
115501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (data) {
116501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        if (data->symbol_table) {
117501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            free_symbol_table(data->symbol_table);
118501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
119501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#ifdef CORKSCREW_HAVE_ARCH
120501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        free_ptrace_map_info_data_arch(mi, data);
121501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#endif
122501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        free(data);
123501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        mi->data = NULL;
124501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
125501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
126501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
127501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownvoid free_ptrace_context(ptrace_context_t* context) {
128501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) {
129501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        free_ptrace_map_info_data(mi);
130501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
131501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    free_map_info_list(context->map_info_list);
132501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
133501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
134501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownvoid find_symbol_ptrace(const ptrace_context_t* context,
135501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol) {
136501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    const map_info_t* mi = find_map_info(context->map_info_list, addr);
137501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    const symbol_t* symbol = NULL;
138501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (mi) {
139501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        const map_info_data_t* data = (const map_info_data_t*)mi->data;
140501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        if (data && data->symbol_table) {
141501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            symbol = find_symbol(data->symbol_table, addr - mi->start);
142501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
143501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
144501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    *out_map_info = mi;
145501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    *out_symbol = symbol;
146501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
147