My hello LD_PRELOAD library segfaults when used with an already test and working non-rust LD_PRELOAD library

I have a segfault my libswisktrack.so ld_preload library when working with an existing libcpio_preload.so.

We have an existing libcpio_preload.so that is written in C and has been working.

I am building a new ld_preload library libwisktrack.so

The following segfaults

LD_PRELOAD=“/nobackup/sarvi/iosxr/platforms/common/thinxr/build/obj-x86-linux/libcpio_preload.so:/ws/sarvi-sjc/wisktrack/\${LIB}/libwisktrack.so” <anyhello world program>

The following does not segfault. The difference is the order of the libwisktrack.so and libcpio_preload.so

LD_PRELOAD=“/ws/sarvi-sjc/wisktrack/\${LIB}/libwisktrack.so:/nobackup/sarvi/iosxr/platforms/common/thinxr/build/obj-x86-linux/libcpio_preload.so” <anyhello world program>

libwisktrack.so has been stripped down to the following code.

#[link_section = ".init_array"]
pub static INITIALIZE: extern "C" fn() = crate::rust_ctor;

#[no_mangle]
pub extern "C" fn rust_ctor() {
    eprintln!("Hello World");
}

If a similar hello world libwisktrack.so is written in C, there is no crash.
If the order of LD_PRELOAD library is reversed, then there is no crash.

The following is hhow the strace ends

execve("/ws/sarvi-sjc/wisktrack/tests/testprog64", ["/ws/sarvi-sjc/wisktrack/tests/testprog64", "rea
dlink"], ["LD_PRELOAD=/nobackup/sarvi/iosxr/platforms/common/thinxr/build/obj-x86-linux/libcpio_prel
oad.so:/ws/sarvi-sjc/wisktrack/${LIB}/libwisktrack.so", "RUST_BACKTRACE=1", "TERM=xterm-256color", "
HOME=/users/sarvi", "PATH=/users/sarvi/.cargo/bin:/auto/binos-tools/bin:/router/bin:/usr/cisco/bin:/
usr/atria/bin:/usr/bin:/usr/local/bin:/usr/local/etc:/bin:/usr/X11R6/bin:/usr/sbin:/sbin:/usr/bin:/a
uto/nova-env/Lab/Labpatch/bin", "USER=sarvi", "WISK_TRACE=/nobackup/sarvi/iosxr/wisktrace.log", "WIS
K_TRACK=", "WISK_CONFIG=", "WISK_WSROOT=/nobackup/sarvi/iosxr"]) = 0
brk(NULL)                               = 0x103b000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c81469000
open("/nobackup/sarvi/iosxr/platforms/common/thinxr/build/obj-x86-linux/libcpio_preload.so", O_RDONL
Y|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\260\f\0\0\0\0\0\0@\0\0\0\0\0\0\0\320,\0\0\0\
0\0\0\0\0\0\0@\0008\0\6\0@\0\32\0\31\0\1\0\0\0\5\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0
\0D(\0\0\0\0\0\0D(\0\0\0\0\0\0\0\20\0\0\0\0\0\0\1\0\0\0\6\0\0\0H(\0\0\0\0\0\0H8\0\0\0\0\0\0H8\0\0\0\
0\0\0,\3\0\0\0\0\0\0\250\3\0\0\0\0\0\0\0\20\0\0\0\0\0\0\2\0\0\0\6\0\0\0p(\0\0\0\0\0\0p8\0\0\0\0\0\0p
8\0\0\0\0\0\0\0\2\0\0\0\0\0\0\0\2\0\0\0\0\0\0\10\0\0\0\0\0\0\0\4\0\0\0\4\0\0\0\220\1\0\0\0\0\0\0\220
\1\0\0\0\0\0\0\220\1\0\0\0\0\0\0$\0\0\0\0\0\0\0$\0\0\0\0\0\0\0\4\0\0\0\0\0\0\0P\345td\4\0\0\0@&\0\0\
0\0\0\0@&\0\0\0\0\0\0@&\0\0\0\0\0\0T\0\0\0\0\0\0\0T\0\0\0\0\0\0\0\4\0\0\0\0\0\0\0Q\345td\6\0\0\0\0\0
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\10\0\0\0\0\0\0\0\4\0\0\
0\24\0\0\0\3\0\0\0GNU\0004$\326\346^\205&\331\327\200\7MTl\371\346\315\7hW\0\0\0\0\3\0\0\0\34\0\0\0\
1\0\0\0\6\0\0\0\250\320a\t\2\6H\t\34\0\0\0\37\0\0\0$\0\0\0BE\325\354\366#\316 \273\343\222|Z\232;\17
\330qX\34P\265\250\20\262\25\232|\271\215\361\16\352\323\357\16`\230\357\22M\221Z\272\0\0\0\0\0\0\0\
0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\3\0\t\0\20\v\0\0\0\0\0\0\0\0\0\0\0\0\0\0\217\0\0\0\22\0\0\
0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0006\1\0\0\22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\37\1\0\0\22\0\0\
0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\34\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\226\0\0\0\22\0\0\0
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\274\0\0\0\22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\210\0\0\0\22\0\0
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\206\0\0\0\22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\246\0\0\0\22\0
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\260\0\0\0\22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\3\1\0\0\22\0
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 832) = 832
fstat(3, {st_dev=makedev(253, 3), st_ino=51388685, st_mode=S_IFREG|0755, st_nlink=1, st_uid=19375, s
t_gid=25, st_blksize=4096, st_blocks=32, st_size=13136, st_atime=2020/12/30-14:48:10.109712353, st_m
time=2020/12/30-14:46:47.683102408, st_ctime=2020/12/30-14:46:48.034109262}) = 0
mmap(NULL, 15344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c81465000
mmap(0x7f7c81468000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0
x7f7c81468000
close(3)                                = 0
readlink("/proc/self/exe", "/ws/sarvi-sjc/wisktrack/tests/testprog64", 4096) = 40
open("/ws/sarvi-sjc/wisktrack/lib64/libwisktrack.so", 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\240\265\7\0\0\0\0\0@\0\0\0\0\0\0\0P\17\202\2
\0\0\0\0\0\0\0\0@\0008\0\10\0@\0)\0(\0\1\0\0\0\5\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0
\0h\30\\\0\0\0\0\0h\30\\\0\0\0\0\0\0\0 \0\0\0\0\0\1\0\0\0\6\0\0\0`'\\\0\0\0\0\0`'|\0\0\0\0\0`'|\0\0\
0\0\0Hi\4\0\0\0\0\0\260o\4\0\0\0\0\0\0\0 \0\0\0\0\0\2\0\0\0\6\0\0\0@?_\0\0\0\0\0@?\177\0\0\0\0\0@?\1
77\0\0\0\0\0@\2\0\0\0\0\0\0@\2\0\0\0\0\0\0\10\0\0\0\0\0\0\0\4\0\0\0\4\0\0\0\0\2\0\0\0\0\0\0\0\2\0\0\0\0\0\0\0\2\0\0\0\0\0\0$\0\0\0\0\0\0\0$\0\0\0\0\0\0\0\4\0\0\0\0\0\0\0\7\0\0\0\4\0\0\0`'\\\0\0\0\0\0`'|\0\0\0\0\0`'|\0\0\0\0\0\2\0\0\0\0\0\0\0\370\2\0\0\0\0\0\0 \0\0\0\0\0\0\0P\345td\4\0\0\0\214\236N\0\0\0\0\0\214\236N\0\0\0\0\0\214\236N\0\0\0\0\0\224\202\2\0\0\0\0\0\224\202\2\0\0\0\0\0\4\0\0\0\0\0\0\0Q\345td\6\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\20\0\0\0\0\0\0\0R\345td\4\0\0\0`'\\\0\0\0\0\0`'|\0\0\0\0\0`'|\0\0\0\0\0\240h\4\0\0\0\0\0\240h\4\0\0\0\0\0\1\0\0\0\0\0\0\0\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\272E@\202\277\214A\334I\325AQh\326\16I\2741\274\f\0\0\0\0\21\0\0\0V\0\0\0\4\0\0\0\10\0\0\0\0\10\0\204\0\4 \16B\30\312M\3\t\2108\301\5pA\220\0\10\200 \t\21\200\250\200\300\0V\0\0\0W\0\0\0\\\0\0\0_\0\0\0a\0\0\0\0\0\0\0c\0\0\0e\0\0\0f\0\0\0g\0\0\0i\0\0\0j\0\0\0\0\0\0\0k\0\0\0q\0\0\0\0\0\0\0s\0\0\0\321\237,\374h[v\vv\327\233|\320H3\2224\232\300\201\321_9\17\356\vQ\371`\274\273:\263\25\232||\213s\17\213\344\356\34L\221Z\272\347e\204\272\304\237,\374\247\277\213$W\323\3\266k\6\347\214\366#\316 a3f\17\207\236,\374\v\37?]`\230\357\22\206\200(\20@!D\273Z\232;\17\324\267>\17\231H\222\377\226\224\7\375;\373\237|V3f\17*9\214u{\236,\374\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0a\1\0\0\22\0\0\0", 832) = 832
fstat(3, {st_dev=makedev(0, 55), st_ino=177719567, st_mode=S_IFREG|0755, st_nlink=2, st_uid=19375, st_gid=25, st_blksize=32768, st_blocks=82528, st_size=42080656, st_atime=2020/12/30-16:56:57.466090000, st_mtime=2020/12/30-16:56:51.235143000, st_ctime=2020/12/30-16:56:51.598101000}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c81464000
mmap(NULL, 8427280, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c80a3f000
mprotect(0x7f7c81001000, 2097152, PROT_NONE) = 0
mmap(0x7f7c81201000, 294912, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5c2000) = 0x7f7c81201000
close(3)                                = 0
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_dev=makedev(253, 0), st_ino=33595555, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=496, st_size=253881, st_atime=2020/12/30-11:54:30.116897437, st_mtime=2020/09/23-12:52:35.432225297, st_ctime=2020/09/23-12:52:35.432225297}) = 0
mmap(NULL, 253881, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f7c81426000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
.............................
............................
fstat(3, {st_dev=makedev(253, 0), st_ino=50413653, st_mode=S_IFREG|0755, st_nlink=1, st_uid=0, st_gi
d=0, st_blksize=4096, st_blocks=88, st_size=43712, st_atime=2020/12/30-11:38:30.250166206, st_mtime=
2019/04/30-08:24:35, st_ctime=2020/06/01-19:15:59.387341147}) = 0
mmap(NULL, 2128952, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c7ff4b000
mprotect(0x7f7c7ff52000, 2093056, PROT_NONE) = 0
mmap(0x7f7c80151000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0
x7f7c80151000
close(3)                                = 0
open("/lib64/libdl.so.2", 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\0P\16\0\0\0\0\0\0@\0\0\0\0\0\0\0000C\0\0\0\0\0
\0\0\0\0\0@\0008\0\7\0@\0 \0\37\0\1\0\0\0\5\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\30
4\37\0\0\0\0\0\0\304\37\0\0\0\0\0\0\0\0 \0\0\0\0\0\1\0\0\0\6\0\0\0X-\0\0\0\0\0\0X- \0\0\0\0\0X- \0\0
\0\0\0@\3\0\0\0\0\0\0\330\3\0\0\0\0\0\0\0\0 \0\0\0\0\0\2\0\0\0\6\0\0\0\210-\0\0\0\0\0\0\210- \0\0\0\
0\0\210- \0\0\0\0\0\20\2\0\0\0\0\0\0\20\2\0\0\0\0\0\0\10\0\0\0\0\0\0\0\4\0\0\0\4\0\0\0\310\1\0\0\0\0
\0\0\310\1\0\0\0\0\0\0\310\1\0\0\0\0\0\0D\0\0\0\0\0\0\0D\0\0\0\0\0\0\0\4\0\0\0\0\0\0\0P\345td\4\0\0\
0\0\32\0\0\0\0\0\0\0\32\0\0\0\0\0\0\0\32\0\0\0\0\0\0\274\0\0\0\0\0\0\0\274\0\0\0\0\0\0\0\4\0\0\0\0\0
\0\0Q\345td\6\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
20\0\0\0\0\0\0\0R\345td\4\0\0\0X-\0\0\0\0\0\0X- \0\0\0\0\0X- \0\0\0\0\0\250\2\0\0\0\0\0\0\250\2\0\0\
0\0\0\0\1\0\0\0\0\0\0\0\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\261o\311\311\22\25\1\1\336\243\341N_\317\331\3
51\367\36ZE\4\0\0\0\20\0\0\0\1\0\0\0GNU\0\0\0\0\0\2\0\0\0\6\0\0\0 \0\0\0\0\0\0\0\33\0\0\0\32\0\0\0\2
\0\0\0\7\0\0\0\230\2\21\0\200H\0\4\22\0\0@\203(\10\236\32\0\0\0\0\0\0\0\33\0\0\0\0\0\0\0\0\0\0\0\34\0\0\0\0\0\0\0\35\0\0\0\0\0\0\0\36\0\0\0\0\0\0\0\37\0\0\0\0\0\0\0 \0\0\0\"\0\0\0#\0\0\0%\0\0\0&\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'\0\0\0\0\0\0\0\0\0\0\0\353\26\251\30a\257\0\371\301S\200\30\273\25sB\257\304M\17\221!\374\370\6\2\4\371\3733\373\17\371\31sB\372\31sB\225\263_\31\177\236\320\30a\242\222\6\5\350\7\371\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0=\1\0\0\22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\375\0\0\0\22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\337\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\20\0\0\0 \0\0\0", 832) = 832
fstat(3, {st_dev=makedev(253, 0), st_ino=50385584, st_mode=S_IFREG|0755, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=40, st_size=19248, st_atime=2020/12/30-11:38:30.239165965, st_mtime=2019/04/30-08:24:35, st_ctime=2020/06/01-19:15:59.328340803}) = 0
mmap(NULL, 2109744, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c7fd47000
mprotect(0x7f7c7fd49000, 2097152, PROT_NONE) = 0
mmap(0x7f7c7ff49000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f7c7ff49000
close(3)                                = 0
open("/lib64/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\220*\0\0\0\0\0\0@\0\0\0\0\0\0\0\10S\1\0\0\0\0\0\0\0\0\0@\0008\0\7\0@\0\37\0\36\0\1\0\0\0\5\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0TL\1\0\0\0\0\0TL\1\0\0\0\0\0\0\0 \0\0\0\0\0\1\0\0\0\6\0\0\0\260M\1\0\0\0\0\0\260M!\0\0\0\0\0\260M!\0\0\0\0\0\300\3\0\0\0\0\0\0P\6\0\0\0\0\0\0\0\0 \0\0\0\0\0\2\0\0\0\6\0\0\0\330M\1\0\0\0\0\0\330M!\0\0\0\0\0\330M!\0\0\0\0\0\360\1\0\0\0\0\0\0\360\1\0\0\0\0\0\0\10\0\0\0\0\0\0\0\4\0\0\0\4\0\0\0\310\1\0\0\0\0\0\0\310\1\0\0\0\0\0\0\310\1\0\0\0\0\0\0$\0\0\0\0\0\0\0$\0\0\0\0\0\0\0\4\0\0\0\0\0\0\0P\345td\4\0\0\0\304,\1\0\0\0\0\0\304,\1\0\0\0\0\0\304,\1\0\0\0\0\0D\5\0\0\0\0\0\0D\5\0\0\0\0\0\0\4\0\0\0\0\0\0\0Q\345td\6\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\20\0\0\0\0\0\0\0R\345td\4\0\0\0\260M\1\0\0\0\0\0\260M!\0\0\0\0\0\260M!\0\0\0\0\0P\2\0\0\0\0\0\0P\2\0\0\0\0\0\0\1\0\0\0\0\0\0\0\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\332\300\27\237EU\256\376\311\351tv \30\2\375 \300>\305\0\0\0\0\203\0\0\0\26\0\0\0\20\0\0\0\n\0\0\0\23\34\3\30\4$\0\1\1\201\0T\4\6\210\20\200\204\0\10\0\10\5\200\2\0@\20D\20\0\266\2\200\320 \301\0\220\2024\1\0\4\10(@\2H\1\v \320\1\2(\0235\4`l\322\0!\3p`@\322\200\10`\0@\1\0\200\1\0\0\2$\0\10\21\2\10H\300\1$\t\21(  \10C\v\246\202H\t\10\10\3D\f\n\34\22O\306\1\207\va\204\226\315\4\302\305\f\242\333\16\314\26\0\0\0\27\0\0\0\32\0\0\0\0\0\0\0\35\0\0\0\0\0\0\0\37\0\0\0 \0\0\0\"\0\0\0%\0\0\0'\0\0\0\0\0\0\0(\0\0\0*\0\0\0+\0\0\0.\0\0\0000\0\0\0003\0\0\0004\0\0\0007\0\0\0\0\0\0\0008\0\0\0009\0\0\0<\0\0\0=\0\0\0?\0\0\0@\0\0\0A\0\0\0B\0\0\0C\0\0\0E\0\0\0\0\0\0\0F\0\0\0\0\0\0\0G\0\0\0\0\0\0\0\0\0\0\0H\0\0\0I\0\0\0K\0\0\0L\0\0\0M\0\0\0\0\0\0\0\0\0\0\0O\0\0\0Q\0\0\0\0\0\0\0\0\0\0\0", 832) = 832
fstat(3, {st_dev=makedev(253, 0), st_ino=50405815, st_mode=S_IFREG|0755, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=176, st_size=88776, st_atime=2020/12/30-11:38:31.003182769, st_mtime=2019/02/26-09:34:11, st_ctime=2020/06/01-19:09:21.949915863}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c81424000
mmap(NULL, 2184192, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7c7fb31000
mprotect(0x7f7c7fb46000, 2093056, PROT_NONE) = 0
mmap(0x7f7c7fd45000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14000) = 0x7f7c7fd45000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c81423000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c81421000
arch_prctl(ARCH_SET_FS, 0x7f7c81421a40) = 0
mprotect(0x7f7c80a34000, 16384, PROT_READ) = 0
mprotect(0x7f7c7fd45000, 4096, PROT_READ) = 0
mprotect(0x7f7c7ff49000, 4096, PROT_READ) = 0
mprotect(0x7f7c80369000, 4096, PROT_READ) = 0
mprotect(0x7f7c80151000, 4096, PROT_READ) = 0
mprotect(0x7f7c8066f000, 4096, PROT_READ) = 0
mprotect(0x7f7c81201000, 290816, PROT_READ) = 0
mprotect(0x601000, 4096, PROT_READ)     = 0
mprotect(0x7f7c8146a000, 4096, PROT_READ) = 0
munmap(0x7f7c81426000, 253881)          = 0
set_tid_address(0x7f7c81421d10)         = 32504
set_robust_list(0x7f7c81421d20, 24)     = 0
rt_sigaction(SIGRTMIN, {0x7f7c80159860, [], SA_RESTORER|SA_SIGINFO, 0x7f7c80162630}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {0x7f7c801598f0, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x7f7c80162630}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
brk(NULL)                               = 0x103b000
brk(0x105c000)                          = 0x105c000
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0} ---
+++ killed by SIGSEGV (core dumped) +++
(END)

Any advice on what could be wrong or how to debug this further.

Can you get a backtrace using a debugger?

Usually when I run into a segfault I'll start it under gdbor lldb and let the program until it crashes, then use the backtrace command to find which code triggered the segfault.

There is no backtrace

(gdb) exec-file /ws/sarvi-sjc/wisktrack/tests/testprog64
(gdb) run
Starting program: /ws/sarvi-sjc/wisktrack/tests/testprog64 readlink
During startup program terminated with signal SIGSEGV, Segmentation fault.
(gdb) bt
No stack.
(gdb) show environment LD_PRELOAD
LD_PRELOAD = /nobackup/sarvi/iosxr/platforms/common/thinxr/build/obj-x86-linux/libcpio_preload.so:/ws/sarvi-sjc/wisktrack/${LIB}/libwisktrack.so
(gdb) show args
Argument list to give program being debugged when it is started is "readlink".
(gdb) 
(gdb) exec-file /ws/sarvi-sjc/wisktrack/tests/testprog64
(gdb) run
Starting program: /ws/sarvi-sjc/wisktrack/tests/testprog64 readlink
During startup program terminated with signal SIGSEGV, Segmentation fault.
(gdb) bt
No stack.
(gdb) show environment LD_PRELOAD
LD_PRELOAD = /nobackup/sarvi/iosxr/platforms/common/thinxr/build/obj-x86-linux/libcpio_preload.so:/ws/sarvi-sjc/wisktrack/${LIB}/libwisktrack.so
(gdb) show args
Argument list to give program being debugged when it is started is "readlink".
(gdb) 

The segfault seems to be happening in the library constructor of libwisktrack.so or earlier.
If I dont do eprintln!() in the constructor, the crash doesnt happen, when I replace it with a simple assignment statement.

That said, gdb seems to crashig early and without backtrace.

It almost, looks to me like there is something about the RUST compiled code running in the library constructor context that is causing the crash. If wrote a comparable "Hello World" LD_PRELOAD code in C, the crash doesnt happen.

Do you think it is worth taking this thread to rust internals forum for some extra eyes on the problem?

Rusts stdio is initialized with a lock and buffer. I expect your code is jumping in before that can happen. (Don't have the internal knowledge to say where it happens, without spending time reading source.)

Here is an interesting data point. The following works. It uses direct syscalls to write to a fd

pub fn write(fd: usize, buf: &[u8]) -> Result<usize, &'static str> {
    let x = unsafe { libc::syscall(libc::SYS_write, fd, buf.as_ptr() as usize, buf.len()) };
    if x < 0 {
        return Err("Error Writing to FD");
    }
    Ok(x as usize)
}
#[ctor]
fn cfoo() {
    let x = format!("Hello World: {}\n", 1);
    write(0, x.as_bytes()).unwrap();
//    let mut f:fs::File = unsafe { FromRawFd::from_raw_fd(0) };
}

Just uncommenting that one line segfaults. All the line does is create a File object crashes/segfaults.

Basically just turning an File descriptor number to a file object in the constructor crashes, when the LD_PRELOAD happens along with the other libcpio_preload.so

mprotect(0x7feacd1bd000, 12288, PROT_READ) = 0
mprotect(0x601000, 4096, PROT_READ)     = 0
mprotect(0x7feacd3e2000, 4096, PROT_READ) = 0
munmap(0x7feacd39e000, 253881)          = 0
set_tid_address(0x7feacd399a90)         = 43339
set_robust_list(0x7feacd399aa0, 24)     = 0
rt_sigaction(SIGRTMIN, {0x7feacc698860, [], SA_RESTORER|SA_SIGINFO, 0x7feacc6a1630}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {0x7feacc6988f0, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x7feacc6a1630}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
brk(NULL)                               = 0x23b8000
brk(0x23d9000)                          = 0x23d9000
write(0, "Hello World: 1\n", 15)        = 15
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0} ---
+++ killed by SIGSEGV (core dumped) +++

Are you using dylib or cdylib as crate type?

[lib]
crate-type = ["cdylib"]

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.