This is an archive of the discontinued LLVM Phabricator instance.

[opt] Fix run-twice crash and detection problem
ClosedPublic

Authored by Kokan on Dec 28 2019, 1:03 PM.

Details

Summary
   
1. As reported in the original issue[1], simply execute the `opt -run-twice` with
   any LLVM IR and it is going to crash. For details see the isssue.
2. The `-run-twice` compares the produced output that it saves into two buffer.
   When outputing the result is disabled, that produces two empty string thus
   they are going to be equal all the time resulting false-positive results.

The proposed solution is to generate the results even if the output will not be
emitted, as that is required for the comparision.

[1] https://bugs.llvm.org/show_bug.cgi?id=44382

Diff Detail

Event Timeline

Kokan created this revision.Dec 28 2019, 1:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 28 2019, 1:03 PM
Kokan retitled this revision from [opt] run-twice crash to [opt] Fix run-twice crash and detection problem.Dec 28 2019, 1:10 PM
Kokan edited the summary of this revision. (Show Details)
Kokan added a comment.Dec 28 2019, 2:55 PM

Tests?

Hello, @xbolva00

Thanks for checkout out my patch, and sorry for the lack of test or explanation beforehand.

Regarding the tests I seek some support. I was thinking about the following test scenarios, but failed to find any of the proper here:

  1. unittests: With proper refactoring of the opt that should be doable, although that would be out of the scope of this current fix.
  2. test-suit[1]: The issue only happens when the output file is a TTY, a bitcode format is requested and -run-twice is used. I could not trigger the failure with llvm-lit test. Please advice.
  3. Creating a negative test for -run-twice, as every Pass should behave the same way after executing multiple time, there is not test that confirms that -run-twice actually could catch different results (just like it does not work now with bitcode format), there could be a dummy Pass created that in fact does reproduce different results with each execution in order to test this.

Could you or anyone please help with it ?

[1] I am referring to those tests that are executed via llvm-lit

Tests?

Hello, @xbolva00

Thanks for checkout out my patch, and sorry for the lack of test or explanation beforehand.

Regarding the tests I seek some support. I was thinking about the following test scenarios, but failed to find any of the proper here:

  1. unittests: With proper refactoring of the opt that should be doable, although that would be out of the scope of this current fix.
  2. test-suit[1]: The issue only happens when the output file is a TTY, a bitcode format is requested and -run-twice is used. I could not trigger the failure with llvm-lit test. Please advice.
  3. Creating a negative test for -run-twice, as every Pass should behave the same way after executing multiple time, there is not test that confirms that -run-twice actually could catch different results (just like it does not work now with bitcode format), there could be a dummy Pass created that in fact does reproduce different results with each execution in order to test this.

Could you or anyone please help with it ?

[1] I am referring to those tests that are executed via llvm-lit

Due to

if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
  if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
    NoOutput = true;

This is difficult to test. The bug relies on stdout being a pty.

I think it may be worth defining a variable for !NoOutput && !AnalyzeOnly, because the pattern occurs multiple times.

Kokan added a comment.Dec 29 2019, 1:45 AM

Tests?

Hello, @xbolva00

Thanks for checkout out my patch, and sorry for the lack of test or explanation beforehand.

Regarding the tests I seek some support. I was thinking about the following test scenarios, but failed to find any of the proper here:

  1. unittests: With proper refactoring of the opt that should be doable, although that would be out of the scope of this current fix.
  2. test-suit[1]: The issue only happens when the output file is a TTY, a bitcode format is requested and -run-twice is used. I could not trigger the failure with llvm-lit test. Please advice.
  3. Creating a negative test for -run-twice, as every Pass should behave the same way after executing multiple time, there is not test that confirms that -run-twice actually could catch different results (just like it does not work now with bitcode format), there could be a dummy Pass created that in fact does reproduce different results with each execution in order to test this.

Could you or anyone please help with it ?

[1] I am referring to those tests that are executed via llvm-lit

Due to

if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
  if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
    NoOutput = true;

This is difficult to test. The bug relies on stdout being a pty.

I think it may be worth defining a variable for !NoOutput && !AnalyzeOnly, because the pattern occurs multiple times.

Sure thing, but that does not help much. While checking the issue I spent some time to see the originalish version of opt. The lack of proper abstraction lead to this condition jungle that is very much error prone. IMHO a proper refactor is needed here, depending on my time I plan to do it in a separate patchset.

Kokan updated this revision to Diff 235505.Dec 29 2019, 1:47 AM
aqjune added a subscriber: aqjune.Dec 29 2019, 12:01 PM
MaskRay accepted this revision.Dec 29 2019, 5:35 PM

I have verified the logic is correct.

This revision is now accepted and ready to land.Dec 29 2019, 5:35 PM

I do not have commit rights to the repository, someone please commit this change.

I do not have commit rights to the repository, someone please commit this change.

@Kokan git commit --amend --author='what-is-your-<email@address>' ?

Kokan added a comment.EditedDec 30 2019, 12:27 AM

I do not have commit rights to the repository, someone please commit this change.

@Kokan git commit --amend --author='what-is-your-<email@address>' ?

It is Peter Kokai <kokaipeter@gmail.com>

This revision was automatically updated to reflect the committed changes.