Short of super-intelligent general AI writing programs for us yadda yadda, I believe the answer will be to adopt formal methods more widely: Bake the intended semantics of your software with regards to some formal system into the program itself and have it only be valid if you or the computer can prove that it meets that specification (and have the specification be complete and correct of course (which can be difficult when you consider what e.g. cryptographic strength or "real-time properties" actually mean); if proof is not feasible, add in checks that make it fail gracefully at least or generate tests).
Where today the "engineering mindset" is more prevalent (Look, I can do things with it! And it does them correctly I hope.), there needs to be a shift to a more mathematical view on computation (Your program is an instance of a certain calculus and claiming to fulfill a formal specification, which needs proof), if the software complexity mentioned is to be managed better (there's a whole zoo of specification techniques and low-level formal systems available, which doesn't make this any easier).
Unfortunately support for any of this is severely lacking in popular tools (yes, there's a lot going on in academia, but I don't feel this has spilled over yet): Languages lacking a formal spec and changing frequently, compilers not proven correct, programs running on buggy processors, operating systems with unclear semantics, written in (from a type-system PoV) weakly-expressive languages using meaningless types (void, ugh), black-box peripherals. In that light I'm amazed my computer does anything at all, but clearly most of the time these things don't matter.
Then again, I'm not aware of a fully-featured and usable (comparatively to today's personal computers) formally-specified (whatever this would mean here, surely there's more than one way to define an OS for instance) and proven computing system that goes from high-level language to operating system and application ecosystem to whatever machinery is used at the bottom.
Coq and co. to the rescue (see e.g. http://compcert.inria.fr/)?