Date: Fri, 21 Mar 2003 11:55:20 -0500 From: "Paco Hope" <paco@cigital.com> To: <hackers@freebsd.org> Subject: ld.so and hard links Message-ID: <3E7B43F8.6070405@cigital.com>
next in thread | raw e-mail | index | archive | help
Hi.
This is a really specific, technical question (and I think it's
fascinating to those of us who don't know the answer) about how the text
(code) segment of a program gets loaded into memory. I'm hoping hackers
is the right place for this. If not, please forgive and suggest another
venue.
Here's my baseline assumption. If I'm wrong here, I'm only going to get
wronger as I go:
If I have two different programs that both use a shared library,
libfoo.so, the system memory maps the object code they need into the
processes' address spaces. There's only one copy of libfoo.so in memory,
and the two processes each have handles into it (or maybe just to the
pieces of it that they use?).
Step 2 of my question. This gets closer to my real query:
Ok now consider a hard link (not a symlink) from libfoo.so to libbar.so.
One inode, two directory entries. Consider my two programs again, one
linked against libfoo.so and the other linked against libbar.so. When
they run, how many copies of the lib{foo,bar}.so object code are in RAM?
My current hypothesis is 1. Isn't it mmapped off the disk? The inode
matters, not the file name, right? With me so far? Great.
Now consider jail(8). Let's say I have two jail environments (If you
think I mean chroot here, go read jail(8), it's not the same. I'm
assuming folks on hackers know jail.). To make my first jail, I make a
copy of the FreeBSD stuff that my jail needs. To make the second jail, I
create a directory hiearachy, but I *hard link* all the binaries and
libraries and stuff to the same inodes that the first jail uses. Is that
clear? Let's pick a specific example: 'ln /jail1/usr/sbin/sshd
/jail2/usr/sbin/sshd'. Now, sshd uses /usr/lib/libz.so.2. In my example,
I have (effectively) done 'ln /jail1/usr/lib/libz.so.2
/jail2/usr/lib/libz.so.2'. These are not symlinks, so this works across
jails. Now I launch both jails. Two sshd processes are running, one in
each jail.
Now the $64K question: How many instances of, for example, the libz.so.2
object code are in memory? Did my use of jail(8) make any difference? My
intuition is that only one copy of the code is in memory for the same
reason as in step 2 above. This is the real question I am interested in.
I'm also interested in a broader question. Consider instantiating many
jails this way--say 50 or 100 all hard linked to the same base set of
files. Can we characterize in some general hand-waving way how much
memory (RAM) I would save doing it this way as opposed to the naive way
of 50 or 100 copies of the files? I am assuming that if I have 50 copies
of the files and I run 50 processes in 50 jails, then I will use more
RAM than if I had 50 hard links to the same inode and ran 50 processes
in 50 jails from that one inode. The naive copy method will use more
RAM, but not 50 times more than the hard linking way.
Thank you to any who respond. I hope I'm not completely out to lunch on
this.
Regards,
Paco
--
Consultant, Cigital, Inc.
http://www.cigital.com/
----------------------------------------------------------------------------
This electronic message transmission contains information that may be
confidential or privileged. The information contained herein is intended
solely for the recipient and use by any other party is not authorized. If
you are not the intended recipient (or otherwise authorized to receive this
message by the intended recipient), any disclosure, copying, distribution or
use of the contents of the information is prohibited. If you have received
this electronic message transmission in error, please contact the sender by
reply email and delete all copies of this message. Cigital, Inc. accepts no
responsibility for any loss or damage resulting directly or indirectly from
the use of this email or its contents.
Thank You.
----------------------------------------------------------------------------
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3E7B43F8.6070405>
