bytecodealliance / cranelift-jit-demo Goto Github PK
View Code? Open in Web Editor NEWJIT compiler and runtime for a toy language, using Cranelift
License: Apache License 2.0
JIT compiler and runtime for a toy language, using Cranelift
License: Apache License 2.0
Toy lang can call puts function in libc. How to call a function writen by Rust?
Thanks a lot!
Taken from #37:
As a policy, the Bytecode Alliance is changing the default branch names in all repositories. We would like for all projects to change the default to main
by June 26. (We mention June 26th because there is some suggestion that GitHub may be adding the ability to make this process more seamless. Feel free to wait for that, but only up to June 26. We'll provide further support and documentation before that date.)
Please consider this a tracking issue. It is not intended for public debate.
At first, I appreciate your great effort, thank you for this nice demo of cranelift-jit.
I love this demo a lot.
My question is, it seems that functions are called with input of tuple. For example, (1, 0)
is passed as arguments in the call below.
cranelift-jit-demo/src/bin/toy.rs
Line 21 in 4f70b58
But tuple's number of elements cannot be defined dynamically. Let's say, a user defines a function with 5 arguments, then Rust program must supply a tuple of 5 elements as an input (arguments) to run the function.
But Rust program cannot know how many arguments a user defines for a function before compilation, so I think it is impossible to define them as tuple. So how can I pass such arguments to a function?
I know you can print the cranelift IR with ctx.func
(#55), but is there a way to print the generated x86 instructions before or after the module.finalize_definitions()
line? I want to see the final code that gets generated so I can debug issues with it and perhaps optimize the IR I'm generating.
Thanks!
This demo is great, so I'm enjoying creating some software with this, but currently I'm struggling to get type of "Value" of cranelift. How can I check if string is inside or if number is inside Value? Or can we use this immediate instead of Value?
In the example provided, the function signature for the to-be compiled code-string must be known in advance in order to be able to call into the function properly.
Is it possible to make a call dynamically using an array of parameters instead of being required to mem::transmute
to the correct function signature?
For example, write a simple print
function which can be called from compiled code to print a value.
I want to learn from the generated IR, how can I print it out?
Thanks
my OS: Fedora 33,
Rust: 1.49.0
so when I do cargo run in "cranelift-jit-demo-main" it compiles but then:
thread 'main' panicked at 'unable to make memory readable+executable: SystemCall(Os { code: 13, kind: PermissionDenied, message: "Permission denied" })', /home/adsick/.cargo/registry/src/github.com-1ecc6299db9ec823/cranelift-simplejit-0.68.0/src/memory.rs:197:30
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
why is it so and how do I fix it (or rather it should be fixed somewhere in the actual project, I dunno)
I've been trying to create a fixed length of memory and load from it. I keep getting
terminated by signal SIGSEGV (Address boundary error)
If you could add an example showing such a usecase it would be very helpful.
bytecodealliance/wasmtime#2479
The code and docs should be updated to reflect this change.
How can I disable the access to libc using dlysm?
Thanks,
Dominik
Hi,
I am writing an in browser application via Rust compiled to wasm. In this in-browser application, I would like to be able to dynamically generate code and call it. Is it possible to do this via cranelift-simplejit ? [I am having problem building it for the wasm32-unknown-unknown target.]
Thanks!
I removed the assert to see that the demo does work in Windows. I haven't dug in further to see why it works, but figured I'd share.
Error when compile on linux with
cranelift-jit = { version = "0.79", features = ['selinux-fix'] }
With versions 0.77, 0.78 also not compiled.
error[E0308]: mismatched types
--> /home/ali/.cargo/registry/src/github.com-1ecc6299db9ec823/cranelift-jit-0.79.0/src/memory.rs:37:9
|
35 | fn with_size(size: usize) -> io::Result {
| ---------------- expected std::result::Result<PtrLen, std::io::Error>
because of return type
36 | let alloc_size = region::page::ceil(size);
37 | / MmapMut::map_anon(alloc_size).map(|mut mmap| {
38 | | // The order here is important; we assign the pointer first to get
39 | | // around compile time borrow errors.
40 | | Ok(Self {
... |
44 | | })
45 | | })
| |__________^ expected struct PtrLen
, found enum std::result::Result
|
= note: expected enum std::result::Result<PtrLen, _>
found enum std::result::Result<std::result::Result<PtrLen, _>, _>
Hello, I am a beginner wasm
how do I concatenate strings? Send string?in this jit-demo
as the title says. Do you have to compile them individually and then use indirect call? or is there some other way of doing it?
Tys-MacBook-Pro-2:simplejit-demo tyoverby$ lldb target/debug/toy
(lldb) target create "target/debug/toy"
Current executable set to 'target/debug/toy' (x86_64).
(lldb) run
Process 82396 launched: '/Users/tyoverby/clean/simplejit-demo/target/debug/toy' (x86_64)
the answer is: 42
recursive_fib(10) = 55
iterative_fib(10) = 55
Process 82396 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x102800408)
frame #0: 0x00007fff684413b8 libsystem_malloc.dylib`small_free_list_add_ptr + 314
libsystem_malloc.dylib`small_free_list_add_ptr:
-> 0x7fff684413b8 <+314>: movb %r10b, 0x8(%rdx)
0x7fff684413bc <+318>: movq $0x0, (%rdx)
0x7fff684413c3 <+325>: movq %rax, %r10
0x7fff684413c6 <+328>: shrq $0x8, %r10
Target 0: (toy) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x102800408)
* frame #0: 0x00007fff684413b8 libsystem_malloc.dylib`small_free_list_add_ptr + 314
frame #1: 0x00007fff6844d4f3 libsystem_malloc.dylib`free_small + 921
frame #2: 0x00007fff68442d87 libsystem_malloc.dylib`szone_memalign + 1126
frame #3: 0x00007fff684428d9 libsystem_malloc.dylib`malloc_zone_memalign + 154
frame #4: 0x00007fff68444386 libsystem_malloc.dylib`posix_memalign + 25
frame #5: 0x000000010005dae5 toy`cretonne_simplejit::memory::PtrLen::with_size::hfa3dd636d07d161e(size=12) at memory.rs:25
frame #6: 0x000000010005ddbb toy`cretonne_simplejit::memory::Memory::allocate::h918029ba354eb4c1(self=0x00007ffeefbff3f8, size=12) at memory.rs:75
frame #7: 0x0000000100059f3e toy`_$LT$cretonne_simplejit..backend..SimpleJITBackend$u20$as$u20$cretonne_module..backend..Backend$GT$::define_data::h573815a0e058ec5a(self=0x00007ffeefbff378, _name=(data_ptr = "hello_string\x04", length = 12), data=0x00007ffeefbff2a0, _namespace=0x00007ffeefbfe5b0) at backend.rs:159
frame #8: 0x000000010003f81c toy`_$LT$cretonne_module..module..Module$LT$B$GT$$GT$::define_data::h2a9842b185d866dd(self=0x00007ffeefbff320, data=(__0= 0), data_ctx=0x00007ffeefbff2a0) at module.rs:517
frame #9: 0x000000010004679a toy`toy::jit::JIT::create_data::h8851a848fef777bc(self=0x00007ffeefbfe9a8, name=(data_ptr = "hello_stringhello world", length = 12), contents=<unavailable>) at jit.rs:125
frame #10: 0x00000001000422d8 toy`toy::main::hd9bd26c7b8da2354 at toy.rs:122
frame #11: 0x000000010003a8c2 toy`std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::hfac8eb5e2ec564ba at rt.rs:74
frame #12: 0x00000001002a9db8 toy`std::panicking::try::do_call::h6688c75b958424d8 [inlined] std::rt::lang_start_internal::_$u7b$$u7b$closure$u7d$$u7d$::h577272da3909533e at rt.rs:59 [opt]
frame #13: 0x00000001002a9dac toy`std::panicking::try::do_call::h6688c75b958424d8 at panicking.rs:305 [opt]
frame #14: 0x00000001002bd0bf toy`__rust_maybe_catch_panic at lib.rs:101 [opt]
frame #15: 0x00000001002a6878 toy`std::rt::lang_start_internal::h1d7922f9dfd201cc [inlined] std::panicking::try::h6dbcd1a44c5bb721 at panicking.rs:284 [opt]
frame #16: 0x00000001002a6845 toy`std::rt::lang_start_internal::h1d7922f9dfd201cc [inlined] std::panic::catch_unwind::hdd63a658020b6001 at panic.rs:361 [opt]
frame #17: 0x00000001002a6845 toy`std::rt::lang_start_internal::h1d7922f9dfd201cc at rt.rs:58 [opt]
frame #18: 0x000000010003a8a2 toy`std::rt::lang_start::heba532bdd26b7181(main=(toy`toy::main::hd9bd26c7b8da2354 at toy.rs:10), argc=1, argv=0x00007ffeefbff818) at rt.rs:74
frame #19: 0x0000000100042395 toy`main + 37
frame #20: 0x00007fff68297115 libdyld.dylib`start + 1
frame #21: 0x00007fff68297115 libdyld.dylib`start + 1
(lldb)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.