diff mbox series

[v5,2/2] selftests/sgx: Migrate to kselftest harness

Message ID 20210512215323.420639-2-jarkko@kernel.org (mailing list archive)
State New, archived
Headers show
Series [v5,1/2] selftests/sgx: Rename 'eenter' and 'sgx_call_vdso' | expand

Commit Message

Jarkko Sakkinen May 12, 2021, 9:53 p.m. UTC
Migrate to kselftest harness. Use a fixture test with enclave initialized
and de-initialized for each of the existing three tests, in other words:

1. One FIXTURE() for managing the enclave life-cycle.
2. Three TEST_F()'s, one for each test case.

This gives a leaps better reporting than before. Here's an example
transcript:

TAP version 13
1..3

ok 1 enclave.unclobbered_vdso

ok 2 enclave.clobbered_vdso

ok 3 enclave.clobbered_vdso_and_user_function

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---

v5:
* Use TH_LOG() for printing enclave address ranges instead of printf(),
  based on Reinette's remark.

v4:
* Refine to take better use of the kselftest harness macros.
* Fix: TCS base address was not initialized for a run struct.

v3:
* Use helper macros.

v2:
* Add the missing string argument to ksft_test_result_pass() and
  ksft_test_result_fail() calls.

 tools/testing/selftests/sgx/load.c |   3 -
 tools/testing/selftests/sgx/main.c | 170 ++++++++++++++---------------
 2 files changed, 85 insertions(+), 88 deletions(-)

Comments

Reinette Chatre May 17, 2021, 5:03 p.m. UTC | #1
Hi Jarkko,

On 5/12/2021 2:53 PM, Jarkko Sakkinen wrote:
> Migrate to kselftest harness. Use a fixture test with enclave initialized
> and de-initialized for each of the existing three tests, in other words:
> 
> 1. One FIXTURE() for managing the enclave life-cycle.
> 2. Three TEST_F()'s, one for each test case.
> 
> This gives a leaps better reporting than before. Here's an example
> transcript:
> 
> TAP version 13
> 1..3
> 
> ok 1 enclave.unclobbered_vdso
> 
> ok 2 enclave.clobbered_vdso
> 
> ok 3 enclave.clobbered_vdso_and_user_function
> 
> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> ---
> 
> v5:
> * Use TH_LOG() for printing enclave address ranges instead of printf(),
>    based on Reinette's remark.

Thank you for considering my feedback. The motivation for my comment was 
to consider how this test output will be parsed. If these tests will 
have their output parsed by automated systems then it needs to conform 
to the TAP13 format as supported by kselftest.

In your latest version the output printed during a successful test has 
been changed, using TH_LOG() as you noted. From what I can tell this is 
the only output addressed - failing tests continue to print error 
messages (perror, fprintf) without consideration of how they will be 
parsed. My apologies, I am not a kselftest expert to know what the best 
way for this integration is.

Reinette
Jarkko Sakkinen May 18, 2021, 5:48 p.m. UTC | #2
On Mon, May 17, 2021 at 10:03:42AM -0700, Reinette Chatre wrote:
> Hi Jarkko,
> 
> On 5/12/2021 2:53 PM, Jarkko Sakkinen wrote:
> > Migrate to kselftest harness. Use a fixture test with enclave initialized
> > and de-initialized for each of the existing three tests, in other words:
> > 
> > 1. One FIXTURE() for managing the enclave life-cycle.
> > 2. Three TEST_F()'s, one for each test case.
> > 
> > This gives a leaps better reporting than before. Here's an example
> > transcript:
> > 
> > TAP version 13
> > 1..3
> > 
> > ok 1 enclave.unclobbered_vdso
> > 
> > ok 2 enclave.clobbered_vdso
> > 
> > ok 3 enclave.clobbered_vdso_and_user_function
> > 
> > Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> > ---
> > 
> > v5:
> > * Use TH_LOG() for printing enclave address ranges instead of printf(),
> >    based on Reinette's remark.
> 
> Thank you for considering my feedback. The motivation for my comment was to
> consider how this test output will be parsed. If these tests will have their
> output parsed by automated systems then it needs to conform to the TAP13
> format as supported by kselftest.
> 
> In your latest version the output printed during a successful test has been
> changed, using TH_LOG() as you noted. From what I can tell this is the only
> output addressed - failing tests continue to print error messages (perror,
> fprintf) without consideration of how they will be parsed. My apologies, I
> am not a kselftest expert to know what the best way for this integration is.
> 
> Reinette

It's a valid question, yes.

The problem is that only main.c can use kselftest macros because
kselftest_harness.h pulls 

static int test_harness_run(int __attribute__((unused)) argc,
			    char __attribute__((unused)) **argv)

which will not end up having a call site (because there's no
"TEST_HARNESS_MAIN").

The whole logging thing in kselftest harness is a bit ambiguous.
Namely:

1. There's a macro TH_LOG() defined in kselftest_harness.h, which
   "internally" uses fprintf().
2. There's an inline function ksft_print_msg() in kselftest.h
   using vsprintf().

To add to that, kselftest_harness.h internally prints by using
ksft_print_msg(), and provides TH_LOG(), which does not use
ksft_print_msg().

I don't really get the logic in all this.

/Jarkko
Jarkko Sakkinen May 18, 2021, 7:57 p.m. UTC | #3
On Tue, May 18, 2021 at 08:49:00PM +0300, Jarkko Sakkinen wrote:
> On Mon, May 17, 2021 at 10:03:42AM -0700, Reinette Chatre wrote:
> > Hi Jarkko,
> > 
> > On 5/12/2021 2:53 PM, Jarkko Sakkinen wrote:
> > > Migrate to kselftest harness. Use a fixture test with enclave initialized
> > > and de-initialized for each of the existing three tests, in other words:
> > > 
> > > 1. One FIXTURE() for managing the enclave life-cycle.
> > > 2. Three TEST_F()'s, one for each test case.
> > > 
> > > This gives a leaps better reporting than before. Here's an example
> > > transcript:
> > > 
> > > TAP version 13
> > > 1..3
> > > 
> > > ok 1 enclave.unclobbered_vdso
> > > 
> > > ok 2 enclave.clobbered_vdso
> > > 
> > > ok 3 enclave.clobbered_vdso_and_user_function
> > > 
> > > Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> > > ---
> > > 
> > > v5:
> > > * Use TH_LOG() for printing enclave address ranges instead of printf(),
> > >    based on Reinette's remark.
> > 
> > Thank you for considering my feedback. The motivation for my comment was to
> > consider how this test output will be parsed. If these tests will have their
> > output parsed by automated systems then it needs to conform to the TAP13
> > format as supported by kselftest.
> > 
> > In your latest version the output printed during a successful test has been
> > changed, using TH_LOG() as you noted. From what I can tell this is the only
> > output addressed - failing tests continue to print error messages (perror,
> > fprintf) without consideration of how they will be parsed. My apologies, I
> > am not a kselftest expert to know what the best way for this integration is.
> > 
> > Reinette
> 
> It's a valid question, yes.
> 
> The problem is that only main.c can use kselftest macros because
> kselftest_harness.h pulls 
> 
> static int test_harness_run(int __attribute__((unused)) argc,
> 			    char __attribute__((unused)) **argv)
> 
> which will not end up having a call site (because there's no
> "TEST_HARNESS_MAIN").
> 
> The whole logging thing in kselftest harness is a bit ambiguous.
> Namely:
> 
> 1. There's a macro TH_LOG() defined in kselftest_harness.h, which
>    "internally" uses fprintf().
> 2. There's an inline function ksft_print_msg() in kselftest.h
>    using vsprintf().
> 
> To add to that, kselftest_harness.h internally prints by using
> ksft_print_msg(), and provides TH_LOG(), which does not use
> ksft_print_msg().
> 
> I don't really get the logic in all this.

I tried to split TH_LOG() as separate entity but it's not possible, as the
macros access a static variable called '_metadata'.

I'm not exactly sure how to proceed from this, if we want to make logging
consistent.

I would personally suggest to leave the error messages intact in load.c,
because there is no way to make them consistent, except by removing them.

/Jarkko
Reinette Chatre May 18, 2021, 8:07 p.m. UTC | #4
Hi Jarkko,

On 5/18/2021 12:57 PM, Jarkko Sakkinen wrote:
> On Tue, May 18, 2021 at 08:49:00PM +0300, Jarkko Sakkinen wrote:
>> On Mon, May 17, 2021 at 10:03:42AM -0700, Reinette Chatre wrote:
>>> Hi Jarkko,
>>>
>>> On 5/12/2021 2:53 PM, Jarkko Sakkinen wrote:
>>>> Migrate to kselftest harness. Use a fixture test with enclave initialized
>>>> and de-initialized for each of the existing three tests, in other words:
>>>>
>>>> 1. One FIXTURE() for managing the enclave life-cycle.
>>>> 2. Three TEST_F()'s, one for each test case.
>>>>
>>>> This gives a leaps better reporting than before. Here's an example
>>>> transcript:
>>>>
>>>> TAP version 13
>>>> 1..3
>>>>
>>>> ok 1 enclave.unclobbered_vdso
>>>>
>>>> ok 2 enclave.clobbered_vdso
>>>>
>>>> ok 3 enclave.clobbered_vdso_and_user_function
>>>>
>>>> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
>>>> ---
>>>>
>>>> v5:
>>>> * Use TH_LOG() for printing enclave address ranges instead of printf(),
>>>>     based on Reinette's remark.
>>>
>>> Thank you for considering my feedback. The motivation for my comment was to
>>> consider how this test output will be parsed. If these tests will have their
>>> output parsed by automated systems then it needs to conform to the TAP13
>>> format as supported by kselftest.
>>>
>>> In your latest version the output printed during a successful test has been
>>> changed, using TH_LOG() as you noted. From what I can tell this is the only
>>> output addressed - failing tests continue to print error messages (perror,
>>> fprintf) without consideration of how they will be parsed. My apologies, I
>>> am not a kselftest expert to know what the best way for this integration is.
>>>
>>> Reinette
>>
>> It's a valid question, yes.
>>
>> The problem is that only main.c can use kselftest macros because
>> kselftest_harness.h pulls
>>
>> static int test_harness_run(int __attribute__((unused)) argc,
>> 			    char __attribute__((unused)) **argv)
>>
>> which will not end up having a call site (because there's no
>> "TEST_HARNESS_MAIN").
>>
>> The whole logging thing in kselftest harness is a bit ambiguous.
>> Namely:
>>
>> 1. There's a macro TH_LOG() defined in kselftest_harness.h, which
>>     "internally" uses fprintf().
>> 2. There's an inline function ksft_print_msg() in kselftest.h
>>     using vsprintf().
>>
>> To add to that, kselftest_harness.h internally prints by using
>> ksft_print_msg(), and provides TH_LOG(), which does not use
>> ksft_print_msg().
>>
>> I don't really get the logic in all this.
> 
> I tried to split TH_LOG() as separate entity but it's not possible, as the
> macros access a static variable called '_metadata'.
> 
> I'm not exactly sure how to proceed from this, if we want to make logging
> consistent.
> 
> I would personally suggest to leave the error messages intact in load.c,
> because there is no way to make them consistent, except by removing them.


It is not clear to me why ksft_print_msg() cannot be used but an 
alternative to it may be to just prefix all existing diagnostic messages 
with "# ".

Reinette
Jarkko Sakkinen May 20, 2021, 4:32 p.m. UTC | #5
On Tue, May 18, 2021 at 01:07:16PM -0700, Reinette Chatre wrote:
> Hi Jarkko,
> 
> On 5/18/2021 12:57 PM, Jarkko Sakkinen wrote:
> > On Tue, May 18, 2021 at 08:49:00PM +0300, Jarkko Sakkinen wrote:
> > > On Mon, May 17, 2021 at 10:03:42AM -0700, Reinette Chatre wrote:
> > > > Hi Jarkko,
> > > > 
> > > > On 5/12/2021 2:53 PM, Jarkko Sakkinen wrote:
> > > > > Migrate to kselftest harness. Use a fixture test with enclave initialized
> > > > > and de-initialized for each of the existing three tests, in other words:
> > > > > 
> > > > > 1. One FIXTURE() for managing the enclave life-cycle.
> > > > > 2. Three TEST_F()'s, one for each test case.
> > > > > 
> > > > > This gives a leaps better reporting than before. Here's an example
> > > > > transcript:
> > > > > 
> > > > > TAP version 13
> > > > > 1..3
> > > > > 
> > > > > ok 1 enclave.unclobbered_vdso
> > > > > 
> > > > > ok 2 enclave.clobbered_vdso
> > > > > 
> > > > > ok 3 enclave.clobbered_vdso_and_user_function
> > > > > 
> > > > > Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> > > > > ---
> > > > > 
> > > > > v5:
> > > > > * Use TH_LOG() for printing enclave address ranges instead of printf(),
> > > > >     based on Reinette's remark.
> > > > 
> > > > Thank you for considering my feedback. The motivation for my comment was to
> > > > consider how this test output will be parsed. If these tests will have their
> > > > output parsed by automated systems then it needs to conform to the TAP13
> > > > format as supported by kselftest.
> > > > 
> > > > In your latest version the output printed during a successful test has been
> > > > changed, using TH_LOG() as you noted. From what I can tell this is the only
> > > > output addressed - failing tests continue to print error messages (perror,
> > > > fprintf) without consideration of how they will be parsed. My apologies, I
> > > > am not a kselftest expert to know what the best way for this integration is.
> > > > 
> > > > Reinette
> > > 
> > > It's a valid question, yes.
> > > 
> > > The problem is that only main.c can use kselftest macros because
> > > kselftest_harness.h pulls
> > > 
> > > static int test_harness_run(int __attribute__((unused)) argc,
> > > 			    char __attribute__((unused)) **argv)
> > > 
> > > which will not end up having a call site (because there's no
> > > "TEST_HARNESS_MAIN").
> > > 
> > > The whole logging thing in kselftest harness is a bit ambiguous.
> > > Namely:
> > > 
> > > 1. There's a macro TH_LOG() defined in kselftest_harness.h, which
> > >     "internally" uses fprintf().
> > > 2. There's an inline function ksft_print_msg() in kselftest.h
> > >     using vsprintf().
> > > 
> > > To add to that, kselftest_harness.h internally prints by using
> > > ksft_print_msg(), and provides TH_LOG(), which does not use
> > > ksft_print_msg().
> > > 
> > > I don't really get the logic in all this.
> > 
> > I tried to split TH_LOG() as separate entity but it's not possible, as the
> > macros access a static variable called '_metadata'.
> > 
> > I'm not exactly sure how to proceed from this, if we want to make logging
> > consistent.
> > 
> > I would personally suggest to leave the error messages intact in load.c,
> > because there is no way to make them consistent, except by removing them.
> 
> 
> It is not clear to me why ksft_print_msg() cannot be used but an alternative
> to it may be to just prefix all existing diagnostic messages with "# ".
> 
> Reinette

How is using ksft_print_msg() better than using fprintf()? It's as
incompatible with the logging used by fixtures, as is a raw fprintf().

What is the gain of doing that?

/Jarkko
diff mbox series

Patch

diff --git a/tools/testing/selftests/sgx/load.c b/tools/testing/selftests/sgx/load.c
index f441ac34b4d4..00928be57fc4 100644
--- a/tools/testing/selftests/sgx/load.c
+++ b/tools/testing/selftests/sgx/load.c
@@ -239,9 +239,6 @@  bool encl_load(const char *path, struct encl *encl)
 		seg->offset = (phdr->p_offset & PAGE_MASK) - src_offset;
 		seg->size = (phdr->p_filesz + PAGE_SIZE - 1) & PAGE_MASK;
 
-		printf("0x%016lx 0x%016lx 0x%02x\n", seg->offset, seg->size,
-		       seg->prot);
-
 		j++;
 	}
 
diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c
index 43da68388e25..78b2c8b27e07 100644
--- a/tools/testing/selftests/sgx/main.c
+++ b/tools/testing/selftests/sgx/main.c
@@ -17,8 +17,8 @@ 
 #include <sys/types.h>
 #include <sys/auxv.h>
 #include "defines.h"
+#include "../kselftest_harness.h"
 #include "main.h"
-#include "../kselftest.h"
 
 static const uint64_t MAGIC = 0x1122334455667788ULL;
 vdso_sgx_enter_enclave_t vdso_sgx_enter_enclave;
@@ -107,85 +107,49 @@  static Elf64_Sym *vdso_symtab_get(struct vdso_symtab *symtab, const char *name)
 	return NULL;
 }
 
-bool report_results(struct sgx_enclave_run *run, int ret, uint64_t result,
-		  const char *test)
-{
-	bool valid = true;
-
-	if (ret) {
-		printf("FAIL: %s() returned: %d\n", test, ret);
-		valid = false;
-	}
-
-	if (run->function != EEXIT) {
-		printf("FAIL: %s() function, expected: %u, got: %u\n", test, EEXIT,
-		       run->function);
-		valid = false;
-	}
-
-	if (result != MAGIC) {
-		printf("FAIL: %s(), expected: 0x%lx, got: 0x%lx\n", test, MAGIC,
-		       result);
-		valid = false;
-	}
-
-	if (run->user_data) {
-		printf("FAIL: %s() user data, expected: 0x0, got: 0x%llx\n",
-		       test, run->user_data);
-		valid = false;
-	}
-
-	return valid;
-}
-
-static int user_handler(long rdi, long rsi, long rdx, long ursp, long r8, long r9,
-			struct sgx_enclave_run *run)
-{
-	run->user_data = 0;
-	return 0;
-}
+FIXTURE(enclave) {
+	struct encl encl;
+	struct sgx_enclave_run run;
+};
 
-int main(int argc, char *argv[])
+FIXTURE_SETUP(enclave)
 {
-	struct sgx_enclave_run run;
+	Elf64_Sym *sgx_enter_enclave_sym = NULL;
 	struct vdso_symtab symtab;
-	Elf64_Sym *sgx_enter_enclave_sym;
-	uint64_t result = 0;
-	struct encl encl;
+	struct encl_segment *seg;
 	unsigned int i;
 	void *addr;
-	int ret;
-
-	memset(&run, 0, sizeof(run));
 
-	if (!encl_load("test_encl.elf", &encl)) {
-		encl_delete(&encl);
+	if (!encl_load("test_encl.elf", &self->encl)) {
+		encl_delete(&self->encl);
 		ksft_exit_skip("cannot load enclaves\n");
 	}
 
-	if (!encl_measure(&encl))
+	for (i = 0; i < self->encl.nr_segments; i++) {
+		seg = &self->encl.segment_tbl[i];
+
+		TH_LOG("0x%016lx 0x%016lx 0x%02x\n", seg->offset, seg->size, seg->prot);
+	}
+
+	if (!encl_measure(&self->encl))
 		goto err;
 
-	if (!encl_build(&encl))
+	if (!encl_build(&self->encl))
 		goto err;
 
 	/*
 	 * An enclave consumer only must do this.
 	 */
-	for (i = 0; i < encl.nr_segments; i++) {
-		struct encl_segment *seg = &encl.segment_tbl[i];
-
-		addr = mmap((void *)encl.encl_base + seg->offset, seg->size,
-			    seg->prot, MAP_SHARED | MAP_FIXED, encl.fd, 0);
-		if (addr == MAP_FAILED) {
-			perror("mmap() segment failed");
-			exit(KSFT_FAIL);
-		}
+	for (i = 0; i < self->encl.nr_segments; i++) {
+		struct encl_segment *seg = &self->encl.segment_tbl[i];
+
+		addr = mmap((void *)self->encl.encl_base + seg->offset, seg->size,
+			    seg->prot, MAP_SHARED | MAP_FIXED, self->encl.fd, 0);
+		EXPECT_NE(addr, MAP_FAILED);
+		if (addr == MAP_FAILED)
+			goto err;
 	}
 
-	memset(&run, 0, sizeof(run));
-	run.tcs = encl.encl_base;
-
 	/* Get vDSO base address */
 	addr = (void *)getauxval(AT_SYSINFO_EHDR);
 	if (!addr)
@@ -200,32 +164,68 @@  int main(int argc, char *argv[])
 
 	vdso_sgx_enter_enclave = addr + sgx_enter_enclave_sym->st_value;
 
-	ret = sgx_enter_enclave((void *)&MAGIC, &result, 0, EENTER,
-					    NULL, NULL, &run);
-	if (!report_results(&run, ret, result, "sgx_enter_enclave_unclobbered"))
-		goto err;
+	memset(&self->run, 0, sizeof(self->run));
+	self->run.tcs = self->encl.encl_base;
 
+err:
+	if (!sgx_enter_enclave_sym)
+		encl_delete(&self->encl);
 
-	/* Invoke the vDSO directly. */
-	result = 0;
-	ret = vdso_sgx_enter_enclave((unsigned long)&MAGIC, (unsigned long)&result,
-				     0, EENTER, 0, 0, &run);
-	if (!report_results(&run, ret, result, "sgx_enter_enclave"))
-		goto err;
+	ASSERT_NE(sgx_enter_enclave_sym, NULL);
+}
 
-	/* And with an exit handler. */
-	run.user_handler = (__u64)user_handler;
-	run.user_data = 0xdeadbeef;
-	ret = vdso_sgx_enter_enclave((unsigned long)&MAGIC, (unsigned long)&result,
-				     0, EENTER, 0, 0, &run);
-	if (!report_results(&run, ret, result, "user_handler"))
-		goto err;
+FIXTURE_TEARDOWN(enclave)
+{
+	encl_delete(&self->encl);
+	vdso_sgx_enter_enclave = NULL;
+}
 
-	printf("SUCCESS\n");
-	encl_delete(&encl);
-	exit(KSFT_PASS);
 
-err:
-	encl_delete(&encl);
-	exit(KSFT_FAIL);
+TEST_F(enclave, unclobbered_vdso)
+{
+	uint64_t result = 0;
+
+	EXPECT_EQ(sgx_enter_enclave((void *)&MAGIC, &result, 0, EENTER, NULL, NULL, &self->run), 0);
+
+	EXPECT_EQ(result, MAGIC);
+	EXPECT_EQ(self->run.function, EEXIT);
+	EXPECT_EQ(self->run.user_data, 0);
+}
+
+TEST_F(enclave, clobbered_vdso)
+{
+	uint64_t result = 0;
+
+	EXPECT_EQ(vdso_sgx_enter_enclave((unsigned long)&MAGIC, (unsigned long)&result, 0,
+					 EENTER, 0, 0, &self->run), 0);
+
+
+	EXPECT_EQ(result, MAGIC);
+	EXPECT_EQ(self->run.function, EEXIT);
+	EXPECT_EQ(self->run.user_data, 0);
 }
+
+static int test_handler(long rdi, long rsi, long rdx, long ursp, long r8, long r9,
+			struct sgx_enclave_run *run)
+{
+	run->user_data = 0;
+
+	return 0;
+}
+
+TEST_F(enclave, clobbered_vdso_and_user_function)
+{
+	uint64_t result = 0;
+
+	self->run.user_handler = (__u64)test_handler;
+	self->run.user_data = 0xdeadbeef;
+
+	EXPECT_EQ(vdso_sgx_enter_enclave((unsigned long)&MAGIC, (unsigned long)&result, 0,
+					 EENTER, 0, 0, &self->run), 0);
+
+	EXPECT_EQ(result, MAGIC);
+	EXPECT_EQ(self->run.function, EEXIT);
+	EXPECT_EQ(self->run.user_data, 0);
+}
+
+TEST_HARNESS_MAIN