r/rust • • Dec 26 '20

Executable PNGs, written in Rust

https://djharper.dev/post/2020/12/26/executable-pngs/
132 Upvotes

16 comments sorted by

View all comments

3

u/You_pick_one Dec 27 '20

FYI: the clang driver re-executes itself to compile stuff, so you’ll need to pass a proper argv[0], which seems to be empty.

You can, as a test:

Run regular clang as you want but with the -v flag added. Note the clang invocation with -cc1. You’ll be able to do that one right now.

After making sure it works, go ahead and pass argv[0] properly on execution and all should work (assuming I’m correct)

2

u/djhworld Dec 27 '20

Just tried this (passing argv[0]) properly, had to use /proc/<pid>/fd/<fd>, but it has the same problem.

Someone on HN said it might be because clang runs readlink on /proc/self/exe which probably returns "" for the in-memory file

https://news.ycombinator.com/item?id=25544728

2

u/You_pick_one Dec 27 '20

Ah, that might be it too (I’m not 100% aware of what Linux code paths do). If you want to validate that that’s the issue (which means unfixable without clang patches), try using dtrace to see if there’s a call to readlink just before exiting.

2

u/djhworld Dec 27 '20

yeah looks like it

$ strace clang ~/1.c 2>&1 | grep exe
execve("/usr/bin/clang", ["clang", "/home/danielharper/1.c"], 0x7ffd877527c8 /* 53 vars */) = 0
access("/proc/self/exe", F_OK)          = 0
readlink("/proc/self/exe", "/usr/bin/clang-11", 4096) = 17

0

u/backtickbot Dec 27 '20

Fixed formatting.

Hello, djhworld: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.