| 42 | #define PAGE_COVER(addr) (PAGE_END(addr) - PAGE_START(addr)) |
| 43 | |
| 44 | int xh_util_get_mem_protect(uintptr_t addr, size_t len, const char *pathname, unsigned int *prot) |
| 45 | { |
| 46 | uintptr_t start_addr = addr; |
| 47 | uintptr_t end_addr = addr + len; |
| 48 | FILE *fp; |
| 49 | char line[512]; |
| 50 | uintptr_t start, end; |
| 51 | char perm[5]; |
| 52 | int load0 = 1; |
| 53 | int found_all = 0; |
| 54 | |
| 55 | *prot = 0; |
| 56 | |
| 57 | if(NULL == (fp = fopen("/proc/self/maps", "r"))) return XH_ERRNO_BADMAPS; |
| 58 | |
| 59 | while(fgets(line, sizeof(line), fp)) |
| 60 | { |
| 61 | if(NULL != pathname) |
| 62 | if(NULL == strstr(line, pathname)) continue; |
| 63 | |
| 64 | if(sscanf(line, "%"PRIxPTR"-%"PRIxPTR" %4s ", &start, &end, perm) != 3) continue; |
| 65 | |
| 66 | if(perm[3] != 'p') continue; |
| 67 | |
| 68 | if(start_addr >= start && start_addr < end) |
| 69 | { |
| 70 | if(load0) |
| 71 | { |
| 72 | //first load segment |
| 73 | if(perm[0] == 'r') *prot |= PROT_READ; |
| 74 | if(perm[1] == 'w') *prot |= PROT_WRITE; |
| 75 | if(perm[2] == 'x') *prot |= PROT_EXEC; |
| 76 | load0 = 0; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | //others |
| 81 | if(perm[0] != 'r') *prot &= ~PROT_READ; |
| 82 | if(perm[1] != 'w') *prot &= ~PROT_WRITE; |
| 83 | if(perm[2] != 'x') *prot &= ~PROT_EXEC; |
| 84 | } |
| 85 | |
| 86 | if(end_addr <= end) |
| 87 | { |
| 88 | found_all = 1; |
| 89 | break; //finished |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | start_addr = end; //try to find the next load segment |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fclose(fp); |
| 99 | |
| 100 | if(!found_all) return XH_ERRNO_SEGVERR; |
| 101 |
no outgoing calls
no test coverage detected