comparison src/os/windows/vm/os_windows.cpp @ 3833:b1cbb0907b36

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 bf6481e5f96d
children 279ef1916773
comparison
equal deleted inserted replaced
3821:8107273fd204 3833:b1cbb0907b36
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
937 } else if (os::win32::is_windows_server() && !FLAG_IS_DEFAULT(CreateMinidumpOnCrash) && !CreateMinidumpOnCrash) { 936 } else if (os::win32::is_windows_server() && !FLAG_IS_DEFAULT(CreateMinidumpOnCrash) && !CreateMinidumpOnCrash) {
938 VMError::report_coredump_status("Minidump has been disabled from the command line", false); 937 VMError::report_coredump_status("Minidump has been disabled from the command line", false);
939 return; 938 return;
940 } 939 }
941 940
942 dbghelp = LoadLibrary("DBGHELP.DLL"); 941 dbghelp = os::win32::load_Windows_dll("DBGHELP.DLL", NULL, 0);
943 942
944 if (dbghelp == NULL) { 943 if (dbghelp == NULL) {
945 VMError::report_coredump_status("Failed to load dbghelp.dll", false); 944 VMError::report_coredump_status("Failed to load dbghelp.dll", false);
946 return; 945 return;
947 } 946 }
1202 return _getcwd(buf, buflen); 1201 return _getcwd(buf, buflen);
1203 } 1202 }
1204 1203
1205 //----------------------------------------------------------- 1204 //-----------------------------------------------------------
1206 // Helper functions for fatal error handler 1205 // Helper functions for fatal error handler
1207
1208 // The following library functions are resolved dynamically at runtime:
1209
1210 // PSAPI functions, for Windows NT, 2000, XP
1211
1212 // psapi.h doesn't come with Visual Studio 6; it can be downloaded as Platform
1213 // SDK from Microsoft. Here are the definitions copied from psapi.h
1214 typedef struct _MODULEINFO {
1215 LPVOID lpBaseOfDll;
1216 DWORD SizeOfImage;
1217 LPVOID EntryPoint;
1218 } MODULEINFO, *LPMODULEINFO;
1219
1220 static BOOL (WINAPI *_EnumProcessModules) ( HANDLE, HMODULE *, DWORD, LPDWORD );
1221 static DWORD (WINAPI *_GetModuleFileNameEx) ( HANDLE, HMODULE, LPTSTR, DWORD );
1222 static BOOL (WINAPI *_GetModuleInformation)( HANDLE, HMODULE, LPMODULEINFO, DWORD );
1223
1224 // ToolHelp Functions, for Windows 95, 98 and ME
1225
1226 static HANDLE(WINAPI *_CreateToolhelp32Snapshot)(DWORD,DWORD) ;
1227 static BOOL (WINAPI *_Module32First) (HANDLE,LPMODULEENTRY32) ;
1228 static BOOL (WINAPI *_Module32Next) (HANDLE,LPMODULEENTRY32) ;
1229
1230 bool _has_psapi;
1231 bool _psapi_init = false;
1232 bool _has_toolhelp;
1233
1234 static bool _init_psapi() {
1235 HINSTANCE psapi = LoadLibrary( "PSAPI.DLL" ) ;
1236 if( psapi == NULL ) return false ;
1237
1238 _EnumProcessModules = CAST_TO_FN_PTR(
1239 BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD),
1240 GetProcAddress(psapi, "EnumProcessModules")) ;
1241 _GetModuleFileNameEx = CAST_TO_FN_PTR(
1242 DWORD (WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD),
1243 GetProcAddress(psapi, "GetModuleFileNameExA"));
1244 _GetModuleInformation = CAST_TO_FN_PTR(
1245 BOOL (WINAPI *)(HANDLE, HMODULE, LPMODULEINFO, DWORD),
1246 GetProcAddress(psapi, "GetModuleInformation"));
1247
1248 _has_psapi = (_EnumProcessModules && _GetModuleFileNameEx && _GetModuleInformation);
1249 _psapi_init = true;
1250 return _has_psapi;
1251 }
1252
1253 static bool _init_toolhelp() {
1254 HINSTANCE kernel32 = LoadLibrary("Kernel32.DLL") ;
1255 if (kernel32 == NULL) return false ;
1256
1257 _CreateToolhelp32Snapshot = CAST_TO_FN_PTR(
1258 HANDLE(WINAPI *)(DWORD,DWORD),
1259 GetProcAddress(kernel32, "CreateToolhelp32Snapshot"));
1260 _Module32First = CAST_TO_FN_PTR(
1261 BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32),
1262 GetProcAddress(kernel32, "Module32First" ));
1263 _Module32Next = CAST_TO_FN_PTR(
1264 BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32),
1265 GetProcAddress(kernel32, "Module32Next" ));
1266
1267 _has_toolhelp = (_CreateToolhelp32Snapshot && _Module32First && _Module32Next);
1268 return _has_toolhelp;
1269 }
1270
1271 #ifdef _WIN64 1206 #ifdef _WIN64
1272 // Helper routine which returns true if address in 1207 // Helper routine which returns true if address in
1273 // within the NTDLL address space. 1208 // within the NTDLL address space.
1274 // 1209 //
1275 static bool _addr_in_ntdll( address addr ) 1210 static bool _addr_in_ntdll( address addr )
1277 HMODULE hmod; 1212 HMODULE hmod;
1278 MODULEINFO minfo; 1213 MODULEINFO minfo;
1279 1214
1280 hmod = GetModuleHandle("NTDLL.DLL"); 1215 hmod = GetModuleHandle("NTDLL.DLL");
1281 if ( hmod == NULL ) return false; 1216 if ( hmod == NULL ) return false;
1282 if ( !_GetModuleInformation( GetCurrentProcess(), hmod, 1217 if ( !os::PSApiDll::GetModuleInformation( GetCurrentProcess(), hmod,
1283 &minfo, sizeof(MODULEINFO)) ) 1218 &minfo, sizeof(MODULEINFO)) )
1284 return false; 1219 return false;
1285 1220
1286 if ( (addr >= minfo.lpBaseOfDll) && 1221 if ( (addr >= minfo.lpBaseOfDll) &&
1287 (addr < (address)((uintptr_t)minfo.lpBaseOfDll + (uintptr_t)minfo.SizeOfImage))) 1222 (addr < (address)((uintptr_t)minfo.lpBaseOfDll + (uintptr_t)minfo.SizeOfImage)))
1316 # define MAX_NUM_MODULES 128 1251 # define MAX_NUM_MODULES 128
1317 HMODULE modules[MAX_NUM_MODULES]; 1252 HMODULE modules[MAX_NUM_MODULES];
1318 static char filename[ MAX_PATH ]; 1253 static char filename[ MAX_PATH ];
1319 int result = 0; 1254 int result = 0;
1320 1255
1321 if (!_has_psapi && (_psapi_init || !_init_psapi())) return 0; 1256 if (!os::PSApiDll::PSApiAvailable()) {
1257 return 0;
1258 }
1322 1259
1323 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 1260 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
1324 FALSE, pid ) ; 1261 FALSE, pid ) ;
1325 if (hProcess == NULL) return 0; 1262 if (hProcess == NULL) return 0;
1326 1263
1327 DWORD size_needed; 1264 DWORD size_needed;
1328 if (!_EnumProcessModules(hProcess, modules, 1265 if (!os::PSApiDll::EnumProcessModules(hProcess, modules,
1329 sizeof(modules), &size_needed)) { 1266 sizeof(modules), &size_needed)) {
1330 CloseHandle( hProcess ); 1267 CloseHandle( hProcess );
1331 return 0; 1268 return 0;
1332 } 1269 }
1333 1270
1334 // number of modules that are currently loaded 1271 // number of modules that are currently loaded
1335 int num_modules = size_needed / sizeof(HMODULE); 1272 int num_modules = size_needed / sizeof(HMODULE);
1336 1273
1337 for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) { 1274 for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) {
1338 // Get Full pathname: 1275 // Get Full pathname:
1339 if(!_GetModuleFileNameEx(hProcess, modules[i], 1276 if(!os::PSApiDll::GetModuleFileNameEx(hProcess, modules[i],
1340 filename, sizeof(filename))) { 1277 filename, sizeof(filename))) {
1341 filename[0] = '\0'; 1278 filename[0] = '\0';
1342 } 1279 }
1343 1280
1344 MODULEINFO modinfo; 1281 MODULEINFO modinfo;
1345 if (!_GetModuleInformation(hProcess, modules[i], 1282 if (!os::PSApiDll::GetModuleInformation(hProcess, modules[i],
1346 &modinfo, sizeof(modinfo))) { 1283 &modinfo, sizeof(modinfo))) {
1347 modinfo.lpBaseOfDll = NULL; 1284 modinfo.lpBaseOfDll = NULL;
1348 modinfo.SizeOfImage = 0; 1285 modinfo.SizeOfImage = 0;
1349 } 1286 }
1350 1287
1364 { 1301 {
1365 HANDLE hSnapShot ; 1302 HANDLE hSnapShot ;
1366 static MODULEENTRY32 modentry ; 1303 static MODULEENTRY32 modentry ;
1367 int result = 0; 1304 int result = 0;
1368 1305
1369 if (!_has_toolhelp) return 0; 1306 if (!os::Kernel32Dll::HelpToolsAvailable()) {
1307 return 0;
1308 }
1370 1309
1371 // Get a handle to a Toolhelp snapshot of the system 1310 // Get a handle to a Toolhelp snapshot of the system
1372 hSnapShot = _CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid ) ; 1311 hSnapShot = os::Kernel32Dll::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid ) ;
1373 if( hSnapShot == INVALID_HANDLE_VALUE ) { 1312 if( hSnapShot == INVALID_HANDLE_VALUE ) {
1374 return FALSE ; 1313 return FALSE ;
1375 } 1314 }
1376 1315
1377 // iterate through all modules 1316 // iterate through all modules
1378 modentry.dwSize = sizeof(MODULEENTRY32) ; 1317 modentry.dwSize = sizeof(MODULEENTRY32) ;
1379 bool not_done = _Module32First( hSnapShot, &modentry ) != 0; 1318 bool not_done = os::Kernel32Dll::Module32First( hSnapShot, &modentry ) != 0;
1380 1319
1381 while( not_done ) { 1320 while( not_done ) {
1382 // invoke the callback 1321 // invoke the callback
1383 result=func(pid, modentry.szExePath, (address)modentry.modBaseAddr, 1322 result=func(pid, modentry.szExePath, (address)modentry.modBaseAddr,
1384 modentry.modBaseSize, param); 1323 modentry.modBaseSize, param);
1385 if (result) break; 1324 if (result) break;
1386 1325
1387 modentry.dwSize = sizeof(MODULEENTRY32) ; 1326 modentry.dwSize = sizeof(MODULEENTRY32) ;
1388 not_done = _Module32Next( hSnapShot, &modentry ) != 0; 1327 not_done = os::Kernel32Dll::Module32Next( hSnapShot, &modentry ) != 0;
1389 } 1328 }
1390 1329
1391 CloseHandle(hSnapShot); 1330 CloseHandle(hSnapShot);
1392 return result; 1331 return result;
1393 } 1332 }
1629 int pid = os::current_process_id(); 1568 int pid = os::current_process_id();
1630 st->print_cr("Dynamic libraries:"); 1569 st->print_cr("Dynamic libraries:");
1631 enumerate_modules(pid, _print_module, (void *)st); 1570 enumerate_modules(pid, _print_module, (void *)st);
1632 } 1571 }
1633 1572
1634 // function pointer to Windows API "GetNativeSystemInfo".
1635 typedef void (WINAPI *GetNativeSystemInfo_func_type)(LPSYSTEM_INFO);
1636 static GetNativeSystemInfo_func_type _GetNativeSystemInfo;
1637
1638 void os::print_os_info(outputStream* st) { 1573 void os::print_os_info(outputStream* st) {
1639 st->print("OS:"); 1574 st->print("OS:");
1640 1575
1641 OSVERSIONINFOEX osvi; 1576 OSVERSIONINFOEX osvi;
1642 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); 1577 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
1659 case 6001: { 1594 case 6001: {
1660 // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could 1595 // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could
1661 // find out whether we are running on 64 bit processor or not. 1596 // find out whether we are running on 64 bit processor or not.
1662 SYSTEM_INFO si; 1597 SYSTEM_INFO si;
1663 ZeroMemory(&si, sizeof(SYSTEM_INFO)); 1598 ZeroMemory(&si, sizeof(SYSTEM_INFO));
1664 // Check to see if _GetNativeSystemInfo has been initialized. 1599 if (!os::Kernel32Dll::GetNativeSystemInfoAvailable()){
1665 if (_GetNativeSystemInfo == NULL) {
1666 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32.dll"));
1667 _GetNativeSystemInfo =
1668 CAST_TO_FN_PTR(GetNativeSystemInfo_func_type,
1669 GetProcAddress(hKernel32,
1670 "GetNativeSystemInfo"));
1671 if (_GetNativeSystemInfo == NULL)
1672 GetSystemInfo(&si); 1600 GetSystemInfo(&si);
1673 } else { 1601 } else {
1674 _GetNativeSystemInfo(&si); 1602 os::Kernel32Dll::GetNativeSystemInfo(&si);
1675 } 1603 }
1676 if (os_vers == 5002) { 1604 if (os_vers == 5002) {
1677 if (osvi.wProductType == VER_NT_WORKSTATION && 1605 if (osvi.wProductType == VER_NT_WORKSTATION &&
1678 si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) 1606 si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
1679 st->print(" Windows XP x64 Edition"); 1607 st->print(" Windows XP x64 Edition");
2681 2609
2682 #ifndef MEM_LARGE_PAGES 2610 #ifndef MEM_LARGE_PAGES
2683 #define MEM_LARGE_PAGES 0x20000000 2611 #define MEM_LARGE_PAGES 0x20000000
2684 #endif 2612 #endif
2685 2613
2686 // GetLargePageMinimum is only available on Windows 2003. The other functions
2687 // are available on NT but not on Windows 98/Me. We have to resolve them at
2688 // runtime.
2689 typedef SIZE_T (WINAPI *GetLargePageMinimum_func_type) (void);
2690 typedef BOOL (WINAPI *AdjustTokenPrivileges_func_type)
2691 (HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
2692 typedef BOOL (WINAPI *OpenProcessToken_func_type) (HANDLE, DWORD, PHANDLE);
2693 typedef BOOL (WINAPI *LookupPrivilegeValue_func_type) (LPCTSTR, LPCTSTR, PLUID);
2694
2695 static GetLargePageMinimum_func_type _GetLargePageMinimum;
2696 static AdjustTokenPrivileges_func_type _AdjustTokenPrivileges;
2697 static OpenProcessToken_func_type _OpenProcessToken;
2698 static LookupPrivilegeValue_func_type _LookupPrivilegeValue;
2699
2700 static HINSTANCE _kernel32;
2701 static HINSTANCE _advapi32;
2702 static HANDLE _hProcess; 2614 static HANDLE _hProcess;
2703 static HANDLE _hToken; 2615 static HANDLE _hToken;
2704 2616
2705 static size_t _large_page_size = 0; 2617 static size_t _large_page_size = 0;
2706 2618
2707 static bool resolve_functions_for_large_page_init() { 2619 static bool resolve_functions_for_large_page_init() {
2708 _kernel32 = LoadLibrary("kernel32.dll"); 2620 return os::Kernel32Dll::GetLargePageMinimumAvailable() &&
2709 if (_kernel32 == NULL) return false; 2621 os::Advapi32Dll::AdvapiAvailable();
2710
2711 _GetLargePageMinimum = CAST_TO_FN_PTR(GetLargePageMinimum_func_type,
2712 GetProcAddress(_kernel32, "GetLargePageMinimum"));
2713 if (_GetLargePageMinimum == NULL) return false;
2714
2715 _advapi32 = LoadLibrary("advapi32.dll");
2716 if (_advapi32 == NULL) return false;
2717
2718 _AdjustTokenPrivileges = CAST_TO_FN_PTR(AdjustTokenPrivileges_func_type,
2719 GetProcAddress(_advapi32, "AdjustTokenPrivileges"));
2720 _OpenProcessToken = CAST_TO_FN_PTR(OpenProcessToken_func_type,
2721 GetProcAddress(_advapi32, "OpenProcessToken"));
2722 _LookupPrivilegeValue = CAST_TO_FN_PTR(LookupPrivilegeValue_func_type,
2723 GetProcAddress(_advapi32, "LookupPrivilegeValueA"));
2724 return _AdjustTokenPrivileges != NULL &&
2725 _OpenProcessToken != NULL &&
2726 _LookupPrivilegeValue != NULL;
2727 } 2622 }
2728 2623
2729 static bool request_lock_memory_privilege() { 2624 static bool request_lock_memory_privilege() {
2730 _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, 2625 _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
2731 os::current_process_id()); 2626 os::current_process_id());
2732 2627
2733 LUID luid; 2628 LUID luid;
2734 if (_hProcess != NULL && 2629 if (_hProcess != NULL &&
2735 _OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) && 2630 os::Advapi32Dll::OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) &&
2736 _LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) { 2631 os::Advapi32Dll::LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) {
2737 2632
2738 TOKEN_PRIVILEGES tp; 2633 TOKEN_PRIVILEGES tp;
2739 tp.PrivilegeCount = 1; 2634 tp.PrivilegeCount = 1;
2740 tp.Privileges[0].Luid = luid; 2635 tp.Privileges[0].Luid = luid;
2741 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 2636 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
2742 2637
2743 // AdjustTokenPrivileges() may return TRUE even when it couldn't change the 2638 // AdjustTokenPrivileges() may return TRUE even when it couldn't change the
2744 // privilege. Check GetLastError() too. See MSDN document. 2639 // privilege. Check GetLastError() too. See MSDN document.
2745 if (_AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) && 2640 if (os::Advapi32Dll::AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) &&
2746 (GetLastError() == ERROR_SUCCESS)) { 2641 (GetLastError() == ERROR_SUCCESS)) {
2747 return true; 2642 return true;
2748 } 2643 }
2749 } 2644 }
2750 2645
2751 return false; 2646 return false;
2752 } 2647 }
2753 2648
2754 static void cleanup_after_large_page_init() { 2649 static void cleanup_after_large_page_init() {
2755 _GetLargePageMinimum = NULL;
2756 _AdjustTokenPrivileges = NULL;
2757 _OpenProcessToken = NULL;
2758 _LookupPrivilegeValue = NULL;
2759 if (_kernel32) FreeLibrary(_kernel32);
2760 _kernel32 = NULL;
2761 if (_advapi32) FreeLibrary(_advapi32);
2762 _advapi32 = NULL;
2763 if (_hProcess) CloseHandle(_hProcess); 2650 if (_hProcess) CloseHandle(_hProcess);
2764 _hProcess = NULL; 2651 _hProcess = NULL;
2765 if (_hToken) CloseHandle(_hToken); 2652 if (_hToken) CloseHandle(_hToken);
2766 _hToken = NULL; 2653 _hToken = NULL;
2767 } 2654 }
2775 bool success = false; 2662 bool success = false;
2776 2663
2777 # define WARN(msg) if (warn_on_failure) { warning(msg); } 2664 # define WARN(msg) if (warn_on_failure) { warning(msg); }
2778 if (resolve_functions_for_large_page_init()) { 2665 if (resolve_functions_for_large_page_init()) {
2779 if (request_lock_memory_privilege()) { 2666 if (request_lock_memory_privilege()) {
2780 size_t s = _GetLargePageMinimum(); 2667 size_t s = os::Kernel32Dll::GetLargePageMinimum();
2781 if (s) { 2668 if (s) {
2782 #if defined(IA32) || defined(AMD64) 2669 #if defined(IA32) || defined(AMD64)
2783 if (s > 4*M || LargePageSizeInBytes > 4*M) { 2670 if (s > 4*M || LargePageSizeInBytes > 4*M) {
2784 WARN("JVM cannot use large pages bigger than 4mb."); 2671 WARN("JVM cannot use large pages bigger than 4mb.");
2785 } else { 2672 } else {
3188 typedef BOOL (WINAPI * STTSignature)(void) ; 3075 typedef BOOL (WINAPI * STTSignature)(void) ;
3189 3076
3190 os::YieldResult os::NakedYield() { 3077 os::YieldResult os::NakedYield() {
3191 // Use either SwitchToThread() or Sleep(0) 3078 // Use either SwitchToThread() or Sleep(0)
3192 // Consider passing back the return value from SwitchToThread(). 3079 // Consider passing back the return value from SwitchToThread().
3193 // We use GetProcAddress() as ancient Win9X versions of windows doen't support SwitchToThread. 3080 if (os::Kernel32Dll::SwitchToThreadAvailable()) {
3194 // In that case we revert to Sleep(0). 3081 return SwitchToThread() ? os::YIELD_SWITCHED : os::YIELD_NONEREADY ;
3195 static volatile STTSignature stt = (STTSignature) 1 ;
3196
3197 if (stt == ((STTSignature) 1)) {
3198 stt = (STTSignature) ::GetProcAddress (LoadLibrary ("Kernel32.dll"), "SwitchToThread") ;
3199 // It's OK if threads race during initialization as the operation above is idempotent.
3200 }
3201 if (stt != NULL) {
3202 return (*stt)() ? os::YIELD_SWITCHED : os::YIELD_NONEREADY ;
3203 } else { 3082 } else {
3204 Sleep (0) ; 3083 Sleep(0);
3205 } 3084 }
3206 return os::YIELD_UNKNOWN ; 3085 return os::YIELD_UNKNOWN ;
3207 } 3086 }
3208 3087
3209 void os::yield() { os::NakedYield(); } 3088 void os::yield() { os::NakedYield(); }
3423 StarvationMonitorInterval = 6000; 3302 StarvationMonitorInterval = 6000;
3424 } 3303 }
3425 } 3304 }
3426 3305
3427 3306
3307 HINSTANCE os::win32::load_Windows_dll(const char* name, char *ebuf, int ebuflen) {
3308 char path[MAX_PATH];
3309 DWORD size;
3310 DWORD pathLen = (DWORD)sizeof(path);
3311 HINSTANCE result = NULL;
3312
3313 // only allow library name without path component
3314 assert(strchr(name, '\\') == NULL, "path not allowed");
3315 assert(strchr(name, ':') == NULL, "path not allowed");
3316 if (strchr(name, '\\') != NULL || strchr(name, ':') != NULL) {
3317 jio_snprintf(ebuf, ebuflen,
3318 "Invalid parameter while calling os::win32::load_windows_dll(): cannot take path: %s", name);
3319 return NULL;
3320 }
3321
3322 // search system directory
3323 if ((size = GetSystemDirectory(path, pathLen)) > 0) {
3324 strcat(path, "\\");
3325 strcat(path, name);
3326 if ((result = (HINSTANCE)os::dll_load(path, ebuf, ebuflen)) != NULL) {
3327 return result;
3328 }
3329 }
3330
3331 // try Windows directory
3332 if ((size = GetWindowsDirectory(path, pathLen)) > 0) {
3333 strcat(path, "\\");
3334 strcat(path, name);
3335 if ((result = (HINSTANCE)os::dll_load(path, ebuf, ebuflen)) != NULL) {
3336 return result;
3337 }
3338 }
3339
3340 jio_snprintf(ebuf, ebuflen,
3341 "os::win32::load_windows_dll() cannot load %s from system directories.", name);
3342 return NULL;
3343 }
3344
3428 void os::win32::setmode_streams() { 3345 void os::win32::setmode_streams() {
3429 _setmode(_fileno(stdin), _O_BINARY); 3346 _setmode(_fileno(stdin), _O_BINARY);
3430 _setmode(_fileno(stdout), _O_BINARY); 3347 _setmode(_fileno(stdout), _O_BINARY);
3431 _setmode(_fileno(stderr), _O_BINARY); 3348 _setmode(_fileno(stderr), _O_BINARY);
3432 } 3349 }
3655 // VM_Exit VMOperation's doit method. 3572 // VM_Exit VMOperation's doit method.
3656 if (atexit(perfMemory_exit_helper) != 0) { 3573 if (atexit(perfMemory_exit_helper) != 0) {
3657 warning("os::init_2 atexit(perfMemory_exit_helper) failed"); 3574 warning("os::init_2 atexit(perfMemory_exit_helper) failed");
3658 } 3575 }
3659 } 3576 }
3660
3661 // initialize PSAPI or ToolHelp for fatal error handler
3662 if (win32::is_nt()) _init_psapi();
3663 else _init_toolhelp();
3664 3577
3665 #ifndef _WIN64 3578 #ifndef _WIN64
3666 // Print something if NX is enabled (win32 on AMD64) 3579 // Print something if NX is enabled (win32 on AMD64)
3667 NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection()); 3580 NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection());
3668 #endif 3581 #endif
4706 4619
4707 4620
4708 // We don't build a headless jre for Windows 4621 // We don't build a headless jre for Windows
4709 bool os::is_headless_jre() { return false; } 4622 bool os::is_headless_jre() { return false; }
4710 4623
4711 // OS_SocketInterface
4712 // Not used on Windows
4713
4714 // OS_SocketInterface
4715 typedef struct hostent * (PASCAL FAR *ws2_ifn_ptr_t)(...);
4716 ws2_ifn_ptr_t *get_host_by_name_fn = NULL;
4717 4624
4718 typedef CRITICAL_SECTION mutex_t; 4625 typedef CRITICAL_SECTION mutex_t;
4719 #define mutexInit(m) InitializeCriticalSection(m) 4626 #define mutexInit(m) InitializeCriticalSection(m)
4720 #define mutexDestroy(m) DeleteCriticalSection(m) 4627 #define mutexDestroy(m) DeleteCriticalSection(m)
4721 #define mutexLock(m) EnterCriticalSection(m) 4628 #define mutexLock(m) EnterCriticalSection(m)
4722 #define mutexUnlock(m) LeaveCriticalSection(m) 4629 #define mutexUnlock(m) LeaveCriticalSection(m)
4723 4630
4724 static bool sockfnptrs_initialized = FALSE; 4631 static bool sock_initialized = FALSE;
4725 static mutex_t sockFnTableMutex; 4632 static mutex_t sockFnTableMutex;
4726 4633
4727 /* is Winsock2 loaded? better to be explicit than to rely on sockfnptrs */ 4634 static void initSock() {
4728 static bool winsock2Available = FALSE;
4729
4730
4731 static void initSockFnTable() {
4732 int (PASCAL FAR* WSAStartupPtr)(WORD, LPWSADATA);
4733 WSADATA wsadata; 4635 WSADATA wsadata;
4636
4637 if (!os::WinSock2Dll::WinSock2Available()) {
4638 jio_fprintf(stderr, "Could not load Winsock 2 (error: %d)\n",
4639 ::GetLastError());
4640 return;
4641 }
4642 if (sock_initialized == TRUE) return;
4734 4643
4735 ::mutexInit(&sockFnTableMutex); 4644 ::mutexInit(&sockFnTableMutex);
4736 ::mutexLock(&sockFnTableMutex); 4645 ::mutexLock(&sockFnTableMutex);
4737 4646 if (os::WinSock2Dll::WSAStartup(MAKEWORD(1,1), &wsadata) != 0) {
4738 if (sockfnptrs_initialized == FALSE) { 4647 jio_fprintf(stderr, "Could not initialize Winsock\n");
4739 HMODULE hWinsock; 4648 }
4740 4649 sock_initialized = TRUE;
4741 /* try to load Winsock2, and if that fails, load Winsock */
4742 hWinsock = ::LoadLibrary("ws2_32.dll");
4743
4744 if (hWinsock == NULL) {
4745 jio_fprintf(stderr, "Could not load Winsock 2 (error: %d)\n",
4746 ::GetLastError());
4747 return;
4748 }
4749
4750 /* If we loaded a DLL, then we might as well initialize it. */
4751 WSAStartupPtr = (int (PASCAL FAR *)(WORD, LPWSADATA))
4752 ::GetProcAddress(hWinsock, "WSAStartup");
4753
4754 if (WSAStartupPtr(MAKEWORD(1,1), &wsadata) != 0) {
4755 jio_fprintf(stderr, "Could not initialize Winsock\n");
4756 }
4757
4758 get_host_by_name_fn
4759 = (ws2_ifn_ptr_t*) GetProcAddress(hWinsock, "gethostbyname");
4760 }
4761
4762 assert(get_host_by_name_fn != NULL,
4763 "gethostbyname function not found");
4764 sockfnptrs_initialized = TRUE;
4765 ::mutexUnlock(&sockFnTableMutex); 4650 ::mutexUnlock(&sockFnTableMutex);
4766 } 4651 }
4767 4652
4768 struct hostent* os::get_host_by_name(char* name) { 4653 struct hostent* os::get_host_by_name(char* name) {
4769 if (!sockfnptrs_initialized) { 4654 if (!sock_initialized) {
4770 initSockFnTable(); 4655 initSock();
4771 } 4656 }
4772 4657 if (!os::WinSock2Dll::WinSock2Available()) {
4773 assert(sockfnptrs_initialized == TRUE && get_host_by_name_fn != NULL, 4658 return NULL;
4774 "sockfnptrs is not initialized or pointer to gethostbyname function is NULL"); 4659 }
4775 return (*get_host_by_name_fn)(name); 4660 return (struct hostent*)os::WinSock2Dll::gethostbyname(name);
4776 } 4661 }
4777 4662
4778 4663
4779 int os::socket_close(int fd) { 4664 int os::socket_close(int fd) {
4780 ShouldNotReachHere(); 4665 ShouldNotReachHere();
4867 int os::set_sock_opt(int fd, int level, int optname, 4752 int os::set_sock_opt(int fd, int level, int optname,
4868 const char *optval, int optlen) { 4753 const char *optval, int optlen) {
4869 ShouldNotReachHere(); 4754 ShouldNotReachHere();
4870 return 0; 4755 return 0;
4871 } 4756 }
4757
4758
4759 // Kernel32 API
4760 typedef SIZE_T (WINAPI* GetLargePageMinimum_Fn)(void);
4761 GetLargePageMinimum_Fn os::Kernel32Dll::_GetLargePageMinimum = NULL;
4762 BOOL os::Kernel32Dll::initialized = FALSE;
4763 SIZE_T os::Kernel32Dll::GetLargePageMinimum() {
4764 assert(initialized && _GetLargePageMinimum != NULL,
4765 "GetLargePageMinimumAvailable() not yet called");
4766 return _GetLargePageMinimum();
4767 }
4768
4769 BOOL os::Kernel32Dll::GetLargePageMinimumAvailable() {
4770 if (!initialized) {
4771 initialize();
4772 }
4773 return _GetLargePageMinimum != NULL;
4774 }
4775
4776
4777 #ifndef JDK6_OR_EARLIER
4778
4779 void os::Kernel32Dll::initialize() {
4780 if (!initialized) {
4781 HMODULE handle = ::GetModuleHandle("Kernel32.dll");
4782 assert(handle != NULL, "Just check");
4783 _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
4784 initialized = TRUE;
4785 }
4786 }
4787
4788
4789 // Kernel32 API
4790 inline BOOL os::Kernel32Dll::SwitchToThread() {
4791 return ::SwitchToThread();
4792 }
4793
4794 inline BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
4795 return true;
4796 }
4797
4798 // Help tools
4799 inline BOOL os::Kernel32Dll::HelpToolsAvailable() {
4800 return true;
4801 }
4802
4803 inline HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,DWORD th32ProcessId) {
4804 return ::CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
4805 }
4806
4807 inline BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4808 return ::Module32First(hSnapshot, lpme);
4809 }
4810
4811 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4812 return ::Module32Next(hSnapshot, lpme);
4813 }
4814
4815
4816 inline BOOL os::Kernel32Dll::GetNativeSystemInfoAvailable() {
4817 return true;
4818 }
4819
4820 inline void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
4821 ::GetNativeSystemInfo(lpSystemInfo);
4822 }
4823
4824 // PSAPI API
4825 inline BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess, HMODULE *lpModule, DWORD cb, LPDWORD lpcbNeeded) {
4826 return ::EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
4827 }
4828
4829 inline DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize) {
4830 return ::GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
4831 }
4832
4833 inline BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb) {
4834 return ::GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
4835 }
4836
4837 inline BOOL os::PSApiDll::PSApiAvailable() {
4838 return true;
4839 }
4840
4841
4842 // WinSock2 API
4843 inline BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData) {
4844 return ::WSAStartup(wVersionRequested, lpWSAData);
4845 }
4846
4847 inline struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
4848 return ::gethostbyname(name);
4849 }
4850
4851 inline BOOL os::WinSock2Dll::WinSock2Available() {
4852 return true;
4853 }
4854
4855 // Advapi API
4856 inline BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
4857 BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength,
4858 PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength) {
4859 return ::AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
4860 BufferLength, PreviousState, ReturnLength);
4861 }
4862
4863 inline BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess,
4864 PHANDLE TokenHandle) {
4865 return ::OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
4866 }
4867
4868 inline BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName, LPCTSTR lpName, PLUID lpLuid) {
4869 return ::LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
4870 }
4871
4872 inline BOOL os::Advapi32Dll::AdvapiAvailable() {
4873 return true;
4874 }
4875
4876 #else
4877 // Kernel32 API
4878 typedef BOOL (WINAPI* SwitchToThread_Fn)(void);
4879 typedef HANDLE (WINAPI* CreateToolhelp32Snapshot_Fn)(DWORD,DWORD);
4880 typedef BOOL (WINAPI* Module32First_Fn)(HANDLE,LPMODULEENTRY32);
4881 typedef BOOL (WINAPI* Module32Next_Fn)(HANDLE,LPMODULEENTRY32);
4882 typedef void (WINAPI* GetNativeSystemInfo_Fn)(LPSYSTEM_INFO);
4883
4884 SwitchToThread_Fn os::Kernel32Dll::_SwitchToThread = NULL;
4885 CreateToolhelp32Snapshot_Fn os::Kernel32Dll::_CreateToolhelp32Snapshot = NULL;
4886 Module32First_Fn os::Kernel32Dll::_Module32First = NULL;
4887 Module32Next_Fn os::Kernel32Dll::_Module32Next = NULL;
4888 GetNativeSystemInfo_Fn os::Kernel32Dll::_GetNativeSystemInfo = NULL;
4889
4890 void os::Kernel32Dll::initialize() {
4891 if (!initialized) {
4892 HMODULE handle = ::GetModuleHandle("Kernel32.dll");
4893 assert(handle != NULL, "Just check");
4894
4895 _SwitchToThread = (SwitchToThread_Fn)::GetProcAddress(handle, "SwitchToThread");
4896 _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
4897 _CreateToolhelp32Snapshot = (CreateToolhelp32Snapshot_Fn)
4898 ::GetProcAddress(handle, "CreateToolhelp32Snapshot");
4899 _Module32First = (Module32First_Fn)::GetProcAddress(handle, "Module32First");
4900 _Module32Next = (Module32Next_Fn)::GetProcAddress(handle, "Module32Next");
4901 _GetNativeSystemInfo = (GetNativeSystemInfo_Fn)::GetProcAddress(handle, "GetNativeSystemInfo");
4902
4903 initialized = TRUE;
4904 }
4905 }
4906
4907 BOOL os::Kernel32Dll::SwitchToThread() {
4908 assert(initialized && _SwitchToThread != NULL,
4909 "SwitchToThreadAvailable() not yet called");
4910 return _SwitchToThread();
4911 }
4912
4913
4914 BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
4915 if (!initialized) {
4916 initialize();
4917 }
4918 return _SwitchToThread != NULL;
4919 }
4920
4921 // Help tools
4922 BOOL os::Kernel32Dll::HelpToolsAvailable() {
4923 if (!initialized) {
4924 initialize();
4925 }
4926 return _CreateToolhelp32Snapshot != NULL &&
4927 _Module32First != NULL &&
4928 _Module32Next != NULL;
4929 }
4930
4931 HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,DWORD th32ProcessId) {
4932 assert(initialized && _CreateToolhelp32Snapshot != NULL,
4933 "HelpToolsAvailable() not yet called");
4934
4935 return _CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
4936 }
4937
4938 BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4939 assert(initialized && _Module32First != NULL,
4940 "HelpToolsAvailable() not yet called");
4941
4942 return _Module32First(hSnapshot, lpme);
4943 }
4944
4945 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4946 assert(initialized && _Module32Next != NULL,
4947 "HelpToolsAvailable() not yet called");
4948
4949 return _Module32Next(hSnapshot, lpme);
4950 }
4951
4952
4953 BOOL os::Kernel32Dll::GetNativeSystemInfoAvailable() {
4954 if (!initialized) {
4955 initialize();
4956 }
4957 return _GetNativeSystemInfo != NULL;
4958 }
4959
4960 void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
4961 assert(initialized && _GetNativeSystemInfo != NULL,
4962 "GetNativeSystemInfoAvailable() not yet called");
4963
4964 _GetNativeSystemInfo(lpSystemInfo);
4965 }
4966
4967 // PSAPI API
4968
4969
4970 typedef BOOL (WINAPI *EnumProcessModules_Fn)(HANDLE, HMODULE *, DWORD, LPDWORD);
4971 typedef BOOL (WINAPI *GetModuleFileNameEx_Fn)(HANDLE, HMODULE, LPTSTR, DWORD);;
4972 typedef BOOL (WINAPI *GetModuleInformation_Fn)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
4973
4974 EnumProcessModules_Fn os::PSApiDll::_EnumProcessModules = NULL;
4975 GetModuleFileNameEx_Fn os::PSApiDll::_GetModuleFileNameEx = NULL;
4976 GetModuleInformation_Fn os::PSApiDll::_GetModuleInformation = NULL;
4977 BOOL os::PSApiDll::initialized = FALSE;
4978
4979 void os::PSApiDll::initialize() {
4980 if (!initialized) {
4981 HMODULE handle = os::win32::load_Windows_dll("PSAPI.DLL", NULL, 0);
4982 if (handle != NULL) {
4983 _EnumProcessModules = (EnumProcessModules_Fn)::GetProcAddress(handle,
4984 "EnumProcessModules");
4985 _GetModuleFileNameEx = (GetModuleFileNameEx_Fn)::GetProcAddress(handle,
4986 "GetModuleFileNameExA");
4987 _GetModuleInformation = (GetModuleInformation_Fn)::GetProcAddress(handle,
4988 "GetModuleInformation");
4989 }
4990 initialized = TRUE;
4991 }
4992 }
4993
4994
4995
4996 BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess, HMODULE *lpModule, DWORD cb, LPDWORD lpcbNeeded) {
4997 assert(initialized && _EnumProcessModules != NULL,
4998 "PSApiAvailable() not yet called");
4999 return _EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
5000 }
5001
5002 DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize) {
5003 assert(initialized && _GetModuleFileNameEx != NULL,
5004 "PSApiAvailable() not yet called");
5005 return _GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
5006 }
5007
5008 BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb) {
5009 assert(initialized && _GetModuleInformation != NULL,
5010 "PSApiAvailable() not yet called");
5011 return _GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
5012 }
5013
5014 BOOL os::PSApiDll::PSApiAvailable() {
5015 if (!initialized) {
5016 initialize();
5017 }
5018 return _EnumProcessModules != NULL &&
5019 _GetModuleFileNameEx != NULL &&
5020 _GetModuleInformation != NULL;
5021 }
5022
5023
5024 // WinSock2 API
5025 typedef int (PASCAL FAR* WSAStartup_Fn)(WORD, LPWSADATA);
5026 typedef struct hostent *(PASCAL FAR *gethostbyname_Fn)(...);
5027
5028 WSAStartup_Fn os::WinSock2Dll::_WSAStartup = NULL;
5029 gethostbyname_Fn os::WinSock2Dll::_gethostbyname = NULL;
5030 BOOL os::WinSock2Dll::initialized = FALSE;
5031
5032 void os::WinSock2Dll::initialize() {
5033 if (!initialized) {
5034 HMODULE handle = os::win32::load_Windows_dll("ws2_32.dll", NULL, 0);
5035 if (handle != NULL) {
5036 _WSAStartup = (WSAStartup_Fn)::GetProcAddress(handle, "WSAStartup");
5037 _gethostbyname = (gethostbyname_Fn)::GetProcAddress(handle, "gethostbyname");
5038 }
5039 initialized = TRUE;
5040 }
5041 }
5042
5043
5044 BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData) {
5045 assert(initialized && _WSAStartup != NULL,
5046 "WinSock2Available() not yet called");
5047 return _WSAStartup(wVersionRequested, lpWSAData);
5048 }
5049
5050 struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
5051 assert(initialized && _gethostbyname != NULL,
5052 "WinSock2Available() not yet called");
5053 return _gethostbyname(name);
5054 }
5055
5056 BOOL os::WinSock2Dll::WinSock2Available() {
5057 if (!initialized) {
5058 initialize();
5059 }
5060 return _WSAStartup != NULL &&
5061 _gethostbyname != NULL;
5062 }
5063
5064 typedef BOOL (WINAPI *AdjustTokenPrivileges_Fn)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
5065 typedef BOOL (WINAPI *OpenProcessToken_Fn)(HANDLE, DWORD, PHANDLE);
5066 typedef BOOL (WINAPI *LookupPrivilegeValue_Fn)(LPCTSTR, LPCTSTR, PLUID);
5067
5068 AdjustTokenPrivileges_Fn os::Advapi32Dll::_AdjustTokenPrivileges = NULL;
5069 OpenProcessToken_Fn os::Advapi32Dll::_OpenProcessToken = NULL;
5070 LookupPrivilegeValue_Fn os::Advapi32Dll::_LookupPrivilegeValue = NULL;
5071 BOOL os::Advapi32Dll::initialized = FALSE;
5072
5073 void os::Advapi32Dll::initialize() {
5074 if (!initialized) {
5075 HMODULE handle = os::win32::load_Windows_dll("advapi32.dll", NULL, 0);
5076 if (handle != NULL) {
5077 _AdjustTokenPrivileges = (AdjustTokenPrivileges_Fn)::GetProcAddress(handle,
5078 "AdjustTokenPrivileges");
5079 _OpenProcessToken = (OpenProcessToken_Fn)::GetProcAddress(handle,
5080 "OpenProcessToken");
5081 _LookupPrivilegeValue = (LookupPrivilegeValue_Fn)::GetProcAddress(handle,
5082 "LookupPrivilegeValue");
5083 }
5084 initialized = TRUE;
5085 }
5086 }
5087
5088 BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
5089 BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength,
5090 PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength) {
5091 assert(initialized && _AdjustTokenPrivileges != NULL,
5092 "AdvapiAvailable() not yet called");
5093 return _AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
5094 BufferLength, PreviousState, ReturnLength);
5095 }
5096
5097 BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess,
5098 PHANDLE TokenHandle) {
5099 assert(initialized && _OpenProcessToken != NULL,
5100 "AdvapiAvailable() not yet called");
5101 return _OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
5102 }
5103
5104 BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName, LPCTSTR lpName, PLUID lpLuid) {
5105 assert(initialized && _LookupPrivilegeValue != NULL,
5106 "AdvapiAvailable() not yet called");
5107 return _LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
5108 }
5109
5110 BOOL os::Advapi32Dll::AdvapiAvailable() {
5111 if (!initialized) {
5112 initialize();
5113 }
5114 return _AdjustTokenPrivileges != NULL &&
5115 _OpenProcessToken != NULL &&
5116 _LookupPrivilegeValue != NULL;
5117 }
5118
5119 #endif
5120