<div dir="ltr">Mind testing <a href="https://github.com/openjdk/jdk/pull/14531">https://github.com/openjdk/jdk/pull/14531</a> ?</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Jun 17, 2023 at 8:20 PM Thomas Stüfe <<a href="mailto:thomas.stuefe@gmail.com">thomas.stuefe@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Daniel,<div><br></div><div>thank you for that thorough analysis. I opened <a href="https://bugs.openjdk.org/browse/JDK-8310265" target="_blank">https://bugs.openjdk.org/browse/JDK-8310265</a> to track this issue.</div><div><br></div><div>Looking at the code, this should be easy to fix, we would not even have to fix jspawnhelper itself, just its spawn point.</div><div><br></div><div>I did a small patch which seemed to work. But this code is notorious for being tricky and I may overlooking something. Lets see.</div><div><br></div><div>Cheers, Thomas</div><div><br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Jun 17, 2023 at 6:33 AM Daniel Jarabek <<a href="mailto:jarabekit@gmail.com" target="_blank">jarabekit@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
While trying to run jshell in qemu-user-static using binfmt_misc <br>
(specifically, using this docker based tool [1]), I ran into an issue. <br>
Jshell would fail to start and print `This command is not for general <br>
use and should only be run as the result of a call to <br>
ProcessBuilder.start() or Runtime.exec() in a java application` along <br>
with a stack trace including `Failed to exec spawn helper`. After <br>
searching this error, I found JDK-8296001 [2]. This issue presented a <br>
very similar scenario (using a userspace architecture emulator). I <br>
looked into this issue and I believe I have identified the root cause.<br>
<br>
My specific investigation was done around jdk17, but this issue also <br>
occurs on jdk20 and older jdk versions (though it seems like the "spawn" <br>
strategy is not used by default on linux with jdk 8 and 11).<br>
<br>
Note: I use argv0 to refer to "argv[0]" to prevent confusion with footnotes.<br>
<br>
Traditionally, argv0 contains the name of the program being executed. <br>
While this is more a convention than anything [3], it is very widely <br>
used. jspawnhelper [4] is an executable used to assist in launching <br>
processes via ProcessBuilder and Runtime.exec. Unlike the convention, <br>
jspawnhelper uses argv0 for passing data [5]. When binfmts_misc [6] <br>
executes an interpreter, it overwrites argv0, unless a flag is set. This <br>
causes jspawnhelper to fail to parse argv0 (since it's the application <br>
path, and not the data sent by the JDK), so it prints the warning about <br>
not being for general use. By setting the binfmt_misc `P` <br>
(preserve-argv0) flag, it preserves argv0 which fixes this problem.<br>
<br>
Since qemu-user correctly supports this flag, all I had to do to fix the <br>
issue in my case was run qemu-binfmt-conf.sh with `--preserve-argv0 yes` <br>
to set that flag. It's likely that in the case of the above issue, the <br>
same problem is occurring and it may have a similar resolution. This <br>
could be fixed in OpenJDK by using argv1 instead, but at least in my <br>
case of binfmts_misc, this is an external configuration issue that can <br>
be easily fixed. However, I would appreciate it if someone with <br>
permission could comment on the above issue with a link to this email <br>
for anyone who runs into this in the future.<br>
<br>
-DJ<br>
<br>
[1] <a href="https://github.com/multiarch/qemu-user-static" rel="noreferrer" target="_blank">https://github.com/multiarch/qemu-user-static</a><br>
[2] <a href="https://bugs.openjdk.org/browse/JDK-8296001" rel="noreferrer" target="_blank">https://bugs.openjdk.org/browse/JDK-8296001</a><br>
[3] <a href="https://stackoverflow.com/a/2051031" rel="noreferrer" target="_blank">https://stackoverflow.com/a/2051031</a><br>
[4] <br>
<a href="https://github.com/openjdk/jdk/blob/master/src/java.base/unix/native/jspawnhelper/jspawnhelper.c" rel="noreferrer" target="_blank">https://github.com/openjdk/jdk/blob/master/src/java.base/unix/native/jspawnhelper/jspawnhelper.c</a><br>
[5] <br>
<a href="https://github.com/openjdk/jdk/blob/bcc4d36857b0907e865d0afc4447f9b0780f8101/src/java.base/unix/native/libjava/ProcessImpl_md.c#L498-L500" rel="noreferrer" target="_blank">https://github.com/openjdk/jdk/blob/bcc4d36857b0907e865d0afc4447f9b0780f8101/src/java.base/unix/native/libjava/ProcessImpl_md.c#L498-L500</a><br>
[6] <a href="https://docs.kernel.org/admin-guide/binfmt-misc.html" rel="noreferrer" target="_blank">https://docs.kernel.org/admin-guide/binfmt-misc.html</a><br>
</blockquote></div>
</blockquote></div>