/* * Copyright (c) 2006, 2019, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "precompiled.hpp" #include "runtime/os.hpp" #include "runtime/vm_version.hpp" #include #include #pragma comment(lib, "wbemuuid.lib") #define VM_MODEL L"Virtual Machine" bool VM_Version::is_in_VM() { HRESULT hres; hres = CoInitializeEx(0, COINIT_MULTITHREADED); if (hres != S_OK) { return false; } hres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); if (hres != S_OK) { CoUninitialize(); return false; } IWbemLocator* pLoc = NULL; hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc); if (hres != S_OK) { CoUninitialize(); return false; } IWbemServices* pSvc = NULL; BSTR resource = SysAllocString(L"ROOT\\CIMV2"); hres = pLoc->ConnectServer(resource, NULL, NULL, 0, NULL, 0, 0, &pSvc); SysFreeString(resource); if (hres != S_OK) { pLoc->Release(); CoUninitialize(); return false; } hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); if (hres != S_OK) { pSvc->Release(); pLoc->Release(); CoUninitialize(); return false; } IEnumWbemClassObject* pEnumerator = NULL; BSTR lang = SysAllocString(L"WQL"); BSTR query = SysAllocString(L"SELECT Model FROM Win32_ComputerSystem"); hres = pSvc->ExecQuery(lang, query, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); SysFreeString(lang); SysFreeString(query); if (hres != S_OK) { pSvc->Release(); pLoc->Release(); CoUninitialize(); return false; } bool result = false; IWbemClassObject* pclsObj = NULL; ULONG uReturn = 0; hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if (uReturn != 0) { VARIANT vtProp; hres = pclsObj->Get(L"Model", 0, &vtProp, 0, 0); if (hres == S_OK) { if (wcscmp(VM_MODEL, vtProp.bstrVal) == 0) { result = true; } VariantClear(&vtProp); } pclsObj->Release(); } pSvc->Release(); pLoc->Release(); pEnumerator->Release(); CoUninitialize(); return result; }