CensoredUsername / dynasm-rs

A dynasm-like tool for rust.
https://censoredusername.github.io/dynasm-rs/language/index.html
Mozilla Public License 2.0
712 stars 52 forks source link

no_std support #30

Open losfair opened 6 years ago

losfair commented 6 years ago

I'm building a JIT that runs on bare metal and therefore std is not available. Is it possible to make dynasm-rs compatible with the no_std mode (with alloc), as long as OS-dependent things like executable memory management are left to the user?

CensoredUsername commented 6 years ago

I can investigate adding a no_std mode, but in this mode only basic things like the trait definitions will be exported. Most of the interesting runtime parts (executable memory handling) do require std. Particularly annoying is that alloc doesn't contain HashMap, so implementing the label API as only requiring alloc would require changes. Outside of that though, as long as you just define the methods required by the DynasmApi/DynasmLabelApi traits you can already use the plugin on nightly now.

CensoredUsername commented 6 years ago

@losfair regarding leaving executable memory management to the user, the way current Assemblers are written is quite intertwined with this memory management, and there are a few ways I could go about this.

The first approach would be to just assemble into a temporary buffer, and then hand it over to the user to remap it (if necessary) after assembling. This would be OK for x64 but would not work for x86 as relocation information requires knowledge of the address the code will end up at.

The second approach would be to have the user reserve a large enough buffer at the start, and pass this to an Assembler for assembling. This would be ok on most instruction sets, as we can assume the buffer never moves. For bare metal situations this would probably be preferable as it works even on systems with complete manual memory management.

Finally, more complex approaches would be possible where dynasmrt would have an unsafe trait for memory management systems that the user could implement and use the full-fledged Assemblers. This would be more flexible but I'm not sure if there's any platforms that you would need this complexity on that do not have a functional libstd.

Do you have an idea of what would suit you best?

losfair commented 6 years ago

I'd prefer the first or the second method to keep things simple. However, I have a few questions about these two approaches:

CensoredUsername commented 6 years ago

I think it would be possible to have relocations functioning properly with the first approach. This is already how the x86 Assembler backend deals with resizing its internal buffer, so copying the mechanism would be ok.

For the second approach, that would fully depend on the user being able to determine the code size ahead of time.

So the first approach is probably preferable (even though it'll cost a bit of extra memory due to the buffer copy involved).

eira-fransham commented 5 years ago

hashbrown works with alloc only AFAIK.

1c3t3a commented 3 years ago

Are there any plans on implementing this? I am asking because I am looking for no_std support in Wasmer which uses this crate for code generation. I could also try to implement this if there are mentoring instructions to it.

CensoredUsername commented 3 years ago

@1c3t3a Currently not from my side, but I'd be happy to assist with mentoring. The runtime support crate got some significant rework since this issue was opened in order to separate label, memory and relocation handling, which should make the required changes easier to implement.

rmccrystal commented 2 years ago

Shouldn't it be pretty easy to add no_std + alloc support with no-std-compat?

1c3t3a commented 2 years ago

That wouldn't work apparently, as no-std-compat doesn't handle std::io (how should it in an environment where it's unclear if there even is a filesystem).

CensoredUsername commented 2 years ago

std::io, std::fmt and friends are only used for Display/Debug implementations, which you probably don't need in no-std environments anyway.

As such an environment is probably a lot more limited in needed features, only the basic assembler, label handling and relocation logic needs to be properly ported.

Lucky4Luuk commented 1 year ago

Any updates on this? I'm also interested in using wasmer in a no_std environment where I do have alloc support.

CensoredUsername commented 1 year ago

Unfortunately I don't think anyone picked this up. I don't think I'll handle this one myself but I'd be happy to mentor / answer questions if anyone wants to work on it.

Lucky4Luuk commented 1 year ago

Fair enough. I saw there is a pull request open for no_std support, but I have no idea how far in progress it is. Regardless, I think even if this library supported no_std, wasmer would still require a bunch of work to become no_std compatible anyway. I'll simply have to take a look at the existing PR and see what I can do. Thanks for the work on dynasm though!

CensoredUsername commented 1 year ago

I completely forgot about that one, whoops. I also don't have any idea how up-to-date it is, but it is probably a good starting point!

stevefan1999-personal commented 1 year ago

I have added the support in #67, feel free to move the discussion over there