Skip to content

Fix snprintf_os use in runtime_events_consumer.c#13089

Merged
gasche merged 2 commits intoocaml:trunkfrom
eutro:runtime-events-consumer-snprintf
Apr 10, 2024
Merged

Fix snprintf_os use in runtime_events_consumer.c#13089
gasche merged 2 commits intoocaml:trunkfrom
eutro:runtime-events-consumer-snprintf

Conversation

@eutro
Copy link
Copy Markdown
Contributor

@eutro eutro commented Apr 10, 2024

This PR fixes an incorrect use of snprintf_os for formatting the runtime events ring file path in the implementation of otherlibs/runtime_events. Specifically, this changes the call to use the OS string rather than a char * copy, which both matches how it is constructed in runtime_events.c:

static char_os *runtime_events_path;

snprintf_os(current_ring_loc, RUNTIME_EVENTS_MAX_MSG_LENGTH,
T("%s/%ld.events"), runtime_events_path, pid);

and is actually correct. It also has the benefit of removing a single alloc/free pair on all OSes.


This bug only manifests on Windows, where char_os is not char, and thus was unlikely to have been tested by anyone (before me, I suppose1). Windows has further issues with this API, in how the Unix library handles PIDs on Windows (as HANDLEs casted to int, rather than real PIDs, see a similar issue in #11021), which makes some existing uses of Runtime_events.create_cursor still broken on Windows, with no unambiguous fix.2

At a glance, there seem to be further bugs in this function too. For instance, runtime_events_loc is unconditionally initialised to caml_stat_alloc_noexc(RING_FILE_NAME_MAX_LEN), but isn't freed on the code path with pid < 0, nor does it seem to be freed at all if the function completes unexceptionally. I'm also slightly weary of the use of int for the PID3.

I don't think a change entry is needed for this fix specifically, but it could be linked to the change entry with a bug fix for the above.

Footnotes

  1. I've successfully attached to another process using runtime events on a build of OCaml that includes this fix as well as another to fix the PID issue, but I could produce a program that attaches with just this fix, if desired.

  2. There are many different options: This function could perform the HANDLE to PID conversion, however that would fail if the user does provide an actual PID. The Unix library could also be changed: a pidfd type; providing a way to get the real PID; returning actual PIDs even on Windows; etc. Another option would be to sidestep the use of PIDs altogether, and add something like OCAML_RUNTIME_EVENTS_FILE to specify an exact file.

  3. It should work fine on Linux (PID_MAX_LIMIT is 2^22 on 64-bit systems, according to proc(5)) and macOS (according to this SO post, PIDs don't go higher than 99998). However it could be an issue on Windows, either if we interpret it as a HANDLE (which is a pointer), or as an actual PID (Raymond Chen claims to have seen PIDs in the 4 bn. range in this SO comment).

Copy link
Copy Markdown
Contributor

@nojb nojb left a comment

Choose a reason for hiding this comment

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

Good catch! LGTM

I don't think a change entry is needed for this fix specifically

This is a bug fix, so I do think it deserves a Changes entry.

By the way, how did the bug manifest for you?

@nojb nojb added the windows label Apr 10, 2024
@eutro eutro force-pushed the runtime-events-consumer-snprintf branch from 1455244 to 3f25ba5 Compare April 10, 2024 11:59
Specifically, use the OS string for the `%s` parameter, as it's supposed to be used
@eutro eutro force-pushed the runtime-events-consumer-snprintf branch from 3f25ba5 to 0d9d194 Compare April 10, 2024 12:01
@eutro
Copy link
Copy Markdown
Contributor Author

eutro commented Apr 10, 2024

This is a bug fix, so I do think it deserves a Changes entry.

It's turning out to be harder than the bug fix itself...

By the way, how did the bug manifest for you?

I was trying to get https://github.com/tarides/runtime_events_tools/ working on Windows and when I fixed the PID issue I ran into it formatting the completely wrong path anyway.

@nojb
Copy link
Copy Markdown
Contributor

nojb commented Apr 10, 2024

Thanks for adding the Changes entry. I tweaked it to mention the terms that may be relevant for the ocassional Changes reader ("Windows", runtime_events library).

@gasche gasche merged commit 4c6a384 into ocaml:trunk Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants