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

Enclave stack overflow and malloc

Criston__Anna
New Contributor I
2,255 Views

Hi! I am trying to allocate 1.8MB memory on the heap, with malloc, inside the enclave, then make an OCALL where I pass as parameter a [in, out] pointer(*message_in) to this allocated memory(size_t message_len = 1823577 

 

 

 

	ocall_get_next_message_size(&res_len, message_len);
	free(message_in);
	message_in = (unsigned char*) malloc(*message_len + 2);
    ocall_get_next_message(&res_message, message_in, *message_len + 2, &len_in, &len_out, path, 512);

 

 

 

(*message_in has been allocated memory in the enclave before). 

And in the .edl file: 

 

 

 

[cdecl] int ocall_get_next_message_size([in, out]size_t *message_len);
[cdecl] int ocall_get_next_message([in,out, size=message_len]unsigned char *message_in,size_t message_len,[in,out] size_t *len_in, [in, out]size_t *len_out, [in, out, size=len] char* path, size_t len);
		

 

 

 

 However, I get stack overflow on ocall_get_next_message:

Screenshot (87).png

 

I believe I have allocated enough memory on the stack:

Screenshot (84).png

 

Moreover, trying to increase the stack size(even only to 0x3600000) or the heap size more (still multiple of 4k) makes the program not start anymore:

Screenshot (86).png

 

I wanted to ask where could be the problem and what are the limitations of an enclave, besides the 128MB size. How much memory can pass a pointer to in an OCALL/ECALL and what is the maximum size my stack and heap can have?  I could not find information about this in the developer guide.

Thank you.

0 Kudos
1 Solution
Criston__Anna
New Contributor I
2,215 Views

Hi, Jesus. I've solved the problem. Looks like the stack overflow came from copying the buffer to the untrusted stack. Apparently, Visual Studio set the untrusted stack size to 1MB by default. Increasing the stack reserve size to more than 2MB solved the issue.

View solution in original post

0 Kudos
8 Replies
JesusG_Intel
Moderator
2,251 Views

Hello Anna,


We are looking into your issue. Please stay tuned.


0 Kudos
JesusG_Intel
Moderator
2,244 Views

Hello Anna,


I was able to reproduce your issue but not solve it. You must have enough space in your EPC for SGX Enclave Control Structure, Thread Control Structure, Save State Area, Stack, Heap, and Code and Data. I noticed that decreasing the size of the allocated buffer to a little more than 1000000 made the allocation successful. I could not find a max stack and heap size config that would allow your size of buffer.


Have you verified in your BIOS how much Processor Reserved Memory you have?


Regards,

Jesus


0 Kudos
Criston__Anna
New Contributor I
2,242 Views
Hi Jesus, thank you for your reply. I checked and I had enabled 128 MB of PRM in BIOS.
0 Kudos
JesusG_Intel
Moderator
2,232 Views

Hi Anna, we are still looking into this issue. I apologize for the delay.


0 Kudos
Criston__Anna
New Contributor I
2,216 Views

Hi, Jesus. I've solved the problem. Looks like the stack overflow came from copying the buffer to the untrusted stack. Apparently, Visual Studio set the untrusted stack size to 1MB by default. Increasing the stack reserve size to more than 2MB solved the issue.

0 Kudos
JesusG_Intel
Moderator
2,181 Views

Thanks for sharing the solution, Anna. I'm glad you got it working.


0 Kudos
JesusG_Intel
Moderator
2,178 Views

Hi Anna, I have been playing around with the C++ option /F and setting the linker options for stack reserve and commit sizes but I still cannot get the program to work. I got around the stack overflow but run into a different error.


Would you mind telling us what specifically you changed to resolve the issue?


0 Kudos
Criston__Anna
New Contributor I
2,168 Views

Hi Jesus,

For the stack reserve memory:

I selected the untrusted app project and then went to Proprieties->Configuration Proprieties->Linker->System->Stack Reserve Size and increased the stack size to 4MB at first at think, and it solved the issue.

I don't recall having another problem around here after that, my code is compiling and working now (the same code I posted here). The enclave memory settings are: stack - 0x4000, heap 0x2000000. I have been able to copy chunks of 10MB, 20 MB and more back and forth.

Anna

 

 

0 Kudos
Reply