# Who is doing read("/proc/self/maps", 1024) at startup?

**URL:** <https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348>\
**Category:** Uncategorized\
**Created:** [September 8, 2022, 6:40pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348 "2022-09-08T18:40:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![adobriyan](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@adobriyan](https://internals.rust-lang.org/u/adobriyan)\
**Post date:** [September 8, 2022, 6:40pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/1 "2022-09-08T18:40:04Z")

</div>

To my embarrassment I can't find the source of these system calls

```rust
openat(AT_FDCWD, "/proc/self/maps", O_RDONLY|O_CLOEXEC) = 3
newfstatat(3, "", {st_mode=S_IFREG|0444, st_size=0, ...}, AT_EMPTY_PATH) = 0
read(3, "5641f7469000-5641f746f000 r--p 0"..., 1024) = 1024
read(3, "79cdf5000-7fd79cdf9000 r--p 001f"..., 1024) = 1024
read(3, "0000000 00:00 0 \n7fd79ce59000-7f"..., 1024) = 832
close(3)

```

Regardless, these reads are inefficient, they should be PAGE\_SIZE. Linux /proc implementation allocates internal buffer and "renders" virtual file contents there, and then serves short reads from there so it doesn't make sense to read less than internal buffer size, it is just waste of system calls.

See [seq\_file.c « fs - kernel/git/torvalds/linux.git - Linux kernel source tree](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/seq_file.c?h=v6.0-rc4#n90)

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [September 8, 2022, 7:15pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/2 "2022-09-08T19:15:20Z")

</div>

Which target is this - `*-linux-gnu`?   
Also, which program - something simple like hello-world?

Neither Rust library nor compiler read `/proc/self/maps`.   
This looks more like something glibc would do.

---

<div class="post-metadata">

**Author:** ![adobriyan](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@adobriyan](https://internals.rust-lang.org/u/adobriyan)\
**Post date:** [September 8, 2022, 7:24pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/3 "2022-09-08T19:24:01Z")

</div>

Yes this is Linux.

I found \_\_pthread\_getattr\_np() which does fopen ("/proc/self/maps", "rce"); immediately. And getrlimit and sched\_getaffinity.

Thanks!

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [September 8, 2022, 7:33pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/4 "2022-09-08T19:33:40Z")

</div>

That is probably a consequence of Rust init calling `pthread_attr_getstack` -- or rather `pthread_getattr_np` in preparation for that.

> <https://github.com/rust-lang/rust/blob/87788097b776f8e3662f76627944230684b671bd/library/std/src/sys/unix/thread.rs#L697-L704>

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [September 8, 2022, 7:38pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/5 "2022-09-08T19:38:13Z")

</div>

> [@petrochenkov](#):
>
> Neither Rust library nor compiler read `/proc/self/maps`. This looks more like something glibc would do.

No, it's definitely Rust's runtime doing this, _somehow_.

I have observed this behavior also. It happens with a program as simple as `fn main() {}`.

```sh
$ printf 'fn main() {}\n' > test-rust.rs
$ rustc --version
rustc 1.59.0
$ rustc -g -O test-rust.rs -o test-rust
$ strace -e trace=openat,read ./test-rust
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 832) = 832
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832
openat(AT_FDCWD, "/proc/self/maps", O_RDONLY|O_CLOEXEC) = 3
read(3, "561342b1d000-561342b23000 r--p 0"..., 1024) = 1024
read(3, "0000-7f33e4d24000 r--p 00214000 "..., 1024) = 1024
read(3, "linux-x86-64.so.2\n7f33e4d89000-7"..., 1024) = 823
+++ exited with 0 +++

```

Compare what happens with the equivalent C program:

```sh
$ printf 'int main(void) { return 0; }\n' > test-c.c
$ gcc -O2 -g test-c.c -Wl,--no-as-needed -lgcc_s -o test-c
$ strace -e trace=openat,read ./test-c
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 832) = 832
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832
+++ exited with 0 +++

```

So we can rule out both glibc and libgcc\_s (the stack unwinder, iirc).

---

<div class="post-metadata">

**Author:** ![adobriyan](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@adobriyan](https://internals.rust-lang.org/u/adobriyan)\
**Post date:** [September 8, 2022, 8:07pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/6 "2022-09-08T20:07:41Z")

</div>

> [@cuviper](#):
>
> Rust init calling `pthread_attr_getstack` -- or rather `pthread_getattr_np` in preparation for that.

Yes.

This is what glibc does: [https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread\_getattr\_np.c;h=9c5b73b452ab0faa70f8e734c6c725e98f2fa7e3;hb=c804cd1c00adde061ca51711f63068c103e94eef#l85](https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=9c5b73b452ab0faa70f8e734c6c725e98f2fa7e3;hb=c804cd1c00adde061ca51711f63068c103e94eef#l85)

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [September 8, 2022, 8:10pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/7 "2022-09-08T20:10:34Z")

</div>

> [@zackw](#):
>
> No, it's definitely Rust's runtime doing this, _somehow_.

It's not really "somehow", rustc runs `lang_start` before `main` and that initializes thread stuff.

> <https://github.com/rust-lang/rust/blob/3810d4a3680d825448f5034262cc6ad63586b02e/library/std/src/rt.rs#L95-L107>

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [September 8, 2022, 8:22pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/8 "2022-09-08T20:22:25Z")

</div>

Several other posts happened while I was failing to persuade GDB to let me set breakpoints in there. It is clear to me now what is going on.

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [September 9, 2022, 9:06pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/9 "2022-09-09T21:06:39Z")

</div>

I'm just gonna leave this here: [RFC PATCH: Don't use /proc/self/maps to calculate size of initial thread stack](https://sourceware.org/pipermail/libc-alpha/2022-September/141932.html)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [December 8, 2022, 9:07pm UTC](https://internals.rust-lang.org/t/who-is-doing-read-proc-self-maps-1024-at-startup/17348/10 "2022-12-08T21:07:00Z")

</div>

Closed on December 8, 2022, 9:07pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
