Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-33359] Fix image name argument parsing when combined with -m option #3622

Merged
merged 2 commits into from
Sep 1, 2021

Conversation

ivan-ristovic
Copy link
Contributor

@ivan-ristovic ivan-ristovic commented Jul 23, 2021

Summary

This PR adresses two problems related to native-image argument handling. Namely, it:

  • fixes NullPointerException in cases when a class could not be found on the provided module path
  • modifies argument handling so that when image name is combined with --module argument, the image name is not mistakenly interpreted as a class name

Details

Main class resolving

Currently, our main class resolving is done via:

There is a catch block present for ClassNotFoundException, however, looking at the call chain, it is possible that control will reach java.lang.Class.forName(Module, String), and by looking at the documentation for this method the return value could be null - which is currently not covered. Therefore, the following line will produce a NullPointerException:

mainEntryPoint = mainClass.getDeclaredMethod(mainEntryPointName, int.class, CCharPointerPointer.class);

Image name handling in the presence of --module option

Invoking native-image with the following arguments

$ native-image MyClass foo

will produce a binary with the name foo. However, combining this with the --module (-m for short) option, for example

$ native-image -m my.module/MyClass foo

will cause a NullPointerException in the same line as mentioned above. This is tied to class resolving which could return null, however it is a misinterpreted main class argument when -m option is present - the image name will be interpreted as an overrule for the class name.

Implementation details

This PR solves this problem by reusing the same overrule exclusion code used when -jar option is present:


As for main class resolving, a check is added to ensure that the mainClass field is not null before proceeding onto entry method lookup.

@olpaw olpaw self-assigned this Aug 27, 2021
@olpaw olpaw added this to To do in Native Image via automation Aug 27, 2021
@olpaw olpaw moved this from To do to In progress in Native Image Aug 27, 2021
@olpaw olpaw changed the title Fix image name argument parsing when combined with -m option [GR-33359] Fix image name argument parsing when combined with -m option Aug 27, 2021
@olpaw
Copy link
Member

olpaw commented Aug 30, 2021

@ivan-ristovic unfortunately these changes break

    def native_image_func(args, **kwargs):
        all_args = base_args + common_args + args
        path = query_native_image(all_args, r'^-H:Path(@[^=]*)?=')
        name = query_native_image(all_args, r'^-H:Name(@[^=]*)?=') <<< THIS
        image = join(path, name)
        _native_image(all_args, **kwargs)
        return image

in substratevm/mx.substratevm/mx_substratevm.py in the context of

        built_image = native_image([
            '--verbose', '-H:Path=' + build_dir,
            '--trace-class-initialization=hello.lib.Greeter', # also test native-image-diagnostics-agent
            ] + moduletest_run_args)

in mx_substratevm.hellomodule.

There is no -H:Name=... argument emitted by the driver and so mx cannot determine the full path to the image that is going to be created anymore.

@olpaw
Copy link
Member

olpaw commented Aug 30, 2021

The reason is that now

                            if (hasMainClassModule) {
                                imageBuilderArgs.add(oH(SubstrateOptions.Name, "image-name from module-name") + mainClassModule.toLowerCase());
                            } 

is not used anymore.

@olpaw olpaw self-requested a review August 30, 2021 13:21
Copy link
Member

@olpaw olpaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments

@ivan-ristovic
Copy link
Contributor Author

@olpaw Yes, I realise now that in fact we wish to do the same as before, just skip overruling - I moved the check, I think it should be fine now.

@olpaw
Copy link
Member

olpaw commented Aug 31, 2021

Rerunning in internal CI now ...

@olpaw
Copy link
Member

olpaw commented Sep 1, 2021

PR is in merge queue

@graalvmbot graalvmbot merged commit 4631e0c into oracle:master Sep 1, 2021
Native Image automation moved this from In progress to Done Sep 1, 2021
@ivan-ristovic ivan-ristovic deleted the ni-module-arg-fix branch September 1, 2021 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Native Image
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants