Skip to content

unsafe indexing by unboxed nativeint (and bigger) explicitly truncates the index when it shouldn't #3889

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

Open
jvanburen opened this issue Apr 17, 2025 · 0 comments
Assignees
Labels
bug Something isn't working cmm Cmm language / helpers changes flambda2 Prerequisite for, or part of, flambda2

Comments

@jvanburen
Copy link
Contributor

The below code should either segfault immediately, or it should print both values. I claim it should do the former but 5.2.0minus-9 does the latter. Even worse, 5.2.0minus-8 and before prints a single value and then segfaults on the second invocation of go.

We should probably fix this by making the indexing arguments take a Naked_nativeint rather than a Naked_immediate.

type bigstring = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t

external unsafe_get_int32_u_le
  :  local_ bigstring
  -> pos:int64#
  -> int32#
  = "%caml_bigstring_get32u#_indexed_by_int64#"

external opaque_i64 : int64# -> int64# = "%opaque"
external box_int32 : int32# -> int32 = "%box_int32"
external unbox_int64 : int64 -> int64# = "%unbox_int64"

let[@inline never] print_int32u i32 =
  Printf.printf "%04lx\n%!" (box_int32 i32)

let[@inline never] go bigstring =
  let pos = unbox_int64 Int64.min_int in
  print_int32u (unsafe_get_int32_u_le bigstring ~pos);
  print_int32u (unsafe_get_int32_u_le bigstring ~pos:(opaque_i64 pos))

let () = go (Bigarray.Array1.of_array Bigarray.char Bigarray.c_layout [| 'a' ;'b' ; 'c'; 'd'|])
@jvanburen jvanburen self-assigned this Apr 17, 2025
@jvanburen jvanburen added bug Something isn't working flambda2 Prerequisite for, or part of, flambda2 cmm Cmm language / helpers changes labels Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cmm Cmm language / helpers changes flambda2 Prerequisite for, or part of, flambda2
Projects
None yet
Development

No branches or pull requests

1 participant