From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1SPBtB-0006P5-6A for mharc-grub-devel@gnu.org; Tue, 01 May 2012 08:09:05 -0400 Received: from eggs.gnu.org ([208.118.235.92]:56802) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SPBt3-0006Oh-Rn for grub-devel@gnu.org; Tue, 01 May 2012 08:09:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SPBsx-00029J-Dv for grub-devel@gnu.org; Tue, 01 May 2012 08:08:57 -0400 Received: from mail-wg0-f41.google.com ([74.125.82.41]:64290) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SPBsw-00022M-O2 for grub-devel@gnu.org; Tue, 01 May 2012 08:08:51 -0400 Received: by wgbds1 with SMTP id ds1so2964443wgb.0 for ; Tue, 01 May 2012 05:08:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type; bh=0iIUe7NZ84wXA3QbLaF3/jRxc/aE9ffXG52RCkB6BiA=; b=IkyB/8CKR8i2NcbMMp7XpX7KpIFuTfKknr8wi/Mt2QfqmdXpYNiCs58r0f1Vy1dRsY zMZuGlBmCHiogRH3lZhBXftJUwUIFJBEiZYx2a3x66sJ8LOTZH402WxT/qKzIcOio9Ob Or700hWHIK8HESlBhTqm/o8HsW8fuTwM7mpYFKyPgYQB1eWI2RfY/NZd3n3E43ILyKPI ICu463R3H7O8IEn04LjcTLNuSXb4TJpUUDfoC18YZXA7ripUdIkCjOabVUJ/NP5PO47a vBIP5mhcBABCVZgqcxCA1iw4cqMsQ1kzNw95dg7SotHDjPx0vcLunMDRfT7b+jqTTIBV cMzw== Received: by 10.180.80.104 with SMTP id q8mr4755713wix.14.1335874125459; Tue, 01 May 2012 05:08:45 -0700 (PDT) Received: from debian.x201.phnet (95-232.197-178.cust.bluewin.ch. [178.197.232.95]) by mx.google.com with ESMTPS id gd4sm56968195wib.6.2012.05.01.05.08.42 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 01 May 2012 05:08:44 -0700 (PDT) Message-ID: <4F9FD247.6050203@gmail.com> Date: Tue, 01 May 2012 14:08:39 +0200 From: =?UTF-8?B?VmxhZGltaXIgJ8+GLWNvZGVyL3BoY29kZXInIFNlcmJpbmVua28=?= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120329 Icedove/10.0.3 MIME-Version: 1.0 To: grub-devel@gnu.org Subject: Re: RFC: enhanced memory protection support References: <4F9ECACF.6050803@gmail.com> <4F9FBAEC.7080709@gmail.com> In-Reply-To: <4F9FBAEC.7080709@gmail.com> X-Enigmail-Version: 1.4.1 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="------------enig028C6FB8C87BFBB077C058F9" X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 74.125.82.41 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 May 2012 12:09:03 -0000 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig028C6FB8C87BFBB077C058F9 Content-Type: multipart/mixed; boundary="------------040001040801000607030502" This is a multi-part message in MIME format. --------------040001040801000607030502 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 01.05.2012 12:29, Vladimir '=CF=86-coder/phcoder' Serbinenko wrote: > On 30.04.2012 19:24, Vladimir '=CF=86-coder/phcoder' Serbinenko wrote: >> On 30.04.2012 17:26, Bean wrote: >>> Hi, >>> >>> While testing network function in efi mode, I've found several memory= >>> leak related to fragmentation, but there is still some memory problem= >>> that's very tricky to locate. For example, you can run testspeed on a= >>> large file several times in a row and it could show the memory error.= >>> Since network condition are very difficult to reproduce, I have to >>> look at the source of this issue, memory allocation. Currently it has= >>> mm_debug option that could print out file and line number of each >>> memory call, but it's quite useless since we can't find the relevant >>> informaton with full screen of prints. >>> Here are some of my ideas for enhanced memory protection support: >>> >>> 1, when we allocate memory, we append some information at the end of >>> the buffer, which include filename, lineno of caller, and tag to >>> indicate what is used for and some padding to detect memory overwrite= >>> problem. >>> >>> 2. add a command to print the current memory list with extended >>> information, this can be used to find memory leaks. >>> >>> 3. it's also a good idea to run the memory check in automated test to= >>> locate potential issue. >> This is pretty easy to do leveraging some of the code I did for POSIX >> support. But: >> - Due to additional time and space required it should be done only whe= n >> mm-debug is enabled. >> - The additional data has to be stored before rather than after the >> range since we store all the info before. >> - Integrating with automated tests isn't that easy since some memory i= s >> intentionally never freed i.a. disk cache or otherwise in use (i.a. >> terminal). We need exceptions for those. >> I have half-working patch, just needs few fixes. >> >> > Patch attached. It still has a bug since printing the report to termina= l > may change allocations and so iterator may become invalid. That's > another reason why terminal should be excluded altogether from memory > tracking. > > --=20 Regards Vladimir '=CF=86-coder/phcoder' Serbinenko --------------040001040801000607030502 Content-Type: text/x-diff; name="mm2.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="mm2.diff" =3D=3D=3D modified file 'config.h.in' --- config.h.in 2012-02-04 20:47:29 +0000 +++ config.h.in 2012-05-01 09:53:02 +0000 @@ -8,7 +8,11 @@ #if defined (GRUB_UTIL) || !defined (GRUB_MACHINE) #include #define NESTED_FUNC_ATTR +#define MM_DEBUG 0 #else + +/* Define to 1 if you enable memory manager debugging. */ +#define MM_DEBUG @MM_DEBUG@ /* Define if C symbols get an underscore after compilation. */ #define HAVE_ASM_USCORE @HAVE_ASM_USCORE@ /* Define it to \"addr32\" or \"addr32;\" to make GAS happy. */ =3D=3D=3D modified file 'configure.ac' --- configure.ac 2012-04-29 16:15:24 +0000 +++ configure.ac 2012-05-01 09:53:58 +0000 @@ -759,9 +759,14 @@ # Memory manager debugging. AC_ARG_ENABLE([mm-debug], AS_HELP_STRING([--enable-mm-debug], - [include memory manager debugging]), - [AC_DEFINE([MM_DEBUG], [1], - [Define to 1 if you enable memory manager debug= ging.])]) + [include memory manager debugging])) +if test x$enable_mm_debug =3D xyes; then + MM_DEBUG=3D1 +else + MM_DEBUG=3D0 +fi + +AC_SUBST([MM_DEBUG]) =20 AC_ARG_ENABLE([cache-stats], AS_HELP_STRING([--enable-cache-stats], @@ -786,6 +791,10 @@ [AS_HELP_STRING([--enable-grub-emu-pci], [build and install the `grub-emu' debugging= utility with PCI support (potentially dangerous) (default=3Dno)])]) =20 +if test "$platform" =3D emu && test "$MM_DEBUG" =3D 1; then + AC_MSG_ERROR([grub-emu doesn't support mm-debug]) +fi + if test "$platform" =3D emu; then missing_ncurses=3D [# Check for curses libraries.] @@ -1132,6 +1141,7 @@ AM_CONDITIONAL([COND_APPLE_CC], [test x$TARGET_APPLE_CC =3D x1]) AM_CONDITIONAL([COND_ENABLE_EFIEMU], [test x$enable_efiemu =3D xyes]) AM_CONDITIONAL([COND_ENABLE_CACHE_STATS], [test x$DISK_CACHE_STATS =3D x= 1]) +AM_CONDITIONAL([COND_ENABLE_MM_DEBUG], [test x$MM_DEBUG =3D x1]) =20 AM_CONDITIONAL([COND_HAVE_ASM_USCORE], [test x$HAVE_ASM_USCORE =3D x1]) AM_CONDITIONAL([COND_CYGWIN], [test x$host_os =3D xcygwin]) =3D=3D=3D modified file 'grub-core/Makefile.core.def' --- grub-core/Makefile.core.def 2012-04-01 19:35:18 +0000 +++ grub-core/Makefile.core.def 2012-04-30 18:22:33 +0000 @@ -1816,6 +1816,13 @@ }; =20 module =3D { + name =3D printmem; + common =3D commands/printmem.c; + condition =3D COND_ENABLE_MM_DEBUG; + enable =3D noemu; +}; + +module =3D { name =3D adler32; common =3D lib/adler32.c; }; =3D=3D=3D modified file 'grub-core/commands/boot.c' --- grub-core/commands/boot.c 2012-04-11 20:32:31 +0000 +++ grub-core/commands/boot.c 2012-05-01 11:29:30 +0000 @@ -62,7 +62,7 @@ return 0; =20 new_preboot =3D (struct grub_preboot *) - grub_malloc (sizeof (struct grub_preboot)); + grub_malloc_notrack (sizeof (struct grub_preboot)); if (! new_preboot) return 0; =20 =3D=3D=3D modified file 'grub-core/commands/extcmd.c' --- grub-core/commands/extcmd.c 2011-04-11 21:01:51 +0000 +++ grub-core/commands/extcmd.c 2012-05-01 11:02:53 +0000 @@ -77,7 +77,7 @@ grub_extcmd_t ext; grub_command_t cmd; =20 - ext =3D (grub_extcmd_t) grub_malloc (sizeof (*ext)); + ext =3D (grub_extcmd_t) grub_malloc_notrack (sizeof (*ext)); if (! ext) return 0; =20 =3D=3D=3D added file 'grub-core/commands/printmem.c' --- grub-core/commands/printmem.c 1970-01-01 00:00:00 +0000 +++ grub-core/commands/printmem.c 2012-05-01 10:57:02 +0000 @@ -0,0 +1,61 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2012 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by= + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +static grub_err_t +grub_cmd_print_mem (grub_command_t ctxt __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + grub_mm_header_t cur; + for (cur =3D grub_mm_allocated; cur; cur =3D cur->next) + { + struct grub_mm_alloc_info *info =3D (struct grub_mm_alloc_info *) = (cur + 1); + /* TRANSLATORS: This line lists currently allocated memory. + First two fiels are filename and line number. + It's followed by the pointer and the size. */ + grub_printf_ (N_("%s:%d %p %lu B\n"), info->file, info->lineno, cu= r + 1, + (unsigned long) + ((cur->size << GRUB_MM_ALIGN_LOG2) - sizeof (*cur) + - sizeof (*info))); + } + return 0; +} + +static grub_command_t cmd; + +GRUB_MOD_INIT(printmem) +{ + cmd =3D grub_register_command ("printmem", grub_cmd_print_mem, 0, + N_("Print currently used memory.")); +} + +GRUB_MOD_FINI(printmem) +{ + grub_unregister_command (cmd); +} =3D=3D=3D modified file 'grub-core/font/font.c' --- grub-core/font/font.c 2012-04-01 19:27:17 +0000 +++ grub-core/font/font.c 2012-05-01 12:02:33 +0000 @@ -146,7 +146,8 @@ for (current =3D 0; current < 0x80; current++) { ascii_font_glyph[current] =3D - grub_malloc (sizeof (struct grub_font_glyph) + ASCII_BITMAP_SIZE); + grub_malloc_notrack (sizeof (struct grub_font_glyph) + + ASCII_BITMAP_SIZE); =20 ascii_font_glyph[current]->width =3D 8; ascii_font_glyph[current]->height =3D 16; @@ -178,8 +179,8 @@ return; =20 /* Make glyph for unknown glyph. */ - unknown_glyph =3D grub_malloc (sizeof (struct grub_font_glyph) - + sizeof (unknown_glyph_bitmap)); + unknown_glyph =3D grub_malloc_notrack (sizeof (struct grub_font_glyph)= + + sizeof (unknown_glyph_bitmap)); if (!unknown_glyph) return; =20 @@ -310,11 +311,11 @@ font->num_chars =3D sect_length / FONT_CHAR_INDEX_ENTRY_SIZE; =20 /* Allocate the character index array. */ - font->char_index =3D grub_malloc (font->num_chars - * sizeof (struct char_index_entry)); + font->char_index =3D grub_malloc_notrack (font->num_chars + * sizeof (struct char_index_entry)); if (!font->char_index) return 1; - font->bmp_idx =3D grub_malloc (0x10000 * sizeof (grub_uint16_t)); + font->bmp_idx =3D grub_malloc_notrack (0x10000 * sizeof (grub_uint16_t= )); if (!font->bmp_idx) return 1; grub_memset (font->bmp_idx, 0xff, 0x10000 * sizeof (grub_uint16_t)); @@ -380,7 +381,7 @@ char *str; grub_ssize_t ret; =20 - str =3D grub_malloc (section->length + 1); + str =3D grub_malloc_notrack (section->length + 1); if (!str) return 0; =20 @@ -514,7 +515,7 @@ #endif =20 /* Allocate the font object. */ - font =3D (grub_font_t) grub_zalloc (sizeof (struct grub_font)); + font =3D (grub_font_t) grub_zalloc_notrack (sizeof (struct grub_font))= ; if (!font) goto fail; =20 @@ -774,7 +775,7 @@ } =20 len =3D (width * height + 7) / 8; - glyph =3D grub_malloc (sizeof (struct grub_font_glyph) + len); + glyph =3D grub_malloc_notrack (sizeof (struct grub_font_glyph) + l= en); if (!glyph) { remove_font (font); @@ -837,7 +838,7 @@ { struct grub_font_node *node =3D 0; =20 - node =3D grub_malloc (sizeof (struct grub_font_node)); + node =3D grub_malloc_notrack (sizeof (struct grub_font_node)); if (!node) return 1; =20 =3D=3D=3D modified file 'grub-core/fs/fshelp.c' --- grub-core/fs/fshelp.c 2012-02-08 18:26:01 +0000 +++ grub-core/fs/fshelp.c 2012-05-01 11:39:01 +0000 @@ -136,6 +136,7 @@ found =3D iterate_dir (currnode, iterate); if (! found) { + free_node (currnode); if (grub_errno) return grub_errno; =20 =3D=3D=3D modified file 'grub-core/gettext/gettext.c' --- grub-core/gettext/gettext.c 2012-04-04 12:41:55 +0000 +++ grub-core/gettext/gettext.c 2012-05-01 12:06:28 +0000 @@ -109,7 +109,7 @@ length =3D grub_cpu_to_le32 (desc.length); offset =3D grub_cpu_to_le32 (desc.offset); =20 - translation =3D grub_malloc (length + 1); + translation =3D grub_malloc_notrack (length + 1); if (!translation) return NULL; =20 @@ -323,8 +323,8 @@ for (ctx->grub_gettext_max_log =3D 0; ctx->grub_gettext_max >> ctx->gr= ub_gettext_max_log; ctx->grub_gettext_max_log++); =20 - ctx->grub_gettext_msg_list =3D grub_zalloc (ctx->grub_gettext_max - * sizeof (ctx->grub_gettext_msg_list[0])); + ctx->grub_gettext_msg_list =3D grub_zalloc_notrack (ctx->grub_gettext_= max + * sizeof (ctx->grub_gettext_msg_list[0])); if (!ctx->grub_gettext_msg_list) { grub_file_close (fd); @@ -430,7 +430,7 @@ if (err) grub_print_error (); =20 - return grub_strdup (val); + return grub_strdup_notrack (val); } =20 void =3D=3D=3D modified file 'grub-core/kern/command.c' --- grub-core/kern/command.c 2012-02-12 02:52:17 +0000 +++ grub-core/kern/command.c 2012-05-01 11:01:46 +0000 @@ -34,7 +34,7 @@ =20 grub_command_t *p, q; =20 - cmd =3D (grub_command_t) grub_zalloc (sizeof (*cmd)); + cmd =3D (grub_command_t) grub_zalloc_notrack (sizeof (*cmd)); if (! cmd) return 0; =20 =3D=3D=3D modified file 'grub-core/kern/disk.c' --- grub-core/kern/disk.c 2012-03-03 11:59:28 +0000 +++ grub-core/kern/disk.c 2012-05-01 10:56:51 +0000 @@ -163,7 +163,8 @@ cache->data =3D 0; cache->lock =3D 0; =20 - cache->data =3D grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_= BITS); + cache->data =3D grub_malloc_notrack (GRUB_DISK_SECTOR_SIZE + << GRUB_DISK_CACHE_BITS); if (! cache->data) return grub_errno; =20 =3D=3D=3D modified file 'grub-core/kern/dl.c' --- grub-core/kern/dl.c 2012-03-19 10:10:11 +0000 +++ grub-core/kern/dl.c 2012-05-01 11:01:29 +0000 @@ -141,13 +141,13 @@ grub_symbol_t sym; unsigned k; =20 - sym =3D (grub_symbol_t) grub_malloc (sizeof (*sym)); + sym =3D (grub_symbol_t) grub_malloc_notrack (sizeof (*sym)); if (! sym) return grub_errno; =20 if (mod) { - sym->name =3D grub_strdup (name); + sym->name =3D grub_strdup_notrack (name); if (! sym->name) { grub_free (sym); @@ -276,7 +276,7 @@ tsize =3D ALIGN_UP (tsize, 8192 * 16); #endif =20 - mod->base =3D grub_memalign (talign, tsize); + mod->base =3D grub_memalign_notrack (talign, tsize); if (!mod->base) return grub_errno; mod->sz =3D tsize; @@ -294,7 +294,7 @@ { grub_dl_segment_t seg; =20 - seg =3D (grub_dl_segment_t) grub_malloc (sizeof (*seg)); + seg =3D (grub_dl_segment_t) grub_malloc_notrack (sizeof (*seg)); if (! seg) return grub_errno; =20 @@ -499,7 +499,7 @@ i++, s =3D (Elf_Shdr *) ((char *) s + e->e_shentsize)) if (grub_strcmp (str + s->sh_name, ".modname") =3D=3D 0) { - mod->name =3D grub_strdup ((char *) e + s->sh_offset); + mod->name =3D grub_strdup_notrack ((char *) e + s->sh_offset); if (! mod->name) return grub_errno; break; @@ -540,7 +540,7 @@ =20 grub_dl_ref (m); =20 - dep =3D (grub_dl_dep_t) grub_malloc (sizeof (*dep)); + dep =3D (grub_dl_dep_t) grub_malloc_notrack (sizeof (*dep)); if (! dep) return grub_errno; =20 @@ -617,7 +617,7 @@ return 0; } =20 - mod =3D (grub_dl_t) grub_zalloc (sizeof (*mod)); + mod =3D (grub_dl_t) grub_zalloc_notrack (sizeof (*mod)); if (! mod) return 0; =20 =3D=3D=3D modified file 'grub-core/kern/env.c' --- grub-core/kern/env.c 2012-02-26 22:53:21 +0000 +++ grub-core/kern/env.c 2012-05-01 11:23:52 +0000 @@ -92,7 +92,7 @@ if (var->write_hook) var->value =3D var->write_hook (var, val); else - var->value =3D grub_strdup (val); + var->value =3D grub_strdup_notrack (val); =20 if (! var->value) { @@ -105,15 +105,15 @@ } =20 /* The variable does not exist, so create a new one. */ - var =3D grub_zalloc (sizeof (*var)); + var =3D grub_zalloc_notrack (sizeof (*var)); if (! var) return grub_errno; =20 - var->name =3D grub_strdup (name); + var->name =3D grub_strdup_notrack (name); if (! var->name) goto fail; =20 - var->value =3D grub_strdup (val); + var->value =3D grub_strdup_notrack (val); if (! var->value) goto fail; =20 =3D=3D=3D modified file 'grub-core/kern/main.c' --- grub-core/kern/main.c 2011-12-13 14:13:51 +0000 +++ grub-core/kern/main.c 2012-05-01 11:10:06 +0000 @@ -95,7 +95,7 @@ if (val[0] =3D=3D '(' && val[len - 1] =3D=3D ')') return grub_strndup (val + 1, len - 2); =20 - return grub_strdup (val); + return grub_strdup_notrack (val); } =20 static void =3D=3D=3D modified file 'grub-core/kern/misc.c' --- grub-core/kern/misc.c 2012-02-29 18:35:19 +0000 +++ grub-core/kern/misc.c 2012-05-01 10:54:23 +0000 @@ -437,21 +437,42 @@ } =20 char * -grub_strdup (const char *s) -{ - grub_size_t len; - char *p; - - len =3D grub_strlen (s) + 1; - p =3D (char *) grub_malloc (len); - if (! p) - return 0; - - return grub_memcpy (p, s, len); -} - +grub_strdup_notrack (const char *s) +{ + grub_size_t len; + char *p; + + len =3D grub_strlen (s) + 1; + p =3D (char *) grub_malloc_notrack (len); =20 + if (! p) + return 0; + + return grub_memcpy (p, s, len); +} + +#if MM_DEBUG +char * +grub_debug_strdup (const char *file, int line, const char *s) +{ + grub_size_t len; + char *p; + + len =3D grub_strlen (s) + 1; + p =3D (char *) grub_debug_malloc (file, line, len); + if (! p) + return 0; + + return grub_memcpy (p, s, len); +} +#endif + +#if MM_DEBUG +char * +grub_debug_strndup (const char *file, int line, const char *s, grub_size= _t n) +#else char * grub_strndup (const char *s, grub_size_t n) +#endif { grub_size_t len; char *p; @@ -459,7 +480,11 @@ len =3D grub_strlen (s); if (len > n) len =3D n; +#if MM_DEBUG + p =3D (char *) grub_debug_malloc (file, line, len + 1); +#else p =3D (char *) grub_malloc (len + 1); +#endif if (! p) return 0; =20 @@ -1051,8 +1076,13 @@ return ret; } =20 +#if MM_DEBUG +char * +grub_debug_xvasprintf (const char *file, int line, const char *fmt, va_l= ist ap) +#else char * grub_xvasprintf (const char *fmt, va_list ap) +#endif { grub_size_t s, as =3D PREALLOC_SIZE; char *ret; @@ -1060,7 +1090,11 @@ while (1) { va_list ap2; +#if MM_DEBUG + ret =3D grub_debug_malloc (file, line, as + 1); +#else ret =3D grub_malloc (as + 1); +#endif if (!ret) return NULL; =20 @@ -1078,14 +1112,23 @@ } } =20 +#if MM_DEBUG +char * +grub_debug_xasprintf (const char *file, int line, const char *fmt, ...) +#else char * grub_xasprintf (const char *fmt, ...) +#endif { va_list ap; char *ret; =20 va_start (ap, fmt); +#if MM_DEBUG + ret =3D grub_debug_xvasprintf (file, line, fmt, ap); +#else ret =3D grub_xvasprintf (fmt, ap); +#endif va_end (ap); =20 return ret; =3D=3D=3D modified file 'grub-core/kern/mm.c' --- grub-core/kern/mm.c 2012-02-03 13:20:31 +0000 +++ grub-core/kern/mm.c 2012-05-01 11:08:22 +0000 @@ -68,7 +68,7 @@ #include #include =20 -#ifdef MM_DEBUG +#if MM_DEBUG # undef grub_malloc # undef grub_zalloc # undef grub_realloc @@ -84,10 +84,13 @@ to the header and a pointer to its region, respectively. PTR must be allocated. */ static void -get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_= t *r) +get_header_from_pointer (void *ptr_in, grub_mm_header_t *p, grub_mm_regi= on_t *r) { - if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1)) - grub_fatal ("unaligned pointer %p", ptr); + void *ptr; + if ((grub_addr_t) ptr_in & (GRUB_MM_ALIGN - 1)) + grub_fatal ("unaligned pointer %p", ptr_in); + + ptr =3D (grub_mm_header_t) ptr_in - (GRUB_MM_CELLS_METADATA - 1); =20 for (*r =3D grub_mm_base; *r; *r =3D (*r)->next) if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1) @@ -95,11 +98,11 @@ break; =20 if (! *r) - grub_fatal ("out of range pointer %p", ptr); + grub_fatal ("out of range pointer %p", ptr_in); =20 *p =3D (grub_mm_header_t) ptr - 1; if ((*p)->magic !=3D GRUB_MM_ALLOC_MAGIC) - grub_fatal ("alloc magic is broken at %p", *p); + grub_fatal ("alloc magic is broken at %p: 0x%x", *p, (*p)->magic); } =20 /* Initialize a region starting from ADDR and whose size is SIZE, @@ -150,9 +153,12 @@ { grub_mm_header_t p, q; =20 + COMPILE_TIME_ASSERT (sizeof (struct grub_mm_header) =3D=3D GRUB_MM_ALI= GN); + COMPILE_TIME_ASSERT (sizeof (struct grub_mm_region) =3D=3D GRUB_MM_ALI= GN); + /* When everything is allocated side effect is that *first will have a= lloc magic marked, meaning that there is no room in this region. */ - if ((*first)->magic =3D=3D GRUB_MM_ALLOC_MAGIC) + if (!*first) return 0; =20 /* Try to search free slot for allocation in this memory region. */ @@ -160,7 +166,8 @@ { grub_off_t extra; =20 - extra =3D ((grub_addr_t) (p + 1) >> GRUB_MM_ALIGN_LOG2) % align; + extra =3D ((grub_addr_t) (p + GRUB_MM_CELLS_METADATA) + >> GRUB_MM_ALIGN_LOG2) & (align - 1); if (extra) extra =3D align - extra; =20 @@ -260,13 +267,18 @@ =20 p->magic =3D GRUB_MM_ALLOC_MAGIC; p->size =3D n; + p->next =3D 0; + p->prev =3D 0; =20 /* Mark find as a start marker for next allocation to fasten it. This will have side effect of fragmenting memory as small pieces before this will be un-used. */ - *first =3D q; + if (q->magic =3D=3D GRUB_MM_ALLOC_MAGIC) + *first =3D 0; + else + *first =3D q; =20 - return p + 1; + return p + GRUB_MM_CELLS_METADATA; } =20 /* Search was completed without result. */ @@ -279,10 +291,11 @@ =20 /* Allocate SIZE bytes with the alignment ALIGN and return the pointer. = */ void * -grub_memalign (grub_size_t align, grub_size_t size) +grub_memalign_notrack (grub_size_t align, grub_size_t size) { grub_mm_region_t r; - grub_size_t n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) += 1; + grub_size_t n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + + GRUB_MM_CELLS_METADATA; int count =3D 0; =20 if (!grub_mm_base) @@ -331,18 +344,18 @@ =20 /* Allocate SIZE bytes and return the pointer. */ void * -grub_malloc (grub_size_t size) +grub_malloc_notrack (grub_size_t size) { - return grub_memalign (0, size); + return grub_memalign_notrack (0, size); } =20 /* Allocate SIZE bytes, clear them and return the pointer. */ void * -grub_zalloc (grub_size_t size) +grub_zalloc_notrack (grub_size_t size) { void *ret; =20 - ret =3D grub_memalign (0, size); + ret =3D grub_memalign_notrack (0, size); if (ret) grub_memset (ret, 0, size); =20 @@ -361,7 +374,7 @@ =20 get_header_from_pointer (ptr, &p, &r); =20 - if (r->first->magic =3D=3D GRUB_MM_ALLOC_MAGIC) + if (!r->first) { p->magic =3D GRUB_MM_FREE_MAGIC; r->first =3D p->next =3D p; @@ -418,7 +431,7 @@ /* Reallocate SIZE bytes and return the pointer. The contents will be the same as that of PTR. */ void * -grub_realloc (void *ptr, grub_size_t size) +grub_realloc_notrack (void *ptr, grub_size_t size) { grub_mm_header_t p; grub_mm_region_t r; @@ -426,7 +439,7 @@ grub_size_t n; =20 if (! ptr) - return grub_malloc (size); + return grub_malloc_notrack (size); =20 if (! size) { @@ -435,13 +448,14 @@ } =20 /* FIXME: Not optimal. */ - n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1; + n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + + GRUB_MM_CELLS_METADATA; get_header_from_pointer (ptr, &p, &r); =20 if (p->size >=3D n) return ptr; =20 - q =3D grub_malloc (size); + q =3D grub_malloc_notrack (size); if (! q) return q; =20 @@ -450,7 +464,7 @@ return q; } =20 -#ifdef MM_DEBUG +#if MM_DEBUG int grub_mm_debug =3D 0; =20 void @@ -510,6 +524,50 @@ grub_printf ("\n"); } =20 +grub_mm_header_t grub_mm_allocated; + +static void +save_ref (void *ptr, int line, const char *file) +{ + grub_mm_header_t head; + struct grub_mm_alloc_info *info; + + COMPILE_TIME_ASSERT ((sizeof (struct grub_mm_alloc_info) + & (GRUB_MM_ALIGN - 1)) =3D=3D 0); + + if (!ptr) + return; + + head =3D ptr; + head -=3D GRUB_MM_CELLS_METADATA; + info =3D (struct grub_mm_alloc_info *) (head + 1); + head->next =3D grub_mm_allocated; + if (head->next) + head->next->prev =3D head; + head->prev =3D 0; + info->lineno =3D line; + grub_strncpy (info->file, file, sizeof (info->file)); + grub_mm_allocated =3D head; +} + +static void +delete_ref (void *ptr) +{ + grub_mm_header_t head; + + if (!ptr) + return; + + head =3D ptr; + head -=3D GRUB_MM_CELLS_METADATA; + if (head->next) + head->next->prev =3D head->prev; + if (head->prev) + head->prev->next =3D head->next; + else if (grub_mm_allocated =3D=3D head) + grub_mm_allocated =3D head->next; +} + void * grub_debug_malloc (const char *file, int line, grub_size_t size) { @@ -517,7 +575,8 @@ =20 if (grub_mm_debug) grub_printf ("%s:%d: malloc (0x%" PRIxGRUB_SIZE ") =3D ", file, line= , size); - ptr =3D grub_malloc (size); + ptr =3D grub_malloc_notrack (size); + save_ref (ptr, line, file); if (grub_mm_debug) grub_printf ("%p\n", ptr); return ptr; @@ -530,7 +589,8 @@ =20 if (grub_mm_debug) grub_printf ("%s:%d: zalloc (0x%" PRIxGRUB_SIZE ") =3D ", file, line= , size); - ptr =3D grub_zalloc (size); + ptr =3D grub_zalloc_notrack (size); + save_ref (ptr, line, file); if (grub_mm_debug) grub_printf ("%p\n", ptr); return ptr; @@ -539,6 +599,7 @@ void grub_debug_free (const char *file, int line, void *ptr) { + delete_ref (ptr); if (grub_mm_debug) grub_printf ("%s:%d: free (%p)\n", file, line, ptr); grub_free (ptr); @@ -547,9 +608,11 @@ void * grub_debug_realloc (const char *file, int line, void *ptr, grub_size_t s= ize) { + delete_ref (ptr); if (grub_mm_debug) grub_printf ("%s:%d: realloc (%p, 0x%" PRIxGRUB_SIZE ") =3D ", file,= line, ptr, size); - ptr =3D grub_realloc (ptr, size); + ptr =3D grub_realloc_notrack (ptr, size); + save_ref (ptr, line, file); if (grub_mm_debug) grub_printf ("%p\n", ptr); return ptr; @@ -564,7 +627,8 @@ if (grub_mm_debug) grub_printf ("%s:%d: memalign (0x%" PRIxGRUB_SIZE ", 0x%" PRIxGRUB_= SIZE =20 ") =3D ", file, line, align, size); - ptr =3D grub_memalign (align, size); + ptr =3D grub_memalign_notrack (align, size); + save_ref (ptr, line, file); if (grub_mm_debug) grub_printf ("%p\n", ptr); return ptr; =3D=3D=3D modified file 'grub-core/kern/parser.c' --- grub-core/kern/parser.c 2011-11-11 19:34:37 +0000 +++ grub-core/kern/parser.c 2012-05-01 11:07:30 +0000 @@ -204,12 +204,12 @@ } =20 /* Reserve memory for the return values. */ - args =3D grub_malloc (bp - buffer); + args =3D grub_malloc_notrack (bp - buffer); if (!args) return grub_errno; grub_memcpy (args, buffer, bp - buffer); =20 - *argv =3D grub_malloc (sizeof (char *) * (*argc + 1)); + *argv =3D grub_malloc_notrack (sizeof (char *) * (*argc + 1)); if (!*argv) { grub_free (args); =3D=3D=3D modified file 'grub-core/lib/relocator.c' --- grub-core/lib/relocator.c 2012-04-13 14:55:20 +0000 +++ grub-core/lib/relocator.c 2012-05-01 09:34:38 +0000 @@ -358,7 +358,9 @@ r2->first =3D r1->first; hl->next =3D r2->first; *rp =3D (*rp)->next; - grub_free (g + 1); + g->next =3D 0; + g->prev =3D 0; + grub_free (g + GRUB_MM_CELLS_METADATA); } break; } @@ -368,10 +370,11 @@ GRUB_MM_ALIGN); h->size =3D ((subchu->start + subchu->size + GRUB_MM_ALIGN - 1) / GRUB_MM_ALI= GN) - - (subchu->start / GRUB_MM_ALIGN) - 1; - h->next =3D h; + - (subchu->start / GRUB_MM_ALIGN); + h->next =3D 0; + h->prev =3D 0; h->magic =3D GRUB_MM_ALLOC_MAGIC; - grub_free (h + 1); + grub_free (h + GRUB_MM_CELLS_METADATA); break; } #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS @@ -428,6 +431,9 @@ unsigned j, N =3D 0; grub_addr_t target =3D 0; =20 + COMPILE_TIME_ASSERT (sizeof (struct grub_mm_header) =3D=3D GRUB_MM_ALI= GN); + COMPILE_TIME_ASSERT (sizeof (struct grub_mm_region) =3D=3D GRUB_MM_ALI= GN); + grub_dprintf ("relocator", "trying to allocate in 0x%lx-0x%lx aligned 0x%lx size 0x%lx\n", (unsigned long) start, (unsigned long) end, =3D=3D=3D modified file 'grub-core/normal/autofs.c' --- grub-core/normal/autofs.c 2012-02-04 20:47:29 +0000 +++ grub-core/normal/autofs.c 2012-05-01 11:04:40 +0000 @@ -104,13 +104,20 @@ =20 /* If the line is empty, skip it. */ if (p >=3D q) - continue; + { + grub_free (buf); + continue; + } =20 - fs_mod =3D grub_malloc (sizeof (*fs_mod)); + fs_mod =3D grub_malloc_notrack (sizeof (*fs_mod)); if (! fs_mod) - continue; + { + grub_free (buf); + continue; + } =20 - fs_mod->name =3D grub_strdup (p); + fs_mod->name =3D grub_strdup_notrack (p); + grub_free (buf); if (! fs_mod->name) { grub_free (fs_mod); =3D=3D=3D modified file 'grub-core/normal/charset.c' --- grub-core/normal/charset.c 2012-04-07 18:11:04 +0000 +++ grub-core/normal/charset.c 2012-05-01 11:44:22 +0000 @@ -160,19 +160,37 @@ =20 /* Convert UCS-4 to UTF-8. */ char * -grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t size) -{ - grub_uint8_t *ret; - grub_size_t cnt =3D grub_get_num_of_utf8_bytes (src, size) + 1; - - ret =3D grub_malloc (cnt); - if (!ret) - return 0; - - grub_ucs4_to_utf8 (src, size, ret, cnt); - - return (char *) ret; -} +grub_ucs4_to_utf8_alloc_notrack (const grub_uint32_t *src, grub_size_t s= ize) +{ + grub_uint8_t *ret; + grub_size_t cnt =3D grub_get_num_of_utf8_bytes (src, size) + 1; + + ret =3D grub_malloc_notrack (cnt); + if (!ret) + return 0; + + grub_ucs4_to_utf8 (src, size, ret, cnt); + + return (char *) ret; +} + +#if MM_DEBUG +char * +grub_ucs4_to_utf8_alloc_debug (const char *file, int line, + const grub_uint32_t *src, grub_size_t size) +{ + grub_uint8_t *ret; + grub_size_t cnt =3D grub_get_num_of_utf8_bytes (src, size) + 1; + + ret =3D grub_debug_malloc (file, line, cnt); + if (!ret) + return 0; + + grub_ucs4_to_utf8 (src, size, ret, cnt); + + return (char *) ret; +} +#endif =20 int grub_is_valid_utf8 (const grub_uint8_t *src, grub_size_t srcsize) @@ -269,7 +287,7 @@ unsigned i; struct grub_unicode_compact_range *cur; =20 - join_types =3D grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR); + join_types =3D grub_zalloc_notrack (GRUB_UNICODE_MAX_CACHED_CHAR); if (!join_types) { grub_errno =3D GRUB_ERR_NONE; @@ -289,7 +307,7 @@ unsigned i; struct grub_unicode_compact_range *cur; =20 - bidi_types =3D grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR); + bidi_types =3D grub_zalloc_notrack (GRUB_UNICODE_MAX_CACHED_CHAR); if (!bidi_types) { grub_errno =3D GRUB_ERR_NONE; @@ -367,7 +385,7 @@ if (!comb_types) { unsigned i; - comb_types =3D grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR); + comb_types =3D grub_zalloc_notrack (GRUB_UNICODE_MAX_CACHED_CHAR);= if (comb_types) for (cur =3D grub_unicode_compact; cur->len; cur++) for (i =3D cur->start; i < cur->start + (unsigned) cur->len =3D=3D=3D modified file 'grub-core/normal/cmdline.c' --- grub-core/normal/cmdline.c 2012-04-01 19:25:17 +0000 +++ grub-core/normal/cmdline.c 2012-05-01 11:21:01 +0000 @@ -41,7 +41,7 @@ grub_set_history (int newsize) { grub_uint32_t **old_hist_lines =3D hist_lines; - hist_lines =3D grub_malloc (sizeof (grub_uint32_t *) * newsize); + hist_lines =3D grub_malloc_notrack (sizeof (grub_uint32_t *) * newsize= ); =20 /* Copy the old lines into the new buffer. */ if (old_hist_lines) @@ -114,7 +114,7 @@ grub_history_set (int pos, grub_uint32_t *s, grub_size_t len) { grub_free (hist_lines[pos]); - hist_lines[pos] =3D grub_malloc ((len + 1) * sizeof (grub_uint32_t)); + hist_lines[pos] =3D grub_malloc_notrack ((len + 1) * sizeof (grub_uint= 32_t)); if (!hist_lines[pos]) { grub_print_error (); @@ -656,7 +656,7 @@ grub_history_add (empty, 0); } =20 - ret =3D grub_ucs4_to_utf8_alloc (buf, llen + 1); + ret =3D grub_ucs4_to_utf8_alloc_notrack (buf, llen + 1); grub_free (buf); grub_free (cl_terms); return ret; =3D=3D=3D modified file 'grub-core/normal/color.c' --- grub-core/normal/color.c 2011-11-30 15:20:13 +0000 +++ grub-core/normal/color.c 2012-05-01 11:07:03 +0000 @@ -133,7 +133,7 @@ =20 set_colors (); =20 - return grub_strdup (val); + return grub_strdup_notrack (val); } =20 /* Replace default `highlight' colors with the ones specified by user (i= f any). */ @@ -146,5 +146,5 @@ =20 set_colors (); =20 - return grub_strdup (val); + return grub_strdup_notrack (val); } =3D=3D=3D modified file 'grub-core/normal/crypto.c' --- grub-core/normal/crypto.c 2012-03-11 13:43:18 +0000 +++ grub-core/normal/crypto.c 2012-05-01 10:55:20 +0000 @@ -128,14 +128,14 @@ while (*p =3D=3D ' ' || *p =3D=3D '\t') p++; =20 - cur =3D grub_malloc (sizeof (*cur)); + cur =3D grub_malloc_notrack (sizeof (*cur)); if (!cur) { grub_errno =3D GRUB_ERR_NONE; continue; } =20 - cur->name =3D grub_strdup (name); + cur->name =3D grub_strdup_notrack (name); if (! name) { grub_errno =3D GRUB_ERR_NONE; @@ -143,7 +143,7 @@ continue; } =09 - cur->modname =3D grub_strdup (p); + cur->modname =3D grub_strdup_notrack (p); if (! cur->modname) { grub_errno =3D GRUB_ERR_NONE; =3D=3D=3D modified file 'grub-core/normal/dyncmd.c' --- grub-core/normal/dyncmd.c 2012-03-11 13:43:18 +0000 +++ grub-core/normal/dyncmd.c 2012-05-01 10:56:14 +0000 @@ -168,11 +168,11 @@ if (grub_dl_get (p)) continue; =20 - name =3D grub_strdup (name); + name =3D grub_strdup_notrack (name); if (! name) continue; =20 - modname =3D grub_strdup (p); + modname =3D grub_strdup_notrack (p); if (! modname) { grub_free (name); =3D=3D=3D modified file 'grub-core/normal/main.c' --- grub-core/normal/main.c 2012-03-11 13:43:18 +0000 +++ grub-core/normal/main.c 2012-05-01 11:13:07 +0000 @@ -41,8 +41,13 @@ int grub_normal_exit_level =3D 0; =20 /* Read a line from the file FILE. */ +#if MM_DEBUG +char * +grub_file_debug_getline (const char *cfile, int line, grub_file_t file) +#else char * grub_file_getline (grub_file_t file) +#endif { char c; grub_size_t pos =3D 0; @@ -51,7 +56,11 @@ grub_size_t max_len =3D 64; =20 /* Initially locate some space. */ - cmdline =3D grub_malloc (max_len); +#if MM_DEBUG + cmdline =3D grub_debug_malloc (cfile, line, max_len); +#else + cmdline =3D grub_debug_malloc (max_len); +#endif if (! cmdline) return 0; =20 @@ -69,7 +78,11 @@ { char *old_cmdline =3D cmdline; max_len =3D max_len * 2; +#if MM_DEBUG + cmdline =3D grub_debug_realloc (cfile, line, cmdline, max_len); +#else cmdline =3D grub_realloc (cmdline, max_len); +#endif if (! cmdline) { grub_free (old_cmdline); @@ -164,7 +177,7 @@ newmenu =3D grub_env_get_menu (); if (! newmenu) { - newmenu =3D grub_zalloc (sizeof (*newmenu)); + newmenu =3D grub_zalloc_notrack (sizeof (*newmenu)); if (! newmenu) return 0; =20 @@ -314,9 +327,13 @@ prefix =3D grub_env_get ("prefix"); if (prefix) { - config =3D grub_xasprintf ("%s/grub.cfg", prefix); + char *ptr; + config =3D grub_malloc_notrack (grub_strlen (prefix) + + sizeof ("/grub.cfg")); if (! config) goto quit; + ptr =3D grub_stpcpy (config, prefix); + grub_memcpy (ptr, "/grub.cfg", sizeof ("/grub.cfg")); =20 grub_enter_normal_mode (config); grub_free (config); =3D=3D=3D modified file 'grub-core/normal/term.c' --- grub-core/normal/term.c 2012-03-27 15:07:26 +0000 +++ grub-core/normal/term.c 2012-05-01 10:53:34 +0000 @@ -373,14 +373,14 @@ while (*p =3D=3D ' ' || *p =3D=3D '\t') p++; =20 - cur =3D grub_malloc (sizeof (*cur)); + cur =3D grub_malloc_notrack (sizeof (*cur)); if (!cur) { grub_errno =3D GRUB_ERR_NONE; continue; } =20 - cur->name =3D grub_strdup (name); + cur->name =3D grub_strdup_notrack (name); if (! name) { grub_errno =3D GRUB_ERR_NONE; @@ -388,7 +388,7 @@ continue; } =09 - cur->modname =3D grub_strdup (p); + cur->modname =3D grub_strdup_notrack (p); if (! cur->modname) { grub_errno =3D GRUB_ERR_NONE; =3D=3D=3D modified file 'grub-core/script/argv.c' --- grub-core/script/argv.c 2012-03-19 12:29:43 +0000 +++ grub-core/script/argv.c 2012-05-01 11:06:18 +0000 @@ -85,7 +85,8 @@ if (argv->args && argv->argc && argv->args[argv->argc - 1] =3D=3D 0) return 0; =20 - p =3D grub_realloc (p, round_up_exp ((argv->argc + 2) * sizeof (char *= ))); + p =3D grub_realloc_notrack (p, + round_up_exp ((argv->argc + 2) * sizeof (char *))); if (! p) return 1; =20 @@ -111,7 +112,7 @@ =20 a =3D p ? grub_strlen (p) : 0; =20 - p =3D grub_realloc (p, round_up_exp ((a + slen + 1) * sizeof (char)));= + p =3D grub_realloc_notrack (p, round_up_exp ((a + slen + 1) * sizeof (= char))); if (! p) return 1; =20 =3D=3D=3D modified file 'grub-core/script/script.c' --- grub-core/script/script.c 2010-11-25 13:26:20 +0000 +++ grub-core/script/script.c 2012-05-01 11:05:25 +0000 @@ -42,8 +42,8 @@ grub_script_malloc (struct grub_parser_param *state, grub_size_t size) { struct grub_script_mem *mem; - mem =3D (struct grub_script_mem *) grub_malloc (size + sizeof (*mem) - - sizeof (char)); + mem =3D (struct grub_script_mem *) grub_malloc_notrack (size + sizeof = (*mem) + - sizeof (char)); if (!mem) return 0; =20 @@ -324,7 +324,7 @@ { struct grub_script *parsed; =20 - parsed =3D grub_malloc (sizeof (*parsed)); + parsed =3D grub_malloc_notrack (sizeof (*parsed)); if (! parsed) return 0; =20 =3D=3D=3D modified file 'grub-core/term/gfxterm.c' --- grub-core/term/gfxterm.c 2012-03-11 22:36:35 +0000 +++ grub-core/term/gfxterm.c 2012-05-01 11:45:31 +0000 @@ -232,9 +232,9 @@ =20 /* Allocate memory for text buffer. */ virtual_screen.text_buffer =3D - (struct grub_colored_char *) grub_malloc (virtual_screen.columns - * virtual_screen.rows - * sizeof (*virtual_screen.= text_buffer)); + (struct grub_colored_char *) grub_malloc_notrack (virtual_screen.col= umns + * virtual_screen.rows + * sizeof (*virtual_screen.text_buffer)); if (grub_errno !=3D GRUB_ERR_NONE) return grub_errno; =20 =3D=3D=3D modified file 'grub-core/video/fb/video_fb.c' --- grub-core/video/fb/video_fb.c 2012-02-08 18:26:01 +0000 +++ grub-core/video/fb/video_fb.c 2012-05-01 11:45:19 +0000 @@ -128,9 +128,9 @@ if (start + count > framebuffer.palette_size) { framebuffer.palette_size =3D start + count; - framebuffer.palette =3D grub_realloc (framebuffer.palette, - sizeof (framebuffer.palette[0]) - * framebuffer.palette_size); + framebuffer.palette =3D grub_realloc_notrack (framebuffer.palette,= + sizeof (framebuffer.palette[0]) + * framebuffer.palette_size); if (!framebuffer.palette) { grub_video_fb_fini (); @@ -1134,7 +1134,7 @@ "invalid argument given"); =20 /* Allocate memory for render target. */ - target =3D grub_malloc (sizeof (struct grub_video_fbrender_target)); + target =3D grub_malloc_notrack (sizeof (struct grub_video_fbrender_tar= get)); if (! target) return grub_errno; =20 @@ -1173,7 +1173,7 @@ /* Calculate size needed for the data. */ size =3D (width * target->mode_info.bytes_per_pixel) * height; =20 - target->data =3D grub_malloc (size); + target->data =3D grub_malloc_notrack (size); if (! target->data) { grub_free (target); @@ -1209,7 +1209,7 @@ #endif =20 /* Allocate memory for render target. */ - target =3D grub_malloc (sizeof (struct grub_video_fbrender_target)); + target =3D grub_malloc_notrack (sizeof (struct grub_video_fbrender_tar= get)); if (! target) return grub_errno; =20 =3D=3D=3D modified file 'include/grub/charset.h' --- include/grub/charset.h 2012-04-07 18:11:04 +0000 +++ include/grub/charset.h 2012-05-01 11:20:01 +0000 @@ -273,7 +273,18 @@ } =20 /* Convert UCS-4 to UTF-8. */ -char *grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t siz= e); +#if MM_DEBUG +char * +grub_ucs4_to_utf8_alloc_debug (const char *file, int line, + const grub_uint32_t *src, grub_size_t size); +#define grub_ucs4_to_utf8_alloc(src, size) grub_ucs4_to_utf8_alloc_debug= (GRUB_FILE, __LINE__, src, size) +#else +#define grub_ucs4_to_utf8_alloc_notrack grub_ucs4_to_utf8_alloc +#endif + +char *grub_ucs4_to_utf8_alloc_notrack (const grub_uint32_t *src, + grub_size_t size); + =20 int grub_is_valid_utf8 (const grub_uint8_t *src, grub_size_t srcsize); =3D=3D=3D modified file 'include/grub/misc.h' --- include/grub/misc.h 2012-03-05 00:17:55 +0000 +++ include/grub/misc.h 2012-05-01 10:54:59 +0000 @@ -314,8 +314,17 @@ } } =20 -char *EXPORT_FUNC(grub_strdup) (const char *s) __attribute__ ((warn_unus= ed_result)); +#if MM_DEBUG +char *EXPORT_FUNC(grub_debug_strdup) (const char *file, int line, const = char *s) __attribute__ ((warn_unused_result)); +char *EXPORT_FUNC(grub_debug_strndup) (const char *file, int line, const= char *s, grub_size_t n) __attribute__ ((warn_unused_result)); + +#define grub_strdup(s) grub_debug_strdup (GRUB_FILE, __LINE__, s) +#define grub_strndup(s, n) grub_debug_strndup (GRUB_FILE, __LINE__, s, n= ) +#else char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n) __attribu= te__ ((warn_unused_result)); +#define grub_strdup_notrack grub_strdup +#endif +char *EXPORT_FUNC(grub_strdup_notrack) (const char *s) __attribute__ ((w= arn_unused_result)); void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n); grub_size_t EXPORT_FUNC(grub_strlen) (const char *s) __attribute__ ((war= n_unused_result)); int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((form= at (printf, 1, 2))); @@ -363,9 +372,17 @@ __attribute__ ((format (printf, 3, 4))); int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *f= mt, va_list args); +#if MM_DEBUG +char *EXPORT_FUNC(grub_debug_xasprintf) (const char *file, int line, con= st char *fmt, ...) + __attribute__ ((format (printf, 3, 4))) __attribute__ ((warn_unused= _result)); +char *EXPORT_FUNC(grub_debug_xvasprintf) (const char *file, int line, co= nst char *fmt, va_list args) __attribute__ ((warn_unused_result)); +#define grub_xasprintf(fmt, args...) grub_debug_xasprintf (GRUB_FILE, __= LINE__, fmt, ## args) +#define grub_xvasprintf(fmt, args) grub_debug_xvasprintf (GRUB_FILE, __L= INE__, fmt, args) +#else char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))) __attribute__ ((warn_unused= _result)); char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) __att= ribute__ ((warn_unused_result)); +#endif void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn)); void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn)); grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n, =3D=3D=3D modified file 'include/grub/mm.h' --- include/grub/mm.h 2011-11-13 11:48:39 +0000 +++ include/grub/mm.h 2012-05-01 11:59:32 +0000 @@ -28,18 +28,25 @@ # define NULL ((void *) 0) #endif =20 +#if !MM_DEBUG +#define grub_malloc_notrack grub_malloc +#define grub_realloc_notrack grub_realloc +#define grub_zalloc_notrack grub_zalloc +#define grub_memalign_notrack grub_memealign +#endif + void grub_mm_init_region (void *addr, grub_size_t size); -void *EXPORT_FUNC(grub_malloc) (grub_size_t size); -void *EXPORT_FUNC(grub_zalloc) (grub_size_t size); +void *EXPORT_FUNC(grub_malloc_notrack) (grub_size_t size); +void *EXPORT_FUNC(grub_zalloc_notrack) (grub_size_t size); void EXPORT_FUNC(grub_free) (void *ptr); -void *EXPORT_FUNC(grub_realloc) (void *ptr, grub_size_t size); -void *EXPORT_FUNC(grub_memalign) (grub_size_t align, grub_size_t size); +void *EXPORT_FUNC(grub_realloc_notrack) (void *ptr, grub_size_t size); +void *EXPORT_FUNC(grub_memalign_notrack) (grub_size_t align, grub_size_t= size); =20 void grub_mm_check_real (char *file, int line); #define grub_mm_check() grub_mm_check_real (GRUB_FILE, __LINE__); =20 /* For debugging. */ -#if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_E= MU) +#if MM_DEBUG /* Set this variable to 1 when you want to trace all memory function cal= ls. */ extern int EXPORT_VAR(grub_mm_debug); =20 =3D=3D=3D modified file 'include/grub/mm_private.h' --- include/grub/mm_private.h 2010-08-28 12:52:25 +0000 +++ include/grub/mm_private.h 2012-05-01 11:09:26 +0000 @@ -30,13 +30,7 @@ struct grub_mm_header *next; grub_size_t size; grub_size_t magic; -#if GRUB_CPU_SIZEOF_VOID_P =3D=3D 4 - char padding[4]; -#elif GRUB_CPU_SIZEOF_VOID_P =3D=3D 8 - char padding[8]; -#else -# error "unknown word size" -#endif + struct grub_mm_header *prev; } *grub_mm_header_t; =20 @@ -48,6 +42,17 @@ =20 #define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2) =20 +#if MM_DEBUG +struct grub_mm_alloc_info +{ + int lineno; + char file[64 - sizeof (int)]; +}; +static const grub_size_t GRUB_MM_CELLS_METADATA =3D ((sizeof (struct gru= b_mm_alloc_info) >> GRUB_MM_ALIGN_LOG2) + 1); +#else +#define GRUB_MM_CELLS_METADATA 1 +#endif + typedef struct grub_mm_region { struct grub_mm_header *first; @@ -61,4 +66,8 @@ extern grub_mm_region_t EXPORT_VAR (grub_mm_base); #endif =20 +#if MM_DEBUG +extern grub_mm_header_t EXPORT_VAR(grub_mm_allocated); +#endif + #endif =3D=3D=3D modified file 'include/grub/normal.h' --- include/grub/normal.h 2012-03-10 22:25:34 +0000 +++ include/grub/normal.h 2012-05-01 10:15:01 +0000 @@ -51,7 +51,14 @@ void grub_menu_init_page (int nested, int edit, int *num_entries, struct grub_term_output *term); void grub_normal_init_page (struct grub_term_output *term); +#if MM_DEBUG +char *grub_file_debug_getline (const char *call_file, + int line, grub_file_t file); +#define grub_file_getline(file) \ + grub_file_debug_getline (GRUB_FILE, __LINE__, file) +#else char *grub_file_getline (grub_file_t file); +#endif void grub_cmdline_run (int nested); =20 /* Defined in `cmdline.c'. */ =3D=3D=3D modified file 'include/grub/unicode.h' --- include/grub/unicode.h 2012-03-27 15:07:26 +0000 +++ include/grub/unicode.h 2012-05-01 11:44:04 +0000 @@ -237,13 +237,14 @@ static inline struct grub_unicode_glyph * grub_unicode_glyph_dup (const struct grub_unicode_glyph *in) { - struct grub_unicode_glyph *out =3D grub_malloc (sizeof (*out)); + struct grub_unicode_glyph *out =3D grub_malloc_notrack (sizeof (*out))= ; if (!out) return NULL; grub_memcpy (out, in, sizeof (*in)); if (in->combining) { - out->combining =3D grub_malloc (in->ncomb * sizeof (out->combining= [0])); + out->combining =3D grub_malloc_notrack (in->ncomb + * sizeof (out->combining[0])); if (!out->combining) { grub_free (out); @@ -259,7 +260,7 @@ grub_unicode_glyph_from_code (grub_uint32_t code) { struct grub_unicode_glyph *ret; - ret =3D grub_zalloc (sizeof (*ret)); + ret =3D grub_zalloc_notrack (sizeof (*ret)); if (!ret) return NULL; =20 --------------040001040801000607030502-- --------------enig028C6FB8C87BFBB077C058F9 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iF4EAREKAAYFAk+f0kcACgkQNak7dOguQgmbUwEAnaEVQ51mLi/7KToTj8z5th/M wMz7UNLi0PQX2c7EYHwA/jjdm6W7uNYAwKbb91LI3N6CrAIIdrvmyTAe3K7tV1d0 =u5qV -----END PGP SIGNATURE----- --------------enig028C6FB8C87BFBB077C058F9--