2009/7/18 Andrew Pinski <pins...@gmail.com>: > On Sat, Jul 18, 2009 at 12:22 PM, Kai Tietz<ktiet...@googlemail.com> wrote: >> Well, uintptr_t/intptr_t are available to most (but not all hosts). >> IIRC there is a gcc version of stdint.h (gstdint.h), which could be >> used here. The mode version is fine too, as long as we can assume that >> libjava isn't build by any other compiler then gcc. > > It is provided by GCC even without gstdint.h. See bug 448. Since > __UINTPTR_TYPE__ is provided to be able to use stdint.h :). > > Thanks, > Andrew PInski >
So, I was able to build libjave (using the upstream boehm-gc source as the version in gcc at the moment doesn't support x64) for x64 windows. There are a lot of issues about casting HANDLE values into jint types, which is for x86 valid, but for x64 can lead potential to pointer truncations. Those part need some review by libjava maintainers. My patch simply casts those kind of pointers via __UINTPTR_TYPE__ into integer scalar before casting it into jint. I put comments at those places, where some rework is necessary. Additionally I removed the use of srand()/rand () and just throw an exception in the Win32 specific implementation of gnu::java::security::jce::prng::VMSecureRandom::natGenerateSeed. ChangeLog 2009-07-19 Kai Tietz <kai.ti...@onevision.com> * gnu/java/security/jce/prng/natVMSecureRandomWin32.cc: New Win32 specific implementation. * nclude/win32.h (_Jv_platform_ffi_abi): Use for _WIN64 case FFI_DEFAULT_ABI. * java/lang/natString.cc (UNMASK_PTR, MASK_PTR, PTR_MASKED): Use __UINTPTR_TYPE__ instead of unsigned long for pointer casts. * gnu/java/net/natPlainDatagramSocketImplWin32.cc: Likewise. * gnu/java/net/natPlainSocketImplWin32.cc: Likwise. * gnu/java/nio/channels/natFileChannelWin32.cc: Likewise. * include/jvm.h: Likewise. * java/lang/natClass.cc: Likewise. * java/lang/natClassLoader.cc: Likewise. * java/lang/natWin32Process.cc: Likewise. * link.cc: Likewise. Regression tested for i686-pc-mingw32. Ok, for apply? Cheers, Kai -- | (\_/) This is Bunny. Copy and paste | (='.'=) Bunny into your signature to help | (")_(") him gain world domination
Index: gcc/libjava/gnu/java/security/jce/prng/natVMSecureRandomWin32.cc =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ gcc/libjava/gnu/java/security/jce/prng/natVMSecureRandomWin32.cc 2009-07-19 12:13:45.715476500 +0200 @@ -0,0 +1,32 @@ +// natVMSecureRandomPosix.cc - Native part of VMSecureRandom class for POSIX. + +/* Copyright (C) 2009 Free Software Foundation + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +#include <config.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +#include <gcj/cni.h> +#include <java/lang/InternalError.h> +#include <gnu/java/security/jce/prng/VMSecureRandom.h> + +jint +gnu::java::security::jce::prng::VMSecureRandom::natGenerateSeed(jbyteArray byte_array, jint offset, jint length) +{ + if (length != 0) + throw new ::java::lang::InternalError + (JvNewStringLatin1 ("Error function not implemented for Win32 target.")); + return 0; +} + Index: gcc/libjava/gnu/java/net/natPlainDatagramSocketImplWin32.cc =================================================================== --- gcc.orig/libjava/gnu/java/net/natPlainDatagramSocketImplWin32.cc 2009-07-19 12:06:54.197476000 +0200 +++ gcc/libjava/gnu/java/net/natPlainDatagramSocketImplWin32.cc 2009-07-19 12:13:45.721476500 +0200 @@ -75,7 +75,8 @@ // We use native_fd in place of fd here. From leaving fd null we avoid // the double close problem in FileDescriptor.finalize. - native_fd = (jint) hSocket; + // Fixme, it is pretty dangerous to cast here a HANDLE to integer scalar. + native_fd = (jint) (__UINTPTR_TYPE__) hSocket; } void Index: gcc/libjava/gnu/java/net/natPlainSocketImplWin32.cc =================================================================== --- gcc.orig/libjava/gnu/java/net/natPlainSocketImplWin32.cc 2009-07-19 12:06:54.200476000 +0200 +++ gcc/libjava/gnu/java/net/natPlainSocketImplWin32.cc 2009-07-19 12:13:45.727476500 +0200 @@ -58,7 +58,8 @@ // We use native_fd in place of fd here. From leaving fd null we avoid // the double close problem in FileDescriptor.finalize. - native_fd = (jint) hSocket; + // Fixme, it isn't correct to cast a HANDLE to integer scalar here for x64 + native_fd = (jint) (__UINTPTR_TYPE__) hSocket; } void @@ -304,7 +305,8 @@ // Cast this to a HANDLE so we can make // it non-inheritable via _Jv_platform_close_on_exec. - hSocket = (HANDLE) new_socket; + // Fixme, it isn't correct to cast a integer scalar here to HANDLE for x64 + hSocket = (HANDLE) (__UINTPTR_TYPE__) new_socket; _Jv_platform_close_on_exec (hSocket); jbyteArray raddr; @@ -325,8 +327,8 @@ #endif else throw new ::java::net::SocketException (JvNewStringUTF ("invalid family")); - - s->native_fd = (jint) hSocket; + // Fixme, it isn't correct to cast a HANDLE to integer scalar here for x64 + s->native_fd = (jint) (__UINTPTR_TYPE__) hSocket; s->localport = localport; s->address = ::java::net::InetAddress::getByAddress (raddr); s->port = rport; Index: gcc/libjava/gnu/java/nio/channels/natFileChannelWin32.cc =================================================================== --- gcc.orig/libjava/gnu/java/nio/channels/natFileChannelWin32.cc 2009-07-19 12:06:54.205476000 +0200 +++ gcc/libjava/gnu/java/nio/channels/natFileChannelWin32.cc 2009-07-19 12:13:45.731476500 +0200 @@ -66,11 +66,14 @@ void FileChannelImpl::init(void) { - in = new FileChannelImpl((jint)(GetStdHandle (STD_INPUT_HANDLE)), + // Fixme: it isn't correct to cast HANDLE to lower precission integer scalar for x64 + in = new FileChannelImpl((jint)(__UINTPTR_TYPE__)(GetStdHandle (STD_INPUT_HANDLE)), FileChannelImpl::READ); - out = new FileChannelImpl((jint)(GetStdHandle (STD_OUTPUT_HANDLE)), + // Fixme: it isn't correct to cast HANDLE to lower precission integer scalar for x64 + out = new FileChannelImpl((jint)(__UINTPTR_TYPE__)(GetStdHandle (STD_OUTPUT_HANDLE)), FileChannelImpl::WRITE); - err = new FileChannelImpl((jint)(GetStdHandle (STD_ERROR_HANDLE)), + // Fixme: it isn't correct to cast HANDLE to lower precission integer scalar for x64 + err = new FileChannelImpl((jint)(__UINTPTR_TYPE__)(GetStdHandle (STD_ERROR_HANDLE)), FileChannelImpl::WRITE); } @@ -144,7 +147,8 @@ // closing this file. _Jv_platform_close_on_exec (handle); - return (jint) handle; + // Fixme: it isn't correct to cast HANDLE to lower precission integer scalar for x64 + return (jint) (__UINTPTR_TYPE__) handle; } void @@ -197,8 +201,9 @@ void FileChannelImpl::implCloseChannel (void) { - HANDLE save = (HANDLE)fd; - fd = (jint)INVALID_HANDLE_VALUE; + HANDLE save = (HANDLE) (__UINTPTR_TYPE__) fd; + // Fixme: it isn't correct to cast HANDLE to lower precission integer scalar for x64 + fd = (jint) (__UINTPTR_TYPE__) INVALID_HANDLE_VALUE; if (! CloseHandle (save)) _Jv_ThrowIOException (); } Index: gcc/libjava/include/jvm.h =================================================================== --- gcc.orig/libjava/include/jvm.h 2009-07-19 12:06:54.215476000 +0200 +++ gcc/libjava/include/jvm.h 2009-07-19 12:13:45.737476500 +0200 @@ -481,7 +481,7 @@ // This was chosen to yield relatively well distributed results on // both 32- and 64-bit architectures. Note 0x7fffffff is prime. // FIXME: we assume sizeof(long) == sizeof(void *). - return (jint) ((unsigned long) obj % 0x7fffffff); + return (jint) ((__UINTPTR_TYPE__) obj % 0x7fffffff); } // Return a raw pointer to the elements of an array given the array Index: gcc/libjava/include/win32.h =================================================================== --- gcc.orig/libjava/include/win32.h 2009-07-19 12:06:54.219476000 +0200 +++ gcc/libjava/include/win32.h 2009-07-19 12:13:45.743476500 +0200 @@ -93,7 +93,12 @@ // Type of libffi ABI used by JNICALL methods. NOTE: This must agree // with the JNICALL definition in jni.h +#ifdef _WIN64 +// For x64 Windows there is no stdcall specific call. +#define _Jv_platform_ffi_abi FFI_DEFAULT_ABI +#else #define _Jv_platform_ffi_abi FFI_STDCALL +#endif /* Useful helper classes and methods. */ Index: gcc/libjava/java/lang/natClass.cc =================================================================== --- gcc.orig/libjava/java/lang/natClass.cc 2009-07-19 12:06:54.228476000 +0200 +++ gcc/libjava/java/lang/natClass.cc 2009-07-19 12:13:45.748476500 +0200 @@ -732,7 +732,7 @@ // Step 2. java::lang::Thread *self = java::lang::Thread::currentThread(); - self = (java::lang::Thread *) ((long) self | 1); + self = (java::lang::Thread *) ((__UINTPTR_TYPE__) self | 1); while (state == JV_STATE_IN_PROGRESS && thread && thread != self) wait (); Index: gcc/libjava/java/lang/natClassLoader.cc =================================================================== --- gcc.orig/libjava/java/lang/natClassLoader.cc 2009-07-19 12:06:54.232476000 +0200 +++ gcc/libjava/java/lang/natClassLoader.cc 2009-07-19 12:13:45.754476500 +0200 @@ -290,7 +290,7 @@ { jclass klass = *classes; - _Jv_CheckABIVersion ((unsigned long) klass->next_or_version); + _Jv_CheckABIVersion ((__UINTPTR_TYPE__) klass->next_or_version); (*_Jv_RegisterClassHook) (klass); } } @@ -307,7 +307,7 @@ { jclass klass = classes[i]; - _Jv_CheckABIVersion ((unsigned long) klass->next_or_version); + _Jv_CheckABIVersion ((__UINTPTR_TYPE__) klass->next_or_version); (*_Jv_RegisterClassHook) (klass); } } @@ -317,7 +317,7 @@ _Jv_NewClassFromInitializer (const char *class_initializer) { const unsigned long version - = ((unsigned long) + = ((__UINTPTR_TYPE__) ((::java::lang::Class *)class_initializer)->next_or_version); _Jv_CheckABIVersion (version); @@ -391,7 +391,7 @@ if (system_class_list != SYSTEM_LOADER_INITIALIZED) { - unsigned long abi = (unsigned long) klass->next_or_version; + unsigned long abi = (__UINTPTR_TYPE__) klass->next_or_version; if (! _Jv_ClassForBootstrapLoader (abi)) { klass->next_or_version = system_class_list; Index: gcc/libjava/java/lang/natString.cc =================================================================== --- gcc.orig/libjava/java/lang/natString.cc 2009-07-19 12:06:54.236476000 +0200 +++ gcc/libjava/java/lang/natString.cc 2009-07-19 12:13:45.758476500 +0200 @@ -50,9 +50,9 @@ #define DELETED_STRING ((jstring)(~0)) #define SET_STRING_IS_INTERNED(STR) /* nothing */ -#define UNMASK_PTR(Ptr) (((unsigned long) (Ptr)) & ~0x01) -#define MASK_PTR(Ptr) (((unsigned long) (Ptr)) | 0x01) -#define PTR_MASKED(Ptr) (((unsigned long) (Ptr)) & 0x01) +#define UNMASK_PTR(Ptr) (((__UINTPTR_TYPE__) (Ptr)) & ~0x01) +#define MASK_PTR(Ptr) (((__UINTPTR_TYPE__) (Ptr)) | 0x01) +#define PTR_MASKED(Ptr) (((__UINTPTR_TYPE__) (Ptr)) & 0x01) /* Find a slot where the string with elements DATA, length LEN, and hash HASH should go in the strhash table of interned strings. */ Index: gcc/libjava/java/lang/natWin32Process.cc =================================================================== --- gcc.orig/libjava/java/lang/natWin32Process.cc 2009-07-19 12:06:54.240476000 +0200 +++ gcc/libjava/java/lang/natWin32Process.cc 2009-07-19 12:13:45.762476500 +0200 @@ -59,7 +59,7 @@ if (procHandle) { CloseHandle((HANDLE) procHandle); - procHandle = (jint) INVALID_HANDLE_VALUE; + procHandle = (jint) (__UINTPTR_TYPE__) INVALID_HANDLE_VALUE; } } @@ -221,7 +221,7 @@ { using namespace java::io; - procHandle = (jint) INVALID_HANDLE_VALUE; + procHandle = (jint) (__UINTPTR_TYPE__) INVALID_HANDLE_VALUE; // Reconstruct the command line. jstring *elts = elements (progarray); @@ -293,16 +293,16 @@ : ChildProcessPipe::OUTPUT); outputStream = new FileOutputStream (new FileChannelImpl ( - (jint) aChildStdIn.getParentHandle (), + (jint) (__UINTPTR_TYPE__) aChildStdIn.getParentHandle (), FileChannelImpl::WRITE)); inputStream = new FileInputStream (new FileChannelImpl ( - (jint) aChildStdOut.getParentHandle (), + (jint) (__UINTPTR_TYPE__) aChildStdOut.getParentHandle (), FileChannelImpl::READ)); if (redirect) errorStream = Win32Process$EOFInputStream::instance; else errorStream = new FileInputStream (new FileChannelImpl ( - (jint) aChildStdErr.getParentHandle (), + (jint) (__UINTPTR_TYPE__) aChildStdErr.getParentHandle (), FileChannelImpl::READ)); // Now create the child process. @@ -343,7 +343,7 @@ _Jv_WinStrError (_T("Error creating child process"), dwErrorCode)); } - procHandle = (jint ) pi.hProcess; + procHandle = (jint ) (__UINTPTR_TYPE__) pi.hProcess; _Jv_Free (cmdLine); if (env != NULL) Index: gcc/libjava/link.cc =================================================================== --- gcc.orig/libjava/link.cc 2009-07-19 12:06:54.244476000 +0200 +++ gcc/libjava/link.cc 2009-07-19 12:13:45.767476500 +0200 @@ -1408,7 +1408,7 @@ (const char*)signature->chars()); fprintf (stderr, " [%d] = offset %d\n", index + 1, - (int)(unsigned long)klass->itable->addresses[index * 2 + 1]); + (int)(__UINTPTR_TYPE__)klass->itable->addresses[index * 2 + 1]); } }