Women's world number one Victoria Azarenka survived a scare to reach the last four of the Porsche Tennis Grand Prix in Stuttgart.