aboutsummaryrefslogtreecommitdiffstats
path: root/get_stack_space.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-07-02 14:05:37 +0200
committerMattias Andrée <maandree@kth.se>2024-07-02 14:05:37 +0200
commit2268eb37c71d8cc83887b93ca8d81289798a0552 (patch)
tree4b3a10898aa4978730e94a357f21ba9a1f02b8b1 /get_stack_space.c
parentAdd missing .c files (diff)
downloadlibsimple-2268eb37c71d8cc83887b93ca8d81289798a0552.tar.gz
libsimple-2268eb37c71d8cc83887b93ca8d81289798a0552.tar.bz2
libsimple-2268eb37c71d8cc83887b93ca8d81289798a0552.tar.xz
Add needstack, get_stack_direction, get_stack_limit, and get_stack_space
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'get_stack_space.c')
-rw-r--r--get_stack_space.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/get_stack_space.c b/get_stack_space.c
new file mode 100644
index 0000000..7f1fdd3
--- /dev/null
+++ b/get_stack_space.c
@@ -0,0 +1,71 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+int
+libsimple_get_stack_space(uintptr_t *restrict low, uintptr_t *restrict high)
+{
+ char buf[4096], *line;
+ int fd;
+ ssize_t r;
+ size_t off = 0;
+ size_t lineoff, linelen;
+
+ fd = open("/proc/self/maps", O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ for (;;) {
+ read_again:
+ r = read(fd, &buf[off], sizeof(buf) - off);
+ if (r <= 0) {
+ if (r && errno == EINTR)
+ continue;
+ close(fd);
+ if (!r)
+ errno = EIO;
+ return -1;
+ }
+ off += (size_t)r;
+
+ for (lineoff = 0; lineoff < off; lineoff += linelen) {
+ char *lf = strchr(&buf[lineoff], '\n');
+ if (!lf) {
+ memmove(&buf[0], &buf[lineoff], off -= lineoff);
+ goto read_again;
+ }
+ line = &buf[lineoff];
+ linelen = (size_t)(&lf[1] - line);
+ if (linelen > sizeof(" [stack]\n") - 1U)
+ if (!strcmp(&line[linelen - (sizeof(" [stack]\n") - 1U)], " [stack]\n"))
+ goto line_found;
+ }
+
+ }
+
+line_found:
+ close(fd);
+
+ *low = (uintptr_t)strtoumax(line, &line, 16);
+ *high = (uintptr_t)strtoumax(&line[1], &line, 16);
+ if (*low > *high) {
+ *low ^= *high;
+ *high ^= *low;
+ *low ^= *high;
+ }
+
+ return 0;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ return 0;
+}
+
+#endif