lwt icon indicating copy to clipboard operation
lwt copied to clipboard

fix setting Lwt_process env on Windows

Open mroch opened this issue 2 years ago • 11 comments

env is an "environment block": a null-terminated block of null-terminated strings. using caml_stat_strdup_to_os on it doesn't work, it only copies the first string.

instead, we need to use an explicit length (caml_string_length(env)), but there's no public *_to_os function for this so we have to use the internal caml_win32_multi_byte_to_wide_char directly.

Fixes https://github.com/ocsigen/lwt/issues/966

mroch avatar Oct 19 '22 22:10 mroch

Thanks a lot for the MR. I don't have a windows machine to test this on so I can't reproduce locally. Nevertheless, I'll be reviewing this MR soon.

raphael-proust avatar Oct 20 '22 06:10 raphael-proust

i dunno if you can get CI to run on the first diff (1d199cd) but it'll show the bug. for example: https://github.com/mroch/lwt/actions/runs/3284275234/jobs/5410034604

I believe the EINVAL comes from passing just a single null terminated string, without the additional \0 to terminate the block (and it's only passing the first of many envs, but I think it's the malformed block that causes the EINVAL specifically)

mroch avatar Oct 20 '22 12:10 mroch

i dunno if you can get CI to run on the first diff

I don't know either. I don't understand github's UI very well. :question: :exclamation:

But that's ok. When I do the review I might push a branch with just the first commit to force the CI.

raphael-proust avatar Oct 20 '22 14:10 raphael-proust

At first I was pleased that the code was so similar.
Then I thought it'd be even better if the code was even more similar.
And then I started thinking about licenses and now I don't know what to do about this PR.

I'll ask if it's ok. Basically, the added code is calling the functions provided by the ocaml project in the appropriate sequence. I think the alternative to using the same code is making a pull-request on the ocaml project to provide this sequence of calls as a function itself to be called. That might not be desirable upstream anyway…

Anyway, I'll ask.

raphael-proust avatar Feb 06 '23 08:02 raphael-proust

A similar situation happened on the eio bugtracker:
https://github.com/ocaml-multicore/eio/pull/352#discussion_r1037282020

It's not exactly the same (not the same code being copied, not the same size of code) but it's also copying parts of the C code.

raphael-proust avatar Feb 06 '23 08:02 raphael-proust

I've made https://github.com/raphael-proust/ocaml/commit/bdbddd2c25687c1bf658be6835f8b12b0c99704d which aims to expose the code that we actually need from the OCaml source. Please leave comments on there, I don't know that I did anything correctly.

raphael-proust avatar Aug 17 '23 12:08 raphael-proust

As per https://github.com/ocaml/ocaml/pull/12492#issuecomment-1728049155 it turns out licensing is not an issue. We can go ahead on this PR.

raphael-proust avatar Oct 09 '23 08:10 raphael-proust

This is long overdue ;)

MisterDA avatar Jun 20 '24 07:06 MisterDA

Do we need to #include an additional file for windows on 5.2? The CI has the following error for that specific combination:

#=== ERROR while compiling lwt.dev ============================================#
# context     2.2.0 | win32/x86_64 | ocaml-base-compiler.5.2.0 | pinned(git+file://D:/a/lwt/lwt#HEAD#9d9efab4e28699c2f9ffd8397f19141dac199ead)
# path        D:\a\lwt\lwt\_opam\.opam-switch\build\lwt.dev
# command     D:\a\lwt\lwt\_opam\bin\dune.exe build -p lwt -j 3 @install
# exit-code   1
# env-file    D:\.opam\log\lwt-5000-9b088c.env
# output-file D:\.opam\log\lwt-5000-9b088c.out
### output ###
# File "_build/.dune/default/src/unix/dune", line 49, characters 2-19:
# 49 |   lwt_process_stubs
#        ^^^^^^^^^^^^^^^^^
# (cd _build/default/src/unix && D:\a\lwt\lwt\_opam\bin\x86_64-w64-mingw32-gcc.exe -O2 -fno-strict-aliasing -fwrapv -mms-bitfields -I. -DUNICODE -D_UNICODE -g -I D:/a/lwt/lwt/_opam/lib/ocaml -I D:/a/lwt/lwt/_opam/lib/ocaml\threads -I D:/a/lwt/lwt/_opam/lib/ocaml\unix -I D:\a\lwt\lwt\_opam\lib\bytes -I D:\a\lwt\lwt\_opam\lib\ocplib-endian -I D:\a\lwt\lwt\_opam\lib\ocplib-endian\bigstring -I ../core -o lwt_process_stubs.o -c lwt_process_stubs.c)
# In file included from D:/a/lwt/lwt/_opam/lib/ocaml/caml/memory.h:27,
#                  from lwt_process_stubs.c:21:
# D:/a/lwt/lwt/_opam/lib/ocaml/caml/domain.h: In function 'caml_check_gc_interrupt':
# D:/a/lwt/lwt/_opam/lib/ocaml/caml/domain.h:43:3: error: 'CAMLalloc_point_here' undeclared (first use in this function)
#    43 |   CAMLalloc_point_here;
#       |   ^~~~~~~~~~~~~~~~~~~~
# D:/a/lwt/lwt/_opam/lib/ocaml/caml/domain.h:43:3: note: each undeclared identifier is reported only once for each function it appears in

raphael-proust avatar Aug 02 '24 18:08 raphael-proust

Hmm, apparently I accidentally allowed https://github.com/ocaml/ocaml/issues/11449 to be closed by the stale bot. The issue is that CAML_INTERNALS is causing more things to happen in the headers than are wanted. I wonder if this might be the best plan until an "official" API function is added: as before, only define CAML_INTERNALS for OCaml < 4.13, but instead duplicate the extern declaration from osdeps.h for caml_win32_multi_byte_to_wide_char?

It might be possible to work around it by explicitly including caml/misc.h, but I think it's better to have it that CAML_INTERNALS is only being defined for old versions of OCaml (i.e. that it's not still required with current versions)?

dra27 avatar Aug 07 '24 13:08 dra27