8207779: Method::is_valid_method() compares 'this' with NULL

Add Method* parameter and make method static to avoid 'thi's comparison with NULL

Reviewed-by: lfoltan, coleenp
diff --git a/src/hotspot/cpu/aarch64/frame_aarch64.cpp b/src/hotspot/cpu/aarch64/frame_aarch64.cpp
index 17093cf..8a5ffa8 100644
--- a/src/hotspot/cpu/aarch64/frame_aarch64.cpp
+++ b/src/hotspot/cpu/aarch64/frame_aarch64.cpp
@@ -539,7 +539,7 @@
   Method* m = *interpreter_frame_method_addr();
 
   // validate the method we'd find in this potential sender
-  if (!m->is_valid_method()) return false;
+  if (!Method::is_valid_method(m)) return false;
 
   // stack frames shouldn't be much larger than max_stack elements
   // this test requires the use of unextended_sp which is the sp as seen by
diff --git a/src/hotspot/cpu/arm/frame_arm.cpp b/src/hotspot/cpu/arm/frame_arm.cpp
index 98d5aed..7b3515e 100644
--- a/src/hotspot/cpu/arm/frame_arm.cpp
+++ b/src/hotspot/cpu/arm/frame_arm.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -500,7 +500,7 @@
   Method* m = *interpreter_frame_method_addr();
 
   // validate the method we'd find in this potential sender
-  if (!m->is_valid_method()) return false;
+  if (!Method::is_valid_method(m)) return false;
 
   // stack frames shouldn't be much larger than max_stack elements
 
diff --git a/src/hotspot/cpu/sparc/frame_sparc.cpp b/src/hotspot/cpu/sparc/frame_sparc.cpp
index 209c3c8..6f5ab67 100644
--- a/src/hotspot/cpu/sparc/frame_sparc.cpp
+++ b/src/hotspot/cpu/sparc/frame_sparc.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -648,7 +648,7 @@
   Method* m = *interpreter_frame_method_addr();
 
   // validate the method we'd find in this potential sender
-  if (!m->is_valid_method()) return false;
+  if (!Method::is_valid_method(m)) return false;
 
   // stack frames shouldn't be much larger than max_stack elements
 
diff --git a/src/hotspot/cpu/x86/frame_x86.cpp b/src/hotspot/cpu/x86/frame_x86.cpp
index a2b7ec4..34a1a73 100644
--- a/src/hotspot/cpu/x86/frame_x86.cpp
+++ b/src/hotspot/cpu/x86/frame_x86.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -526,7 +526,7 @@
   Method* m = *interpreter_frame_method_addr();
 
   // validate the method we'd find in this potential sender
-  if (!m->is_valid_method()) return false;
+  if (!Method::is_valid_method(m)) return false;
 
   // stack frames shouldn't be much larger than max_stack elements
   // this test requires the use the unextended_sp which is the sp as seen by
diff --git a/src/hotspot/os_cpu/linux_ppc/thread_linux_ppc.cpp b/src/hotspot/os_cpu/linux_ppc/thread_linux_ppc.cpp
index 05e5690..3f49fed 100644
--- a/src/hotspot/os_cpu/linux_ppc/thread_linux_ppc.cpp
+++ b/src/hotspot/os_cpu/linux_ppc/thread_linux_ppc.cpp
@@ -67,7 +67,7 @@
     if (ret_frame.is_interpreted_frame()) {
       frame::ijava_state *istate = ret_frame.get_ijava_state();
       const Method *m = (const Method*)(istate->method);
-      if (m == NULL || !m->is_valid_method()) return false;
+      if (!Method::is_valid_method(m)) return false;
       if (!Metaspace::contains((const void*)m->constMethod())) return false;
 
       uint64_t reg_bcp = uc->uc_mcontext.regs->gpr[14/*R14_bcp*/];
diff --git a/src/hotspot/os_cpu/linux_s390/thread_linux_s390.cpp b/src/hotspot/os_cpu/linux_s390/thread_linux_s390.cpp
index 32ba646..c6b183f 100644
--- a/src/hotspot/os_cpu/linux_s390/thread_linux_s390.cpp
+++ b/src/hotspot/os_cpu/linux_s390/thread_linux_s390.cpp
@@ -67,7 +67,7 @@
         return false;
       }
       const Method *m = (const Method*)(istate->method);
-      if (m == NULL || !m->is_valid_method()) return false;
+      if (!Method::is_valid_method(m)) return false;
       if (!Metaspace::contains((const void*)m->constMethod())) return false;
 
       uint64_t reg_bcp = uc->uc_mcontext.gregs[13/*Z_BCP*/];
diff --git a/src/hotspot/share/jfr/periodic/sampling/jfrCallTrace.cpp b/src/hotspot/share/jfr/periodic/sampling/jfrCallTrace.cpp
index 5d76bfb..0777b7e 100644
--- a/src/hotspot/share/jfr/periodic/sampling/jfrCallTrace.cpp
+++ b/src/hotspot/share/jfr/periodic/sampling/jfrCallTrace.cpp
@@ -51,7 +51,7 @@
       const bool known_valid = (state == _thread_in_native || state == _thread_in_vm || state == _thread_blocked);
       if (known_valid || candidate.is_interpreted_frame_valid(_thread)) {
         Method* im = candidate.interpreter_frame_method();
-        if (known_valid && !im->is_valid_method()) {
+        if (known_valid && !Method::is_valid_method(im)) {
           return false;
         }
         *method = im;
diff --git a/src/hotspot/share/jfr/recorder/stacktrace/jfrStackTrace.cpp b/src/hotspot/share/jfr/recorder/stacktrace/jfrStackTrace.cpp
index 8cb326c..630116b 100644
--- a/src/hotspot/share/jfr/recorder/stacktrace/jfrStackTrace.cpp
+++ b/src/hotspot/share/jfr/recorder/stacktrace/jfrStackTrace.cpp
@@ -188,7 +188,7 @@
       break;
     }
     const Method* method = st.method();
-    if (!method->is_valid_method()) {
+    if (!Method::is_valid_method(method)) {
       // we throw away everything we've gathered in this sample since
       // none of it is safe
       return false;
diff --git a/src/hotspot/share/oops/method.cpp b/src/hotspot/share/oops/method.cpp
index 9e842f8..576da96 100644
--- a/src/hotspot/share/oops/method.cpp
+++ b/src/hotspot/share/oops/method.cpp
@@ -1153,7 +1153,7 @@
 }
 
 void Method::restore_unshareable_info(TRAPS) {
-  assert(is_method() && is_valid_method(), "ensure C++ vtable is restored");
+  assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
 
   // Since restore_unshareable_info can be called more than once for a method, don't
   // redo any work.
@@ -2225,16 +2225,16 @@
 }
 
 // Check that this pointer is valid by checking that the vtbl pointer matches
-bool Method::is_valid_method() const {
-  if (this == NULL) {
+bool Method::is_valid_method(const Method* m) {
+  if (m == NULL) {
     return false;
-  } else if ((intptr_t(this) & (wordSize-1)) != 0) {
+  } else if ((intptr_t(m) & (wordSize-1)) != 0) {
     // Quick sanity check on pointer.
     return false;
-  } else if (is_shared()) {
-    return MetaspaceShared::is_valid_shared_method(this);
-  } else if (Metaspace::contains_non_shared(this)) {
-    return has_method_vptr((const void*)this);
+  } else if (m->is_shared()) {
+    return MetaspaceShared::is_valid_shared_method(m);
+  } else if (Metaspace::contains_non_shared(m)) {
+    return has_method_vptr((const void*)m);
   } else {
     return false;
   }
diff --git a/src/hotspot/share/oops/method.hpp b/src/hotspot/share/oops/method.hpp
index 1ddf965..da8468d 100644
--- a/src/hotspot/share/oops/method.hpp
+++ b/src/hotspot/share/oops/method.hpp
@@ -995,7 +995,7 @@
 
   // Check for valid method pointer
   static bool has_method_vptr(const void* ptr);
-  bool is_valid_method() const;
+  static bool is_valid_method(const Method* m);
 
   // Verify
   void verify() { verify_on(tty); }
diff --git a/src/hotspot/share/prims/forte.cpp b/src/hotspot/share/prims/forte.cpp
index 1ab813a..c80343e 100644
--- a/src/hotspot/share/prims/forte.cpp
+++ b/src/hotspot/share/prims/forte.cpp
@@ -248,7 +248,7 @@
     // a valid method. Then again we may have caught an interpreter
     // frame in the middle of construction and the bci field is
     // not yet valid.
-    if (!method->is_valid_method()) return false;
+    if (!Method::is_valid_method(method)) return false;
     *method_p = method; // If the Method* found is invalid, it is
                         // ignored by forte_fill_call_trace_given_top().
                         // So set method_p only if the Method is valid.
@@ -434,7 +434,7 @@
   // Check if a Java Method has been found.
   if (method == NULL) return;
 
-  if (!method->is_valid_method()) {
+  if (!Method::is_valid_method(method)) {
     trace->num_frames = ticks_GC_active; // -2
     return;
   }
@@ -445,7 +445,7 @@
     bci = st.bci();
     method = st.method();
 
-    if (!method->is_valid_method()) {
+    if (!Method::is_valid_method(method)) {
       // we throw away everything we've gathered in this sample since
       // none of it is safe
       trace->num_frames = ticks_GC_active; // -2
diff --git a/src/hotspot/share/runtime/os.cpp b/src/hotspot/share/runtime/os.cpp
index 0845e2a..02f3143 100644
--- a/src/hotspot/share/runtime/os.cpp
+++ b/src/hotspot/share/runtime/os.cpp
@@ -1142,7 +1142,7 @@
     if (Klass::is_valid((Klass*)addr)) {
       st->print_cr(INTPTR_FORMAT " is a pointer to class: ", p2i(addr));
       ((Klass*)addr)->print_on(st);
-    } else if (((const Method*)addr)->is_valid_method()) {
+    } else if (Method::is_valid_method((const Method*)addr)) {
       ((Method*)addr)->print_value_on(st);
       st->cr();
     } else {