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

emul: Runtime and build scripts #173

Merged
merged 18 commits into from
Sep 6, 2024
Merged

emul: Runtime and build scripts #173

merged 18 commits into from
Sep 6, 2024

Conversation

naure
Copy link
Collaborator

@naure naure commented Aug 29, 2024

Issue #179. This is an experimental setup to compile programs for the VM.

Runtime

  • Introduce a ceno_rt crate. It is a convention to provide a *_rt for the platform, to handle details of startup, memory, and IO.
  • Minimal linker and build scripts to put the code at the right location.
  • Example ceno_rt_mini that successfully halts.
  • Example ceno_rt_mem that uses the stack and a global variable.
  • Example ceno_rt_panic that panics. The emulator returns an error.

Emulator

  • Parse compiled VM programs. This uses the elf crate and a function taken from Risc0.
  • Load the code and data into ROM and RAM.
  • Execute the program with the emulator.

@naure naure changed the title Runtime emul: Runtime and build scripts Aug 30, 2024
@naure naure mentioned this pull request Aug 30, 2024
@naure naure requested review from hero78119 and kunxian-xia August 30, 2024 10:24
@naure naure marked this pull request as ready for review August 30, 2024 10:29
naure and others added 4 commits August 30, 2024 12:30
* emul-elf: introduce elf crate and risc0 load_elf()

* emul-elf: load and execute a test program

* emul-elf: use examples/ folder

* runtime: example program using memory and stack

---------

Co-authored-by: Aurélien Nicolas <[email protected]>
@naure naure force-pushed the runtime branch 2 times, most recently from c5ad53a to 963d504 Compare September 2, 2024 07:40
Copy link
Collaborator

@hero78119 hero78119 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM! I believe detail can be further adjust once we move to integration phase

just to bring up a question to discuss regarding to overflow handling: does compiler/toolchain seasoning unsigned overflow check in riscv by default?

@naure
Copy link
Collaborator Author

naure commented Sep 2, 2024

Overall LGTM! I believe detail can be further adjust once we move to integration phase

Absolutely, we can go back to the layout of virtual memory and decide what regions the VM provides and how we are going to implement them. Then reflect it in the linker script.

just to bring up a question to discuss regarding to overflow handling: does compiler/toolchain seasoning unsigned overflow check in riscv by default?

I just tried and it is disabled by default. A particular program can enable it with overflow-checks = true; it will cause a trap, and the emulator will return an error.

@hero78119
Copy link
Collaborator

hero78119 commented Sep 2, 2024

I just tried and it is disabled by default. A particular program can enable it with overflow-checks = true; it will cause a trap, and the emulator will return an error.

Thanks for the quick verified :) I think it make sense to disable overflow-checks by default in compiler. The reason, IIUC, having overflow-check will make the trace longer, for example, opcode add roughly contribute to 40% execution traces, and enable overflow-checks=true means those 40% execution trace will be double. For now, prover circuit already constrain overflow at add circuit, and this constraint introduce negligible overhead in favour of longer traces.

In other word, it's like we on purposely design circuit dissatisfy perfect completeness of emulator statement, because it means there $\exists$ valid statements of executor (program + IO), which we are unable to generate respective valid proofs, because those proof will not pass the verifier check due to those are use overflow & wrapping add to deal with program logic. I would claim our program will not have this scenario therefore this limitation will not bring any impact.

cc @kunxian-xia and @zemse

@hero78119
Copy link
Collaborator

hero78119 commented Sep 3, 2024

I just tried and it is disabled by default. A particular program can enable it with overflow-checks = true; it will cause a trap, and the emulator will return an error.

Thanks for the quick verified :) I think it make sense to disable overflow-checks by default in compiler. The reason, IIUC, having overflow-check will make the trace longer, for example, opcode add roughly contribute to 40% execution traces, and enable overflow-checks=true means those 40% execution trace will be double. For now, prover circuit already constrain overflow at add circuit, and this constraint introduce negligible overhead in favour of longer traces.

In other word, it's like we on purposely design circuit dissatisfy perfect completeness of emulator statement, because it means there ∃ valid statements of executor (program + IO), which we are unable to generate respective valid proofs, because those proof will not pass the verifier check due to those are use overflow & wrapping add to deal with program logic. I would claim our program will not have this scenario therefore this limitation will not bring any impact.

cc @kunxian-xia and @zemse

An updated: realized revm i256 wrapping_add -> calling ruint crates overflowing_add https://github.com/recmo/uint/blob/ff2d1a65500172ec3f29313d78c84a20b48490fc/src/add.rs#L62-L63
so I guess respective assembly code should reflect a list of riscv instruction which should allow overflow. So I have to fixed by above statement with

  • keep overflow-checks = false by default
  • expose overflow as witin for external assignment

This was referenced Sep 4, 2024
Copy link
Collaborator

@hero78119 hero78119 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM although not having all the check for the detail!

A suggestion: binary file will be better to compile on the fly instead of commit to repo. But since the size is small with just few kb, we can polishing it as enhancement later

@hero78119
Copy link
Collaborator

build failed should be resolved after this #195
@naure please try to update to latest master branch

@naure naure merged commit 41aa9f8 into master Sep 6, 2024
4 checks passed
@naure naure deleted the runtime branch September 6, 2024 10:11
@naure
Copy link
Collaborator Author

naure commented Sep 6, 2024

@hero78119 That worked, thank you!

kunxian-xia added a commit that referenced this pull request Sep 6, 2024
- Partially address issue #174 
     - fix under constraints
     - optimized constriants register read constraints check
- address the discussion in
#173 (comment) to
support overflow/underflow arithmetics due to wrapping_add/sub in revm
- `MockProver` move to test scope and load u16 table
> Later we need to optimize and support table reuse across unittest to
reduce overhead. cc @zemse :)
- ~~all witness assignment use base field instead of ext field~~ done by
#192

---------

Co-authored-by: kunxian xia <[email protected]>
naure added a commit that referenced this pull request Sep 10, 2024
_Based on #173._

This PR experiments with how Input / Output can work. It is based on
MMIO - Memory-Mapped IO. It works by reading or writing to specific
memory addresses. The system recognizes this and can commit to the
content as part of the proof.

This can also be used for debug statements during development:

```
📜📜📜 Hello, World!
🌏🌍🌎
test test_ceno_rt_io ... ok
```

---------

Co-authored-by: Aurélien Nicolas <[email protected]>
hero78119 added a commit that referenced this pull request Sep 10, 2024
_Based on #173._

- This PR provides a `GlobalAllocator` to support heap memory.
- Example program using `Vec`.

---------

Co-authored-by: Aurélien Nicolas <[email protected]>
Co-authored-by: Ming <[email protected]>
hero78119 pushed a commit that referenced this pull request Sep 30, 2024
_Issue #179._ This is an experimental setup to compile programs for the
VM.

### Runtime

- Introduce a `ceno_rt` crate. It is a convention to provide a `*_rt`
for the platform, to handle details of startup, memory, and IO.
- Minimal linker and build scripts to put the code at the right
location.
- Example `ceno_rt_mini` that successfully halts.
- Example `ceno_rt_mem` that uses the stack and a global variable.
- Example `ceno_rt_panic` that panics. The emulator returns an error.

### Emulator

- Parse compiled VM programs. This uses the [elf
crate](https://docs.rs/elf/latest/elf/) and a function taken from Risc0.
- Load the code and data into ROM and RAM.
- Execute the program with the emulator.

---------

Co-authored-by: Aurélien Nicolas <[email protected]>
hero78119 added a commit that referenced this pull request Sep 30, 2024
- Partially address issue #174 
     - fix under constraints
     - optimized constriants register read constraints check
- address the discussion in
#173 (comment) to
support overflow/underflow arithmetics due to wrapping_add/sub in revm
- `MockProver` move to test scope and load u16 table
> Later we need to optimize and support table reuse across unittest to
reduce overhead. cc @zemse :)
- ~~all witness assignment use base field instead of ext field~~ done by
#192

---------

Co-authored-by: kunxian xia <[email protected]>
hero78119 pushed a commit that referenced this pull request Sep 30, 2024
_Based on #173._

This PR experiments with how Input / Output can work. It is based on
MMIO - Memory-Mapped IO. It works by reading or writing to specific
memory addresses. The system recognizes this and can commit to the
content as part of the proof.

This can also be used for debug statements during development:

```
📜📜📜 Hello, World!
🌏🌍🌎
test test_ceno_rt_io ... ok
```

---------

Co-authored-by: Aurélien Nicolas <[email protected]>
hero78119 added a commit that referenced this pull request Sep 30, 2024
_Based on #173._

- This PR provides a `GlobalAllocator` to support heap memory.
- Example program using `Vec`.

---------

Co-authored-by: Aurélien Nicolas <[email protected]>
Co-authored-by: Ming <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants