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

Segmentation fault (core dumped) and cannot call ecall

double
Novice
995 Views

When I execute the following code, it reports an error: "[ERROR] Fail to Outcpu."

test.cpp

size_t X_row = 1024;
size_t X_col = 1024;
size_t Y_col = 1024;
size_t X_size = X_row * X_col * sizeof(double);
size_t Y_size = X_col * Y_col * sizeof(double);
double *X = (double *)malloc(X_size);
double *Y = (double *)malloc(Y_size);
ecall_status = ecall_Outcpu(eid, &ret,X, Y, X_row, X_col, Y_col, X_size, Y_size);
if (ecall_status != SGX_SUCCESS || is_error(ret))
    error_print("Fail to Outcpu.");
else
    info_print("Successfully Outcpu.");
free(X);
free(Y);

enclave.cpp

void ecall_mult_array(double *x, double *y, double *r,
					  int x_r, int x_c, int y_c, int size_x, int size_y, int size_r)
{
	for (int i = 0; i < x_r; i++)
	{
		for (int y_j = 0; y_j < y_c; y_j++)
		{
			double res = 0;
			for (int j = 0; j < x_c; j++)
				res = x[i * x_c + j] * y[j * y_c + y_j] + res;
			r[i * y_c + y_j] = res;
		}
	}
}

int ecall_Outcpu(double *X, double *Y, size_t X_row, size_t X_col, size_t Y_col, size_t X_size, size_t Y_size)
{
	size_t Z_size = X_col * Y_col * sizeof(double);
	double *Z = (double *)malloc(Z_size);
	ecall_mult_array(X, Y, Z, X_row, X_col, Y_col, X_size, Y_size, Z_size);
	free(Z);
	return RET_SUCCESS;
}

enclave.edl

enclave {
    // define ECALLs
    trusted {
        public void ecall_mult_array(
            [in, size=size_x]double *x,
            [in, size=size_y]double *y,
            [in, size=size_r]double *r,
            int x_r, int x_c, int y_c, 
            int size_x, int size_y, int size_r
        );

        public int ecall_Outcpu(
            [in, size=X_size]double *X, 
            [in, size=Y_size]double *Y,
            size_t X_row, size_t X_col, size_t Y_col, 
            size_t X_size, size_t Y_size
        );
    };

    // define OCALLs
    untrusted {
    };
};

I think the reason for the error is because the parameter passed is too large, so I change the 'test.cpp', 'enclave.cpp' and 'enclave.edl' to:

test.cpp

ecall_status = ecall_Outcpu(eid, &ret);
if (ecall_status != SGX_SUCCESS || is_error(ret))
    error_print("Fail to Outcpu.");
else
    info_print("Successfully Outcpu.");

enclave.cpp

int ecall_Outcpu()
{
	size_t X_row = 1024;
	size_t X_col = 1024;
	size_t Y_col = 1024;
	size_t X_size = X_row * X_col * sizeof(double);
	size_t Y_size = X_col * Y_col * sizeof(double);
	double *X = (double *)malloc(X_size);
	double *Y = (double *)malloc(Y_size);
	size_t Z_size = X_col * Y_col * sizeof(double);
	double *Z = (double *)malloc(Z_size);

	ecall_mult_array(X, Y, Z, X_row, X_col, Y_col, X_size, Y_size, Z_size);
	
    free(X);
    free(Y);
	free(Z);

	return RET_SUCCESS;
}

enclave.edl

enclave {
    // define ECALLs
    trusted {
        public void ecall_mult_array(
            [in, size=size_x]double *x,
            [in, size=size_y]double *y,
            [in, size=size_r]double *r,
            int x_r, int x_c, int y_c, 
            int size_x, int size_y, int size_r
        );

        public int ecall_Outcpu();
    };

    // define OCALLs
    untrusted {
    };
};

It reports an error:"Segmentation fault (core dumped)"

But when I change the 'X_row', 'X_col' and 'Y_col' to '205':

int ecall_Outcpu()
{
	size_t X_row = 205;
	size_t X_col = 205;
	size_t Y_col = 205;
	size_t X_size = X_row * X_col * sizeof(double);
	size_t Y_size = X_col * Y_col * sizeof(double);
	double *X = (double *)malloc(X_size);
	double *Y = (double *)malloc(Y_size);
	size_t Z_size = X_col * Y_col * sizeof(double);
	double *Z = (double *)malloc(Z_size);

	ecall_mult_array(X, Y, Z, X_row, X_col, Y_col, X_size, Y_size, Z_size);
	
    free(X);
    free(Y);
	free(Z);

	return RET_SUCCESS;
}

It works.

I want to know how to pass large parameters like 1024*1024 matrix to enclave. And how to calculate large matrix in enclave.

 

Thank you for your help!

 

0 Kudos
1 Solution
Sahira_Intel
Moderator
941 Views

Hi,

There is not enough memory allocated to the enclave to pass an array or declare an array of that size. You will need to adjust MaxHeapSize inside the enclave configuration file. I tested your code and I set MaxHeapSize to 0xF000000 to pass the array. Watch the number of threads in the TCSNum parameter in the enclave config file. The more threads you use the more memory you need to allocate.

 

You can also use the Enclave Memory Measurement Tool to measure the memory of your enclave so you can set Heap size accordingly.

 

Sincerely,

Sahira

View solution in original post

5 Replies
Sahira_Intel
Moderator
964 Views

Hello,


Thanks for the information. I am currently looking into this and will get back to you when I have a solution.


Sincerely,

Sahira R.


0 Kudos
Sahira_Intel
Moderator
942 Views

Hi,

There is not enough memory allocated to the enclave to pass an array or declare an array of that size. You will need to adjust MaxHeapSize inside the enclave configuration file. I tested your code and I set MaxHeapSize to 0xF000000 to pass the array. Watch the number of threads in the TCSNum parameter in the enclave config file. The more threads you use the more memory you need to allocate.

 

You can also use the Enclave Memory Measurement Tool to measure the memory of your enclave so you can set Heap size accordingly.

 

Sincerely,

Sahira

double
Novice
932 Views

Thank you for your reply.

I try it as your reply, but it doesn't work.

<EnclaveConfiguration>
    <ProdID>0</ProdID>
    <ISVSVN>0</ISVSVN>
    <StackMaxSize>0xf0000</StackMaxSize>
    <HeapMaxSize>0xf000000</HeapMaxSize>
    <TCSNum>10</TCSNum>
    <TCSPolicy>1</TCSPolicy>
    <!-- Recommend changing 'DisableDebug' to 1 to make the enclave undebuggable for enclave release -->
    <DisableDebug>0</DisableDebug>
    <MiscSelect>0</MiscSelect>
    <MiscMask>0xFFFFFFFF</MiscMask>
</EnclaveConfiguration>
tcs_num 10, tcs_max_num 10, tcs_min_pool 1
The required memory is 262217728B.
The required memory is 0xfa12000, 256072 KB.
Succeed.
SIGN =>  enclave.signed.so
The project has been built in debug hardware mode.

When I run the progarm:

a307@A307:~/md/hw_array$ ./sgx-array -C
[INFO] Enclave successfully initilised.
The initialize enclave time cost is: 1.242835 secs
Segmentation fault (core dumped)

The error display is the same as before, and I don't how to fix it.

Thank you for your help!

Sincerely,

double

0 Kudos
JesusG_Intel
Moderator
902 Views

Hello double,


The higher your TCS_Num, the more threads you use, and the more memory your enclave requires. Either increase the heap size or lower the number of threads, or both. If you must use multiple threads, you need to allocate more memory. We were able to use MaxHeapSize = 0xf000000 with a TCS_NUM = 1.


0 Kudos
Sahira_Intel
Moderator
876 Views

Hi @double 

Just following up to see if you were able to solve this issue.

 

 

Sincerely,

Sahira

0 Kudos
Reply