RFR: 8259045: Exception message from saproc.dll is garbled on Windows with Japanese locale

Yasumasa Suenaga ysuenaga at openjdk.java.net
Tue Jan 5 02:42:58 UTC 2021


On Tue, 5 Jan 2021 02:19:37 GMT, Ioi Lam <iklam at openjdk.org> wrote:

>> I got garbled exception message as following when I run `livenmethods` CLHSDB command:
>> 
>> sun.jvm.hotspot.debugger.DebuggerException : ?w???????W
>> 
>> My Windows laptop is set Japanese Locale, garbled message was written in Japanese.
>> saproc.dll would throw exception via [ThrowNew()](https://docs.oracle.com/en/java/javase/15/docs/specs/jni/functions.html#thrownew) JNI function, but it accepts UTF-8 encoded message. However [FormatMessage()](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage) Windows API might not return UTF-8 encoded string on Japanese locale.
>> 
>> java.dll (libjava,so) provides good functions to resolve this issue. We can convert localized (non ascii) chars to UTF-8 string. I use them in this PR and remove `FormatMessage()` call from sadis.c.
>> And also I remove `-D_MBCS` from compiler option because [MBCS has been already deprecated](https://docs.microsoft.com/ja-jp/cpp/text/support-for-multibyte-character-sets-mbcss) - it does not seem to add to any other executables.
>
> Marked as reviewed by iklam (Reviewer).

@iklam Thanks for your review!

> I looked at a cases in the JDK code where `Java_sun_security_pkcs11_wrapper_PKCS11_connect()` calls `FormatMessage()`. It eventually passes the `char*` to `jni_ThrowNew`, which eventually gets to `Exceptions::new_exception` with `to_utf8_safe==safe_to_utf8`. This means the message will be converted with `java_lang_String::create_from_str()`, which does NOT call `JNU_NewStringPlatform`. So I think in this case, the error message will also be garbled.
> 
> java.base/windows/native/libnet/Inet4AddressImpl.c seems to have a similar problem in the `ping4` function.

Agree. `ping4()` calls `NET_ThrowNew()` which passes `ThrowNew()` JNI function without modifying arguments.
I looked at other cases in JDK code, following places has possible to garble:

* `throwByName()` @ jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_util.c
    * `Java_sun_security_pkcs11_Secmod_nssLoadLibrary()` @ j2secmod_md.c
* jdk.jdi/share/native/libdt_shmem/SharedMemoryTransport.c
    * `throwShmemException()` @ SharedMemoryTransport.c

> But, I don't know how to write a simple test case for the above.

Agree, we might need to fix garbled messages one by one when we find out issues.
IMHO we need to establish rule that we have to use `JNU_NewStringPlatform()` if we want to throw exception with message from platform (`FormatMessage()`, `strerror()`) from JNI.

-------------

PR: https://git.openjdk.java.net/jdk/pull/1928



More information about the build-dev mailing list