For a negative `start_depth`, `JvmtiEnvBase::get_stack_trace(javaVFrame*, ...)` first walks to the end of the stack, then walks again from a `javaVFrame` it passed on the way. All `javaVFrame`s of one walk share the `RegisterMap`'s stack chunk handle, and the first walk clears it when it reaches the end of a continuation. If the frame it walks again from is in a stack chunk, the second walk computes its sender as if it were on the thread stack, and the VM crashes with a SIGSEGV in `frame::sender_for_interpreter_frame()`.
### Changes - `get_stack_trace(JavaThread*, ...)` and `GetStackTraceClosure::do_vthread()` convert a negative `start_depth` into the equivalent positive one before they walk the stack. A new helper, `start_depth_from_top()`, counts the frames with `get_frame_count()`, which makes a separate walk. The stack trace is then taken by a fresh walk. - `get_stack_trace(javaVFrame*, ...)` no longer handles a negative `start_depth`. It asserts `start_depth >= 0`. The results do not change. In particular, a `-start_depth` greater than the stack depth still returns `JVMTI_ERROR_ILLEGAL_ARGUMENT`. The negative-depth path now walks the stack twice from the top. Before, it walked the stack once and then part of it again. ### Relation to JDK-8398335 [JDK-8398335](https://bugs.openjdk.org/browse/JDK-8398335) (PR #34089) makes `get_stack_trace(JavaThread*, ...)` walk with `WalkContinuation::include`, so platform threads also walk into stack chunks. Without this fix, that change exposes the crash for a platform thread that runs a `Continuation` that resumed after a yield. This PR must integrate first. ### Testing New test `serviceability/jvmti/thread/GetStackTrace/GetStackTraceNegativeDepthTest`: - A virtual thread recurses 200 frames deep. It is checked while it is unmounted (parked), and then from the thread itself after `Thread.yield()`, when its frames below the top are still in the stack chunk. - For start depths `-1`, `-3`, `-n/2`, `-(n-1)` and `-n`, with `max_count` 1024 and 2, `GetStackTrace` must return the bottom frames of the full trace. `-(n+1)` must return `JVMTI_ERROR_ILLEGAL_ARGUMENT`. Results on linux-x64 fastdebug: | Build | Test | Result | | --- | --- | --- | | without the fix | `GetStackTraceNegativeDepthTest` | fails: `assert(_frame_index == -1) failed: Using absolute addresses with a chunk frame` | | with the fix | `GetStackTraceNegativeDepthTest` | passes with default flags, `-Xint`, `-Xcomp`, `-XX:TieredStopAtLevel=1`, `-XX:+UseZGC` and `-XX:+UseShenandoahGC` | | with the fix | `serviceability/jvmti`, `vmTestbase/nsk/jvmti/{unit,PopFrame,NotifyFramePop,scenarios/sampling}`, `jdk/com/sun/jdi`, `vmTestbase/nsk/jdi/ThreadReference` | 601 passed, 0 failed, 11 skipped | | with the fix and JDK-8398335 | a platform thread in a resumed `Continuation`, start depths `0`, `-3` and `-50` | correct: 208, 3 and 50 frames. JDK-8398335 alone fails the same assert. | --------- - [x] I confirm that I make this contribution in accordance with the [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai). ------------- Commit messages: - 8398467: JVMTI GetStackTrace with a negative start_depth crashes on an unmounted virtual thread Changes: https://git.openjdk.org/jdk/pull/34102/files Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=34102&range=00 Issue: https://bugs.openjdk.org/browse/JDK-8398467 Stats: 284 lines in 3 files changed: 240 ins; 33 del; 11 mod Patch: https://git.openjdk.org/jdk/pull/34102.diff Fetch: git fetch https://git.openjdk.org/jdk.git pull/34102/head:pull/34102 PR: https://git.openjdk.org/jdk/pull/34102
