This research investigates techniques to efficiently detect the first exploit against an unknown vulnerability on a user system, and then safely heal the system against further attacks. The research uses dynamic analysis -- the ability to monitor code as it executes -- because it can be used to check safety and security properties of actual executions and make sure that each step of the execution is safe. However, existing dynamic analysis techniques are far too slow for end users, and currently do not help user systems self-heal. This research investigates techniques to overcome these limitations by developing efficient dynamic analysis techniques, as well as techniques for diagnosing the root cause of an exploited vulnerability, repairing any damage done, and defending the system against further attacks.