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 <corkscrew/map_info.h>
21501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
22501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <ctype.h>
23501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <stdio.h>
24420a7fa82d0fba6516d76d6024728de010b1cb8dElliott Hughes#include <stdlib.h>
25501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <string.h>
26501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <limits.h>
27501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <pthread.h>
28501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <unistd.h>
29501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown#include <cutils/log.h>
30f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown#include <sys/time.h>
31501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
32501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so\n
33501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown// 012345678901234567890123456789012345678901234567890123456789
34501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown// 0         1         2         3         4         5
35501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownstatic map_info_t* parse_maps_line(const char* line)
36501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown{
37501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    unsigned long int start;
38501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    unsigned long int end;
39501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    char permissions[5];
40501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    int name_pos;
41501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n", &start, &end,
42501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            permissions, &name_pos) != 3) {
43501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        return NULL;
44501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
45501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
46501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    while (isspace(line[name_pos])) {
47501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        name_pos += 1;
48501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
49501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    const char* name = line + name_pos;
50501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    size_t name_len = strlen(name);
51501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (name_len && name[name_len - 1] == '\n') {
52501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        name_len -= 1;
53501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
54501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
55501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    map_info_t* mi = calloc(1, sizeof(map_info_t) + name_len + 1);
56501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (mi) {
57501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        mi->start = start;
58501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        mi->end = end;
59f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        mi->is_readable = strlen(permissions) == 4 && permissions[0] == 'r';
60501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        mi->is_executable = strlen(permissions) == 4 && permissions[2] == 'x';
61501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        mi->data = NULL;
62501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        memcpy(mi->name, name, name_len);
63501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        mi->name[name_len] = '\0';
64f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        ALOGV("Parsed map: start=0x%08x, end=0x%08x, "
65f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown                "is_readable=%d, is_executable=%d, name=%s",
66f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown                mi->start, mi->end, mi->is_readable, mi->is_executable, mi->name);
67501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
68501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return mi;
69501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
70501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
71501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownmap_info_t* load_map_info_list(pid_t tid) {
72501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    char path[PATH_MAX];
73501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    char line[1024];
74501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    FILE* fp;
75501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    map_info_t* milist = NULL;
76501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
77501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    snprintf(path, PATH_MAX, "/proc/%d/maps", tid);
78501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    fp = fopen(path, "r");
79501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    if (fp) {
80501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        while(fgets(line, sizeof(line), fp)) {
81501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            map_info_t* mi = parse_maps_line(line);
82501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            if (mi) {
83501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                mi->next = milist;
84501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown                milist = mi;
85501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown            }
86501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        }
87501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        fclose(fp);
88501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
89501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return milist;
90501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
91501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
92501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownvoid free_map_info_list(map_info_t* milist) {
93501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    while (milist) {
94501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        map_info_t* next = milist->next;
95501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        free(milist);
96501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        milist = next;
97501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
98501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
99501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
100501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brownconst map_info_t* find_map_info(const map_info_t* milist, uintptr_t addr) {
101501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    const map_info_t* mi = milist;
102501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    while (mi && !(addr >= mi->start && addr < mi->end)) {
103501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown        mi = mi->next;
104501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    }
105501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown    return mi;
106501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
107501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
108f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownbool is_readable_map(const map_info_t* milist, uintptr_t addr) {
109f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    const map_info_t* mi = find_map_info(milist, addr);
110f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    return mi && mi->is_readable;
111f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown}
112f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
113f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownbool is_executable_map(const map_info_t* milist, uintptr_t addr) {
114f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    const map_info_t* mi = find_map_info(milist, addr);
115f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    return mi && mi->is_executable;
116f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown}
117f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
118f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownstatic pthread_mutex_t g_my_map_info_list_mutex = PTHREAD_MUTEX_INITIALIZER;
119f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownstatic map_info_t* g_my_map_info_list = NULL;
120f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
121f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownstatic const int64_t MAX_CACHE_AGE = 5 * 1000 * 1000000LL;
122f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
123f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Browntypedef struct {
124f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    uint32_t refs;
125f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    int64_t timestamp;
126f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown} my_map_info_data_t;
127f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
128f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownstatic int64_t now() {
129f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    struct timespec t;
130f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    t.tv_sec = t.tv_nsec = 0;
131f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    clock_gettime(CLOCK_MONOTONIC, &t);
132f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    return t.tv_sec * 1000000000LL + t.tv_nsec;
133f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown}
134f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
135f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownstatic void dec_ref(map_info_t* milist, my_map_info_data_t* data) {
136f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    if (!--data->refs) {
137f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        ALOGV("Freed my_map_info_list %p.", milist);
138f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        free(data);
139f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        free_map_info_list(milist);
140f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    }
141f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown}
142f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
143f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownmap_info_t* acquire_my_map_info_list() {
144f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    pthread_mutex_lock(&g_my_map_info_list_mutex);
145501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
146f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    int64_t time = now();
147f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    if (g_my_map_info_list) {
148f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        my_map_info_data_t* data = (my_map_info_data_t*)g_my_map_info_list->data;
149f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        int64_t age = time - data->timestamp;
150f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        if (age >= MAX_CACHE_AGE) {
151f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            ALOGV("Invalidated my_map_info_list %p, age=%lld.", g_my_map_info_list, age);
152f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            dec_ref(g_my_map_info_list, data);
153f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            g_my_map_info_list = NULL;
154f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        } else {
155f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            ALOGV("Reusing my_map_info_list %p, age=%lld.", g_my_map_info_list, age);
156f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        }
157f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    }
158f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
159f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    if (!g_my_map_info_list) {
160f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        my_map_info_data_t* data = (my_map_info_data_t*)malloc(sizeof(my_map_info_data_t));
161f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        g_my_map_info_list = load_map_info_list(getpid());
162f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        if (g_my_map_info_list) {
163f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            ALOGV("Loaded my_map_info_list %p.", g_my_map_info_list);
164f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            g_my_map_info_list->data = data;
165f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            data->refs = 1;
166f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            data->timestamp = time;
167f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        } else {
168f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown            free(data);
169f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        }
170f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    }
171f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
172f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    map_info_t* milist = g_my_map_info_list;
173f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    if (milist) {
174f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        my_map_info_data_t* data = (my_map_info_data_t*)g_my_map_info_list->data;
175f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        data->refs += 1;
176f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    }
177f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
178f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    pthread_mutex_unlock(&g_my_map_info_list_mutex);
179f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    return milist;
180501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
181501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown
182f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brownvoid release_my_map_info_list(map_info_t* milist) {
183f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    if (milist) {
184f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        pthread_mutex_lock(&g_my_map_info_list_mutex);
185f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
186f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        my_map_info_data_t* data = (my_map_info_data_t*)milist->data;
187f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        dec_ref(milist, data);
188f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown
189f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown        pthread_mutex_unlock(&g_my_map_info_list_mutex);
190f0c5872637a63e28e3cd314cfc915c07f76df9c6Jeff Brown    }
191501edd29b823ce1301d2effdd3a9e4b6e2b20b76Jeff Brown}
192