[all-commits] [llvm/llvm-project] b06c66: [NFC][OCaml] Simplify llvm_global_initializer usin...

Josh Berdine via All-commits all-commits at lists.llvm.org
Fri Mar 26 05:01:51 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: b06c669114e2e903ca3941e3ec738f868c863eee
      https://github.com/llvm/llvm-project/commit/b06c669114e2e903ca3941e3ec738f868c863eee
  Author: Josh Berdine <josh at berdine.net>
  Date:   2021-03-26 (Fri, 26 Mar 2021)

  Changed paths:
    M llvm/bindings/ocaml/llvm/llvm_ocaml.c

  Log Message:
  -----------
  [NFC][OCaml] Simplify llvm_global_initializer using ptr_to_option

This diff uses ptr_to_option to convert a nullable C pointer to an
OCaml option instead of the redundant implementation in
llvm_global_initializer.

Differential Revision: https://reviews.llvm.org/D99391


  Commit: 0b1dc49ca38a8569b0aee40ea8d3054bc960e2ed
      https://github.com/llvm/llvm-project/commit/0b1dc49ca38a8569b0aee40ea8d3054bc960e2ed
  Author: Josh Berdine <josh at berdine.net>
  Date:   2021-03-26 (Fri, 26 Mar 2021)

  Changed paths:
    M llvm/bindings/ocaml/llvm/llvm_ocaml.c
    M llvm/bindings/ocaml/llvm/llvm_ocaml.h

  Log Message:
  -----------
  [NFC][OCaml] Resolve const and unsigned compilation warnings

There are a number of compilation warnings regarding disregarding
const qualifiers, and casting between pointers to integer types with
different sign.

The incompatible sign warnings are due to treating the result of
`LLVMGetModuleIdentifier` as `const unsigned char *`, but it is
declared as `const char *`.

The dropped const qualifiers are due to the code pattern
`memcpy(String_val(_),_,_)` which ought to be (following the
implementation of the OCaml runtime)
`memcpy((char *)String_val(_),_,_)`. The issue is that `String_val` is
usually used to get the value of an immutable string. But in the
context of the `memcpy` calls, the string is in the process of being
initialized, so is not yet constant.

Differential Revision: https://reviews.llvm.org/D99392


  Commit: 6f77926f464b6834339895e295f295953686af22
      https://github.com/llvm/llvm-project/commit/6f77926f464b6834339895e295f295953686af22
  Author: Josh Berdine <josh at berdine.net>
  Date:   2021-03-26 (Fri, 26 Mar 2021)

  Changed paths:
    M llvm/bindings/ocaml/llvm/llvm_ocaml.c

  Log Message:
  -----------
  [OCaml] Fix a possible crash in llvm_struct_name

The implementation of `llvm_struct_name` before this diff calls
`caml_copy_string`, which allocates, while the `result` local variable
points to a block allocated by `caml_alloc_small` that has not yet
been initialized. If the allocation in `caml_copy_string` triggers a
garbage collection, then the GC root `result` contains a pointer to
uninitialized data, which may crash the GC or lead to a memory
corruption.

This diff fixes this by allocating and initializing the string first
and then allocating and initializing the option, thereby leaving no
dangling pointers when allocations are made.

The conversion from a C string to an OCaml string option is refactored
into a function, `cstr_to_string_option`. This function is also used
to simplify the definitions of `llvm_get_mdstring` and
`llvm_string_of_const`.

Differential Revision: https://reviews.llvm.org/D99393


Compare: https://github.com/llvm/llvm-project/compare/c39460cc4f7c...6f77926f464b


More information about the All-commits mailing list