Stream: cranelift

Topic: Some JIT Questions (ft. an observation)


view this post on Zulip Endistic (Jul 14 2023 at 15:10):

o/
I'm very new here & to Cranelift, so sorry if I misunderstand some things or struggle alot. I'm just gonna cut to the chase with my questions.

  1. How would I go about interacting with heap memory & stack memory? I'm aware of the load and store methods on InstBuilder, but I always get an access violation.
error: process didn't exit successfully: `target\debug\jitmemtest.exe` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)
  1. I remember seeing that you can interact with libc in the jit demo program. How does one do this?
  2. Not really a question, but more of an observation & idea. I feel like Cranelift doesn't have much documentation. Or that it does, but it's hard to find your way around due to how many crates there are. I think this concept of a "Cranelift Book" akin to the Rust Book but for Cranelift would be really interesting. It could walk you through from, say, making your first function, to advanced higher-level concepts like arrays and strings.
  3. Also, does anyone know if there's a bytecode alliance discord? I'm a lot more familiar with discord than zulip, but if not that's fine.

view this post on Zulip bjorn3 (Jul 14 2023 at 18:21):

@Endistic

  1. What address do you pass to them?
  2. You can use declare_function with Linkage::Import and skip a define_function. Then dlsym will be used to look it up.
  3. Yeah, we could use a lot more docs.
  4. AFAIK there is only this zulip.

view this post on Zulip Endistic (Jul 14 2023 at 18:26):

For the address I passed zero, because I wasn't really sure what else to pass. I couldn't find anything address-related besides the reference types in the docs. It was something like:

{
            let param = builder.block_params(block)[0];
            let addr = builder.ins().iconst(types::I32, 0);
            builder
                .ins()
                .store(MemFlags::new(), param, addr, Offset32::new(0));
            let got = builder
                .ins()
                .load(types::I32, MemFlags::new(), addr, Offset32::new(0));
            builder.ins().return_(&[got]);
        }

I think the zero is probably what caused the access violation, but I don't know how I coul0d get a valid address the function could write to into the function.

Also, for the Cranelift Book, I might give making that a try once I learn how to do those things - I've wrote my fair share of documentation before and have a fair bit in C and other low-level things.

view this post on Zulip bjorn3 (Jul 14 2023 at 18:28):

Right, you have to pass the address you want to load/store. 0 is never a valid address.

view this post on Zulip bjorn3 (Jul 14 2023 at 18:28):

Do you want to access a stack variable?

view this post on Zulip bjorn3 (Jul 14 2023 at 18:29):

Then you can create a stackslot using builder.func.create_sized_stack_slot() and get the address using builder.ins().stack_addr().

view this post on Zulip Endistic (Jul 14 2023 at 18:43):

That did the trick, thank you!!

{
            let slot = builder.func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 4));
            let param = builder.block_params(block)[0];
            let addr = builder.ins().stack_addr(types::I32, slot, Offset32::new(0));
            builder
                .ins()
                .store(MemFlags::new(), param, addr, Offset32::new(0));
            let got = builder
                .ins()
                .load(types::I32, MemFlags::new(), addr, Offset32::new(0));
            builder.ins().return_(&[got]);
        }

And for future reference while you're here, what about memory on the heap? How would I allocate & do stuff with that?

view this post on Zulip bjorn3 (Jul 14 2023 at 18:52):

You could use malloc like you would do in C. load and store will work with the returned address too. Just make sure to free() it again once you are done to avoid a memory leak.

view this post on Zulip Endistic (Jul 14 2023 at 18:56):

Ah ok, thank you so much for your help!

view this post on Zulip Notification Bot (Jul 18 2023 at 21:04):

4 messages were moved from this topic to #cranelift > new documentation book by Jamey Sharp.


Last updated: Nov 22 2024 at 16:03 UTC