Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
281 Views

Enclave stack overflow and malloc

Jump to solution

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

Accepted Solutions
New Contributor I
241 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
Highlighted
Moderator
278 Views

Hello Anna,


We are looking into your issue. Please stay tuned.


Jesus Garcia, Intel Customer Support
0 Kudos
Highlighted
Moderator
270 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


Jesus Garcia, Intel Customer Support
0 Kudos
Highlighted
New Contributor I
268 Views
Hi Jesus, thank you for your reply. I checked and I had enabled 128 MB of PRM in BIOS.
0 Kudos
Highlighted
Moderator
258 Views

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


Jesus Garcia, Intel Customer Support
0 Kudos
New Contributor I
242 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
Highlighted
Moderator
207 Views

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


Jesus Garcia, Intel Customer Support
0 Kudos
Highlighted
Moderator
204 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?


Jesus Garcia, Intel Customer Support
0 Kudos
Highlighted
New Contributor I
194 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