Intel® Software Guard Extensions (Intel® SGX)
Discussion board focused on hardware-based isolation and memory encryption to provide extended code protection in solutions.

enclave field not working, but no error

sang__oh
Beginner
783 Views

i'm trying make simple code, call enclave field and just add 1

i'm reference this site : https://software.intel.com/en-us/articles/getting-started-with-sgx-sdk-for-windows

after finish, there is no error but the enclave code not working.

 

here is my project.zip,with visual studio 2017    https://drive.google.com/open?id=13trTAamhNWaz2Q2BRDtUFP5qCX8Syyuc

and code.

app.cpp

#include <stdio.h>
#include <Windows.h>
#include <tchar.h>

#include "sgx_urts.h"
#include "Enclave1_u.h"

#define ENCLAVE_FILE _T("Enclave1.signed.dll")

int main() {
	int a = 1;
	int i = 0;

	sgx_enclave_id_t eid;
	sgx_status_t ret = SGX_SUCCESS;
	sgx_launch_token_t token = { 0 };
	int updated = 0;
	
	ret = sgx_create_enclave(ENCLAVE_FILE, SGX_DEBUG_FLAG, &token, &updated, &eid, NULL);
	if (ret != SGX_SUCCESS)
	{
		printf("APP error%#x, failed to create enclave. \n", ret);
		return -1;
	}
	
	int *ptr = &a;
	printf("%d\n",*ptr);

	while (i<5) {
		foo(eid, ptr);
		printf("%d\n", *ptr);		
		Sleep(1000);
		i++;
	}

	if (SGX_SUCCESS != sgx_destroy_enclave(eid))
		return -1;
}

 

Enclave1.edl

enclave {
    from "sgx_tstdc.edl" import *;

    trusted {
        /* define ECALLs here. */
		public void foo([in, size = 4]int *ptr);
    };

    untrusted {
        /* define OCALLs here. */

    };
};

 

Enclave1.cpp

#include "Enclave1_t.h"
#include "sgx_trts.h"
#include <string.h>

void foo(int *ptr)
{	
	if (*ptr == 1) *ptr == 43971;
	*ptr += 1;
}

when i expected, print 43971, 43972, 43973, 43974 .....

but the result is print 1, 1, 1, .........

what i missed?

 

0 Kudos
1 Solution
Scott_R_Intel
Employee
783 Views

Hello Oh.

There are a couple of issues with your code for what you're trying to do.

First, you need to add the "out" parameter in addition to the "in" parameter in your edl file.  If you don't have out, the buffer changed inside the enclave will not get copied back out. 

     public void foo([in, out, size = 4]int *ptr);

See this link for more info:  https://software.intel.com/en-us/sgx-sdk-dev-reference-pointer-handling

Also, there is one issue in the enclave code.  Your if statement should look like this:

     if (*ptr == 1) *ptr = 43971;

Note the single equal sign assignment operator when you assign the value to *ptr.  You had a double equal sign, which is the equal to operator.

Regards.

Scott

View solution in original post

0 Kudos
2 Replies
Scott_R_Intel
Employee
784 Views

Hello Oh.

There are a couple of issues with your code for what you're trying to do.

First, you need to add the "out" parameter in addition to the "in" parameter in your edl file.  If you don't have out, the buffer changed inside the enclave will not get copied back out. 

     public void foo([in, out, size = 4]int *ptr);

See this link for more info:  https://software.intel.com/en-us/sgx-sdk-dev-reference-pointer-handling

Also, there is one issue in the enclave code.  Your if statement should look like this:

     if (*ptr == 1) *ptr = 43971;

Note the single equal sign assignment operator when you assign the value to *ptr.  You had a double equal sign, which is the equal to operator.

Regards.

Scott

0 Kudos
sang__oh
Beginner
783 Views

thanks Scott R.  and i solved this problem.  first, as you say, foo needs [out] instad of [in].  and my .signed.dll file is not updated on debug folder. i realize this file is enclave field itself

0 Kudos
Reply