From owner-freebsd-current@FreeBSD.ORG Wed Nov 2 02:12:28 2005 Return-Path: X-Original-To: freebsd-current@freebsd.org Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3749016A41F for ; Wed, 2 Nov 2005 02:12:28 +0000 (GMT) (envelope-from nocool@263.net) Received: from smtp.263.net (smtp.263.net [211.150.96.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 77BA843D45; Wed, 2 Nov 2005 02:12:26 +0000 (GMT) (envelope-from nocool@263.net) Received: from iscas-zfw728iit (smtp1 [127.0.0.1]) by smtp.263.net (Postfix) with ESMTP id B1542E5E; Wed, 2 Nov 2005 10:12:26 +0800 (CST) (envelope-from nocool@263.net) X-Originating-IP: [159.226.5.225] Date: Wed, 2 Nov 2005 10:13:08 +0800 From: "nocool" To: "freebsd-current" X-mailer: Foxmail 5.0 [cn] Mime-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 7bit Message-Id: <20051102021226.B1542E5E@smtp.263.net> Cc: freebsd-hacker Subject: Why INVARIANTS option and sanity checking? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Nov 2005 02:12:28 -0000 Hi, I need some explanation about INVARIANTS compile option. This option has the description that enable calls of extra sanity checking. What does sanity mean here? Where and why we need to use this option? I find some codes in kern/kern_malloc.c in 5.4 kernel: 511 kmemzones[indx].kz_zone = uma_zcreate(name, size, 512 #ifdef INVARIANTS 513 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 514 #else 515 NULL, NULL, NULL, NULL, 516 #endif 517 UMA_ALIGN_PTR, UMA_ZONE_MALLOC); In the case INVARIANTS is defined, kz_zone will be set up with the constructor function mtrash_ctor and destructor function mtrash_dtor. When kz_zone free some items, kernel will call mtrash_dtor(), every item will be filled with the value of uma_junk. When some items will be reallocated, kernel calls mtrash_ctor() and makes sure the constructing item has'nt been overwritten since it was freed through comparing every int of the item with uma_junk. Why kmemzones need this check, while other zones and memory areas need't? Where comes the danger that the memory item will be overwritted after its free? Thanks