Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Last active December 21, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanorelli/5204d92d746b51c312cb9f04ed15add3 to your computer and use it in GitHub Desktop.
Save jordanorelli/5204d92d746b51c312cb9f04ed15add3 to your computer and use it in GitHub Desktop.
vagrant@vagrant[0] ~: cat pwd.zig
const std = @import("std");
const os = std.os;
const print = std.debug.print;
pub fn main() !void {
// I think this is entirely on the stack?
var buf: [4096]u8 = undefined;
// use that fixed array we put on the stack as
// the backing storage for a bump allocator
var fba = std.heap.FixedBufferAllocator.init(&buf);
var allocator = &fba.allocator;
// honestly not sure if I even need to bother with
// this if I already had it on the stack. Maybe
// there's a more idiomatic way to get a slice from
// an array?
const cwd = try allocator.alloc(u8, 4096);
defer allocator.free(cwd);
const z = try os.getcwd(cwd);
print("{}\n", .{z});
}
vagrant@vagrant[0] ~: zig build-exe pwd.zig
# the interesting thing to notice here is that it never calls mmap
vagrant@vagrant[0] ~: sudo strace ./pwd
execve("./pwd", ["./pwd"], [/* 15 vars */]) = 0
arch_prctl(ARCH_SET_FS, 0x23a008) = 0
rt_sigaction(SIGSEGV, {0x22c9e0, [], SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, 0x2048c0}, NULL, 8) = 0
rt_sigaction(SIGILL, {0x22c9e0, [], SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, 0x2048c0}, NULL, 8) = 0
rt_sigaction(SIGBUS, {0x22c9e0, [], SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, 0x2048c0}, NULL, 8) = 0
getcwd("/home/vagrant", 4096) = 14
write(2, "/home/vagrant", 13/home/vagrant) = 13
write(2, "\n", 1
) = 1
exit_group(0) = ?
+++ exited with 0 +++
# here's pwd from coreutils for contrast
vagrant@vagrant[0] ~: sudo strace pwd
execve("/bin/pwd", ["pwd"], [/* 15 vars */]) = 0
brk(NULL) = 0x24fc000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=80230, ...}) = 0
mmap(NULL, 80230, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f024744a000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/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\t\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0247449000
mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f0246e6f000
mprotect(0x7f024702f000, 2097152, PROT_NONE) = 0
mmap(0x7f024722f000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f024722f000
mmap(0x7f0247235000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f0247235000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0247448000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0247447000
arch_prctl(ARCH_SET_FS, 0x7f0247448700) = 0
mprotect(0x7f024722f000, 16384, PROT_READ) = 0
mprotect(0x606000, 4096, PROT_READ) = 0
mprotect(0x7f024745e000, 4096, PROT_READ) = 0
munmap(0x7f024744a000, 80230) = 0
brk(NULL) = 0x24fc000
brk(0x251d000) = 0x251d000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2981280, ...}) = 0
mmap(NULL, 2981280, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f0246b97000
close(3) = 0
getcwd("/home/vagrant", 4096) = 14
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
write(1, "/home/vagrant\n", 14/home/vagrant
) = 14
close(1) = 0
close(2) = 0
exit_group(0) = ?
+++ exited with 0 +++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment