Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1JFPuL-0004c2-Sn for mharc-grub-devel@gnu.org; Thu, 17 Jan 2008 03:15:29 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JFPuK-0004bo-4U for grub-devel@gnu.org; Thu, 17 Jan 2008 03:15:28 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JFPuI-0004ap-K7 for grub-devel@gnu.org; Thu, 17 Jan 2008 03:15:26 -0500 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JFPuI-0004am-FV for grub-devel@gnu.org; Thu, 17 Jan 2008 03:15:26 -0500 Received: from mx20.gnu.org ([199.232.41.8]) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1JFPuI-0001au-DM for grub-devel@gnu.org; Thu, 17 Jan 2008 03:15:26 -0500 Received: from py-out-1112.google.com ([64.233.166.181]) by mx20.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1JFPuG-0000O9-Qk for grub-devel@gnu.org; Thu, 17 Jan 2008 03:15:24 -0500 Received: by py-out-1112.google.com with SMTP id u52so724669pyb.1 for ; Thu, 17 Jan 2008 00:15:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=AWn0cNysyVBKtCLDLDFQH6mHKWpbq84F4zO17QhGXC0=; b=eAAhAKrooFK+M+PrnrjQOVYBzSErrd1tlbTZeOuiTfNJOGlL5gzftrEWKoSPAHzIwo7RT2AD3TVIVRmkgx5dMy6vomuNP+QHfWi2c7gPwwIGPOBz+DbdO0mI1zT5ztnIefCruBOhB3wa/3iNtmADxlCv5TX+r5BMm3inykeggMQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=EeP1AWo2bvzsyxP3vbo8a9zbIw+l5uuEnzqYvu1xyC9cbcqkdjGoIzK5295yRvQbzYJHDlTy3S7hYnUM2+oSsrbWLtnw7n2Kza88L+VSWlaxnlr+OD8u8MQYU14lDkkFd/RWle8IalpXiYTQKMLNOa+JDieU0uUosK/qsHcPAsw= Received: by 10.35.91.1 with SMTP id t1mr2045544pyl.5.1200557723488; Thu, 17 Jan 2008 00:15:23 -0800 (PST) Received: by 10.35.110.12 with HTTP; Thu, 17 Jan 2008 00:15:23 -0800 (PST) Message-ID: Date: Thu, 17 Jan 2008 16:15:23 +0800 From: Bean To: "The development of GRUB 2" In-Reply-To: <20080116230529.GA28677@thorin> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20080116230529.GA28677@thorin> X-detected-kernel: by mx20.gnu.org: Linux 2.6 (newer, 2) X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) Subject: Re: nested functions used by multiboot2 loader corrupt stack 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: Thu, 17 Jan 2008 08:15:28 -0000 On Jan 17, 2008 7:05 AM, Robert Millan wrote: > > I got pretty confused at this one. Maybe someone can sort this out. I'm > afraid I can't :-( > > It seems that at some point when loading multiboot2 images, our stack is > corrupted for no apparent reason and one of the hooks in our nested function > calls ends up jumping to the wrong place. > > This hangs qemu 0.9.0, but qemu 0.9.1 aborts with "triple fault" message. > > I added a few printf calls to trace what's going on, and switched to serial > terminal so that the output can be captured. My debugging patch is attached. > This is the output: > > grub_mb2_load_elf: going to call grub_elf32_load using grub_mb2_arch_elf32_hook=0x7ffc72c as hook > grub_elf32_load: going to call grub_elf32_phdr_iterate using grub_elf32_load_segment=0x7dda4 as hook, and _load_hook=0x7ffc72c as hook's hook > grub_elf32_phdr_iterate: going to call hook=0x7dda4 using hook_arg=0x7ffc72c as hook > grub_elf32_load_segment: going to call load_hook=0x7dd9c > qemu: fatal: triple fault > EAX=0004be50 EBX=0004bf30 ECX=0008de66 EDX=0007dd2c > ESI=0004be50 EDI=0007dd9c EBP=0007dd3c ESP=0007dd10 > EIP=0007dda0 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0 > > This seems to indicate that grub_elf32_phdr_iterate() called its hook, 0x7dda4, > aka grub_elf32_load_segment() with proper hook_arg parameter = 0x7ffc72c, aka > grub_mb2_arch_elf32_hook(). > > When grub_elf32_load_segment() starts, its hook_arg parameter (now known as > load_hook) has been corrupted and now points at 0x7dd9c. The other two > parameters in this function are not tainted, only the third one is. > > I'm not sure how to proceed from here. I really miss a debugger in these > cases :-( > > Also attaching the sample multiboot2 program I used. I'm not sure of its > correctness, but nevertheless GRUB shouldn't crash because of incorrect > images; specially not at this point. You need to add NESTED_FUNC_ATTR to nested callback function that use local variable. here is the patch: diff --git a/kern/elf.c b/kern/elf.c index b362949..4978a27 100644 --- a/kern/elf.c +++ b/kern/elf.c @@ -139,7 +139,7 @@ grub_elf32_load_phdrs (grub_elf_t elf) static grub_err_t grub_elf32_phdr_iterate (grub_elf_t elf, - int (*hook) (grub_elf_t, Elf32_Phdr *, void *), + int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf32_Phdr *, void *), void *hook_arg) { Elf32_Phdr *phdrs; @@ -219,9 +219,8 @@ grub_elf32_load (grub_elf_t _elf, grub_elf32_load_hook_t _load_hook, grub_size_t load_size = 0; grub_err_t err; - auto int grub_elf32_load_segment (grub_elf_t elf, Elf32_Phdr *phdr, - void *hook); - int grub_elf32_load_segment (grub_elf_t elf, Elf32_Phdr *phdr, void *hook) + auto int NESTED_FUNC_ATTR grub_elf32_load_segment (grub_elf_t elf, Elf32_Phdr *phdr, void *hook); + int NESTED_FUNC_ATTR grub_elf32_load_segment (grub_elf_t elf, Elf32_Phdr *phdr, void *hook) { grub_elf32_load_hook_t load_hook = (grub_elf32_load_hook_t) hook; grub_addr_t load_addr; -- Bean