Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8268230

Foreign Linker API & Windows user32/kernel32: String conversion seems broken

    XMLWordPrintable

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      java 16.0.1 2021-04-20
      Java(TM) SE Runtime Environment (build 16.0.1+9-24)
      Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

      Microsoft Windows [Version 10.0.19042.985]

      Error appears during Maven build, when running a standalone Java app and also when running tests or app within the Eclipse IDE.

      A DESCRIPTION OF THE PROBLEM :
      Hi there,

      I recently experimented with the new FLA subsystem. I encountered random situations where strings converted from Java to C seem to end up "longer" than their original Java counterparts.

      I experimented with string length computation but did also encounter the issue when providing the string e. g. as a message box caption (which ends up displaying random non printable characters).

      When converting a Java string with CLinker.toCString and then passing the result (or a pointer to it) to lstrlenW, the function's result is often wrong, i. e. longer than expected, never shorter. I am pretty sure that it is not due to wrong usage, because the result of lstrlenW seems to be non deterministic, e. g. I got the following results on the string "testString":

      10, 10, 10, 10, 19, 10, 40, 10, 10, 10, 32, 10, 10, 32, 10, 16, 10

      It is almost like if the string gets right padded with some portion of uninitialized memory. Maybe array handling is of or something. Some false results appear more often than others. Also it does seem to depend on the timing, e. g. when stopping the code with a debugger, the result is almost always correct.

      However, I only encountered the problem when using Windows library functions from kernel32 or user32. When using the standard library's (LibraryLookup.ofDefault) strlen function, things are working as expected.

      I provided a demo over at GitHub: https://github.com/JanMosigItemis/flabugs

      Best regards!

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Call lstrlenW from "Kernel32" via usual FLA means.

      See the demo at: https://github.com/JanMosigItemis/flabugs

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A deterministic C-string both in content and length / lstrlenW must always yield the same (correct) length of a string.
      ACTUAL -
      C-strings seem to be right padded with random characters / lstrlenW yields random results >= correct string length

      ---------- BEGIN SOURCE ----------
      See the demo at: https://github.com/JanMosigItemis/flabugs
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Currently unknown

      FREQUENCY : often


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jvernee Jorn Vernee
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: