ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
linux/system.c
Go to the documentation of this file.
1
9#include <ascii-chat/common.h>
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
13
26int get_binary_file_address_offsets(const void *addr, platform_binary_match_t *matches, int max_matches) {
27 int count = 0;
28 uintptr_t addr_int = (uintptr_t)addr;
29
30 FILE *maps = fopen("/proc/self/maps", "r");
31 if (!maps) {
32 return 0;
33 }
34
35 char line[512];
36 while (fgets(line, sizeof(line), maps) && count < max_matches) {
37 char perms[5], device[10], path[PLATFORM_MAX_PATH_LENGTH];
38
39 // Parse: start-end perms offset device inode path
40 // Example: 7f3a2b1c0000-7f3a2b1c1000 r-xp 00000000 08:02 12345678 /usr/lib/libsodium.so.23
41 char *p = line;
42 char *end_ptr;
43
44 uintptr_t start = (uintptr_t)strtoul(p, &end_ptr, 16);
45 if (end_ptr == p || *end_ptr != '-') {
46 continue;
47 }
48 p = end_ptr + 1;
49
50 uintptr_t end = (uintptr_t)strtoul(p, &end_ptr, 16);
51 if (end_ptr == p || *end_ptr != ' ') {
52 continue;
53 }
54 p = end_ptr + 1;
55
56 // Parse perms field (4 chars like "r-xp")
57 if (strlen(p) < 4) {
58 continue;
59 }
60 memcpy(perms, p, 4);
61 perms[4] = '\0';
62 p += 4;
63 if (*p != ' ') {
64 continue;
65 }
66 p++;
67
68 uintptr_t offset = (uintptr_t)strtoul(p, &end_ptr, 16);
69 if (end_ptr == p || *end_ptr != ' ') {
70 continue;
71 }
72 p = end_ptr + 1;
73
74 // Parse device field (like "08:02")
75 char *space = strchr(p, ' ');
76 if (!space || (size_t)(space - p) >= sizeof(device)) {
77 continue;
78 }
79 memcpy(device, p, (size_t)(space - p));
80 device[space - p] = '\0';
81 p = space + 1;
82
83 // Skip whitespace and inode field
84 while (*p == ' ')
85 p++;
86 strtoul(p, &end_ptr, 10);
87 if (end_ptr == p) {
88 continue;
89 }
90
91 // Skip lines without executable flag
92 if (perms[2] != 'x') {
93 continue;
94 }
95
96 // Extract path more robustly by searching for the last whitespace-separated token
97 // /proc/self/maps format: start-end perms offset device inode [path]
98 // The path is optional and starts after the inode field
99 path[0] = '\0';
100
101 // Find the last whitespace and take everything after it as the path
102 // This is more robust than trying to count fields
103 int line_len = strlen(line);
104 int path_start = -1;
105
106 // Scan from the end backwards to find the last non-whitespace character
107 for (int i = line_len - 1; i >= 0; i--) {
108 if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n' && line[i] != '\r') {
109 // Found a non-whitespace char, now find the start of this token
110 path_start = i;
111 while (path_start > 0 && line[path_start - 1] != ' ' && line[path_start - 1] != '\t') {
112 path_start--;
113 }
114 break;
115 }
116 }
117
118 if (path_start > 0 && line[path_start - 1] != '\0') {
119 // Extract the path starting from path_start
120 strncpy(path, &line[path_start], PLATFORM_MAX_PATH_LENGTH - 1);
121 path[PLATFORM_MAX_PATH_LENGTH - 1] = '\0';
122 // Remove trailing whitespace
123 int path_len = strlen(path);
124 while (path_len > 0 && (path[path_len - 1] == '\n' || path[path_len - 1] == '\r')) {
125 path[--path_len] = '\0';
126 }
127 }
128
129 // Check if address falls within this segment
130 if (perms[2] == 'x' && path[0] == '/') {
131 if (addr_int >= start && addr_int < end) {
132 strncpy(matches[count].path, path, PLATFORM_MAX_PATH_LENGTH - 1);
133 matches[count].path[PLATFORM_MAX_PATH_LENGTH - 1] = '\0';
134 matches[count].file_offset = (addr_int - start) + offset;
135 count++;
136 }
137 }
138 }
139
140 fclose(maps);
141 return count;
142}
Platform abstraction layer umbrella header providing unified cross-platform API.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
int get_binary_file_address_offsets(const void *addr, platform_binary_match_t *matches, int max_matches)
Get binary that contains address on Linux via /proc/self/maps.
Private implementation helpers for platform abstraction layer.
Find which binary(ies) contain a given address.
Definition system.h:777
uintptr_t file_offset
Definition system.h:781
#define PLATFORM_MAX_PATH_LENGTH
Definition system.c:69