0

I have written a program using OPenSSL shared library and make another copy (Program2 is duplicate copy of program1) . Just to check whether the shared library function is loaded only once , I print the address from both Program1 and program2. To my surprise I got same address sometimes, but on other time I got different address .

As per my understanding , shared library functions loaded into shared memory and only one copy is loaded. Why I am getting two different address which signifies that shared functions are loaded into two different memory location.

Following code I am using when getting same address

CASE 1: Program 1:

#include <openssl/aes.h>

int main(int argc, char ** argv)
{
printf("Address of AES_cbc_encrypt =%p\n",&AES_cbc_encrypt);

while(1) {} 

......

}

Address of AES_cbc_encrypt =0x400600

Program 2:

   #include <openssl/aes.h>

    int main(int argc, char ** argv)
    {
    printf("Address of AES_cbc_encrypt =%p\n",&AES_cbc_encrypt);

    while(1) {} 

    ......

    }

Address of AES_cbc_encrypt =0x400600

CASE2 : When I am getting two different address for shared library function

Program 1:

 #include <openssl/aes.h>

    int main(int argc, char ** argv)
    {
    printf("Address of AES_cbc_encrypt =%p\n",&AES_cbc_encrypt);

    while(1) {} 

    ......

    }

Address of AES_cbc_encrypt =0x400840

Program 2:

   #include <openssl/aes.h>

    int main(int argc, char ** argv)
    {
    printf("Address of AES_cbc_encrypt =%p\n",&AES_cbc_encrypt);

// open function makes the difference  
fd = open(argv[1], O_RDONLY);

    if (fd <= 0)
    {
        strerror(errno);
        exit(0);
    }

    while(1) {} 

    ......

    }

Address of AES_cbc_encrypt =0x4007f0

In case 2 , I am not getting same address for shared library function . What is the reason ? How Can I make sure both shared library function loaded into same memory location.

I am using GCC under Fedora 19.

EDIT 1 :

As per both comment/answer of Paul and John, the above addresses are not Physical address, they are Virtual addresses. So I try to convert the virtual address to physical address ( I use the code of this tool Capturing Process Memory Usage Under Linux) to check whether they are same , but I am getting two different physical addresses . Am I doing something wrong ? Is there some other way to make it forcefully load the shared library into same physical memory.

Thanks in advance.

bholanath
  • 1,699
  • 1
  • 22
  • 40

1 Answers1

1

Your operating system takes care of this for you, and you do not need to worry about it. However, if you are still worried about it, consult the "SHR" (Shared) column in top to see how much memory is being shared between processes.

Also consider that "virtual memory" (do a web search) means that just because two different processes are using two different-valued pointers to refer to something, does not mean that the operating system can't just map them to the same backing storage behind the scenes. In fact that will be typically what happens: the OS will load the shared library once, and map it in two different base addresses in two processes, and there is no functional difference to you, so long as you don't actually care about these virtual address values (which you shouldn't).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • @ John, this means what I am getting is process virtual address space, not physical address . – bholanath Feb 03 '14 at 13:20
  • @ John, if I am not wrong, then if I convert the virtual address to physical address both will point to same physical memory address as well as same cache set . – bholanath Feb 03 '14 at 13:28
  • 1
    Yeah, that's about right. If you want to map your virtual addresses to physical ones, see this question: http://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li – John Zwinck Feb 03 '14 at 14:56
  • I have checked with the code of the tool , but it is giving two different physical address. – bholanath Feb 03 '14 at 15:20