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

[CN] to_bytes and from_bytes crash should display helpful err msg on void pointer #851

Closed
peterohanley opened this issue Feb 6, 2025 · 2 comments · Fixed by #905
Closed
Assignees
Labels
cn technical debt Something for internal cleanup ui/ux Issue with presentation or user experience

Comments

@peterohanley
Copy link

peterohanley commented Feb 6, 2025

I believe the error here is because of trying to convert an Owned resource but I am not actually sure. I would like info about what failed, but if that's tricky or the semantics are in flux just the source location of the failure would be helpful.

This feature is #298

to_bytes.c:

typedef unsigned char uint8_t;
typedef struct s {
    uint8_t a;
} s;
/*@
predicate (map<u64,u8>) Array_u8 (pointer p, u64 l)
{
  take pv = each(u64 i; i >= 0u64 && i < l) {Owned<uint8_t>(array_shift<uint8_t>(p,i))};
  return pv;
}
@*/

#if 0
static void transmute_to_blob(void *p)
/*@
  requires take xsi = Array_u8(p, sizeof<s>);
  ensures take xso = Owned<s>(p);
  @*/
{
  /*@ from_bytes Owned<s>(p); @*/
}
#else

static void untransmute_to_blob(void *p)
/*@
  requires take xsi = Owned<s>(p);
  ensures take xso = Array_u8(p, sizeof<s>);
  @*/
{
  /*@ to_bytes Owned<s>(p); @*/
}
#endif
% cn verify to_bytes.c
to_bytes.c:30:7: warning: experimental keyword 'to_bytes' (use of experimental features is discouraged)
  /*@ to_bytes Owned<s>(p); @*/
      ^~~~~~~~ 
cn: internal error, uncaught exception:
    File "backend/cn/lib/check.ml", line 1866, characters 74-80: Assertion failed
    Raised at Cn__Check.check_expr.bytes_constraints in file "backend/cn/lib/check.ml", line 1866, characters 74-86
...
% cn verify to_bytes.c
to_bytes.c:20:7: warning: experimental keyword 'from_bytes' (use of experimental features is discouraged)
  /*@ from_bytes Owned<s>(p); @*/
      ^~~~~~~~~~ 
cn: internal error, uncaught exception:
    File "backend/cn/lib/check.ml", line 1866, characters 74-80: Assertion failed
    Raised at Cn__Check.check_expr.bytes_constraints in file "backend/cn/lib/check.ml", line 1866, characters 74-86
...
@ZippeyKeys12 ZippeyKeys12 added bug Something isn't working cn labels Feb 6, 2025
@dc-mak
Copy link
Collaborator

dc-mak commented Feb 28, 2025

Agreed the crash is not helpful but I'm confused as you what the code is trying to do. If you are trying to convert the pointer itself, then you need to do /*@ to_bytes Owned<void**>(&p); @*/. Converting a void pointer to bytes is not supported because one does not know how large the resulting byte array needs to be as a resource. I've fixed the error message and it's because I don't support structs yet because it's not super obvious at this stage how to handle padding bytes.

@dc-mak
Copy link
Collaborator

dc-mak commented Feb 28, 2025

Examples here:

09:57 ➜  cerberus git:(cn-unify-function-specs) find tests/cn -name '*.c' | xargs grep "to_bytes\|from_bytes" -n
tests/cn/before_from_bytes.error.c:6:    /*@ to_bytes Owned<int>(p); @*/
tests/cn/to_from_bytes_owned.c:5:    /*@ to_bytes Owned(p); @*/
tests/cn/to_from_bytes_owned.c:9:    /*@ from_bytes Owned<int>(p); @*/
tests/cn/to_from_bytes_owned.c:11:    /*@ to_bytes Owned<int>(p); @*/
tests/cn/to_from_bytes_owned.c:12:    /*@ from_bytes Owned<int>(p); @*/
tests/cn/partial_init_bytes.error.c:5:    /*@ to_bytes Block(p); @*/
tests/cn/partial_init_bytes.error.c:9:    /*@ from_bytes Owned<int>(p); @*/
tests/cn/to_bytes.error.c:5:    /*@ to_bytes Alloc(p); @*/
tests/cn/to_from_bytes_block.c:1:void from_bytes(int *p)
tests/cn/to_from_bytes_block.c:9:    /*@ from_bytes Block(p); @*/
tests/cn/to_from_bytes_block.c:12:void to_bytes(int *p)
tests/cn/to_from_bytes_block.c:20:    /*@ to_bytes Block(p); @*/
tests/cn/to_from_bytes_block.c:27:    to_bytes(p);
tests/cn/to_from_bytes_block.c:28:    from_bytes(p);
tests/cn/to_from_bytes_block.c:29:    to_bytes(p);
tests/cn/to_from_bytes_block.c:30:    from_bytes(p);
tests/cn/from_bytes.error.c:5:    /*@ to_bytes Owned(p); @*/
tests/cn/from_bytes.error.c:6:    /*@ from_bytes Alloc(p); @*/ // <-- proof fails here, but this is a no-op in runtime

@dc-mak dc-mak changed the title [CN] to_bytes and from_bytes crash without info about the error [CN] to_bytes and from_bytes crash should display helpful err msg on void pointer Feb 28, 2025
@dc-mak dc-mak added ui/ux Issue with presentation or user experience technical debt Something for internal cleanup and removed bug Something isn't working labels Feb 28, 2025
@dc-mak dc-mak mentioned this issue Mar 3, 2025
@dc-mak dc-mak self-assigned this Mar 4, 2025
dc-mak added a commit to dc-mak/cerberus that referenced this issue Mar 4, 2025
dc-mak added a commit to dc-mak/cerberus that referenced this issue Mar 5, 2025
@dc-mak dc-mak closed this as completed in 0512fcc Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cn technical debt Something for internal cleanup ui/ux Issue with presentation or user experience
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants