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

Safer traces, part 2: Trace builder #63

Merged
merged 7 commits into from
Nov 14, 2022

Conversation

daladim
Copy link
Collaborator

@daladim daladim commented Nov 7, 2022

Fixes #17
Fixes #45
Fixes #26

Sorry, that's a rather large diff, but I did not manage to split it in smaller commits or PR :-/
As stated on the commit message for Trace now has a Builder, it's probably easier to read the result of this very commit rather than its changes.

This commit does not compile, but will make it possible for a following commit to remove the not-so-idiomatic fill_info()
Also, this commit:
* fixes a possible panic when trying to write an arbitrary-length String to a u8 array
* better checks the error conditions for StartTraceW (a null handle should be considered invalid)
This largely refactors trace.rs and evntrace.rs
* evntrace now solely contains safe wrappers over Windows API functions, without any internal state (struct NativeEtw has been removed)
  that's the duty of trace.rs to handle these API correctly and in the right order.
* Traces instances are now created with a Builder pattern, to clearly mark which fields may be mutable and which will stay constant.
  This will make it very easy to fix races in issue n4r1b#45

Also, as minor improvements:
* the builder now enforces the trace name is truncated to TRACE_NAME_MAX_CHARS, so that both EVENT_TRACE_LOGFILEW and EVENT_TRACE_PROPERTIES have consistently truncated logger names
* TraceData is renamed CallbackData.
  That's mainly a matter of taste, even though it makes its intent clearer, and thus makes it easier to review the `unsafe` blocks
* errors from evntrace are now better forwarded to callers
* checks for invalid handles from the Windows API has been made more explicit
* the public API for traces (and trace builder) is now simplified, and hides some of the "really weird" (to say the least) design choices of ETW.
  Distinction between open/start/process is now clearer to the user
  Also, the `process` now exists in different flavours, that do not all hide the thread spawning. This offers more control to the end user.
* Traces can explictly be closed, and are closed if still open on Drop (maybe that was the case in ferrisetw 0.1, I'm not sure)
* This removes the distinction between TraceTrait and TraceBaseTrait

Sorry, I did not manage to split this large commit into smaller chunks.
It's probably easier to read only the result of it rather than the diffs, which do not make much sense since most of evntrace.rs and trace.rs are now diffs.
Wrapping it into an Arc ensures we're not dropping it when the trace is stopped, but we're waiting for the potential callbacks to terminate first

This fixes "Race 2" in n4r1b#45 (n4r1b#45)
Copy link
Owner

@n4r1b n4r1b left a comment

Choose a reason for hiding this comment

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

This is great! lgtm

@daladim daladim merged commit 050ca1a into n4r1b:next_major_version Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants