Skip to contentSkip to conversation

Add support for kconfig constraints

PATCH New Cyril Hrubis 4 participants 10 messages Nov 3, 2020 – Nov 9, 2020 Library code

lore
Share & tools
lore
#0 lore raw

v4 cover Add support for kconfig constraints

This patchset adds a support for generic boolean expressions in order to be able to check for a more complex kernel configurations.

The motivation for this is recent rename in kernel config names that cannot be checked for by a simplistic approach we previously implemented.

The boolean expression parser was written as a generic as possible since I do expect that we will reuse it for different types of assertions in the future.

Cyril Hrubis (2):
lib: Add generic boolean expression parser and eval lib/tst_kconfig: Make use of boolean expression eval

 doc/test-writing-guidelines.txt         |  21 +-
 include/tst_bool_expr.h                 |  85 +++++
 include/tst_kconfig.h                   |  34 +-
 lib/newlib_tests/.gitignore             |   2 +
 lib/newlib_tests/config06               |   1 +
 lib/newlib_tests/test_kconfig.c         |   2 +
 lib/newlib_tests/test_kconfig01.c       |  23 ++
 lib/newlib_tests/tst_bool_expr.c        | 128 ++++++++
 lib/tst_bool_expr.c                     | 398 ++++++++++++++++++++++++
 lib/tst_kconfig.c                       | 366 ++++++++++++++--------
 testcases/kernel/syscalls/acct/acct02.c |  14 +-
 11 files changed, 919 insertions(+), 155 deletions(-)
 create mode 100644 include/tst_bool_expr.h
 create mode 100644 lib/newlib_tests/config06
 create mode 100644 lib/newlib_tests/test_kconfig01.c
 create mode 100644 lib/newlib_tests/tst_bool_expr.c
 create mode 100644 lib/tst_bool_expr.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH v4 1/2 lib: Add generic boolean expression parser and eval

Add a simple and generic boolean expression parser and evaluator. This is a first step in order to implement more complex kconfig expressions, but since we are likely to reuse the parser for other purposes in the future it's implemented as a generic boolean parser.

---

v4:
- struct tst_expr is now trailing array
- pointer in tst_expr renamed from expr to rpn
- pass just pointer to the array to the tokenize() function

 include/tst_bool_expr.h          |  85 +++++++
 lib/newlib_tests/.gitignore      |   1 +
 lib/newlib_tests/tst_bool_expr.c | 128 ++++++++++
 lib/tst_bool_expr.c              | 398 +++++++++++++++++++++++++++++++
 4 files changed, 612 insertions(+)
 create mode 100644 include/tst_bool_expr.h
 create mode 100644 lib/newlib_tests/tst_bool_expr.c
 create mode 100644 lib/tst_bool_expr.c
include/tst_bool_expr.h new file +85 -0
new file mode 100644
index 000000000..894d21954
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Cyril Hrubis <[email protected]>
+ */
+
+#ifndef TST_BOOL_EXPR_H__
+#define TST_BOOL_EXPR_H__
+
+enum tst_op {
+	TST_OP_NOT,
+	TST_OP_AND,
+	TST_OP_OR,
+	TST_OP_VAR,
+	/* Used only internally */
+	TST_OP_LPAR,
+	TST_OP_RPAR,
+};
+
+struct tst_expr_tok {
+	enum tst_op op;
+	const char *tok;
+	size_t tok_len;
+	struct tst_expr_tok *next;
+	const void *priv;
+};
+
+struct tst_expr {
+	struct tst_expr_tok *rpn;
+	struct tst_expr_tok buf[];
+};
+
+/*
+ * Parses an boolean expression and returns a simplified RPN version.
+ *
+ * If expression is not valid the call prints error into stderr and returns
+ * NULL. On success pointer to an expression is returned which can be evaluated
+ * by the tst_bool_expr_eval() function and has to be later freed by the
+ * caller.
+ *
+ * The boolean expression can consists of:
+ *
+ * - unary negation opeartion !
+ * - two binary operations & and |
+ * - correct sequence of parentheses ()
+ * - strings that are treated as boolean variables
+ *
+ *  e.g. '(A | B) & C' or 'Variable_1 & Variable_2' are both a valid boolean
+ *  expressions.
+ *
+ *  @expr String containing a boolean expression to be parsed.
+ *  @return Pointer to an RPN expression.
+ */
+struct tst_expr *tst_bool_expr_parse(const char *expr);
+
+/*
+ * Prints an string representation of the expression into a FILE.
+ *
+ * @param A FILE to print to.
+ * @expr An expression to print.
+ */
+void tst_bool_expr_print(FILE *f, struct tst_expr *expr);
+
+/*
+ * Evaluates an expression given a map for variables.
+ *
+ * The call will fail if:
+ * - map function returns -1 which indicates undefined variable
+ * - the eval function runs out of stack
+ *
+ * @param expr Boolean expression in RPN.
+ * @param map Mapping function for boolean variables.
+ *
+ * @return Returns 0 or 1 if expression was evaluated correctly and -1 on error.
+ */
+int tst_bool_expr_eval(struct tst_expr *expr,
+                       int (*map)(struct tst_expr_tok *var));
+
+/*
+ * Frees the memory allocated by the tst_bool_expr_parse().
+ *
+ * @param Boolean expression.
+ */
+void tst_bool_expr_free(struct tst_expr *expr);
+
+#endif	/* TST_BOOL_EXPR_H__ */
lib/newlib_tests/.gitignore +1 -0
index 44bc6526f..1e96db1da 100644
@@ -33,3 +33,4 @@ test_exec_child
 test_kconfig
 variant
 test_guarded_buf
+tst_bool_expr
lib/newlib_tests/tst_bool_expr.c new file +128 -0

128 lines not on this page · raw message

lib/tst_bool_expr.c new file +398 -0

398 lines not on this page · raw message

Signature · 1 line
2.26.2
#2 lore raw

PATCH v4 1/2 lib: Add generic boolean expression parser and eval

Hello,

Quoted 6 lines · Cyril Hrubis

Add a simple and generic boolean expression parser and evaluator. This is a first step in order to implement more complex kconfig expressions, but since we are likely to reuse the parser for other purposes in the future it's implemented as a generic boolean parser.

LGTM!

Signature · 2 lines
Thank you,
Richard.
#3 lore raw

PATCH v4 1/2 lib: Add generic boolean expression parser and eval

In reply to Cyril Hrubis
Quoted 6 lines · Cyril Hrubis

Add a simple and generic boolean expression parser and evaluator. This is a first step in order to implement more complex kconfig expressions, but since we are likely to reuse the parser for other purposes in the future it's implemented as a generic boolean parser.

Hi Cyril,

LGTM. :-)

Best Regards,
Xiao Yang

#4 lore raw

PATCH v4 2/2 lib/tst_kconfig: Make use of boolean expression eval

In reply to Cyril Hrubis

Now each string in the kconfig[] array in tst_test structure is an boolean expression which is evaluated. All expressions has to be true in order for the test to continue.

This also makes the parser for the kernel config a bit more robust as it was pointed out that there may have been cases where it could be mislead by hand edited config files.

+ Update the docs.

---
 doc/test-writing-guidelines.txt         |  21 +-
 include/tst_kconfig.h                   |  34 +--
 lib/newlib_tests/.gitignore             |   1 +
 lib/newlib_tests/config06               |   1 +
 lib/newlib_tests/test_kconfig.c         |   2 +
 lib/newlib_tests/test_kconfig01.c       |  23 ++
 lib/tst_kconfig.c                       | 366 ++++++++++++++++--------
 testcases/kernel/syscalls/acct/acct02.c |  14 +-
 8 files changed, 307 insertions(+), 155 deletions(-)
 create mode 100644 lib/newlib_tests/config06
 create mode 100644 lib/newlib_tests/test_kconfig01.c
doc/test-writing-guidelines.txt +13 -8
index 1a51ef7c7..3c2ab7166 100644
@@ -1643,21 +1643,26 @@ on the system, disabled syscalls can be detected by checking for 'ENOSYS'
 errno etc.
 
 However in rare cases core kernel features couldn't be detected based on the
-kernel userspace API and we have to resort to kernel .config parsing.
+kernel userspace API and we have to resort to parse the kernel .config.
 
-For this cases the test should set the 'NULL' terminated '.needs_kconfigs' array
-of kernel config options required for the test. The config option can be
-specified either as plain "CONFIG_FOO" in which case it's sufficient for the
-test continue if it's set to any value (typically =y or =m). Or with a value
-as "CONFIG_FOO=bar" in which case the value has to match as well. The test is
-aborted with 'TCONF' if any of the required options were not set.
+For this cases the test should set the 'NULL' terminated '.needs_kconfigs'
+array of boolean expressions with constraints on the kconfig variables. The
+boolean expression consits of variables, two binary operations '&' and '|',
+negation '!' and correct sequence of parentesis '()'. Variables are expected
+to be in a form of "CONFIG_FOO[=bar]".
+
+The test will continue to run if all expressions are evaluated to 'True'.
+Missing variable is mapped to 'False' as well as variable with different than
+specified value, e.g. 'CONFIG_FOO=bar' will evaluate to 'False' if the value
+is anything else but 'bar'. If config variable is specified as plain
+'CONFIG_FOO' it's evaluated to true it's set to any value (typically =y or =m).
 
 [source,c]
 -------------------------------------------------------------------------------
 #include "tst_test.h"
 
 static const char *kconfigs[] = {
-	"CONFIG_X86_INTEL_UMIP",
+	"CONFIG_X86_INTEL_UMIP | CONFIG_X86_UMIP",
 	NULL
 };
 
include/tst_kconfig.h +18 -16
index 2d2cfd782..1bb21fea8 100644
@@ -6,27 +6,27 @@
 #ifndef TST_KCONFIG_H__
 #define TST_KCONFIG_H__
 
-struct tst_kconfig_res {
-	char match;
-	char *value;
+struct tst_kconfig_var {
+	char id[64];
+	unsigned int id_len;
+	char choice;
+	char *val;
 };
 
 /**
- * Reads a kernel config and parses it for values defined in kconfigs array.
+ *
+ * Reads a kernel config, parses it and writes results into an array of
+ * tst_kconfig_var structures.
  *
  * The path to the kernel config should be autodetected in most of the cases as
  * the code looks for know locations. It can be explicitely set/overrided with
  * the KCONFIG_PATH environment variable as well.
  *
- * The kcofings array is expected to contain strings in a format "CONFIG_FOO"
- * or "CONFIG_FOO=bar". The result array has to be suitably sized to fit the
- * results.
- *
- * @param kconfigs array of config strings to look for
- * @param results array to store results to
- * @param cnt size of the arrays
+ * The caller has to initialize the tst_kconfig_var structure. The id has to be
+ * filled with config variable name such as 'CONFIG_FOO', the id_len should
+ * hold the id string length and the choice and val has to be zeroed.
  *
- * The match in the tst_kconfig_res structure is set as follows:
+ * After a call to this function each choice be set as follows:
  *
  *  'm' - config option set to m
  *  'y' - config option set to y
@@ -34,11 +34,13 @@ struct tst_kconfig_res {
  *  'n' - config option is not set
  *   0  - config option not found
  *
- * In the case that match is set to 'v' the value points to a newly allocated
- * string that holds the value.
+ * In the case that match is set to 'v' the val pointer points to a newly
+ * allocated string that holds the value.
+ *
+ * @param vars An array of caller initalized tst_kconfig_var structures.
+ * @param vars_len Length of the vars array.
  */
-void tst_kconfig_read(const char *const kconfigs[],
-		      struct tst_kconfig_res results[], size_t cnt);
+void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len);
 
 /**
  * Checks if required kernel configuration options are set in the kernel
lib/newlib_tests/.gitignore +1 -0
index 1e96db1da..89de61cf7 100644
@@ -31,6 +31,7 @@ test_timer
 test_exec
 test_exec_child
 test_kconfig
+test_kconfig01
 variant
 test_guarded_buf
 tst_bool_expr
lib/newlib_tests/config06 new file +1 -0
new file mode 100644
index 000000000..b7db25411
@@ -0,0 +1 @@
+# Empty
lib/newlib_tests/test_kconfig.c +2 -0
index d9c662fc5..1f659b95a 100644
@@ -14,6 +14,8 @@ static const char *kconfigs[] = {
 	"CONFIG_MMU",
 	"CONFIG_EXT4_FS=m",
 	"CONFIG_PGTABLE_LEVELS=4",
+	"CONFIG_MMU & CONFIG_EXT4_FS=m",
+	"CONFIG_EXT4_FS=m | CONFIG_MMU",
 	NULL
 };
 
lib/newlib_tests/test_kconfig01.c new file +23 -0
new file mode 100644
index 000000000..ee919dcac
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Cyril Hrubis <[email protected]>
+ *
+ * Invalid boolean expression test.
+ */
+
+#include "tst_test.h"
+
+static void do_test(void)
+{
+	tst_res(TPASS, "Test passed!");
+}
+
+static const char *kconfigs[] = {
+	"CONFIG_EXT4_FS=m | CONFIG_MMU)",
+	NULL
+};
+
+static struct tst_test test = {
+	.test_all = do_test,
+	.needs_kconfigs = kconfigs,
+};
lib/tst_kconfig.c +241 -125

438 lines not on this page · raw message

testcases/kernel/syscalls/acct/acct02.c +8 -6
index 8ee1bfcf8..1bf6378db 100644
@@ -49,18 +49,20 @@ static union acct_union {
 	struct acct_v3	v3;
 } acct_struct;
 
+#define ACCT_V3 "CONFIG_BSD_PROCESS_ACCT_V3"
+
 static int acct_version_is_3(void)
 {
-	const char *kconfig_acct_v3[] = {
-		"CONFIG_BSD_PROCESS_ACCT_V3",
-		NULL
+	struct tst_kconfig_var kconfig = {
+		.id = ACCT_V3,
+		.id_len = sizeof(ACCT_V3)-1,
 	};
 
-	struct tst_kconfig_res results[1];
+	tst_kconfig_read(&kconfig, 1);
 
-	tst_kconfig_read(kconfig_acct_v3, results, 1);
+	tst_res(TINFO, ACCT_V3 "=%c", kconfig.choice);
 
-	return results[0].match == 'y';
+	return kconfig.choice == 'y';
 }
 
 static void run_command(void)
Signature · 1 line
2.26.2
#5 lore raw

PATCH v4 2/2 lib/tst_kconfig: Make use of boolean expression eval

Quoted 4 lines · Cyril Hrubis

Now each string in the kconfig[] array in tst_test structure is an boolean expression which is evaluated. All expressions has to be true in order for the test to continue.

Quoted 8 lines

This also makes the parser for the kernel config a bit more robust as it was pointed out that there may have been cases where it could be mislead by hand edited config files.

+ Update the docs.

Hi Cyril,

LGTM. :-)

Best Regards,
Xiao Yang

#6 lore raw

PATCH v4 2/2 lib/tst_kconfig: Make use of boolean expression eval

In reply to Cyril Hrubis
Quoted 4 lines · Cyril Hrubis

Now each string in the kconfig[] array in tst_test structure is an boolean expression which is evaluated. All expressions has to be true in order for the test to continue.

Quoted 9 lines

This also makes the parser for the kernel config a bit more robust as it was pointed out that there may have been cases where it could be mislead by hand edited config files.

+ Update the docs.

For series:

Quoted 6 lines
lib/newlib_tests/test_kconfig.c +0 -0
index d9c662fc5..1f659b95a 100644

lib/newlib_tests/test_kconfig.c static const char *kconfigs[] = { · line 21

Quoted 8 lines
lib/newlib_tests/test_kconfig.c +2 -0
@@ -14,6 +14,8 @@ static const char *kconfigs[] = {
        "CONFIG_MMU",
        "CONFIG_EXT4_FS=m",
        "CONFIG_PGTABLE_LEVELS=4",
+       "CONFIG_MMU & CONFIG_EXT4_FS=m",
+       "CONFIG_EXT4_FS=m | CONFIG_MMU",
        NULL
 };
 

I tried with more complicated configurations for test parsing, most works as expected but except the below one with parentheses ():

    "CONFIG_DEFAULT_HOSTNAME=\"(none)\"",

That's because the parentheses are the element of expression and used to distinguish tokens, I?m not sure whether we can change it as a general character to fix this problem, if no, we might need to comment on this in the documentation.

lib/newlib_tests/test_kconfig.c +5 -0
@@ -16,6 +16,11 @@ static const char *kconfigs[] = {
        "CONFIG_PGTABLE_LEVELS=4",
        "CONFIG_MMU & CONFIG_EXT4_FS=m",
        "CONFIG_EXT4_FS=m | CONFIG_MMU",
+       "CONFIG_DEFAULT_HOSTNAME=\"(none)\"",
+       "CONFIG_LOCALVERSION=\"\" | CONFIG_MODULE_SIG_HASH=\"sha256\"",
+       "CONFIG_OUTPUT_FORMAT=\"elf64-x86-64\"",
+       "CONFIG_ARCH_DEFCONFIG=\"arch/x86/configs/x86_64_defconfig\"",
+       "CONFIG_ILLEGAL_POINTER_VALUE=0xdead000000000000",
        NULL
 };
 
Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201106/f78e1a20/attachment.htm>
#7 lore raw

PATCH v4 2/2 lib/tst_kconfig: Make use of boolean expression eval

Hi!

Quoted 9 lines

I tried with more complicated configurations for test parsing, most works as expected but except the below one with parentheses ():

    "CONFIG_DEFAULT_HOSTNAME=\"(none)\"",

That's because the parentheses are the element of expression and used to distinguish tokens, I???m not sure whether we can change it as a general character to fix this problem, if no, we might need to comment on this in the documentation.

Well we would have to teach the tokenizer about strings, i.e. treat everything inside of double quotes as single token. That shouldn't be that hard, I can try to add this later on.

Signature · 2 lines
Cyril Hrubis
[email protected]
#8 lore raw

PATCH v4 2/2 lib/tst_kconfig: Make use of boolean expression eval

Quoted 8 lines · Cyril Hrubis

Hi!

Quoted 7 lines

I tried with more complicated configurations for test parsing, most works as expected but except the below one with parentheses ():

    "CONFIG_DEFAULT_HOSTNAME=\"(none)\"",

That's because the parentheses are the element of expression and used to distinguish tokens, I???m not sure whether we can change it as a

Quoted 8 lines

general

Quoted 2 lines

character to fix this problem, if no, we might need to comment on this in the documentation.

Well we would have to teach the tokenizer about strings, i.e. treat everything inside of double quotes as single token. That shouldn't be that hard, I can try to add this later on.

That would be great, thank you!

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201106/aaf5cc1a/attachment.htm>
#9 lore raw

PATCH v4 2/2 lib/tst_kconfig: Make use of boolean expression eval

Hi!

Quoted 1 line

That would be great, thank you!

I've pushed the patches for now, since I want to finally fix umip_basic_test and I will send a follow-up patch for the tokenizer soon.

Signature · 2 lines
Cyril Hrubis
[email protected]