comparison src/os/windows/vm/os_windows.cpp @ 3429:5def270bc147

7016797: Hotspot: securely/restrictive load dlls and new API for loading system dlls Summary: Created Windows Dll wrapped to handle jdk6 and jdk7 platform requirements, also provided more restictive Dll search orders for Windows system Dlls. Reviewed-by: acorn, dcubed, ohair, alanb
author zgu
date Fri, 15 Apr 2011 09:34:43 -0400
parents 083f13976b51
children 089aee76df10
comparison
equal deleted inserted replaced
3428:c97b08c7d72a 3429:5def270bc147
96 #include <io.h> 96 #include <io.h>
97 #include <process.h> // For _beginthreadex(), _endthreadex() 97 #include <process.h> // For _beginthreadex(), _endthreadex()
98 #include <imagehlp.h> // For os::dll_address_to_function_name 98 #include <imagehlp.h> // For os::dll_address_to_function_name
99 99
100 /* for enumerating dll libraries */ 100 /* for enumerating dll libraries */
101 #include <tlhelp32.h>
102 #include <vdmdbg.h> 101 #include <vdmdbg.h>
103 102
104 // for timer info max values which include all bits 103 // for timer info max values which include all bits
105 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 104 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
106 105
239 #define PACKAGE_DIR "\\Sun\\Java" 238 #define PACKAGE_DIR "\\Sun\\Java"
240 { 239 {
241 /* Win32 library search order (See the documentation for LoadLibrary): 240 /* Win32 library search order (See the documentation for LoadLibrary):
242 * 241 *
243 * 1. The directory from which application is loaded. 242 * 1. The directory from which application is loaded.
244 * 2. The current directory 243 * 2. The system wide Java Extensions directory (Java only)
245 * 3. The system wide Java Extensions directory (Java only) 244 * 3. System directory (GetSystemDirectory)
246 * 4. System directory (GetSystemDirectory) 245 * 4. Windows directory (GetWindowsDirectory)
247 * 5. Windows directory (GetWindowsDirectory) 246 * 5. The PATH environment variable
248 * 6. The PATH environment variable 247 * 6. The current directory
249 */ 248 */
250 249
251 char *library_path; 250 char *library_path;
252 char tmp[MAX_PATH]; 251 char tmp[MAX_PATH];
253 char *path_str = ::getenv("PATH"); 252 char *path_str = ::getenv("PATH");
258 library_path[0] = '\0'; 257 library_path[0] = '\0';
259 258
260 GetModuleFileName(NULL, tmp, sizeof(tmp)); 259 GetModuleFileName(NULL, tmp, sizeof(tmp));
261 *(strrchr(tmp, '\\')) = '\0'; 260 *(strrchr(tmp, '\\')) = '\0';
262 strcat(library_path, tmp); 261 strcat(library_path, tmp);
263
264 strcat(library_path, ";.");
265 262
266 GetWindowsDirectory(tmp, sizeof(tmp)); 263 GetWindowsDirectory(tmp, sizeof(tmp));
267 strcat(library_path, ";"); 264 strcat(library_path, ";");
268 strcat(library_path, tmp); 265 strcat(library_path, tmp);
269 strcat(library_path, PACKAGE_DIR BIN_DIR); 266 strcat(library_path, PACKAGE_DIR BIN_DIR);
278 275
279 if (path_str) { 276 if (path_str) {
280 strcat(library_path, ";"); 277 strcat(library_path, ";");
281 strcat(library_path, path_str); 278 strcat(library_path, path_str);
282 } 279 }
280
281 strcat(library_path, ";.");
283 282
284 Arguments::set_library_path(library_path); 283 Arguments::set_library_path(library_path);
285 FREE_C_HEAP_ARRAY(char, library_path); 284 FREE_C_HEAP_ARRAY(char, library_path);
286 } 285 }
287 286
935 } else if (os::win32::is_windows_server() && !FLAG_IS_DEFAULT(CreateMinidumpOnCrash) && !CreateMinidumpOnCrash) { 934 } else if (os::win32::is_windows_server() && !FLAG_IS_DEFAULT(CreateMinidumpOnCrash) && !CreateMinidumpOnCrash) {
936 VMError::report_coredump_status("Minidump has been disabled from the command line", false); 935 VMError::report_coredump_status("Minidump has been disabled from the command line", false);
937 return; 936 return;
938 } 937 }
939 938
940 dbghelp = LoadLibrary("DBGHELP.DLL"); 939 dbghelp = os::win32::load_Windows_dll("DBGHELP.DLL", NULL, 0);
941 940
942 if (dbghelp == NULL) { 941 if (dbghelp == NULL) {
943 VMError::report_coredump_status("Failed to load dbghelp.dll", false); 942 VMError::report_coredump_status("Failed to load dbghelp.dll", false);
944 return; 943 return;
945 } 944 }
1195 return _getcwd(buf, buflen); 1194 return _getcwd(buf, buflen);
1196 } 1195 }
1197 1196
1198 //----------------------------------------------------------- 1197 //-----------------------------------------------------------
1199 // Helper functions for fatal error handler 1198 // Helper functions for fatal error handler
1200
1201 // The following library functions are resolved dynamically at runtime:
1202
1203 // PSAPI functions, for Windows NT, 2000, XP
1204
1205 // psapi.h doesn't come with Visual Studio 6; it can be downloaded as Platform
1206 // SDK from Microsoft. Here are the definitions copied from psapi.h
1207 typedef struct _MODULEINFO {
1208 LPVOID lpBaseOfDll;
1209 DWORD SizeOfImage;
1210 LPVOID EntryPoint;
1211 } MODULEINFO, *LPMODULEINFO;
1212
1213 static BOOL (WINAPI *_EnumProcessModules) ( HANDLE, HMODULE *, DWORD, LPDWORD );
1214 static DWORD (WINAPI *_GetModuleFileNameEx) ( HANDLE, HMODULE, LPTSTR, DWORD );
1215 static BOOL (WINAPI *_GetModuleInformation)( HANDLE, HMODULE, LPMODULEINFO, DWORD );
1216
1217 // ToolHelp Functions, for Windows 95, 98 and ME
1218
1219 static HANDLE(WINAPI *_CreateToolhelp32Snapshot)(DWORD,DWORD) ;
1220 static BOOL (WINAPI *_Module32First) (HANDLE,LPMODULEENTRY32) ;
1221 static BOOL (WINAPI *_Module32Next) (HANDLE,LPMODULEENTRY32) ;
1222
1223 bool _has_psapi;
1224 bool _psapi_init = false;
1225 bool _has_toolhelp;
1226
1227 static bool _init_psapi() {
1228 HINSTANCE psapi = LoadLibrary( "PSAPI.DLL" ) ;
1229 if( psapi == NULL ) return false ;
1230
1231 _EnumProcessModules = CAST_TO_FN_PTR(
1232 BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD),
1233 GetProcAddress(psapi, "EnumProcessModules")) ;
1234 _GetModuleFileNameEx = CAST_TO_FN_PTR(
1235 DWORD (WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD),
1236 GetProcAddress(psapi, "GetModuleFileNameExA"));
1237 _GetModuleInformation = CAST_TO_FN_PTR(
1238 BOOL (WINAPI *)(HANDLE, HMODULE, LPMODULEINFO, DWORD),
1239 GetProcAddress(psapi, "GetModuleInformation"));
1240
1241 _has_psapi = (_EnumProcessModules && _GetModuleFileNameEx && _GetModuleInformation);
1242 _psapi_init = true;
1243 return _has_psapi;
1244 }
1245
1246 static bool _init_toolhelp() {
1247 HINSTANCE kernel32 = LoadLibrary("Kernel32.DLL") ;
1248 if (kernel32 == NULL) return false ;
1249
1250 _CreateToolhelp32Snapshot = CAST_TO_FN_PTR(
1251 HANDLE(WINAPI *)(DWORD,DWORD),
1252 GetProcAddress(kernel32, "CreateToolhelp32Snapshot"));
1253 _Module32First = CAST_TO_FN_PTR(
1254 BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32),
1255 GetProcAddress(kernel32, "Module32First" ));
1256 _Module32Next = CAST_TO_FN_PTR(
1257 BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32),
1258 GetProcAddress(kernel32, "Module32Next" ));
1259
1260 _has_toolhelp = (_CreateToolhelp32Snapshot && _Module32First && _Module32Next);
1261 return _has_toolhelp;
1262 }
1263
1264 #ifdef _WIN64 1199 #ifdef _WIN64
1265 // Helper routine which returns true if address in 1200 // Helper routine which returns true if address in
1266 // within the NTDLL address space. 1201 // within the NTDLL address space.
1267 // 1202 //
1268 static bool _addr_in_ntdll( address addr ) 1203 static bool _addr_in_ntdll( address addr )
1270 HMODULE hmod; 1205 HMODULE hmod;
1271 MODULEINFO minfo; 1206 MODULEINFO minfo;
1272 1207
1273 hmod = GetModuleHandle("NTDLL.DLL"); 1208 hmod = GetModuleHandle("NTDLL.DLL");
1274 if ( hmod == NULL ) return false; 1209 if ( hmod == NULL ) return false;
1275 if ( !_GetModuleInformation( GetCurrentProcess(), hmod, 1210 if ( !os::PSApiDll::GetModuleInformation( GetCurrentProcess(), hmod,
1276 &minfo, sizeof(MODULEINFO)) ) 1211 &minfo, sizeof(MODULEINFO)) )
1277 return false; 1212 return false;
1278 1213
1279 if ( (addr >= minfo.lpBaseOfDll) && 1214 if ( (addr >= minfo.lpBaseOfDll) &&
1280 (addr < (address)((uintptr_t)minfo.lpBaseOfDll + (uintptr_t)minfo.SizeOfImage))) 1215 (addr < (address)((uintptr_t)minfo.lpBaseOfDll + (uintptr_t)minfo.SizeOfImage)))
1309 # define MAX_NUM_MODULES 128 1244 # define MAX_NUM_MODULES 128
1310 HMODULE modules[MAX_NUM_MODULES]; 1245 HMODULE modules[MAX_NUM_MODULES];
1311 static char filename[ MAX_PATH ]; 1246 static char filename[ MAX_PATH ];
1312 int result = 0; 1247 int result = 0;
1313 1248
1314 if (!_has_psapi && (_psapi_init || !_init_psapi())) return 0; 1249 if (!os::PSApiDll::PSApiAvailable()) {
1250 return 0;
1251 }
1315 1252
1316 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 1253 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
1317 FALSE, pid ) ; 1254 FALSE, pid ) ;
1318 if (hProcess == NULL) return 0; 1255 if (hProcess == NULL) return 0;
1319 1256
1320 DWORD size_needed; 1257 DWORD size_needed;
1321 if (!_EnumProcessModules(hProcess, modules, 1258 if (!os::PSApiDll::EnumProcessModules(hProcess, modules,
1322 sizeof(modules), &size_needed)) { 1259 sizeof(modules), &size_needed)) {
1323 CloseHandle( hProcess ); 1260 CloseHandle( hProcess );
1324 return 0; 1261 return 0;
1325 } 1262 }
1326 1263
1327 // number of modules that are currently loaded 1264 // number of modules that are currently loaded
1328 int num_modules = size_needed / sizeof(HMODULE); 1265 int num_modules = size_needed / sizeof(HMODULE);
1329 1266
1330 for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) { 1267 for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) {
1331 // Get Full pathname: 1268 // Get Full pathname:
1332 if(!_GetModuleFileNameEx(hProcess, modules[i], 1269 if(!os::PSApiDll::GetModuleFileNameEx(hProcess, modules[i],
1333 filename, sizeof(filename))) { 1270 filename, sizeof(filename))) {
1334 filename[0] = '\0'; 1271 filename[0] = '\0';
1335 } 1272 }
1336 1273
1337 MODULEINFO modinfo; 1274 MODULEINFO modinfo;
1338 if (!_GetModuleInformation(hProcess, modules[i], 1275 if (!os::PSApiDll::GetModuleInformation(hProcess, modules[i],
1339 &modinfo, sizeof(modinfo))) { 1276 &modinfo, sizeof(modinfo))) {
1340 modinfo.lpBaseOfDll = NULL; 1277 modinfo.lpBaseOfDll = NULL;
1341 modinfo.SizeOfImage = 0; 1278 modinfo.SizeOfImage = 0;
1342 } 1279 }
1343 1280
1357 { 1294 {
1358 HANDLE hSnapShot ; 1295 HANDLE hSnapShot ;
1359 static MODULEENTRY32 modentry ; 1296 static MODULEENTRY32 modentry ;
1360 int result = 0; 1297 int result = 0;
1361 1298
1362 if (!_has_toolhelp) return 0; 1299 if (!os::Kernel32Dll::HelpToolsAvailable()) {
1300 return 0;
1301 }
1363 1302
1364 // Get a handle to a Toolhelp snapshot of the system 1303 // Get a handle to a Toolhelp snapshot of the system
1365 hSnapShot = _CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid ) ; 1304 hSnapShot = os::Kernel32Dll::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid ) ;
1366 if( hSnapShot == INVALID_HANDLE_VALUE ) { 1305 if( hSnapShot == INVALID_HANDLE_VALUE ) {
1367 return FALSE ; 1306 return FALSE ;
1368 } 1307 }
1369 1308
1370 // iterate through all modules 1309 // iterate through all modules
1371 modentry.dwSize = sizeof(MODULEENTRY32) ; 1310 modentry.dwSize = sizeof(MODULEENTRY32) ;
1372 bool not_done = _Module32First( hSnapShot, &modentry ) != 0; 1311 bool not_done = os::Kernel32Dll::Module32First( hSnapShot, &modentry ) != 0;
1373 1312
1374 while( not_done ) { 1313 while( not_done ) {
1375 // invoke the callback 1314 // invoke the callback
1376 result=func(pid, modentry.szExePath, (address)modentry.modBaseAddr, 1315 result=func(pid, modentry.szExePath, (address)modentry.modBaseAddr,
1377 modentry.modBaseSize, param); 1316 modentry.modBaseSize, param);
1378 if (result) break; 1317 if (result) break;
1379 1318
1380 modentry.dwSize = sizeof(MODULEENTRY32) ; 1319 modentry.dwSize = sizeof(MODULEENTRY32) ;
1381 not_done = _Module32Next( hSnapShot, &modentry ) != 0; 1320 not_done = os::Kernel32Dll::Module32Next( hSnapShot, &modentry ) != 0;
1382 } 1321 }
1383 1322
1384 CloseHandle(hSnapShot); 1323 CloseHandle(hSnapShot);
1385 return result; 1324 return result;
1386 } 1325 }
1622 int pid = os::current_process_id(); 1561 int pid = os::current_process_id();
1623 st->print_cr("Dynamic libraries:"); 1562 st->print_cr("Dynamic libraries:");
1624 enumerate_modules(pid, _print_module, (void *)st); 1563 enumerate_modules(pid, _print_module, (void *)st);
1625 } 1564 }
1626 1565
1627 // function pointer to Windows API "GetNativeSystemInfo".
1628 typedef void (WINAPI *GetNativeSystemInfo_func_type)(LPSYSTEM_INFO);
1629 static GetNativeSystemInfo_func_type _GetNativeSystemInfo;
1630
1631 void os::print_os_info(outputStream* st) { 1566 void os::print_os_info(outputStream* st) {
1632 st->print("OS:"); 1567 st->print("OS:");
1633 1568
1634 OSVERSIONINFOEX osvi; 1569 OSVERSIONINFOEX osvi;
1635 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); 1570 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
1652 case 6001: { 1587 case 6001: {
1653 // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could 1588 // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could
1654 // find out whether we are running on 64 bit processor or not. 1589 // find out whether we are running on 64 bit processor or not.
1655 SYSTEM_INFO si; 1590 SYSTEM_INFO si;
1656 ZeroMemory(&si, sizeof(SYSTEM_INFO)); 1591 ZeroMemory(&si, sizeof(SYSTEM_INFO));
1657 // Check to see if _GetNativeSystemInfo has been initialized. 1592 if (!os::Kernel32Dll::GetNativeSystemInfoAvailable()){
1658 if (_GetNativeSystemInfo == NULL) {
1659 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32.dll"));
1660 _GetNativeSystemInfo =
1661 CAST_TO_FN_PTR(GetNativeSystemInfo_func_type,
1662 GetProcAddress(hKernel32,
1663 "GetNativeSystemInfo"));
1664 if (_GetNativeSystemInfo == NULL)
1665 GetSystemInfo(&si); 1593 GetSystemInfo(&si);
1666 } else { 1594 } else {
1667 _GetNativeSystemInfo(&si); 1595 os::Kernel32Dll::GetNativeSystemInfo(&si);
1668 } 1596 }
1669 if (os_vers == 5002) { 1597 if (os_vers == 5002) {
1670 if (osvi.wProductType == VER_NT_WORKSTATION && 1598 if (osvi.wProductType == VER_NT_WORKSTATION &&
1671 si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) 1599 si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
1672 st->print(" Windows XP x64 Edition"); 1600 st->print(" Windows XP x64 Edition");
2670 2598
2671 #ifndef MEM_LARGE_PAGES 2599 #ifndef MEM_LARGE_PAGES
2672 #define MEM_LARGE_PAGES 0x20000000 2600 #define MEM_LARGE_PAGES 0x20000000
2673 #endif 2601 #endif
2674 2602
2675 // GetLargePageMinimum is only available on Windows 2003. The other functions
2676 // are available on NT but not on Windows 98/Me. We have to resolve them at
2677 // runtime.
2678 typedef SIZE_T (WINAPI *GetLargePageMinimum_func_type) (void);
2679 typedef BOOL (WINAPI *AdjustTokenPrivileges_func_type)
2680 (HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
2681 typedef BOOL (WINAPI *OpenProcessToken_func_type) (HANDLE, DWORD, PHANDLE);
2682 typedef BOOL (WINAPI *LookupPrivilegeValue_func_type) (LPCTSTR, LPCTSTR, PLUID);
2683
2684 static GetLargePageMinimum_func_type _GetLargePageMinimum;
2685 static AdjustTokenPrivileges_func_type _AdjustTokenPrivileges;
2686 static OpenProcessToken_func_type _OpenProcessToken;
2687 static LookupPrivilegeValue_func_type _LookupPrivilegeValue;
2688
2689 static HINSTANCE _kernel32;
2690 static HINSTANCE _advapi32;
2691 static HANDLE _hProcess; 2603 static HANDLE _hProcess;
2692 static HANDLE _hToken; 2604 static HANDLE _hToken;
2693 2605
2694 static size_t _large_page_size = 0; 2606 static size_t _large_page_size = 0;
2695 2607
2696 static bool resolve_functions_for_large_page_init() { 2608 static bool resolve_functions_for_large_page_init() {
2697 _kernel32 = LoadLibrary("kernel32.dll"); 2609 return os::Kernel32Dll::GetLargePageMinimumAvailable() &&
2698 if (_kernel32 == NULL) return false; 2610 os::Advapi32Dll::AdvapiAvailable();
2699
2700 _GetLargePageMinimum = CAST_TO_FN_PTR(GetLargePageMinimum_func_type,
2701 GetProcAddress(_kernel32, "GetLargePageMinimum"));
2702 if (_GetLargePageMinimum == NULL) return false;
2703
2704 _advapi32 = LoadLibrary("advapi32.dll");
2705 if (_advapi32 == NULL) return false;
2706
2707 _AdjustTokenPrivileges = CAST_TO_FN_PTR(AdjustTokenPrivileges_func_type,
2708 GetProcAddress(_advapi32, "AdjustTokenPrivileges"));
2709 _OpenProcessToken = CAST_TO_FN_PTR(OpenProcessToken_func_type,
2710 GetProcAddress(_advapi32, "OpenProcessToken"));
2711 _LookupPrivilegeValue = CAST_TO_FN_PTR(LookupPrivilegeValue_func_type,
2712 GetProcAddress(_advapi32, "LookupPrivilegeValueA"));
2713 return _AdjustTokenPrivileges != NULL &&
2714 _OpenProcessToken != NULL &&
2715 _LookupPrivilegeValue != NULL;
2716 } 2611 }
2717 2612
2718 static bool request_lock_memory_privilege() { 2613 static bool request_lock_memory_privilege() {
2719 _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, 2614 _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
2720 os::current_process_id()); 2615 os::current_process_id());
2721 2616
2722 LUID luid; 2617 LUID luid;
2723 if (_hProcess != NULL && 2618 if (_hProcess != NULL &&
2724 _OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) && 2619 os::Advapi32Dll::OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) &&
2725 _LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) { 2620 os::Advapi32Dll::LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) {
2726 2621
2727 TOKEN_PRIVILEGES tp; 2622 TOKEN_PRIVILEGES tp;
2728 tp.PrivilegeCount = 1; 2623 tp.PrivilegeCount = 1;
2729 tp.Privileges[0].Luid = luid; 2624 tp.Privileges[0].Luid = luid;
2730 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 2625 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
2731 2626
2732 // AdjustTokenPrivileges() may return TRUE even when it couldn't change the 2627 // AdjustTokenPrivileges() may return TRUE even when it couldn't change the
2733 // privilege. Check GetLastError() too. See MSDN document. 2628 // privilege. Check GetLastError() too. See MSDN document.
2734 if (_AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) && 2629 if (os::Advapi32Dll::AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) &&
2735 (GetLastError() == ERROR_SUCCESS)) { 2630 (GetLastError() == ERROR_SUCCESS)) {
2736 return true; 2631 return true;
2737 } 2632 }
2738 } 2633 }
2739 2634
2740 return false; 2635 return false;
2741 } 2636 }
2742 2637
2743 static void cleanup_after_large_page_init() { 2638 static void cleanup_after_large_page_init() {
2744 _GetLargePageMinimum = NULL;
2745 _AdjustTokenPrivileges = NULL;
2746 _OpenProcessToken = NULL;
2747 _LookupPrivilegeValue = NULL;
2748 if (_kernel32) FreeLibrary(_kernel32);
2749 _kernel32 = NULL;
2750 if (_advapi32) FreeLibrary(_advapi32);
2751 _advapi32 = NULL;
2752 if (_hProcess) CloseHandle(_hProcess); 2639 if (_hProcess) CloseHandle(_hProcess);
2753 _hProcess = NULL; 2640 _hProcess = NULL;
2754 if (_hToken) CloseHandle(_hToken); 2641 if (_hToken) CloseHandle(_hToken);
2755 _hToken = NULL; 2642 _hToken = NULL;
2756 } 2643 }
2764 bool success = false; 2651 bool success = false;
2765 2652
2766 # define WARN(msg) if (warn_on_failure) { warning(msg); } 2653 # define WARN(msg) if (warn_on_failure) { warning(msg); }
2767 if (resolve_functions_for_large_page_init()) { 2654 if (resolve_functions_for_large_page_init()) {
2768 if (request_lock_memory_privilege()) { 2655 if (request_lock_memory_privilege()) {
2769 size_t s = _GetLargePageMinimum(); 2656 size_t s = os::Kernel32Dll::GetLargePageMinimum();
2770 if (s) { 2657 if (s) {
2771 #if defined(IA32) || defined(AMD64) 2658 #if defined(IA32) || defined(AMD64)
2772 if (s > 4*M || LargePageSizeInBytes > 4*M) { 2659 if (s > 4*M || LargePageSizeInBytes > 4*M) {
2773 WARN("JVM cannot use large pages bigger than 4mb."); 2660 WARN("JVM cannot use large pages bigger than 4mb.");
2774 } else { 2661 } else {
3177 typedef BOOL (WINAPI * STTSignature)(void) ; 3064 typedef BOOL (WINAPI * STTSignature)(void) ;
3178 3065
3179 os::YieldResult os::NakedYield() { 3066 os::YieldResult os::NakedYield() {
3180 // Use either SwitchToThread() or Sleep(0) 3067 // Use either SwitchToThread() or Sleep(0)
3181 // Consider passing back the return value from SwitchToThread(). 3068 // Consider passing back the return value from SwitchToThread().
3182 // We use GetProcAddress() as ancient Win9X versions of windows doen't support SwitchToThread. 3069 if (os::Kernel32Dll::SwitchToThreadAvailable()) {
3183 // In that case we revert to Sleep(0). 3070 return SwitchToThread() ? os::YIELD_SWITCHED : os::YIELD_NONEREADY ;
3184 static volatile STTSignature stt = (STTSignature) 1 ;
3185
3186 if (stt == ((STTSignature) 1)) {
3187 stt = (STTSignature) ::GetProcAddress (LoadLibrary ("Kernel32.dll"), "SwitchToThread") ;
3188 // It's OK if threads race during initialization as the operation above is idempotent.
3189 }
3190 if (stt != NULL) {
3191 return (*stt)() ? os::YIELD_SWITCHED : os::YIELD_NONEREADY ;
3192 } else { 3071 } else {
3193 Sleep (0) ; 3072 Sleep(0);
3194 } 3073 }
3195 return os::YIELD_UNKNOWN ; 3074 return os::YIELD_UNKNOWN ;
3196 } 3075 }
3197 3076
3198 void os::yield() { os::NakedYield(); } 3077 void os::yield() { os::NakedYield(); }
3412 StarvationMonitorInterval = 6000; 3291 StarvationMonitorInterval = 6000;
3413 } 3292 }
3414 } 3293 }
3415 3294
3416 3295
3296 HINSTANCE os::win32::load_Windows_dll(const char* name, char *ebuf, int ebuflen) {
3297 char path[MAX_PATH];
3298 DWORD size;
3299 DWORD pathLen = (DWORD)sizeof(path);
3300 HINSTANCE result = NULL;
3301
3302 // only allow library name without path component
3303 assert(strchr(name, '\\') == NULL, "path not allowed");
3304 assert(strchr(name, ':') == NULL, "path not allowed");
3305 if (strchr(name, '\\') != NULL || strchr(name, ':') != NULL) {
3306 jio_snprintf(ebuf, ebuflen,
3307 "Invalid parameter while calling os::win32::load_windows_dll(): cannot take path: %s", name);
3308 return NULL;
3309 }
3310
3311 // search system directory
3312 if ((size = GetSystemDirectory(path, pathLen)) > 0) {
3313 strcat(path, "\\");
3314 strcat(path, name);
3315 if ((result = (HINSTANCE)os::dll_load(path, ebuf, ebuflen)) != NULL) {
3316 return result;
3317 }
3318 }
3319
3320 // try Windows directory
3321 if ((size = GetWindowsDirectory(path, pathLen)) > 0) {
3322 strcat(path, "\\");
3323 strcat(path, name);
3324 if ((result = (HINSTANCE)os::dll_load(path, ebuf, ebuflen)) != NULL) {
3325 return result;
3326 }
3327 }
3328
3329 jio_snprintf(ebuf, ebuflen,
3330 "os::win32::load_windows_dll() cannot load %s from system directories.", name);
3331 return NULL;
3332 }
3333
3417 void os::win32::setmode_streams() { 3334 void os::win32::setmode_streams() {
3418 _setmode(_fileno(stdin), _O_BINARY); 3335 _setmode(_fileno(stdin), _O_BINARY);
3419 _setmode(_fileno(stdout), _O_BINARY); 3336 _setmode(_fileno(stdout), _O_BINARY);
3420 _setmode(_fileno(stderr), _O_BINARY); 3337 _setmode(_fileno(stderr), _O_BINARY);
3421 } 3338 }
3644 // VM_Exit VMOperation's doit method. 3561 // VM_Exit VMOperation's doit method.
3645 if (atexit(perfMemory_exit_helper) != 0) { 3562 if (atexit(perfMemory_exit_helper) != 0) {
3646 warning("os::init_2 atexit(perfMemory_exit_helper) failed"); 3563 warning("os::init_2 atexit(perfMemory_exit_helper) failed");
3647 } 3564 }
3648 } 3565 }
3649
3650 // initialize PSAPI or ToolHelp for fatal error handler
3651 if (win32::is_nt()) _init_psapi();
3652 else _init_toolhelp();
3653 3566
3654 #ifndef _WIN64 3567 #ifndef _WIN64
3655 // Print something if NX is enabled (win32 on AMD64) 3568 // Print something if NX is enabled (win32 on AMD64)
3656 NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection()); 3569 NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection());
3657 #endif 3570 #endif
4695 4608
4696 4609
4697 // We don't build a headless jre for Windows 4610 // We don't build a headless jre for Windows
4698 bool os::is_headless_jre() { return false; } 4611 bool os::is_headless_jre() { return false; }
4699 4612
4700 // OS_SocketInterface
4701 // Not used on Windows
4702
4703 // OS_SocketInterface
4704 typedef struct hostent * (PASCAL FAR *ws2_ifn_ptr_t)(...);
4705 ws2_ifn_ptr_t *get_host_by_name_fn = NULL;
4706 4613
4707 typedef CRITICAL_SECTION mutex_t; 4614 typedef CRITICAL_SECTION mutex_t;
4708 #define mutexInit(m) InitializeCriticalSection(m) 4615 #define mutexInit(m) InitializeCriticalSection(m)
4709 #define mutexDestroy(m) DeleteCriticalSection(m) 4616 #define mutexDestroy(m) DeleteCriticalSection(m)
4710 #define mutexLock(m) EnterCriticalSection(m) 4617 #define mutexLock(m) EnterCriticalSection(m)
4711 #define mutexUnlock(m) LeaveCriticalSection(m) 4618 #define mutexUnlock(m) LeaveCriticalSection(m)
4712 4619
4713 static bool sockfnptrs_initialized = FALSE; 4620 static bool sock_initialized = FALSE;
4714 static mutex_t sockFnTableMutex; 4621 static mutex_t sockFnTableMutex;
4715 4622
4716 /* is Winsock2 loaded? better to be explicit than to rely on sockfnptrs */ 4623 static void initSock() {
4717 static bool winsock2Available = FALSE;
4718
4719
4720 static void initSockFnTable() {
4721 int (PASCAL FAR* WSAStartupPtr)(WORD, LPWSADATA);
4722 WSADATA wsadata; 4624 WSADATA wsadata;
4625
4626 if (!os::WinSock2Dll::WinSock2Available()) {
4627 jio_fprintf(stderr, "Could not load Winsock 2 (error: %d)\n",
4628 ::GetLastError());
4629 return;
4630 }
4631 if (sock_initialized == TRUE) return;
4723 4632
4724 ::mutexInit(&sockFnTableMutex); 4633 ::mutexInit(&sockFnTableMutex);
4725 ::mutexLock(&sockFnTableMutex); 4634 ::mutexLock(&sockFnTableMutex);
4726 4635 if (os::WinSock2Dll::WSAStartup(MAKEWORD(1,1), &wsadata) != 0) {
4727 if (sockfnptrs_initialized == FALSE) { 4636 jio_fprintf(stderr, "Could not initialize Winsock\n");
4728 HMODULE hWinsock; 4637 }
4729 4638 sock_initialized = TRUE;
4730 /* try to load Winsock2, and if that fails, load Winsock */
4731 hWinsock = ::LoadLibrary("ws2_32.dll");
4732
4733 if (hWinsock == NULL) {
4734 jio_fprintf(stderr, "Could not load Winsock 2 (error: %d)\n",
4735 ::GetLastError());
4736 return;
4737 }
4738
4739 /* If we loaded a DLL, then we might as well initialize it. */
4740 WSAStartupPtr = (int (PASCAL FAR *)(WORD, LPWSADATA))
4741 ::GetProcAddress(hWinsock, "WSAStartup");
4742
4743 if (WSAStartupPtr(MAKEWORD(1,1), &wsadata) != 0) {
4744 jio_fprintf(stderr, "Could not initialize Winsock\n");
4745 }
4746
4747 get_host_by_name_fn
4748 = (ws2_ifn_ptr_t*) GetProcAddress(hWinsock, "gethostbyname");
4749 }
4750
4751 assert(get_host_by_name_fn != NULL,
4752 "gethostbyname function not found");
4753 sockfnptrs_initialized = TRUE;
4754 ::mutexUnlock(&sockFnTableMutex); 4639 ::mutexUnlock(&sockFnTableMutex);
4755 } 4640 }
4756 4641
4757 struct hostent* os::get_host_by_name(char* name) { 4642 struct hostent* os::get_host_by_name(char* name) {
4758 if (!sockfnptrs_initialized) { 4643 if (!sock_initialized) {
4759 initSockFnTable(); 4644 initSock();
4760 } 4645 }
4761 4646 if (!os::WinSock2Dll::WinSock2Available()) {
4762 assert(sockfnptrs_initialized == TRUE && get_host_by_name_fn != NULL, 4647 return NULL;
4763 "sockfnptrs is not initialized or pointer to gethostbyname function is NULL"); 4648 }
4764 return (*get_host_by_name_fn)(name); 4649 return (struct hostent*)os::WinSock2Dll::gethostbyname(name);
4765 } 4650 }
4766 4651
4767 4652
4768 int os::socket_close(int fd) { 4653 int os::socket_close(int fd) {
4769 ShouldNotReachHere(); 4654 ShouldNotReachHere();
4856 int os::set_sock_opt(int fd, int level, int optname, 4741 int os::set_sock_opt(int fd, int level, int optname,
4857 const char *optval, int optlen) { 4742 const char *optval, int optlen) {
4858 ShouldNotReachHere(); 4743 ShouldNotReachHere();
4859 return 0; 4744 return 0;
4860 } 4745 }
4746
4747
4748 // Kernel32 API
4749 typedef SIZE_T (WINAPI* GetLargePageMinimum_Fn)(void);
4750 GetLargePageMinimum_Fn os::Kernel32Dll::_GetLargePageMinimum = NULL;
4751 BOOL os::Kernel32Dll::initialized = FALSE;
4752 SIZE_T os::Kernel32Dll::GetLargePageMinimum() {
4753 assert(initialized && _GetLargePageMinimum != NULL,
4754 "GetLargePageMinimumAvailable() not yet called");
4755 return _GetLargePageMinimum();
4756 }
4757
4758 BOOL os::Kernel32Dll::GetLargePageMinimumAvailable() {
4759 if (!initialized) {
4760 initialize();
4761 }
4762 return _GetLargePageMinimum != NULL;
4763 }
4764
4765
4766 #ifndef JDK6_OR_EARLIER
4767
4768 void os::Kernel32Dll::initialize() {
4769 if (!initialized) {
4770 HMODULE handle = ::GetModuleHandle("Kernel32.dll");
4771 assert(handle != NULL, "Just check");
4772 _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
4773 initialized = TRUE;
4774 }
4775 }
4776
4777
4778 // Kernel32 API
4779 inline BOOL os::Kernel32Dll::SwitchToThread() {
4780 return ::SwitchToThread();
4781 }
4782
4783 inline BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
4784 return true;
4785 }
4786
4787 // Help tools
4788 inline BOOL os::Kernel32Dll::HelpToolsAvailable() {
4789 return true;
4790 }
4791
4792 inline HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,DWORD th32ProcessId) {
4793 return ::CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
4794 }
4795
4796 inline BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4797 return ::Module32First(hSnapshot, lpme);
4798 }
4799
4800 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4801 return ::Module32Next(hSnapshot, lpme);
4802 }
4803
4804
4805 inline BOOL os::Kernel32Dll::GetNativeSystemInfoAvailable() {
4806 return true;
4807 }
4808
4809 inline void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
4810 ::GetNativeSystemInfo(lpSystemInfo);
4811 }
4812
4813 // PSAPI API
4814 inline BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess, HMODULE *lpModule, DWORD cb, LPDWORD lpcbNeeded) {
4815 return ::EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
4816 }
4817
4818 inline DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize) {
4819 return ::GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
4820 }
4821
4822 inline BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb) {
4823 return ::GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
4824 }
4825
4826 inline BOOL os::PSApiDll::PSApiAvailable() {
4827 return true;
4828 }
4829
4830
4831 // WinSock2 API
4832 inline BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData) {
4833 return ::WSAStartup(wVersionRequested, lpWSAData);
4834 }
4835
4836 inline struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
4837 return ::gethostbyname(name);
4838 }
4839
4840 inline BOOL os::WinSock2Dll::WinSock2Available() {
4841 return true;
4842 }
4843
4844 // Advapi API
4845 inline BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
4846 BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength,
4847 PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength) {
4848 return ::AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
4849 BufferLength, PreviousState, ReturnLength);
4850 }
4851
4852 inline BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess,
4853 PHANDLE TokenHandle) {
4854 return ::OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
4855 }
4856
4857 inline BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName, LPCTSTR lpName, PLUID lpLuid) {
4858 return ::LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
4859 }
4860
4861 inline BOOL os::Advapi32Dll::AdvapiAvailable() {
4862 return true;
4863 }
4864
4865 #else
4866 // Kernel32 API
4867 typedef BOOL (WINAPI* SwitchToThread_Fn)(void);
4868 typedef HANDLE (WINAPI* CreateToolhelp32Snapshot_Fn)(DWORD,DWORD);
4869 typedef BOOL (WINAPI* Module32First_Fn)(HANDLE,LPMODULEENTRY32);
4870 typedef BOOL (WINAPI* Module32Next_Fn)(HANDLE,LPMODULEENTRY32);
4871 typedef void (WINAPI* GetNativeSystemInfo_Fn)(LPSYSTEM_INFO);
4872
4873 SwitchToThread_Fn os::Kernel32Dll::_SwitchToThread = NULL;
4874 CreateToolhelp32Snapshot_Fn os::Kernel32Dll::_CreateToolhelp32Snapshot = NULL;
4875 Module32First_Fn os::Kernel32Dll::_Module32First = NULL;
4876 Module32Next_Fn os::Kernel32Dll::_Module32Next = NULL;
4877 GetNativeSystemInfo_Fn os::Kernel32Dll::_GetNativeSystemInfo = NULL;
4878
4879 void os::Kernel32Dll::initialize() {
4880 if (!initialized) {
4881 HMODULE handle = ::GetModuleHandle("Kernel32.dll");
4882 assert(handle != NULL, "Just check");
4883
4884 _SwitchToThread = (SwitchToThread_Fn)::GetProcAddress(handle, "SwitchToThread");
4885 _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
4886 _CreateToolhelp32Snapshot = (CreateToolhelp32Snapshot_Fn)
4887 ::GetProcAddress(handle, "CreateToolhelp32Snapshot");
4888 _Module32First = (Module32First_Fn)::GetProcAddress(handle, "Module32First");
4889 _Module32Next = (Module32Next_Fn)::GetProcAddress(handle, "Module32Next");
4890 _GetNativeSystemInfo = (GetNativeSystemInfo_Fn)::GetProcAddress(handle, "GetNativeSystemInfo");
4891
4892 initialized = TRUE;
4893 }
4894 }
4895
4896 BOOL os::Kernel32Dll::SwitchToThread() {
4897 assert(initialized && _SwitchToThread != NULL,
4898 "SwitchToThreadAvailable() not yet called");
4899 return _SwitchToThread();
4900 }
4901
4902
4903 BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
4904 if (!initialized) {
4905 initialize();
4906 }
4907 return _SwitchToThread != NULL;
4908 }
4909
4910 // Help tools
4911 BOOL os::Kernel32Dll::HelpToolsAvailable() {
4912 if (!initialized) {
4913 initialize();
4914 }
4915 return _CreateToolhelp32Snapshot != NULL &&
4916 _Module32First != NULL &&
4917 _Module32Next != NULL;
4918 }
4919
4920 HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,DWORD th32ProcessId) {
4921 assert(initialized && _CreateToolhelp32Snapshot != NULL,
4922 "HelpToolsAvailable() not yet called");
4923
4924 return _CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
4925 }
4926
4927 BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4928 assert(initialized && _Module32First != NULL,
4929 "HelpToolsAvailable() not yet called");
4930
4931 return _Module32First(hSnapshot, lpme);
4932 }
4933
4934 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4935 assert(initialized && _Module32Next != NULL,
4936 "HelpToolsAvailable() not yet called");
4937
4938 return _Module32Next(hSnapshot, lpme);
4939 }
4940
4941
4942 BOOL os::Kernel32Dll::GetNativeSystemInfoAvailable() {
4943 if (!initialized) {
4944 initialize();
4945 }
4946 return _GetNativeSystemInfo != NULL;
4947 }
4948
4949 void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
4950 assert(initialized && _GetNativeSystemInfo != NULL,
4951 "GetNativeSystemInfoAvailable() not yet called");
4952
4953 _GetNativeSystemInfo(lpSystemInfo);
4954 }
4955
4956 // PSAPI API
4957
4958
4959 typedef BOOL (WINAPI *EnumProcessModules_Fn)(HANDLE, HMODULE *, DWORD, LPDWORD);
4960 typedef BOOL (WINAPI *GetModuleFileNameEx_Fn)(HANDLE, HMODULE, LPTSTR, DWORD);;
4961 typedef BOOL (WINAPI *GetModuleInformation_Fn)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
4962
4963 EnumProcessModules_Fn os::PSApiDll::_EnumProcessModules = NULL;
4964 GetModuleFileNameEx_Fn os::PSApiDll::_GetModuleFileNameEx = NULL;
4965 GetModuleInformation_Fn os::PSApiDll::_GetModuleInformation = NULL;
4966 BOOL os::PSApiDll::initialized = FALSE;
4967
4968 void os::PSApiDll::initialize() {
4969 if (!initialized) {
4970 HMODULE handle = os::win32::load_Windows_dll("PSAPI.DLL", NULL, 0);
4971 if (handle != NULL) {
4972 _EnumProcessModules = (EnumProcessModules_Fn)::GetProcAddress(handle,
4973 "EnumProcessModules");
4974 _GetModuleFileNameEx = (GetModuleFileNameEx_Fn)::GetProcAddress(handle,
4975 "GetModuleFileNameExA");
4976 _GetModuleInformation = (GetModuleInformation_Fn)::GetProcAddress(handle,
4977 "GetModuleInformation");
4978 }
4979 initialized = TRUE;
4980 }
4981 }
4982
4983
4984
4985 BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess, HMODULE *lpModule, DWORD cb, LPDWORD lpcbNeeded) {
4986 assert(initialized && _EnumProcessModules != NULL,
4987 "PSApiAvailable() not yet called");
4988 return _EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
4989 }
4990
4991 DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize) {
4992 assert(initialized && _GetModuleFileNameEx != NULL,
4993 "PSApiAvailable() not yet called");
4994 return _GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
4995 }
4996
4997 BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb) {
4998 assert(initialized && _GetModuleInformation != NULL,
4999 "PSApiAvailable() not yet called");
5000 return _GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
5001 }
5002
5003 BOOL os::PSApiDll::PSApiAvailable() {
5004 if (!initialized) {
5005 initialize();
5006 }
5007 return _EnumProcessModules != NULL &&
5008 _GetModuleFileNameEx != NULL &&
5009 _GetModuleInformation != NULL;
5010 }
5011
5012
5013 // WinSock2 API
5014 typedef int (PASCAL FAR* WSAStartup_Fn)(WORD, LPWSADATA);
5015 typedef struct hostent *(PASCAL FAR *gethostbyname_Fn)(...);
5016
5017 WSAStartup_Fn os::WinSock2Dll::_WSAStartup = NULL;
5018 gethostbyname_Fn os::WinSock2Dll::_gethostbyname = NULL;
5019 BOOL os::WinSock2Dll::initialized = FALSE;
5020
5021 void os::WinSock2Dll::initialize() {
5022 if (!initialized) {
5023 HMODULE handle = os::win32::load_Windows_dll("ws2_32.dll", NULL, 0);
5024 if (handle != NULL) {
5025 _WSAStartup = (WSAStartup_Fn)::GetProcAddress(handle, "WSAStartup");
5026 _gethostbyname = (gethostbyname_Fn)::GetProcAddress(handle, "gethostbyname");
5027 }
5028 initialized = TRUE;
5029 }
5030 }
5031
5032
5033 BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData) {
5034 assert(initialized && _WSAStartup != NULL,
5035 "WinSock2Available() not yet called");
5036 return _WSAStartup(wVersionRequested, lpWSAData);
5037 }
5038
5039 struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
5040 assert(initialized && _gethostbyname != NULL,
5041 "WinSock2Available() not yet called");
5042 return _gethostbyname(name);
5043 }
5044
5045 BOOL os::WinSock2Dll::WinSock2Available() {
5046 if (!initialized) {
5047 initialize();
5048 }
5049 return _WSAStartup != NULL &&
5050 _gethostbyname != NULL;
5051 }
5052
5053 typedef BOOL (WINAPI *AdjustTokenPrivileges_Fn)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
5054 typedef BOOL (WINAPI *OpenProcessToken_Fn)(HANDLE, DWORD, PHANDLE);
5055 typedef BOOL (WINAPI *LookupPrivilegeValue_Fn)(LPCTSTR, LPCTSTR, PLUID);
5056
5057 AdjustTokenPrivileges_Fn os::Advapi32Dll::_AdjustTokenPrivileges = NULL;
5058 OpenProcessToken_Fn os::Advapi32Dll::_OpenProcessToken = NULL;
5059 LookupPrivilegeValue_Fn os::Advapi32Dll::_LookupPrivilegeValue = NULL;
5060 BOOL os::Advapi32Dll::initialized = FALSE;
5061
5062 void os::Advapi32Dll::initialize() {
5063 if (!initialized) {
5064 HMODULE handle = os::win32::load_Windows_dll("advapi32.dll", NULL, 0);
5065 if (handle != NULL) {
5066 _AdjustTokenPrivileges = (AdjustTokenPrivileges_Fn)::GetProcAddress(handle,
5067 "AdjustTokenPrivileges");
5068 _OpenProcessToken = (OpenProcessToken_Fn)::GetProcAddress(handle,
5069 "OpenProcessToken");
5070 _LookupPrivilegeValue = (LookupPrivilegeValue_Fn)::GetProcAddress(handle,
5071 "LookupPrivilegeValue");
5072 }
5073 initialized = TRUE;
5074 }
5075 }
5076
5077 BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
5078 BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength,
5079 PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength) {
5080 assert(initialized && _AdjustTokenPrivileges != NULL,
5081 "AdvapiAvailable() not yet called");
5082 return _AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
5083 BufferLength, PreviousState, ReturnLength);
5084 }
5085
5086 BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess,
5087 PHANDLE TokenHandle) {
5088 assert(initialized && _OpenProcessToken != NULL,
5089 "AdvapiAvailable() not yet called");
5090 return _OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
5091 }
5092
5093 BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName, LPCTSTR lpName, PLUID lpLuid) {
5094 assert(initialized && _LookupPrivilegeValue != NULL,
5095 "AdvapiAvailable() not yet called");
5096 return _LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
5097 }
5098
5099 BOOL os::Advapi32Dll::AdvapiAvailable() {
5100 if (!initialized) {
5101 initialize();
5102 }
5103 return _AdjustTokenPrivileges != NULL &&
5104 _OpenProcessToken != NULL &&
5105 _LookupPrivilegeValue != NULL;
5106 }
5107
5108 #endif
5109