diff mbox

[v2,1/2] kconfig: fix bad syntactic transformation in expr.c

Message ID 1411405999-20212-2-git-send-email-walch.martin@web.de (mailing list archive)
State New, archived
Headers show

Commit Message

Martin Walch Sept. 22, 2014, 5:13 p.m. UTC
expr_eliminate_dups2() in scripts/kconfig/expr.c applies two bad
inference rules:

(FOO || BAR) && (!FOO && !BAR) -> n
(FOO && BAR) || (!FOO || !BAR) -> y

They would be correct in propositional logic, but this is a three-valued
logic, and here it is wrong in that it changes semantics. It becomes
immediately visible when substituting FOO and BAR with m.

Fix it by removing expr_eliminate_dups2() and the functions that have no use
anywhere else: expr_extract_eq_and(), expr_extract_eq_or(),
and expr_extract_eq() from scripts/kconfig/expr.[ch]

Currently the bug is not triggered in mainline, so this patch does not modify
the configuration space there.

As a side effect, this reduces code size in expr.c by roughly 10% and slightly
improves startup time for all configuration frontends.

Signed-off-by: Martin Walch <walch.martin@web.de>
---
 scripts/kconfig/expr.c | 108 -------------------------------------------------
 scripts/kconfig/expr.h |   3 --
 2 files changed, 111 deletions(-)

Comments

Paul Bolle Oct. 7, 2014, 8:10 a.m. UTC | #1
On Mon, 2014-09-22 at 19:13 +0200, Martin Walch wrote:
> expr_eliminate_dups2() in scripts/kconfig/expr.c applies two bad
> inference rules:
> 
> (FOO || BAR) && (!FOO && !BAR) -> n
> (FOO && BAR) || (!FOO || !BAR) -> y
> 
> They would be correct in propositional logic, but this is a three-valued
> logic, and here it is wrong in that it changes semantics. It becomes
> immediately visible when substituting FOO and BAR with m.

Well, not to me. When I apply the rules under "Menu dependencies" in
Documentation/kbuild/kconfig-language.txt, in embarrassingly small
steps, I get:
    (m || m) && (!m && !m) ->
    (1 || 1) && ((2 - 1) && (2 - 1)) ->
    (1 || 1) && (1 && 1) ->
    max(1, 1) && min(1, 1) ->
    1 && 1 ->
    min(1, 1) ->
    1 -> m

and
    (m && m) || (!m || !m) ->
    (1 && 1) || ((2 - 1) || (2 - 1)) ->
    (1 && 1) || (1 || 1) ->
    min(1, 1) || max(1, 1) ->
    1 || 1 ->
    max(1, 1) ->
    1 -> m

Did I do that right? Anyway, perhaps you'd like to add a line or two how
these two expressions actually evaluate when FOO and BAR are m.

> Fix it by removing expr_eliminate_dups2() and the functions that have no use
> anywhere else: expr_extract_eq_and(), expr_extract_eq_or(),
> and expr_extract_eq() from scripts/kconfig/expr.[ch]
> 
> Currently the bug is not triggered in mainline, so this patch does not modify
> the configuration space there.
> 
> As a side effect, this reduces code size in expr.c by roughly 10% and slightly
> improves startup time for all configuration frontends.
> 
> Signed-off-by: Martin Walch <walch.martin@web.de>

Does Yann still care about Kconfig? Yann has been rather quiet for a
while now.


Paul Bolle

--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Dirk Gouders Oct. 7, 2014, 9:21 a.m. UTC | #2
CC: Michal added

Paul Bolle <pebolle@tiscali.nl> writes:

> On Mon, 2014-09-22 at 19:13 +0200, Martin Walch wrote:
>> expr_eliminate_dups2() in scripts/kconfig/expr.c applies two bad
>> inference rules:
>> 
>> (FOO || BAR) && (!FOO && !BAR) -> n
>> (FOO && BAR) || (!FOO || !BAR) -> y
>> 
>> They would be correct in propositional logic, but this is a three-valued
>> logic, and here it is wrong in that it changes semantics. It becomes
>> immediately visible when substituting FOO and BAR with m.
>
> Well, not to me. When I apply the rules under "Menu dependencies" in
> Documentation/kbuild/kconfig-language.txt, in embarrassingly small
> steps, I get:
>     (m || m) && (!m && !m) ->
>     (1 || 1) && ((2 - 1) && (2 - 1)) ->
>     (1 || 1) && (1 && 1) ->
>     max(1, 1) && min(1, 1) ->
>     1 && 1 ->
>     min(1, 1) ->
>     1 -> m
>
> and
>     (m && m) || (!m || !m) ->
>     (1 && 1) || ((2 - 1) || (2 - 1)) ->
>     (1 && 1) || (1 || 1) ->
>     min(1, 1) || max(1, 1) ->
>     1 || 1 ->
>     max(1, 1) ->
>     1 -> m
>
> Did I do that right? Anyway, perhaps you'd like to add a line or two how
> these two expressions actually evaluate when FOO and BAR are m.

Perhaps, it helps to use a Kconfig file that visualizes the problem:

mainmenu "Expr Test"

config expr-config
	bool "Expr Config"
	depends on (m || m) && (!m && !m)
#	depends on (m && m) || (!m || !m)

config dummy
       bool "Dummy"

config modules
       bool
       option modules
       default y

Those two depends, according to your calculation and if I understood
correctly also to Martin's, should evaluate to 'm' and thus the menu
entry should always be visible, because both depends should evaluate to
'm'.  But because of expr_eliminate_dups2() it is either invisible (the
first depends becomes 'n') or always visible (the second depends becomes
'y').

Dirk

>> Fix it by removing expr_eliminate_dups2() and the functions that have no use
>> anywhere else: expr_extract_eq_and(), expr_extract_eq_or(),
>> and expr_extract_eq() from scripts/kconfig/expr.[ch]
>> 
>> Currently the bug is not triggered in mainline, so this patch does not modify
>> the configuration space there.
>> 
>> As a side effect, this reduces code size in expr.c by roughly 10% and slightly
>> improves startup time for all configuration frontends.
>> 
>> Signed-off-by: Martin Walch <walch.martin@web.de>
>
> Does Yann still care about Kconfig? Yann has been rather quiet for a
> while now.
>
>
> Paul Bolle
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Martin Walch Oct. 11, 2014, 12:20 a.m. UTC | #3
On Tuesday 07 October 2014 11:21:51 Dirk Gouders wrote:
> Paul Bolle <pebolle@tiscali.nl> writes:
> 
> > On Mon, 2014-09-22 at 19:13 +0200, Martin Walch wrote:
> >> expr_eliminate_dups2() in scripts/kconfig/expr.c applies two bad
> >> inference rules:
> >> 
> >> (FOO || BAR) && (!FOO && !BAR) -> n
> >> (FOO && BAR) || (!FOO || !BAR) -> y
> >> 
> >> They would be correct in propositional logic, but this is a three-valued
> >> logic, and here it is wrong in that it changes semantics. It becomes
> >> immediately visible when substituting FOO and BAR with m.
> >
> > Well, not to me. When I apply the rules under "Menu dependencies" in
> > Documentation/kbuild/kconfig-language.txt, in embarrassingly small
> > steps, I get:
> >     (m || m) && (!m && !m) ->
> >     (1 || 1) && ((2 - 1) && (2 - 1)) ->
> >     (1 || 1) && (1 && 1) ->
> >     max(1, 1) && min(1, 1) ->
> >     1 && 1 ->
> >     min(1, 1) ->
> >     1 -> m
> >
> > and
> >     (m && m) || (!m || !m) ->
> >     (1 && 1) || ((2 - 1) || (2 - 1)) ->
> >     (1 && 1) || (1 || 1) ->
> >     min(1, 1) || max(1, 1) ->
> >     1 || 1 ->
> >     max(1, 1) ->
> >     1 -> m
> >
> > Did I do that right? Anyway, perhaps you'd like to add a line or two how
> > these two expressions actually evaluate when FOO and BAR are m.

Yes, this is a proper evaluation.

To explain the problem in more detail, I will describe it a bit more formal.
First of all, this is a thing about the formal system of the configuration
logic, so I think it is a good idea to distinguish between syntax and
semantics.

Any expression, e.g. "(FOO || BAR)" is pure syntax. It consists of
non-constant symbols (FOO, BAR...), constant symbols (n, m, y),
operators (||, &&, !), and parenthesis.

Semantics is when evaluating an expression by assigning values to the
constant and the non-constant symbols and functions to the operators
(overriding operator precedence with parenthesis), and then calculating
an actual value.

So, this is pure syntax: 
(FOO || BAR) && (!FOO && !BAR)

And semantics is:
* an assignment of 0, 1, or 2 to each of the non-constant symbols (arbitrary)
* the fixed assignments of 0 to n, 1 to m, and 2 to y
* applying the functions to values that correspond to the respective operators

So, e.g. when assigning 2 to FOO and 0 to BAR, then the evaluation of 

(FOO || BAR) && (!FOO && !BAR)

is 

min(max(2, 0), min(2-2, 2-0)) = 0

Above, we saw that assigning 1 to FOO as well as to BAR yields 1 for the
whole expression.

Now getting back to the problem: the configuration system makes some
syntactical transformations according to some rules, so they are easier to
read in the user interfaces (e.g. mconf). In formal logic, such rules are
called "inference rules". Obviously, inference rules are purely syntactical
transformations, e.g. one rule could be to replace "FOO && FOO" with only
"FOO". Another rule could replace "FOO || BAR" with "FOO && BAR". The
difference between these two example rules is that in semantics, the first
one keeps the result of the evaluation for all possible assignments, while
the second one changes the results for some of the assignments. Formally,
the first rule is "valid" while the second one is not.

With the term "valid" it is now easy to summarize the actual problem:

The inference rule

(FOO || BAR) && (!FOO && !BAR) -> n

is not valid. The proof for this is assigning 1 to FOO and also to BAR.
As we have already seen, for this assignment, the expression evaluates to 1.
In contrast, the expression "n" always evaluates to 0. So, this inference
rule is not valid.

> Perhaps, it helps to use a Kconfig file that visualizes the problem:
> 
> mainmenu "Expr Test"
> 
> config expr-config
> 	bool "Expr Config"
> 	depends on (m || m) && (!m && !m)
> #	depends on (m && m) || (!m || !m)
> 
> config dummy
>        bool "Dummy"
> 
> config modules
>        bool
>        option modules
>        default y
> 
> Those two depends, according to your calculation and if I understood
> correctly also to Martin's, should evaluate to 'm' and thus the menu
> entry should always be visible, because both depends should evaluate to
> 'm'.  But because of expr_eliminate_dups2() it is either invisible (the
> first depends becomes 'n') or always visible (the second depends becomes
> 'y').

This is a good idea. However, I tried to avoid an example that contains
the constant symbol "m". The problem with this is that "m" is treated
specially and can lead to some confusion, although a very short test case is
possible using m:

config expr-config-short
	bool "Expr Config Short"
	depends on m && !m

When this file is read, each of the two "m" symbols is replaced with
"m && MODULES", so the expression reads:

(m && MODULES) && !(m && MODULES)

then the negation of "!(m && MODULES)" is transformed:

(m && MODULES) && (!m || !MODULES)
(this corresponds to one of the "De Morgan's laws" in other logical systems)

Finally, the bad inference rule applies (although the form of the expression is
slightly different from above) and this expression is replaced with:

n

This is not valid (assigning 2 to MODULES should yield 1 for the whole
expression, but "n" evaluates to 0).

Does this make things clearer?

Regards,
Martin Walch
Paul Bolle Oct. 11, 2014, 7:07 p.m. UTC | #4
On Mon, 2014-09-22 at 19:13 +0200, Martin Walch wrote:
> Fix it by removing expr_eliminate_dups2() and the functions that have no use
> anywhere else: expr_extract_eq_and(), expr_extract_eq_or(),
> and expr_extract_eq() from scripts/kconfig/expr.[ch]
> 
> Currently the bug is not triggered in mainline, so this patch does not modify
> the configuration space there.

Would I be whining if I'd complained about the lack of documentation for
expr_eliminate_dups(), expr_eliminate_dups1(), and
expr_eliminate_dups2()?

So, I've put in these two calls of fprintf():
@@ -579,6 +606,7 @@ static void expr_eliminate_dups2(enum expr_type type, struct expr **ep1, struct
                tmp2 = expr_copy(e2);
                tmp = expr_extract_eq_and(&tmp1, &tmp2);
                if (expr_is_yes(tmp1)) {
+                       fprintf(stderr, "%s:%d:\n", __func__, __LINE__);
                        expr_free(e1);
                        e1 = expr_alloc_symbol(&symbol_no);
                        trans_count++;
@@ -594,6 +622,7 @@ static void expr_eliminate_dups2(enum expr_type type, struct expr **ep1, struct
                tmp2 = expr_copy(e2);
                tmp = expr_extract_eq_or(&tmp1, &tmp2);
                if (expr_is_no(tmp1)) {
+                       fprintf(stderr, "%s:%d:\n", __func__, __LINE__);
                        expr_free(e1);
                        e1 = expr_alloc_symbol(&symbol_yes);
                        trans_count++;

None of the 500+ defconfig files in next-20141010 triggered those
fprintf's.

Does that means it's likely that nothing in next-20141010 actually uses
the functionality expr_eliminate_dups2() provides?


Paul Bolle

--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/scripts/kconfig/expr.c b/scripts/kconfig/expr.c
index d662652..4aa171b 100644
--- a/scripts/kconfig/expr.c
+++ b/scripts/kconfig/expr.c
@@ -553,62 +553,6 @@  static void expr_eliminate_dups1(enum expr_type type, struct expr **ep1, struct
 #undef e2
 }
 
-static void expr_eliminate_dups2(enum expr_type type, struct expr **ep1, struct expr **ep2)
-{
-#define e1 (*ep1)
-#define e2 (*ep2)
-	struct expr *tmp, *tmp1, *tmp2;
-
-	if (e1->type == type) {
-		expr_eliminate_dups2(type, &e1->left.expr, &e2);
-		expr_eliminate_dups2(type, &e1->right.expr, &e2);
-		return;
-	}
-	if (e2->type == type) {
-		expr_eliminate_dups2(type, &e1, &e2->left.expr);
-		expr_eliminate_dups2(type, &e1, &e2->right.expr);
-	}
-	if (e1 == e2)
-		return;
-
-	switch (e1->type) {
-	case E_OR:
-		expr_eliminate_dups2(e1->type, &e1, &e1);
-		// (FOO || BAR) && (!FOO && !BAR) -> n
-		tmp1 = expr_transform(expr_alloc_one(E_NOT, expr_copy(e1)));
-		tmp2 = expr_copy(e2);
-		tmp = expr_extract_eq_and(&tmp1, &tmp2);
-		if (expr_is_yes(tmp1)) {
-			expr_free(e1);
-			e1 = expr_alloc_symbol(&symbol_no);
-			trans_count++;
-		}
-		expr_free(tmp2);
-		expr_free(tmp1);
-		expr_free(tmp);
-		break;
-	case E_AND:
-		expr_eliminate_dups2(e1->type, &e1, &e1);
-		// (FOO && BAR) || (!FOO || !BAR) -> y
-		tmp1 = expr_transform(expr_alloc_one(E_NOT, expr_copy(e1)));
-		tmp2 = expr_copy(e2);
-		tmp = expr_extract_eq_or(&tmp1, &tmp2);
-		if (expr_is_no(tmp1)) {
-			expr_free(e1);
-			e1 = expr_alloc_symbol(&symbol_yes);
-			trans_count++;
-		}
-		expr_free(tmp2);
-		expr_free(tmp1);
-		expr_free(tmp);
-		break;
-	default:
-		;
-	}
-#undef e1
-#undef e2
-}
-
 struct expr *expr_eliminate_dups(struct expr *e)
 {
 	int oldcount;
@@ -621,7 +565,6 @@  struct expr *expr_eliminate_dups(struct expr *e)
 		switch (e->type) {
 		case E_OR: case E_AND:
 			expr_eliminate_dups1(e->type, &e, &e);
-			expr_eliminate_dups2(e->type, &e, &e);
 		default:
 			;
 		}
@@ -823,57 +766,6 @@  bool expr_depends_symbol(struct expr *dep, struct symbol *sym)
  	return false;
 }
 
-struct expr *expr_extract_eq_and(struct expr **ep1, struct expr **ep2)
-{
-	struct expr *tmp = NULL;
-	expr_extract_eq(E_AND, &tmp, ep1, ep2);
-	if (tmp) {
-		*ep1 = expr_eliminate_yn(*ep1);
-		*ep2 = expr_eliminate_yn(*ep2);
-	}
-	return tmp;
-}
-
-struct expr *expr_extract_eq_or(struct expr **ep1, struct expr **ep2)
-{
-	struct expr *tmp = NULL;
-	expr_extract_eq(E_OR, &tmp, ep1, ep2);
-	if (tmp) {
-		*ep1 = expr_eliminate_yn(*ep1);
-		*ep2 = expr_eliminate_yn(*ep2);
-	}
-	return tmp;
-}
-
-void expr_extract_eq(enum expr_type type, struct expr **ep, struct expr **ep1, struct expr **ep2)
-{
-#define e1 (*ep1)
-#define e2 (*ep2)
-	if (e1->type == type) {
-		expr_extract_eq(type, ep, &e1->left.expr, &e2);
-		expr_extract_eq(type, ep, &e1->right.expr, &e2);
-		return;
-	}
-	if (e2->type == type) {
-		expr_extract_eq(type, ep, ep1, &e2->left.expr);
-		expr_extract_eq(type, ep, ep1, &e2->right.expr);
-		return;
-	}
-	if (expr_eq(e1, e2)) {
-		*ep = *ep ? expr_alloc_two(type, *ep, e1) : e1;
-		expr_free(e2);
-		if (type == E_AND) {
-			e1 = expr_alloc_symbol(&symbol_yes);
-			e2 = expr_alloc_symbol(&symbol_yes);
-		} else if (type == E_OR) {
-			e1 = expr_alloc_symbol(&symbol_no);
-			e2 = expr_alloc_symbol(&symbol_no);
-		}
-	}
-#undef e1
-#undef e2
-}
-
 struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym)
 {
 	struct expr *e1, *e2;
diff --git a/scripts/kconfig/expr.h b/scripts/kconfig/expr.h
index 412ea8a..9c9fb57 100644
--- a/scripts/kconfig/expr.h
+++ b/scripts/kconfig/expr.h
@@ -214,9 +214,6 @@  struct expr *expr_eliminate_dups(struct expr *e);
 struct expr *expr_transform(struct expr *e);
 int expr_contains_symbol(struct expr *dep, struct symbol *sym);
 bool expr_depends_symbol(struct expr *dep, struct symbol *sym);
-struct expr *expr_extract_eq_and(struct expr **ep1, struct expr **ep2);
-struct expr *expr_extract_eq_or(struct expr **ep1, struct expr **ep2);
-void expr_extract_eq(enum expr_type type, struct expr **ep, struct expr **ep1, struct expr **ep2);
 struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym);
 struct expr *expr_simplify_unmet_dep(struct expr *e1, struct expr *e2);