cloned from https://github.com/cemc/safeexec