JDK-8252476 : as_Worker_thread() doesn't check what it intends
  • Type: Bug
  • Component: hotspot
  • Sub-Component: gc
  • Affected Version: 16
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-08-27
  • Updated: 2021-06-07
  • Resolved: 2021-05-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 17
17 b25Fixed
Related Reports
Relates :  
Relates :  
Description
In the Thread class we have:

 virtual WorkerThread* as_Worker_thread() const     { return NULL; }

and then in WorkerThread we have:

  virtual WorkerThread* as_Worker_thread() const {
    assert(is_Worker_thread(), "Dubious cast to WorkerThread*?");
    return (WorkerThread*) this;
  }

The intent is clear, but the use of virtual functions means this doesn't work as expected. The assertion can never fire because this is the implementation inside the WorkerThread class so you only call this method when you are guaranteed to have a WorkerThread instance. If you happen to call as_Worker_thread on a non-WorkerThread you will just get NULL and no assertion.

If you wanted to use virtual functions then the correct way to implement this would be:

In Thread class:

 virtual WorkerThread* as_Worker_thread() const     { 
    assert(false, "incorrect cast to WorkerThread");
    return NULL; 
 }

and in WorkerThread class simply:

 virtual WorkerThread* as_Worker_thread() const     { return this; }

Or without virtual functions you use the same pattern as as_Compiler_thread (relative to JavaThread) and simply have the following in the Thread class:

  WorkerThread* as_Worker_thread() const {
    assert(is_Worker_thread(), "Dubious cast to WorkerThread*?");
    return (WorkerThread*) this;
  }

Comments
Changeset: bfa46f0a Author: Albert Mingkun Yang <ayang@openjdk.org> Date: 2021-05-27 08:09:58 +0000 URL: https://git.openjdk.java.net/jdk/commit/bfa46f0af30e9ec073ab5e6411ca94e140113790
27-05-2021

Code was introduced by JDK-6983204.
27-08-2020