Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trussed memory usage #112

Open
arturkow2000 opened this issue Mar 30, 2023 · 1 comment
Open

Trussed memory usage #112

arturkow2000 opened this issue Mar 30, 2023 · 1 comment

Comments

@arturkow2000
Copy link

Hello, we are having an issue where Trussed consumes large amounts of stack. The problem happens when constructing a before calling into Trussed.
To construct a message we need to allocate it on stack, since all types must have size known at compile-time, Trussed defines limits in config.rs. Now, when we want to, let's say we want to hash some data:

trussed::syscall!(trussed.hash_sha256(&[1]));

Internally, hash_sha256 calls the hash() functions which accepts Message instead of &[u8], this causes allocation of MAX_MESSAGE_LENGTH which currently is 1 KiB, just to copy that data into IPC buffer. Usually when optimizations are involved, some functions may be inlined, if hash_sha256 get's inlined, this may worsen the problem, as the stack won't be freed until execution exits from the current function. Now, when we call into Trussed, Trussed copies this data onto stack, doubling stack usage.

Things quickly get worse when limits are increased (we had to increase limits due to lack of support for reading/writing files in parts).

Ideally, Trussed APIs should be changed in such a way to allow initialization of IPC buffer in-place. Also, the data returned from Trussed shouldn't be explicitly copied onto stack, instead it should be possible to process the response directly from IPC buffer.

@sosthene-nitrokey
Copy link
Contributor

This was discussed when implementing extensions: #70 (comment)

This could be a significant improvement regarding stack usage, but it would also be a breaking change.
I think it would make sense to use references as part of the builder API we discussed in the past: #71 (review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants