<p>Whilst working on a recent project, I was visited by a customer QA representitive, who asked me a question that I hadn't really considered before:</p>

<blockquote>
  <p>How do you know that the compiler you are using generates machine code that matches the c code's functionality exactly and that the compiler is fully deterministic?</p>
</blockquote>

<p>To this question I had absolutely no reply as I have always taken the compiler for granted. It takes in code and spews out machine code. How can I go about and test that the compiler isn't actually adding functionality that I haven't asked it for? or even more dangerously implementing code in a slightly different manner to that which I expect?</p>

<p>I am aware that this is perhapse not really an issue for everyone, and indeed the answer might just be... "you're over a barrel and deal with it". However, when working in an embedded environment, you trust your compiler implicitly. How can I prove to myself and QA that I am right in doing so?</p>

[{'answer_id': 60551, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Try unit testing.</p>\n\n<p>If that's not enough, use different compilers and compare the results of your unit tests. Compare strace outputs, run your tests in a VM, keep a log of disk and network I/O, then compare those.</p>\n\n<p>Or propose to write your own compiler and tell them what it's going to cost.</p>\n"}, {'answer_id': 60553, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 2, 'selected': False, 'text': "<p>It all boils down to trust. Does your customer trust any compiler? Use that, or at least compare output code between yours and theirs. </p>\n\n<p>If they don't trust any, is there a reference implementation for the language? Could you convince them to trust it? Then compare yours against the reference or use the reference.</p>\n\n<p>This all assuming you actually verify the actual code you get from the vendor/provider and that you check the compiler has not been tampered with, which should be the first step.</p>\n\n<p>Anyhow this still leaves the question about how would you verify, without having references, a compiler, from scratch. That certainly looks like a ton of work and requires a definition of the language, which not always is available, sometimes the definition is the compiler.</p>\n"}, {'answer_id': 60555, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 4, 'selected': False, 'text': '<p>You can apply that argument at any level: do you trust the third party libraries? do you trust the OS?  do you trust the processor?</p>\n\n<p>A good example of why this may be a valid concern of course, is how Ken Thompson put a backdoor into the original \'login\' program ... and modified the C compiler so that even if you recompiled login you still got the backdoor.  See this <a href="http://it.toolbox.com/blogs/puramu/ken-thompson-and-the-selfreferencing-c-compiler-16142" rel="noreferrer">posting</a> for more details.</p>\n\n<p>Similar questions have been raised about encryption algorithms -- how do we know there isn\'t a backdoor in DES for the NSA to snoop through?</p>\n\n<p>At the end of the you have to decide if you trust the infrastructure you are building on enough to not worry about it, otherwise you have to start developing your own silicon chips!</p>\n'}, {'answer_id': 60560, 'author': 'denis phillips', 'author_id': 748, 'author_profile': 'https://Stackoverflow.com/users/748', 'pm_score': 0, 'selected': False, 'text': "<ol>\n<li>Changing the optimization level of the compiler will change the output.</li>\n<li>Slight changes to a function may make the compiler inline or no longer inline a function.</li>\n<li>Changes to the compiler (gcc versions for example) may change the output</li>\n<li>Certain library functions may be instrinic (i.e., emit optimized assembly) while others most are not.</li>\n</ol>\n\n<p>The good news is that for most things it really doesn't matter that much. Where it does, you may want to consider assembly if it really matters (e.g., in an ISR). </p>\n"}, {'answer_id': 60567, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 1, 'selected': False, 'text': "<p>The most you can easily certify is that you are using an untampered compiler from provider X. If they do not trust provider X, it's their problem (if X is reasonably trustworthy). If they do not trust any compiler provider, then they are totally unreasonable.</p>\n\n<p>Answering their question: I make sure I'm using an untampered compiler from X through these means. X is well reputed, plus I have a nice set of tests that show our application behaves as expected.</p>\n\n<p>Everything else is starting to open the can of worms. You have to stop somewhere, as Rob says.</p>\n"}, {'answer_id': 60568, 'author': 'ima', 'author_id': 5733, 'author_profile': 'https://Stackoverflow.com/users/5733', 'pm_score': 0, 'selected': False, 'text': "<p>If you are concerned about unexpected machine code which doesn't produce visible results, the only way is probably to contact compiler vendor for certification of some sort which will satisfy your customer.</p>\n\n<p>Otherwise you'll know it the same you know about bugs in your code - testing.</p>\n\n<p>Machine code from modern compilers can be vastly different and totally incomprehensible for puny humans.</p>\n"}, {'answer_id': 60591, 'author': 'Rob Wells', 'author_id': 2974, 'author_profile': 'https://Stackoverflow.com/users/2974', 'pm_score': 1, 'selected': False, 'text': '<p>Sometimes you do get behavioural changes when you request aggressive levels of optimisation.</p>\n\n<p>And optimisation and floating point numbers? Forget it!</p>\n'}, {'answer_id': 60593, 'author': 'aib', 'author_id': 1088, 'author_profile': 'https://Stackoverflow.com/users/1088', 'pm_score': 0, 'selected': False, 'text': '<p>I think it\'s possible to reduce this problem to the <a href="http://en.wikipedia.org/wiki/Halting_problem" rel="nofollow noreferrer">Halting Problem</a> somehow.</p>\n\n<p>The most obvious problem is that if you use some kind of program to analyze the compiler and its determinism, how do you know that <em>your</em> program gets compiled correctly, and produces the correct result?</p>\n\n<p>If you\'re using another, "safe" compiler though, I\'m not sure. What I\'m sure is writing a compiler from scratch would probably be an easier job.</p>\n'}, {'answer_id': 60611, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 2, 'selected': False, 'text': "<p>You don't know for sure that the compiler will do exactly what you expect.  The reason is, of course, that <strong>a compiler is a peice of software</strong>, and is therefore susceptible to bugs.</p>\n\n<p>Compiler writers have the advantage of working from a high quality spec, while the rest of us have to figure out what we're making as we go along.  However, compiler specs <em>also</em> have bugs, and complex parts with subtle interactions.  So, it's not exactly trivial to figure out what the compiler should be doing.  </p>\n\n<p>Still, once you decide what you think the language spec means, you can write a good, fast, automated test for every nuance.  This is where compiler writing has a huge advantage over writing other kinds of software: in testing.  Every bug becomes an automated test case, and the test suite can very thorough.  Compiler vendors have a lot more budget to invest in verifying the correctness of the compiler than you do (you already have a day job, right?).  </p>\n\n<p>What does this mean for you?  It means that you need to be open to the possibilities of bugs in your compiler, but chances are you won't find any yourself.  </p>\n\n<p>I would pick a compiler vendor that is not likely to go out of business any time soon, that has a history of high quality in their compilers, and that has demonstrated their ability to service (patch) their products.  Compilers seem to get more correct over time, so I'd choose one that's been around a decade or two.  </p>\n\n<p><strong>Focus your attention on getting your code right.</strong>  If it's <strong>clear and simple</strong>, then when you <em>do</em> hit a compiler bug, you won't have to think really hard to decide where the problem lies.  <strong>Write good unit tests</strong>, which will ensure that your code does what you expect it to do.</p>\n"}, {'answer_id': 60738, 'author': 'cschol', 'author_id': 2386, 'author_profile': 'https://Stackoverflow.com/users/2386', 'pm_score': 5, 'selected': True, 'text': '<p>For safety critical embedded application certifying agencies require to satisfy the "proven-in-use" requirement for the compiler. There are typically certain requirements (kind of like "hours of operation") that need to be met and proven by detailed documentation. However, most people either cannot or don\'t want to meet these requirements because it can be very difficult especially on your first project with a new target/compiler. </p>\n\n<p>One other approach is basically to NOT trust the compiler\'s output at all. Any compiler and even language-dependent (Appendix G of the C-90 standard, anyone?) deficiencies need to be covered by a strict set of static analysis, unit- and coverage testing in addition to the later functional testing. </p>\n\n<p>A standard like <a href="http://www.embedded.com/columns/beginerscorner/9900659?_requestid=212266" rel="noreferrer">MISRA-C</a> can help to restrict the input to the compiler to a "safe" subset of the C language. Another approach is to restrict the input to a compiler to a subset of a language and test what the output for the entire subset is. If our application is only built of components from the subset it is assumed to be known what the output of the compiler will be. The usually goes by "qualification of the compiler". </p>\n\n<p>The goal of all of this is to be able to answer the QA representative\'s question with "We don\'t just rely on determinism of the compiler but this is the way we prove it...". </p>\n'}, {'answer_id': 77358, 'author': 'jfm3', 'author_id': 11138, 'author_profile': 'https://Stackoverflow.com/users/11138', 'pm_score': 3, 'selected': False, 'text': "<p>You know by testing.  When you test, you're testing your both code and the compiler.</p>\n\n<p>You will find that the odds that you or the compiler writer have made an error are much smaller than the odds that you would make an error if you wrote the program in question in some assembly language.</p>\n"}, {'answer_id': 87056, 'author': 'Michael Carman', 'author_id': 8233, 'author_profile': 'https://Stackoverflow.com/users/8233', 'pm_score': 1, 'selected': False, 'text': '<p>For most software development (think desktop applications) the answer is probably that you don\'t know and don\'t care.</p>\n\n<p>In safety-critical systems (think nuclear power plants and commercial avionics) you <em>do</em> care and regulatory agencies will require you to prove it. In my experience, you can do this one of two ways:</p>\n\n<ol>\n<li>Use a qualified compiler, where "qualified" means that it has been verified according to the standards set out by the regulatory agency.</li>\n<li>Perform object code analysis. Essentially, you compile a piece of reference code and then manually analyze the output to demonstrate that the compiler has not inserted any instructions that can\'t be traced back to your source code.</li>\n</ol>\n'}, {'answer_id': 88483, 'author': 'Martin York', 'author_id': 14065, 'author_profile': 'https://Stackoverflow.com/users/14065', 'pm_score': 3, 'selected': False, 'text': '<p>There are compiler validation suits available.<br>\nThe one I remember is <a href="http://www.peren.com/pages/cvsa_set.htm" rel="nofollow noreferrer">"Perennial"</a>.</p>\n\n<p>When I worked on a C compiler for a embedded SOC processor we had to validate the compiler against this and two other validation suits (that I forget the name of). Validating the compiler to a certain level of conformance to these test suits was part of the contract.</p>\n'}, {'answer_id': 98487, 'author': 'old_timer', 'author_id': 16007, 'author_profile': 'https://Stackoverflow.com/users/16007', 'pm_score': 0, 'selected': False, 'text': '<p>Even a qualified or certified compiler can produce undesirable results.  Keep your code simple and test, test, test.  That or walk through the machine code by hand while not allowing any human error.  PLus the operating system or whatever environment you are running on (preferably no operating system, just your program).</p>\n\n<p>This problem has been solved in mission critical environments since software and compilers began.  As many of the others who have responded also know.  Each industry has its own rules from certified compilers to programming style (you must always program this way, never use this or that or the other), lots of testing and peer review.  Verifying every execution path, etc.</p>\n\n<p>If you are not in one of those industries, then you get what you get.  A commercial program on a COTS operating system on COTS hardware.  It will fail, that is a guarantee.</p>\n'}, {'answer_id': 125288, 'author': 'Andrew Edgecombe', 'author_id': 11694, 'author_profile': 'https://Stackoverflow.com/users/11694', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>...you trust your compiler implicitly</p>\n</blockquote>\n\n<p>You'll stop doing that the first time you come across a compiler bug. ;-)</p>\n\n<p>But ultimately this is what testing is for. It doesn't matter to your test regime how the bug got in to your product in the first place, all that matters is that it didn't pass your extensive testing regime.</p>\n"}, {'answer_id': 176848, 'author': 'KeyserSoze', 'author_id': 14116, 'author_profile': 'https://Stackoverflow.com/users/14116', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>How do you know that the compiler you are using generates machine code that matches the c code's functionality exactly and that the compiler is fully deterministic?</p>\n</blockquote>\n\n<p>You don't, that's why you test the resultant binary, and why you make sure to ship the same binary you tested with.  And why when you make 'minor' software changes, you regression test to make sure none of the old functionality broke.</p>\n\n<p>The only software I've certified is avionics. FAA certification isn't rigorous enough to prove the software works correctly, while at the same time it does force you to jump through a certain amount of hoops.  The trick is to structure your 'process' so it improves quality as much as possible, with as little extraneous hoop-jumping as you can get away with.  So anything that you know is worthless and won't actually find bugs, you can probably weasel out of.  And anything you know you should do because it <em>will</em> find bugs that isn't explicitly asked for by the FAA, your best bet is to twist words until it sounds like you're giving the FAA/your QA people what they asked for.</p>\n\n<p>This actually isn't as dishonest as I've made it sound, in general the FAA cares more about you being conscientious and confident that you're <em>trying</em> to do a good job, than about what exactly you do.</p>\n"}, {'answer_id': 181141, 'author': 'DarenW', 'author_id': 10468, 'author_profile': 'https://Stackoverflow.com/users/10468', 'pm_score': 2, 'selected': False, 'text': '<p>Some intellectual ammunition might be found in Crosstalk, a magazine for defense software engineers.  This question is the kind of thing they spend many waking hours on.  <a href="http://www.stsc.hill.af.mil/crosstalk/2006/08/index.html" rel="nofollow noreferrer">http://www.stsc.hill.af.mil/crosstalk/2006/08/index.html</a>   (If i can find my old notes from an old project, i\'ll be back here...)</p>\n'}, {'answer_id': 238064, 'author': 'geometrikal', 'author_id': 31045, 'author_profile': 'https://Stackoverflow.com/users/31045', 'pm_score': 2, 'selected': False, 'text': "<p>You can never fully trust the compiler, even highly recommended ones. They could release an update that has a bug, and your code compiles the same. This problem is compounded when updating old code with the buggy compiler, doing testing and shipping out the goods only to have the customer ring you 3 months later with a problem.</p>\n\n<p>It all comes back to testing, and if there is one thing I have learnt it is to thouroughly test after any non-trivial change. If the problem seems impossible to find have a look at the compiled assembler and check it's doing what it should be doing. </p>\n\n<p>On several occasions I have found bugs in the compiler. One time there was a bug where 16 bit variables would get incremented but without carry and only if the 16 bit variable was part of an extern struct defined in a header file. </p>\n"}, {'answer_id': 238082, 'author': 'Watson Ladd', 'author_id': 29025, 'author_profile': 'https://Stackoverflow.com/users/29025', 'pm_score': 1, 'selected': False, 'text': '<p>You get the one Dijkstra wrote.</p>\n'}, {'answer_id': 2020351, 'author': 'abc', 'author_id': 245517, 'author_profile': 'https://Stackoverflow.com/users/245517', 'pm_score': 1, 'selected': False, 'text': '<p>Select a formally verified compiler, like Compcert C compiler.</p>\n'}, {'answer_id': 2022807, 'author': 'BCS', 'author_id': 1343, 'author_profile': 'https://Stackoverflow.com/users/1343', 'pm_score': 0, 'selected': False, 'text': '<p>If your worried about <em>malicious</em> bugs in the compiler, one recommendation (IIRC, an NSA requirement for some projects) is that the compiler binary predate the writing of the code. At least then you know that no one has <em>added</em> bugs targeted at <em>your</em> program.</p>\n'}, {'answer_id': 2025034, 'author': 'figurassa', 'author_id': 220177, 'author_profile': 'https://Stackoverflow.com/users/220177', 'pm_score': 2, 'selected': False, 'text': '<p>Well.. you can\'t simply say that you trust your compiler\'s output - particularly if you work with embedded code. It is not hard to find discrepancies between the code generated when compiling the very same code with different compilers. This is the case because the C standard itself is too loose. Many details can be implemented differently by different compilers without breaking the standard. How do we deal with this stuff? We avoid compiler dependent constructs whenever possible. We may deal with it by choosing a safer subset of C like <a href="http://www.embedded.com/columns/beginerscorner/9900659?_requestid=212266" rel="nofollow noreferrer">Misra-C</a> as previously mentioned by the user cschol. I seldom have to inspect the code generated by the compiler but that has also happened to me at times. But, ultimately, you are relying on your tests in order to make sure that the code behaves as intended. </p>\n\n<p>Is there a better option out there? Some people claim that there is. The other option is to write your code in <a href="http://www.embedded.com/columns/technicalinsights/190400498?_requestid=177701" rel="nofollow noreferrer">SPARK/Ada</a>. I have never written code in SPARK but my understanding is that you would still have to link it against routines written in C that would deal with the "bare metal" stuff. The beauty of SPARK/Ada is that you are absolutely guaranteed that the code generated by any compiler is always going to be the same. No ambiguity whatsoever. On top of that, the language allows you to annotate the code with explanations as to how the code is intended to behave. The SPARK toolset will use these annotations to formally prove that the code written does indeed do what the annotations have described. So I have been told that for critical systems, SPARK/Ada is a pretty good bet. I have never tried it myself though.</p>\n'}]
['https://Stackoverflow.com/questions/60547', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1816/']