Thursday, February 14, 2008

Software is a resilient thing

About 10 years ago I was working on a Just-in-time compiler for a Java-like language. The runtime lived inside Microstation - a huge CAD package - millions and millions of lines of code, a lot of it written in MDL (Microstation Development Language), which was very close to C (and we were migrating it to very close to Java).

Anyway, the compiler generated an intermediate instruction set which I designed and which contained an instruction to copy a structure, so for example, you could write

Point2d new_point = old_point;

and it would compile to one instruction.

So as I was working on the JITter, I accidentally reversed source and destination in the generated machine code, so in the example above instead of copying old_point into the new_point, it would copy the garbage from the uninitialized new_point to the old_point.

Needless to say, in a CAD application this sort of code is very, very common. There were thousands and thousands copy instructions embedded in compiled MDL code.

However, despite my bug Microstation started and even shown part of the design file - which required compiling about 2 megabytes of code (a lot for 1997! - and it executed a big part of it, too) - before it finally crashed.

It was not an isolated incident. It is not an infrequent occurence when I find bugs - on inspection - and the first thought I have is - how on earth did it ever work? Apparently one has to work really hard to produce unstable software :-)!

No comments: