Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1LeVWM-0001as-76 for mharc-grub-devel@gnu.org; Tue, 03 Mar 2009 09:22:58 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LeVWJ-0001Xj-32 for grub-devel@gnu.org; Tue, 03 Mar 2009 09:22:55 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LeVWF-0001VV-59 for grub-devel@gnu.org; Tue, 03 Mar 2009 09:22:53 -0500 Received: from [199.232.76.173] (port=46426 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LeVWD-0001Uu-QK for grub-devel@gnu.org; Tue, 03 Mar 2009 09:22:50 -0500 Received: from ti-out-0910.google.com ([209.85.142.185]:44960) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1LeVWD-0005YC-6M for grub-devel@gnu.org; Tue, 03 Mar 2009 09:22:49 -0500 Received: by ti-out-0910.google.com with SMTP id j3so3006665tid.10 for ; Tue, 03 Mar 2009 06:22:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type:content-transfer-encoding; bh=rI5wse/I05mSY7UlPWJhLFGE+cylG+3WfGPupujsJE4=; b=jBcNwXiq23vHPNV2ptUyo+rW1WJl93O4GPWIsp0GDgFZqI7AwWRt9wvKxYHwgp1xKj icG4A3mLlqF0BYf8LtAyTOoKWoyt41w7HUXBsfd+OWul2P0ct/9U5GEo4B7Uv1+50Jjf LYtZ7iT2z1CgwmlhrO+6BlIQWSsNeR/nV8rq4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type :content-transfer-encoding; b=JUnoGANYv98Lx0xEgcQOtw32NnHIOVTfirfTpEKqMBYsrUqwuVI88JVbkv3NKDednG OR3A6DzdRS9mfycDncz9Gko7dveP9KiUoUVXTW12rtQPwfjmCcDyb+DM5riuk+c29mGk Bc7W3rbZ7jjN+rWzN6WNrcICXZha1btQfH2fY= MIME-Version: 1.0 Received: by 10.110.90.9 with SMTP id n9mr10468848tib.6.1236090161500; Tue, 03 Mar 2009 06:22:41 -0800 (PST) Date: Tue, 3 Mar 2009 22:22:41 +0800 Message-ID: From: Bean To: The development of GRUB 2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: A serious stack issue caused by gcc optimization X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Mar 2009 14:22:55 -0000 Hi, I've found a serious stack issue when debugging EFI amd64 port. The problem is in grub_parser_cmdline_state, the c code is all right, problem is in the assembly code generated by gcc: grub_parser_cmdline_state: mov %edi,-0x14(%rsp) movl $0x1,-0xc(%rsp) leaq EXT_C(my_state_transitions)(%rip),%rcx xor %r8d,%r8d jmp loc_31 loc_1b: cmp %edi,%eax jne loc_2d mov 0x8(%rcx),%al cmp %sil,%al je loc_3e test %al,%al cmove %rcx,%r8 loc_2d: add $0x10,%rcx loc_31: mov (%rcx),%eax test %eax,%eax loc_35: jne loc_1b loc_37: jmp loc_4d loc_39: lea -0x18(%rsp),%rcx loc_3e: xor %eax,%eax cmpl $0x0,0xc(%rcx) cmovne %esi,%eax mov %al,(%rdx) mov 0x4(%rcx),%eax addq $0x18, %rsp retq loc_4d: test %r8,%r8 mov %r8,%rcx jne loc_3e jmp loc_39 You can see that it access local variable default_transition using negative offset from %esp. In EFI, hardware interrupt is enabled. When an interrupt fires (such as timer), it uses the same stack, which in effect overwritten default_transition ! The result is that the parser is halted by wrong grub_parser_cmdline_state result, and the menu is truncated. If I subtract %rsp and uses positive offset, this problem goes away. For grub_parser_cmdline_state itself, there is a simple solution, just change default_transition to global variable: diff --git a/kern/parser.c b/kern/parser.c index e931853..a0ab0e7 100644 --- a/kern/parser.c +++ b/kern/parser.c @@ -54,6 +54,7 @@ static struct grub_parser_state_transition state_transitions[] = { 0, 0, 0, 0} }; +static struct grub_parser_state_transition default_transition; /* Determines the state following STATE, determined by C. */ grub_parser_state_t @@ -61,7 +62,6 @@ grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result) { struct grub_parser_state_transition *transition; struct grub_parser_state_transition *next_match = 0; - struct grub_parser_state_transition default_transition; int found = 0; default_transition.to_state = state; But there could be other hidden bombs lurking around. Especially that random reboot issue, I suspect it's caused by the same bug. I've tried a few gcc options, but doesn't seems to work. Anyone knows how to instruct gcc to subtract %rsp pointer for local variable ? -- Bean