Skip to content

Actually use layouts #205

Merged
N1ark merged 15 commits intomainfrom
good-layouts
Dec 25, 2025
Merged

Actually use layouts #205
N1ark merged 15 commits intomainfrom
good-layouts

Conversation

@N1ark
Copy link
Copy Markdown
Contributor

@N1ark N1ark commented Dec 18, 2025

Encoder.rust_to_cvals / Encoder.rust_of_cvals where both poorly named functions and badly implemented; in particular they relied very extensively on the type of the values, and handling layouts manually, rather than trusting the layout of the type.

This PR fixes it! I renamed these functions to encode and decode respectively, and factored out a lot of code and simplified it. The core idea is:

  • a value with a Primitive layout is written as-is; we don't even check the type
  • a value with a Array or Arbitrary layout has an iterable set fields; iter_fields returns an iterator over these fields and their offset
  • a value with an Enum layout needs special treatment to figure out what variant we're reading/writing; however once we know the variant it's straightforward, as above.

There is relatively little additional effort needed! I also separated the validity checks done for references into its own function, check_valid (this comes with the advantage that we can more easily disable validity checks, if we want to one day).

This is the second part of #127

Additional minor changes:

  • added some utilities to Iter
  • remove the implementation of Binop.Cmp for pointers, as it's never used
  • properly remove metadata from pointers when needed

@N1ark N1ark requested a review from giltho as a code owner December 18, 2025 14:16
@N1ark N1ark added soteria-rust Issues related to soteria-rust cleanliness Quality of life improvements, to keep the codebase clean and healthy labels Dec 18, 2025
@N1ark N1ark mentioned this pull request Dec 18, 2025
3 tasks
Copy link
Copy Markdown
Contributor

@giltho giltho left a comment

Choose a reason for hiding this comment

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

Some nitpicks but overall looks good!

(Reviewing in the Paris metro is not the easiest lmao)

Comment thread soteria-rust/lib/core.ml Outdated
Comment thread soteria-rust/lib/core.ml
Comment on lines +171 to +174
ok
(mk_concrete ~size:(ptr_size * 2) ~align:ptr_size
~fields:(Array (BV.usizei ptr_size))
())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Btw iirc there's an explicit comment in the core lib saying that external users should never rely on the layout of fat pointers. So maybe one day we should make access raw or opaque depending on access within core or outside.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmm this could be interesting yes ! i remember @Nadrieril mentioning that slice layouts or their ABI might eventually become guaranteed, so it would be nice to figure out progress on that (pinging because I cannot find a source anywhere:P )

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The RFC is here: rust-lang/rfcs#3775. Looks stalled for now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ahhh nice thank you !

Comment thread soteria/lib/soteria_std/iter_soteria.ml Outdated
Comment thread soteria/tests/soteria_std/iter_tests.ml
Comment thread soteria-rust/lib/state.ml
Comment thread soteria-rust/lib/rust_val.ml
Comment thread soteria-rust/lib/encoder.ml
@giltho
Copy link
Copy Markdown
Contributor

giltho commented Dec 25, 2025

(You need to promote files)

@N1ark N1ark merged commit bc6107e into main Dec 25, 2025
4 of 5 checks passed
@N1ark N1ark deleted the good-layouts branch December 25, 2025 14:33
pcarrott pushed a commit that referenced this pull request Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanliness Quality of life improvements, to keep the codebase clean and healthy soteria-rust Issues related to soteria-rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants