1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <ctype.h>
30 #include <elf.h>
31 #include <inttypes.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 
36 #include "debug_mapinfo.h"
37 #include "malloc_debug_disable.h"
38 
39 #if defined(__LP64__)
40 #define Elf_W(x) Elf64_##x
41 #else
42 #define Elf_W(x) Elf32_##x
43 #endif
44 
45 // Format of /proc/<PID>/maps:
46 //   6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so
parse_maps_line(char * line)47 static mapinfo_t* parse_maps_line(char* line) {
48   uintptr_t start;
49   uintptr_t end;
50   uintptr_t offset;
51   char permissions[4];
52   int name_pos;
53   if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d%n", &start,
54              &end, permissions, &offset, &name_pos) < 2) {
55     return NULL;
56   }
57 
58   while (isspace(line[name_pos])) {
59     name_pos += 1;
60   }
61   const char* name = line + name_pos;
62   size_t name_len = strlen(name);
63   if (name_len && name[name_len - 1] == '\n') {
64     name_len -= 1;
65   }
66 
67   mapinfo_t* mi = reinterpret_cast<mapinfo_t*>(calloc(1, sizeof(mapinfo_t) + name_len + 1));
68   if (mi) {
69     mi->start = start;
70     mi->end = end;
71     mi->offset = offset;
72     if (permissions[0] != 'r') {
73       // Any unreadable map will just get a zero load base.
74       mi->load_base = 0;
75       mi->load_base_read = true;
76     } else {
77       mi->load_base_read = false;
78     }
79     memcpy(mi->name, name, name_len);
80     mi->name[name_len] = '\0';
81   }
82   return mi;
83 }
84 
mapinfo_create(pid_t pid)85 __LIBC_HIDDEN__ mapinfo_t* mapinfo_create(pid_t pid) {
86   ScopedDisableDebugCalls disable;
87 
88   struct mapinfo_t* milist = NULL;
89   char data[1024]; // Used to read lines as well as to construct the filename.
90   snprintf(data, sizeof(data), "/proc/%d/maps", pid);
91   FILE* fp = fopen(data, "re");
92   if (fp != NULL) {
93     while (fgets(data, sizeof(data), fp) != NULL) {
94       mapinfo_t* mi = parse_maps_line(data);
95       if (mi) {
96         mi->next = milist;
97         milist = mi;
98       }
99     }
100     fclose(fp);
101   }
102   return milist;
103 }
104 
mapinfo_destroy(mapinfo_t * mi)105 __LIBC_HIDDEN__ void mapinfo_destroy(mapinfo_t* mi) {
106   ScopedDisableDebugCalls disable;
107 
108   while (mi != NULL) {
109     mapinfo_t* del = mi;
110     mi = mi->next;
111     free(del);
112   }
113 }
114 
115 template<typename T>
get_val(mapinfo_t * mi,uintptr_t addr,T * store)116 static inline bool get_val(mapinfo_t* mi, uintptr_t addr, T* store) {
117   if (addr < mi->start || addr + sizeof(T) > mi->end) {
118     return false;
119   }
120   // Make sure the address is aligned properly.
121   if (addr & (sizeof(T)-1)) {
122     return false;
123   }
124   *store = *reinterpret_cast<T*>(addr);
125   return true;
126 }
127 
mapinfo_read_loadbase(mapinfo_t * mi)128 __LIBC_HIDDEN__ void mapinfo_read_loadbase(mapinfo_t* mi) {
129   mi->load_base = 0;
130   mi->load_base_read = true;
131   uintptr_t addr = mi->start;
132   Elf_W(Ehdr) ehdr;
133   if (!get_val<Elf_W(Half)>(mi, addr + offsetof(Elf_W(Ehdr), e_phnum), &ehdr.e_phnum)) {
134     return;
135   }
136   if (!get_val<Elf_W(Off)>(mi, addr + offsetof(Elf_W(Ehdr), e_phoff), &ehdr.e_phoff)) {
137     return;
138   }
139   addr += ehdr.e_phoff;
140   for (size_t i = 0; i < ehdr.e_phnum; i++) {
141     Elf_W(Phdr) phdr;
142     if (!get_val<Elf_W(Word)>(mi, addr + offsetof(Elf_W(Phdr), p_type), &phdr.p_type)) {
143       return;
144     }
145     if (!get_val<Elf_W(Off)>(mi, addr + offsetof(Elf_W(Phdr), p_offset), &phdr.p_offset)) {
146       return;
147     }
148     if (phdr.p_type == PT_LOAD && phdr.p_offset == mi->offset) {
149       if (!get_val<Elf_W(Addr)>(mi, addr + offsetof(Elf_W(Phdr), p_vaddr), &phdr.p_vaddr)) {
150         return;
151       }
152       mi->load_base = phdr.p_vaddr;
153       return;
154     }
155     addr += sizeof(phdr);
156   }
157 }
158 
159 // Find the containing map info for the PC.
mapinfo_find(mapinfo_t * mi,uintptr_t pc,uintptr_t * rel_pc)160 __LIBC_HIDDEN__ const mapinfo_t* mapinfo_find(mapinfo_t* mi, uintptr_t pc, uintptr_t* rel_pc) {
161   for (; mi != NULL; mi = mi->next) {
162     if ((pc >= mi->start) && (pc < mi->end)) {
163       if (!mi->load_base_read) {
164         mapinfo_read_loadbase(mi);
165       }
166       *rel_pc = pc - mi->start + mi->load_base;
167       return mi;
168     }
169   }
170   *rel_pc = pc;
171   return NULL;
172 }
173