A team of computer scientists at the University of California San Diego trawled through copies of the code, which ran on Volkswagen's onboard computers for models including the 2009-2015 model year Volkswagen Jetta, Beetle and Golf.
According to the researchers' report [PDF], the code allowed vehicles to detect when they were undergoing emissions testing and activate a "defeat device" to cut down on emissions. If the car determined it wasn't being tested, it would disable these emissions controls, leading some cars to emit up to 40 times the allowed levels of nitrogen oxides.
Volkswagen issued a statement indicating that 11 million vehicles contained the illegal code.
While the researchers said "Volkswagen's cheating was breathtaking in scope," they noted the code was publicly available on the company's maintenance website and on forums run by car enthusiasts. They studied 900 versions of code and found 400 that included the cheat.
"We found evidence of the fraud right there in public view," said lead researcher Kirill Levchenko.
"We were able to find the smoking gun."