Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat happens when you overwrite a memory-mapped executable?
    primarykey
    data
    text
    <p>Following the comments on one of my questions I'm intrigued to know what happens when one overwrites an executable. I need to check my understanding on the matter.</p> <p>Say I have <code>/usr/bin/myprog</code>. I run it and so the OS loads <code>/usr/bin/myprog</code>, probably via <a href="http://en.wikipedia.org/wiki/Memory-mapped_file#Common_uses" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Memory-mapped_file#Common_uses</a>.</p> <p>For whatever reason that process remains in memory and I decide actually I've fixed a bug and I overwrite <code>/usr/bin/myprog</code>.</p> <p>So, as far as I understand it:</p> <ul> <li>If an instance of <code>myprog</code> is already loaded and I replace the file from which <code>myprog</code> was already loaded, the instance of <code>myprog</code> is unmodified.</li> <li>If I run a new instance of <code>myprog</code> it will use the new code.</li> </ul> <p>Am I correct?</p> <p>However, according to the article on memory-mapped files, such a technique allows a developer to treat portions of a file as if they are physical memory.</p> <p>So I see a contradiction in how I understood things. If pages are truly only loaded in on demand, then assuming <code>myprog</code> is not 100% paged, this wikipedia article implies new pages will load from the file on disk, which has changed since the original image was loaded.</p> <p>However, I am pretty certain that my two compiled images would not be the same and that the relevant address offsets for each file are not identical. So, assuming this happens, the instruction pointer is going to get very lost... I am pretty certain an operating system does not load parts of two different images into memory as part of the same process.</p> <p>So how does the combination of memory-mapping/demand-paging work for the execution of programs, please? Would overwriting that file trigger a page fault on each of the executables' pages to ensure it is loaded in for the currently running process?</p> <p>I did a quick experiment with this:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;unistd.h&gt; int main(int argc, char** argv) { printf("Program resident..."); while(1) { printf("??? Just notifying you I'm still here...\n"); usleep(1000000); } return 0; } </code></pre> <p>And sure enough I could a) replace this executable whilst it was running and b) its output isn't changed.</p> <p>SO what is going on? I'd particularly appreciate any suggestions for stuff I can do to see what happens (Linux or Windows).</p> <p>Thanks all.</p> <p>Edit: question to which I was referring that sparked this question: <a href="https://stackoverflow.com/questions/4434302/linux-unattended-upgrades-without-reboot-what-kinds-of-problems-can-happen/4434361#4434361">Upgrades without reboot - what kinds of problems happen in practice?</a></p> <p>Also, I'm aware this doesn't specifically relate to programming, but the outcome of updating an executable. I am still interested, however, and I can't think of a better place to ask it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload