Module Name:    src
Committed By:   rillig
Date:           Sat Jul 15 09:40:37 UTC 2023

Modified Files:
        src/tests/usr.bin/xlint/lint1: d_lint_assert.c
        src/usr.bin/xlint/lint1: cgram.y decl.c lex.c mem1.c

Log Message:
lint: add debug logging for memory management and the symbol table


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/d_lint_assert.c
cvs rdiff -u -r1.461 -r1.462 src/usr.bin/xlint/lint1/cgram.y
cvs rdiff -u -r1.356 -r1.357 src/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.186 -r1.187 src/usr.bin/xlint/lint1/lex.c
cvs rdiff -u -r1.68 -r1.69 src/usr.bin/xlint/lint1/mem1.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/d_lint_assert.c
diff -u src/tests/usr.bin/xlint/lint1/d_lint_assert.c:1.7 src/tests/usr.bin/xlint/lint1/d_lint_assert.c:1.8
--- src/tests/usr.bin/xlint/lint1/d_lint_assert.c:1.7	Tue Mar 28 14:44:34 2023
+++ src/tests/usr.bin/xlint/lint1/d_lint_assert.c	Sat Jul 15 09:40:37 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: d_lint_assert.c,v 1.7 2023/03/28 14:44:34 rillig Exp $	*/
+/*	$NetBSD: d_lint_assert.c,v 1.8 2023/07/15 09:40:37 rillig Exp $	*/
 # 3 "d_lint_assert.c"
 
 /*
@@ -23,3 +23,23 @@ enum {
  */
 /* expect+1: warning: old-style declaration; add 'int' [1] */
 c(void());
+
+
+// As of 2023-07-15, the following code leads to a crash, due to the word
+// 'unknown_type_modifier'.  The parser then goes into error recovery mode and
+// discards the declaration in the 'for' loop.  In the end, the symbol table
+// still contains symbols that were already freed when parsing the '}' from the
+// 'switch' statement.  To reproduce the crash, run 'make -DDEBUG DBG="-O0 -g"'
+// and run with -Sy.
+//
+// static inline void
+// f(void)
+// {
+//	int i = 3;
+//
+//	for (unknown_type_modifier char *p = "";; ) {
+//		switch (i) {
+//		case 3:;
+//		}
+//	}
+// }

Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.461 src/usr.bin/xlint/lint1/cgram.y:1.462
--- src/usr.bin/xlint/lint1/cgram.y:1.461	Thu Jul 13 23:11:11 2023
+++ src/usr.bin/xlint/lint1/cgram.y	Sat Jul 15 09:40:36 2023
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.461 2023/07/13 23:11:11 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.462 2023/07/15 09:40:36 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: cgram.y,v 1.461 2023/07/13 23:11:11 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.462 2023/07/15 09:40:36 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -1782,6 +1782,8 @@ compound_statement_lbrace:
 	T_LBRACE {
 		block_level++;
 		mem_block_level++;
+		debug_step("%s: mem_block_level = %zu",
+		    "compound_statement_lbrace", mem_block_level);
 		begin_declaration_level(DLK_AUTO);
 	}
 ;
@@ -1791,6 +1793,8 @@ compound_statement_rbrace:
 		end_declaration_level();
 		level_free_all(mem_block_level);
 		mem_block_level--;
+		debug_step("%s: mem_block_level = %zu",
+		    "compound_statement_rbrace", mem_block_level);
 		block_level--;
 		suppress_fallthrough = false;
 	}

Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.356 src/usr.bin/xlint/lint1/decl.c:1.357
--- src/usr.bin/xlint/lint1/decl.c:1.356	Fri Jul 14 09:20:23 2023
+++ src/usr.bin/xlint/lint1/decl.c	Sat Jul 15 09:40:36 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.356 2023/07/14 09:20:23 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.357 2023/07/15 09:40:36 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: decl.c,v 1.356 2023/07/14 09:20:23 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.357 2023/07/15 09:40:36 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -521,7 +521,6 @@ end_declaration_level(void)
 {
 
 	debug_dcs(true);
-	debug_leave();
 
 	decl_level *dl = dcs;
 	dcs = dl->d_enclosing;
@@ -578,6 +577,7 @@ end_declaration_level(void)
 		lint_assert(/*CONSTCOND*/false);
 	}
 	free(dl);
+	debug_leave();
 }
 
 /*
@@ -2789,6 +2789,7 @@ global_clean_up(void)
 	clean_up_after_error();
 	block_level = 0;
 	mem_block_level = 0;
+	debug_step("%s: mem_block_level = %zu", __func__, mem_block_level);
 	global_clean_up_decl(true);
 }
 

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.186 src/usr.bin/xlint/lint1/lex.c:1.187
--- src/usr.bin/xlint/lint1/lex.c:1.186	Fri Jul 14 09:32:42 2023
+++ src/usr.bin/xlint/lint1/lex.c	Sat Jul 15 09:40:36 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.186 2023/07/14 09:32:42 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.187 2023/07/15 09:40:36 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: lex.c,v 1.186 2023/07/14 09:32:42 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.187 2023/07/15 09:40:36 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -306,6 +306,7 @@ debug_symtab(void)
 {
 	struct syms syms = { xcalloc(64, sizeof(syms.items[0])), 0, 64 };
 
+	debug_enter();
 	for (int level = -1;; level++) {
 		bool more = false;
 		size_t n = sizeof(symtab) / sizeof(symtab[0]);
@@ -323,7 +324,7 @@ debug_symtab(void)
 		}
 
 		if (syms.len > 0) {
-			debug_printf("symbol table level %d\n", level);
+			debug_step("symbol table level %d", level);
 			debug_indent_inc();
 			qsort(syms.items, syms.len, sizeof(syms.items[0]),
 			    sym_by_name);
@@ -337,6 +338,7 @@ debug_symtab(void)
 		if (!more)
 			break;
 	}
+	debug_leave();
 
 	free(syms.items);
 }
@@ -1278,7 +1280,7 @@ lex_next_line(void)
 {
 	curr_pos.p_line++;
 	curr_pos.p_uniq = 0;
-	debug_step("parsing %s:%d", curr_pos.p_file, curr_pos.p_line);
+	debug_printf("parsing %s:%d\n", curr_pos.p_file, curr_pos.p_line);
 	if (curr_pos.p_file == csrc_pos.p_file) {
 		csrc_pos.p_line++;
 		csrc_pos.p_uniq = 0;
@@ -1423,12 +1425,16 @@ void
 symtab_remove_level(sym_t *syms)
 {
 
+	if (syms != NULL)
+		debug_step("%s %d", __func__, syms->s_block_level);
+
 	/* Note the use of s_level_next instead of s_symtab_next. */
 	for (sym_t *sym = syms; sym != NULL; sym = sym->s_level_next) {
 		if (sym->s_block_level != -1) {
-			debug_step("symtab_remove_level '%s' %s '%s'",
+			debug_step("%s '%s' %s '%s' %d", __func__,
 			    sym->s_name, symt_name(sym->s_kind),
-			    type_name(sym->s_type));
+			    type_name(sym->s_type),
+			    sym->s_block_level);
 			symtab_remove(sym);
 			sym->s_symtab_ref = NULL;
 		}
@@ -1440,10 +1446,11 @@ void
 inssym(int level, sym_t *sym)
 {
 
-	debug_step("inssym '%s' %s '%s'",
-	    sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type));
-	symtab_add(sym);
+	debug_step("%s '%s' %s '%s' %d", __func__,
+	    sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type),
+	    level);
 	sym->s_block_level = level;
+	symtab_add(sym);
 
 	/*
 	 * Placing the inner symbols to the beginning of the list ensures

Index: src/usr.bin/xlint/lint1/mem1.c
diff -u src/usr.bin/xlint/lint1/mem1.c:1.68 src/usr.bin/xlint/lint1/mem1.c:1.69
--- src/usr.bin/xlint/lint1/mem1.c:1.68	Thu Jul 13 08:40:38 2023
+++ src/usr.bin/xlint/lint1/mem1.c	Sat Jul 15 09:40:36 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: mem1.c,v 1.68 2023/07/13 08:40:38 rillig Exp $	*/
+/*	$NetBSD: mem1.c,v 1.69 2023/07/15 09:40:36 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: mem1.c,v 1.68 2023/07/13 08:40:38 rillig Exp $");
+__RCSID("$NetBSD: mem1.c,v 1.69 2023/07/15 09:40:36 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -235,6 +235,7 @@ void
 level_free_all(size_t level)
 {
 
+	debug_step("%s %zu", __func__, level);
 	mpool_free(mpool_at(level));
 }
 
@@ -283,6 +284,7 @@ void
 expr_free_all(void)
 {
 
+	debug_step("%s", __func__);
 	mpool_free(&expr_pool);
 }
 

Reply via email to