Skip to contentSkip to conversation

selftests/sgx: Rename 'eenter' and 'sgx_call_vdso'

PATCH Superseded Jarkko Sakkinen 2 participants 9 messages May 26, 2021 – Jun 9, 2021

Subsystems (2)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
author maintainer
#5 lore raw

PATCH v7 4/4 selftests/sgx: Add EXPECT_EEXIT() macro

In reply to Jarkko Sakkinen

Add EXPECT_EEXIT() macro, which will conditionally print the exception information, in addition to

  EXPECT_EQ(self->run.function, EEXIT);
---
 tools/testing/selftests/sgx/main.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
tools/testing/selftests/sgx/main.c +11 -3
index d658882b5dd2..86d4283f9c61 100644
@@ -207,6 +207,14 @@ static inline int encl_call(const void *in, void *out, struct sgx_enclave_run *r
 	return ret;
 }
 
+#define EXPECT_EEXIT(run) \
+	do { \
+		EXPECT_EQ((run)->function, EEXIT); \
+		if ((run)->function != EEXIT) \
+			TH_LOG("0x%02x 0x%02x 0x%16llx", (run)->exception_vector, \
+			       (run)->exception_error_code, (run)->exception_addr); \
+	} while (0)
+
 TEST_F(enclave, unclobbered_vdso)
 {
 	uint64_t result = 0;
@@ -214,7 +222,7 @@ TEST_F(enclave, unclobbered_vdso)
 	EXPECT_EQ(encl_call(&MAGIC, &result, &self->run, false), 0);
 
 	EXPECT_EQ(result, MAGIC);
-	EXPECT_EQ(self->run.function, EEXIT);
+	EXPECT_EEXIT(&self->run);
 	EXPECT_EQ(self->run.user_data, 0);
 }
 
@@ -225,7 +233,7 @@ TEST_F(enclave, clobbered_vdso)
 	EXPECT_EQ(encl_call(&MAGIC, &result, &self->run, true), 0);
 
 	EXPECT_EQ(result, MAGIC);
-	EXPECT_EQ(self->run.function, EEXIT);
+	EXPECT_EEXIT(&self->run);
 	EXPECT_EQ(self->run.user_data, 0);
 }
 
@@ -247,7 +255,7 @@ TEST_F(enclave, clobbered_vdso_and_user_function)
 	EXPECT_EQ(encl_call(&MAGIC, &result, &self->run, true), 0);
 
 	EXPECT_EQ(result, MAGIC);
-	EXPECT_EQ(self->run.function, EEXIT);
+	EXPECT_EEXIT(&self->run);
 	EXPECT_EQ(self->run.user_data, 0);
 }
 
Signature · 1 line
2.31.1
author maintainer
#4 lore raw

PATCH v7 3/4 selftests/sgx: Dump enclave memory map

In reply to Jarkko Sakkinen

Often, it's useful to check whether /proc/self/maps looks sane when dealing with memory mapped objects, especially when they are JIT'ish dynamically constructed objects. Therefore, dump "/dev/sgx_enclave" matching lines from the memory map in FIXTURE_SETUP().

---
 tools/testing/selftests/sgx/main.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/testing/selftests/sgx/main.c +14 -0
index 8694772c8fd9..d658882b5dd2 100644
@@ -117,6 +117,8 @@ FIXTURE_SETUP(enclave)
 	Elf64_Sym *sgx_enter_enclave_sym = NULL;
 	struct vdso_symtab symtab;
 	struct encl_segment *seg;
+	char maps_line[256];
+	FILE *maps_file;
 	unsigned int i;
 	void *addr;
 
@@ -167,6 +169,18 @@ FIXTURE_SETUP(enclave)
 	memset(&self->run, 0, sizeof(self->run));
 	self->run.tcs = self->encl.encl_base;
 
+	maps_file = fopen("/proc/self/maps", "r");
+	if (maps_file != NULL)  {
+		while (fgets(maps_line, sizeof(maps_line), maps_file) != NULL) {
+			maps_line[strlen(maps_line) - 1] = '\0';
+
+			if (strstr(maps_line, "/dev/sgx_enclave"))
+				TH_LOG("%s", maps_line);
+		}
+
+		fclose(maps_file);
+	}
+
 err:
 	if (!sgx_enter_enclave_sym)
 		encl_delete(&self->encl);
Signature · 1 line
2.31.1

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH v7 1/4 selftests/sgx: Rename 'eenter' and 'sgx_call_vdso'

Rename symbols for better clarity:

* 'eenter' might be confused for directly calling ENCLU[EENTER]. It does not. It calls into the VDSO, which actually has the EENTER instruction.
* 'sgx_call_vdso' is *only* used for entering the enclave. It's not some generic SGX call into the VDSO.

---

v3:
Refine the commit message according to Dave's suggestions.

v2:
Refined the renames just a bit.

 tools/testing/selftests/sgx/call.S |  6 +++---
 tools/testing/selftests/sgx/main.c | 25 +++++++++++++------------
 tools/testing/selftests/sgx/main.h |  4 ++--
 3 files changed, 18 insertions(+), 17 deletions(-)
tools/testing/selftests/sgx/call.S +3 -3
index 4ecadc7490f4..b09a25890f3b 100644
@@ -5,8 +5,8 @@
 
 	.text
 
-	.global sgx_call_vdso
-sgx_call_vdso:
+	.global sgx_enter_enclave
+sgx_enter_enclave:
 	.cfi_startproc
 	push	%r15
 	.cfi_adjust_cfa_offset	8
@@ -27,7 +27,7 @@ sgx_call_vdso:
 	.cfi_adjust_cfa_offset	8
 	push	0x38(%rsp)
 	.cfi_adjust_cfa_offset	8
-	call	*eenter(%rip)
+	call	*vdso_sgx_enter_enclave(%rip)
 	add	$0x10, %rsp
 	.cfi_adjust_cfa_offset	-0x10
 	pop	%rbx
tools/testing/selftests/sgx/main.c +13 -12
index d304a4044eb9..43da68388e25 100644
@@ -21,7 +21,7 @@
 #include "../kselftest.h"
 
 static const uint64_t MAGIC = 0x1122334455667788ULL;
-vdso_sgx_enter_enclave_t eenter;
+vdso_sgx_enter_enclave_t vdso_sgx_enter_enclave;
 
 struct vdso_symtab {
 	Elf64_Sym *elf_symtab;
@@ -149,7 +149,7 @@ int main(int argc, char *argv[])
 {
 	struct sgx_enclave_run run;
 	struct vdso_symtab symtab;
-	Elf64_Sym *eenter_sym;
+	Elf64_Sym *sgx_enter_enclave_sym;
 	uint64_t result = 0;
 	struct encl encl;
 	unsigned int i;
@@ -194,29 +194,30 @@ int main(int argc, char *argv[])
 	if (!vdso_get_symtab(addr, &symtab))
 		goto err;
 
-	eenter_sym = vdso_symtab_get(&symtab, "__vdso_sgx_enter_enclave");
-	if (!eenter_sym)
+	sgx_enter_enclave_sym = vdso_symtab_get(&symtab, "__vdso_sgx_enter_enclave");
+	if (!sgx_enter_enclave_sym)
 		goto err;
 
-	eenter = addr + eenter_sym->st_value;
+	vdso_sgx_enter_enclave = addr + sgx_enter_enclave_sym->st_value;
 
-	ret = sgx_call_vdso((void *)&MAGIC, &result, 0, EENTER, NULL, NULL, &run);
-	if (!report_results(&run, ret, result, "sgx_call_vdso"))
+	ret = sgx_enter_enclave((void *)&MAGIC, &result, 0, EENTER,
+					    NULL, NULL, &run);
+	if (!report_results(&run, ret, result, "sgx_enter_enclave_unclobbered"))
 		goto err;
 
 
 	/* Invoke the vDSO directly. */
 	result = 0;
-	ret = eenter((unsigned long)&MAGIC, (unsigned long)&result, 0, EENTER,
-		     0, 0, &run);
-	if (!report_results(&run, ret, result, "eenter"))
+	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;
 
 	/* And with an exit handler. */
 	run.user_handler = (__u64)user_handler;
 	run.user_data = 0xdeadbeef;
-	ret = eenter((unsigned long)&MAGIC, (unsigned long)&result, 0, EENTER,
-		     0, 0, &run);
+	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;
 
tools/testing/selftests/sgx/main.h +2 -2
index 67211a708f04..68672fd86cf9 100644
@@ -35,7 +35,7 @@ bool encl_load(const char *path, struct encl *encl);
 bool encl_measure(struct encl *encl);
 bool encl_build(struct encl *encl);
 
-int sgx_call_vdso(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9,
-		  struct sgx_enclave_run *run);
+int sgx_enter_enclave(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9,
+		      struct sgx_enclave_run *run);
 
 #endif /* MAIN_H */
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH v7 2/4 selftests/sgx: Migrate to kselftest harness

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.

Dump lines of /proc/self/maps matching "sgx" in FIXTURE_SETUP() as this can be very useful debugging information later on.

---

v7:
* Implement encl_call() that wraps the logic of invoking the vDSO for the test enclave.

v6:
* Remove extra '\n' from TH_LOG() calls.

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 | 179 +++++++++++++++--------------
 2 files changed, 94 insertions(+), 88 deletions(-)
tools/testing/selftests/sgx/load.c +0 -3
index f441ac34b4d4..00928be57fc4 100644
@@ -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++;
 	}
 
tools/testing/selftests/sgx/main.c +94 -85

219 lines not on this page · raw message

Signature · 1 line
2.31.1
#6 lore raw
In reply to Jarkko Sakkinen

Hi Jarkko,

tools/testing/selftests/sgx/main.h bool encl_load(const char *path, struct encl *encl); · line 40

Quoted 13 lines · Jarkko Sakkinen
tools/testing/selftests/sgx/main.h +2 -2
index 67211a708f04..68672fd86cf9 100644
@@ -35,7 +35,7 @@ bool encl_load(const char *path, struct encl *encl);
  bool encl_measure(struct encl *encl);
  bool encl_build(struct encl *encl);
 
-int sgx_call_vdso(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9,
-		  struct sgx_enclave_run *run);
+int sgx_enter_enclave(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9,
+		      struct sgx_enclave_run *run);
 

Is there a reason why all registers except rdx are "void *"?

Reinette

#2 lore raw

Re: [PATCH v7 2/4] selftests/sgx: Migrate to kselftest harness

In reply to Jarkko Sakkinen

Hi Jarkko,

On 5/26/2021 5:47 AM, Jarkko Sakkinen wrote:

...

Quoted 7 lines
tools/testing/selftests/sgx/main.c +4 -2
-	exit(KSFT_PASS);
+static inline int encl_call(const void *in, void *out, struct sgx_enclave_run *run, bool clobbered)
+{
+	void *in2 = (void *)in;
+	int ret;
 
-err:

tools/testing/selftests/sgx/main.c int main(int argc, char *argv[]) · line 194

Quoted 10 lines
tools/testing/selftests/sgx/main.c +8 -2
-	encl_delete(&encl);
-	exit(KSFT_FAIL);
+	if (clobbered)
+		ret = vdso_sgx_enter_enclave((unsigned long)in2, (unsigned long)out, 0,
+					     EENTER, 0, 0, run);
+	else
+		ret = sgx_enter_enclave(in2, out, 0, EENTER, NULL, NULL, run);
+
+	return ret;
+}

I find this change unnecessary because it is very specific to the current test cases and limiting future test cases. From what I understand it attempts to create a generic "call into the enclave" wrapper but in doing so it constrains the call to use only two registers, assuming there will always and only be just an "in" and "out" parameter needed.

Reinette

author maintainer
#7 lore raw
In reply to Reinette Chatre
Quoted 16 lines · Reinette Chatre

Hi Jarkko,

tools/testing/selftests/sgx/main.h bool encl_load(const char *path, struct encl *encl); · line 38

Quoted 11 lines · Jarkko Sakkinen
tools/testing/selftests/sgx/main.h +2 -2
index 67211a708f04..68672fd86cf9 100644
@@ -35,7 +35,7 @@ bool encl_load(const char *path, struct encl *encl);
  bool encl_measure(struct encl *encl);
  bool encl_build(struct encl *encl);
-int sgx_call_vdso(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9,
-		  struct sgx_enclave_run *run);
+int sgx_enter_enclave(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9,
+		      struct sgx_enclave_run *run);

Is there a reason why all registers except rdx are "void *"?

Evolution? It's test code.

There's neither reason to change this for no reason.

/Jarkko

author maintainer
#3 lore raw

Re: [PATCH v7 2/4] selftests/sgx: Migrate to kselftest harness

In reply to Reinette Chatre
Quoted 29 lines · Reinette Chatre

Hi Jarkko,

On 5/26/2021 5:47 AM, Jarkko Sakkinen wrote:

...

Quoted 6 lines
tools/testing/selftests/sgx/main.c +4 -2
-	exit(KSFT_PASS);
+static inline int encl_call(const void *in, void *out, struct sgx_enclave_run *run, bool clobbered)
+{
+	void *in2 = (void *)in;
+	int ret;
-err:

tools/testing/selftests/sgx/main.c int main(int argc, char *argv[]) · line 194

Quoted 10 lines
tools/testing/selftests/sgx/main.c +8 -2
-	encl_delete(&encl);
-	exit(KSFT_FAIL);
+	if (clobbered)
+		ret = vdso_sgx_enter_enclave((unsigned long)in2, (unsigned long)out, 0,
+					     EENTER, 0, 0, run);
+	else
+		ret = sgx_enter_enclave(in2, out, 0, EENTER, NULL, NULL, run);
+
+	return ret;
+}

I find this change unnecessary because it is very specific to the current test cases and limiting future test cases. From what I understand it attempts to create a generic "call into the enclave" wrapper but in doing so it constrains the call to use only two registers, assuming there will always and only be just an "in" and "out" parameter needed.

Quoted 1 line

Reinette

Thank you, good catch, it should be a macro. I'll turn in such.

Other than that, it does not limit anything as we are talking neither API or ABI.

/Jarkko

#8 lore raw
In reply to Jarkko Sakkinen

Hi Jarkko,

Quoted 18 lines · Jarkko Sakkinen
Quoted 16 lines · Reinette Chatre

Hi Jarkko,

tools/testing/selftests/sgx/main.h bool encl_load(const char *path, struct encl *encl); · line 38

Quoted 11 lines · Jarkko Sakkinen
tools/testing/selftests/sgx/main.h +2 -2
index 67211a708f04..68672fd86cf9 100644
@@ -35,7 +35,7 @@ bool encl_load(const char *path, struct encl *encl);
   bool encl_measure(struct encl *encl);
   bool encl_build(struct encl *encl);
-int sgx_call_vdso(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9,
-		  struct sgx_enclave_run *run);
+int sgx_enter_enclave(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9,
+		      struct sgx_enclave_run *run);

Is there a reason why all registers except rdx are "void *"?

Quoted 4 lines

Evolution? It's test code.

There's neither reason to change this for no reason.

One reason would be to make the code consistent. This would reduce confusion.

Reinette