Improve heap checks for primops. Fix #8326, #8317