[#117392] [Ruby master Feature#20405] Inline comments — "nobu (Nobuyoshi Nakada) via ruby-core" <ruby-core@...>

Issue #20405 has been reported by nobu (Nobuyoshi Nakada).

11 messages 2024/04/01

[#117434] [Ruby master Bug#20409] Missing reporting some invalid breaks — "kddnewton (Kevin Newton) via ruby-core" <ruby-core@...>

Issue #20409 has been reported by kddnewton (Kevin Newton).

8 messages 2024/04/03

[#117458] [Ruby master Bug#20414] `Fiber#raise` should recurse to `resumed_fiber` rather than failing. — "ioquatix (Samuel Williams) via ruby-core" <ruby-core@...>

Issue #20414 has been reported by ioquatix (Samuel Williams).

10 messages 2024/04/07

[#117469] [Ruby master Feature#20415] Precompute literal String hash code during compilation — "byroot (Jean Boussier) via ruby-core" <ruby-core@...>

Issue #20415 has been reported by byroot (Jean Boussier).

10 messages 2024/04/09

[#117494] [Ruby master Bug#20421] String#index and String#byteindex don't clear `$~` when offset > size (or bytesize) — "andrykonchin (Andrew Konchin) via ruby-core" <ruby-core@...>

Issue #20421 has been reported by andrykonchin (Andrew Konchin).

7 messages 2024/04/11

[#117498] [Ruby master Feature#20425] Optimize forwarding callers and callees — "tenderlovemaking (Aaron Patterson) via ruby-core" <ruby-core@...>

Issue #20425 has been reported by tenderlovemaking (Aaron Patterson).

14 messages 2024/04/11

[#117531] [Ruby master Bug#20431] Ruby 3.3.0 build fail with make: *** [io_buffer.o] Error 1 — "shubham_yadav (Shubham Yadav) via ruby-core" <ruby-core@...>

SXNzdWUgIzIwNDMxIGhhcyBiZWVuIHJlcG9ydGVkIGJ5IHNodWJoYW1feWFkYXYgKFNodWJoYW0g

11 messages 2024/04/16

[#117564] [Ruby master Bug#20433] Hash.inspect for some hash returns syntax invalid representation — "tompng (tomoya ishida) via ruby-core" <ruby-core@...>

Issue #20433 has been reported by tompng (tomoya ishida).

15 messages 2024/04/17

[#117572] [Ruby master Misc#20435] DevMeeting-2024-06-13 — "mame (Yusuke Endoh) via ruby-core" <ruby-core@...>

Issue #20435 has been reported by mame (Yusuke Endoh).

12 messages 2024/04/17

[#117588] [Ruby master Misc#20436] DevMeeting at RubyKaigi 2024 — "ko1 (Koichi Sasada) via ruby-core" <ruby-core@...>

SXNzdWUgIzIwNDM2IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGtvMSAoS29pY2hpIFNhc2FkYSkuDQoN

14 messages 2024/04/18

[#117624] [Ruby master Bug#20440] `super` from child class passing keyword arg as Hash if in a method with passthrough args called from base class — "ozydingo (Andrew Schwartz) via ruby-core" <ruby-core@...>

Issue #20440 has been reported by ozydingo (Andrew Schwartz).

7 messages 2024/04/20

[#117644] [Ruby master Feature#20443] Allow Major GC's to be disabled — "eightbitraptor (Matthew Valentine-House) via ruby-core" <ruby-core@...>

Issue #20443 has been reported by eightbitraptor (Matthew Valentine-House).

25 messages 2024/04/22

[#117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly — "esad (Esad Hajdarevic) via ruby-core" <ruby-core@...>

Issue #20444 has been reported by esad (Esad Hajdarevic).

9 messages 2024/04/22

[#117653] [Ruby master Bug#20446] OUtdated https://cache.ruby-lang.org/pub/ruby/index.txt — "vo.x (Vit Ondruch) via ruby-core" <ruby-core@...>

Issue #20446 has been reported by vo.x (Vit Ondruch).

7 messages 2024/04/23

[#117657] [Ruby master Bug#20447] Ruby 3.3.1 broken on i686 — "vo.x (Vit Ondruch) via ruby-core" <ruby-core@...>

SXNzdWUgIzIwNDQ3IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IHZvLnggKFZpdCBPbmRydWNoKS4NCg0K

15 messages 2024/04/23

[#117658] [Ruby master Feature#20448] Make coverage event hooking C API public — "ms-tob (Matt S) via ruby-core" <ruby-core@...>

Issue #20448 has been reported by ms-tob (Matt S).

9 messages 2024/04/23

[#117674] [Ruby master Bug#20450] Ruby 3.1.1 broken with bootsnap — "philippe.bs.noel@... (Philippe Noel) via ruby-core" <ruby-core@...>

Issue #20450 has been reported by [email protected] (Philippe Noel).

11 messages 2024/04/24

[#117684] [Ruby master Bug#20452] Ruby 3.3 on Alpine Linux results in a relatively shallow SystemStackError exception — "Earlopain (A S) via ruby-core" <ruby-core@...>

Issue #20452 has been reported by Earlopain (A S).

12 messages 2024/04/24

[#117711] [Ruby master Bug#20456] Hash can get stuck marked as iterating through process forking — "blowfishpro (Talia Wong) via ruby-core" <ruby-core@...>

Issue #20456 has been reported by blowfishpro (Talia Wong).

7 messages 2024/04/25

[ruby-core:117683] [Ruby master Feature#20448] Make coverage event hooking C API public

From: "ms-tob (Matt S) via ruby-core" <ruby-core@...>
Date: 2024-04-24 12:07:24 UTC
List: ruby-core #117683
Issue #20448 has been updated by ms-tob (Matt S).


> I understand that what you need is a hook for LINE and BRANCH events. Am I correct?

I just need a hook for BRANCH events. You can find that C extension code here:

https://github.com/trailofbits/ruzzy/blob/v0.7.0/ext/cruzzy/cruzzy.c#L220-L231

And I manually define `RUBY_EVENT_COVERAGE_BRANCH` above with `#define RUBY_EVENT_COVERAGE_BRANCH 0x020000`.

> RUBY_EVENT_COVERAGE_BRANCH is more complicated because it strongly depends on compiler implementation details. This feature requires an instrumented bytecode to be output at compile time, so it must be enabled before compilation of target Ruby code. Also, currently, column information is not left in the byte code. The current branch coverage measurement is achieved by separately bookkeeping the column information at compile time. I don't know if these can be cut out as a clean API.

I was able to figure out a workaround for this. By reviewing the [`Coverage`](https://ruby-doc.org/3.3.0/exts/coverage/Coverage.html#module-Coverage-label-Usage) module documentation I learned that you have to start coverage gathering, and then `require` a separate Ruby module. My C extension takes the following actions to perform this:

- Enable coverage gathering: https://github.com/trailofbits/ruzzy/blob/v0.7.0/ext/cruzzy/cruzzy.c#L195-L211
- Then, `require` the caller-provided module name: https://github.com/trailofbits/ruzzy/blob/v0.7.0/ext/cruzzy/cruzzy.c#L233

Due to this behavior, when fuzzing pure Ruby code (not C extensions), Ruzzy requires two separate scripts. A "tracer" script and a fuzzing harness: https://github.com/trailofbits/ruzzy#fuzzing-pure-ruby-code.

Having this behavior built into the language is extremely helpful. Without it, as you mentioned, we would have to manually instrument the bytecode at runtime to achieve coverage-guided fuzzing. This is what Atheris has to do to fuzz Python code, and it requires significantly more investment into the solution. So, I really appreciate that Ruby has this built in, even if its private, internal-only functionality.

----------------------------------------
Feature #20448: Make coverage event hooking C API public
https://bugs.ruby-lang.org/issues/20448#change-108093

* Author: ms-tob (Matt S)
* Status: Open
----------------------------------------
# Abstract

Gathering code coverage information is a well-known goal within software engineering. It is most commonly used to assess code coverage during automated testing. A lesser known use-case is coverage-guided fuzz testing, which will be the primary use-case presented in this issue. This issue exists to request that Ruby coverage event hooking be made part of its official, public C API.

# Background

Ruby currently provides a number of avenues for hooking events *or* gathering coverage information:

1. The [Coverage](https://ruby-doc.org/3.3.0/exts/coverage/Coverage.html) module
2. The [TracePoint](https://ruby-doc.org/3.3.0/TracePoint.html) module
3. The [rb_add_event_hook](https://ruby-doc.org/3.3.0/extension_rdoc.html#label-Hooks+for+the+interpreter+events) extension function

Unfortunately, none of these pieces of functionality solve this issue's specific use-case. The `Coverage` module is not a great fit for real-time coverage analysis with an unknown start and stop point. Coverage-guided fuzz testing requires this. The `TracePoint` module and `rb_add_event_hook` are not able to hook branch and line coverage events. Coverage-guided fuzz testing typically tracks branch events.

# Proposal

The ultimate goal is to enable Ruby C extensions to process coverage events in real-time. I did some cursory investigation into the Ruby C internals to determine what it would take to achieve this, but I'm by no means an expert, so my list may be incomplete.

The good news is that much of this functionality already exists, but it's part of the private, internal-only C API.

1. Make `RUBY_EVENT_COVERAGE_LINE` and `RUBY_EVENT_COVERAGE_BRANCH` public: https://github.com/ruby/ruby/blob/v3_3_0/vm_core.h#L2182-L2184
  a. This would be an addition to the current public event types: https://github.com/ruby/ruby/blob/v3_3_0/include/ruby/internal/event.h#L32-L46
2. Allow initializing global coverage state so that coverage tracking can be fully enabled
  a. Currently, if `Coverage.setup` or `Coverage.start` is not called, then coverage events cannot be hooked. I do not fully understand why this is, but I believe it has something to do with `rb_get_coverages` and `rb_set_coverages`. If calls to `rb_get_coverages` return `NULL` (https://github.com/ruby/ruby/blob/v3_3_0/iseq.c#L641-L647, https://github.com/ruby/ruby/blob/v3_3_0/iseq.c#L864-L868), then coverage hooking will not be enabled. I believe the `Coverage` module initializes that state via a `rb_set_coverages` call here: https://github.com/ruby/ruby/blob/v3_3_0/ext/coverage/coverage.c#L112-L120.
  b. So, to achieve this goal, a C extension would need to be able to call `rb_set_coverages` or somehow initialize the global coverage state.

I've actually been able to achieve this functionality by calling undocumented features and defining `RUBY_EVENT_COVERAGE_BRANCH`:

```c
#include <ruby.h>
#include <ruby/debug.h>

#define RUBY_EVENT_COVERAGE_BRANCH 0x020000

// ...

rb_event_flag_t events = RUBY_EVENT_COVERAGE_BRANCH;
rb_event_hook_flag_t flags = (
    RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG
);
rb_add_event_hook2(
    (rb_event_hook_func_t) event_hook_branch,
    events,
    counter_hash,
    flags
);
```

If I call `Coverage.setup(branches: true)`, and add this event hook, then branch hooking works as expected. `rb_add_event_hook2` will still respect the `RUBY_EVENT_COVERAGE_BRANCH` value if its passed. But it would be better if I could rely on official functionality rather than undocumented features.

The above two points would be requirements for this functionality, but there's an additional nice-to-have:

3. Extend the public `tracearg` functionality to include additional coverage information
  a. Currently, `tracearg` offers information like `rb_tracearg_lineno` and `rb_tracearg_path`. It would be helpful if it also provided additional coverage information like `coverage.c`'s column information and a unique identifier for each branch. Currently, I can only use `(path, lineno)` as a unique identifier for a branch because that's what's offered by the public API, but more information like column number would be helpful for uniquely identify branches. Since there can be multiple `if` statements on a single line, this can provide ambiguous identification for a branch event.

# Use cases

This use-case was born out of a new coverage-guided Ruby fuzzer: https://github.com/trailofbits/ruzzy. You can read more about its implementation details here: https://blog.trailofbits.com/2024/03/29/introducing-ruzzy-a-coverage-guided-ruby-fuzzer/. You can also find the Ruby C extension code behind its implementation here: https://github.com/trailofbits/ruzzy/blob/v0.7.0/ext/cruzzy/cruzzy.c#L220-L231.

So, the primary use-case here is enabling real-time, coverage-guided fuzz testing of Ruby code. However, as mentioned in the abstract, gathering code coverage information is useful in many domains. For example, it could enable new workflows in standard unit/integration test coverage. It could also enable gathering coverage information in real-time as an application is running. I see this as the most generalized form of gathering code coverage information, and something like the `Coverage` module as a specialized implementation. Another example, https://bugs.ruby-lang.org/issues/20282 may be solved by this more generalized solution.

We are tracking this request downstream here: https://github.com/trailofbits/ruzzy/issues/9

# Discussion

Fuzz testing is another tool in a testers toolbelt. It is an increasingly common way to improve software's robustness. Go has it built in to the standard library, Python has Atheris, Java has Jazzer, JavaScript has Jazzer.js, etc. OSS-Fuzz has helped identify and fix over 10,000 vulnerabilities and 36,000 bugs [using fuzzing](https://google.github.io/oss-fuzz/#trophies). Ruby deserves a good fuzzer, and improving coverage gathering would help achieve that goal.

The `Coverage` module, `TracePoint` module, and `rb_add_event_hook` function seem like they could fulfill this goal. However, after deeper investigation, none of them fit the exact requirements for this use-case.

# See also

- https://bugs.ruby-lang.org/issues/20282
- https://github.com/google/atheris
  - https://security.googleblog.com/2020/12/how-atheris-python-fuzzer-works.html
- https://github.com/CodeIntelligenceTesting/jazzer/
  - https://www.code-intelligence.com/blog/java-fuzzing-with-jazzer
- https://go.dev/doc/security/fuzz/



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- [email protected]
 To unsubscribe send an email to [email protected]
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

In This Thread