Skip to content

[JIT] Fix crash in unit tests #113492

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

Merged
merged 1 commit into from
Oct 28, 2024
Merged

Conversation

redstar
Copy link
Member

@redstar redstar commented Oct 23, 2024

The unit tests ReOptimizeLayerTest.BasicReOptimization and JITLinkRedirectionManagerTest.BasicRedirectionOperation are failing for me with the error:

Program aborted due to an unhandled Error:
Error value was Success. (Note: Success values must still be checked prior to being destroyed).

The error is raised when a value is assigned to Err, due to the the missing ErrorAsOutParameter.

Fixing this problem uncovered another problem: if AnonymousPtrCreator or PtrJumpStubCreator is an error, then the object is not registered at the resource manager. However, the destructor tries to de-register the object. This fails then with

Assertion `I != ResourceManagers.end() && "RM not registered"'

Fix is to always register the object at the resource manager.

@redstar redstar requested a review from sunho October 23, 2024 20:15
@redstar redstar self-assigned this Oct 23, 2024
Comment on lines 62 to 67
ErrorAsOutParameter _(&Err);
ObjLinkingLayer.getExecutionSession().registerResourceManager(*this);
if (!AnonymousPtrCreator || !PtrJumpStubCreator)
Err = make_error<StringError>("Architecture not supported",
inconvertibleErrorCode());
if (Err)
return;
ObjLinkingLayer.getExecutionSession().registerResourceManager(*this);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to move the

ObjLinkingLayer.getExecutionSession().registerResourceManager(*this);

line up here, or can that stay where it was?

Copy link
Member Author

Choose a reason for hiding this comment

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

Without moving the line up, no resource manager is registered, and I get an assertion in the destructor:

  ~JITLinkRedirectableSymbolManager() {
    ObjLinkingLayer.getExecutionSession().deregisterResourceManager(*this);
  }

because of the check in the else branch:

void ExecutionSession::deregisterResourceManager(ResourceManager &RM) {
  runSessionLocked([&] {
    assert(!ResourceManagers.empty() && "No managers registered");
    if (ResourceManagers.back() == &RM)
      ResourceManagers.pop_back();
    else {
      auto I = llvm::find(ResourceManagers, &RM);
      assert(I != ResourceManagers.end() && "RM not registered");
      ResourceManagers.erase(I);
    }
  });
}

So yes, moving it up is necessary. And the if (Err) return; is then meaningless.
I'm running with assertions enabled, btw.

@lhames
Copy link
Contributor

lhames commented Oct 24, 2024

Thanks for answering those questions.

I think there's an even easier solution here: We can hoist the creator functions up into the named constructor and then pass them into the regular constructor, which would no longer be able to error out:

static Expected<std::unique_ptr<RedirectableSymbolManager>>
Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &JD) {
  auto AnonymousPtrCreator(jitlink::getAnonymousPointerCreator(
    ObjLinkingLayer.getExecutionSession().getTargetTriple()));
  auto PtrJumpStubCreator(jitlink::getPointerJumpStubCreator(
    ObjLinkingLayer.getExecutionSession().getTargetTriple()));
  if (!AnonymousPtrCreator || !PtrJumpStubCreator)
    return make_error<StringError>("Architecture not supported",
                                    inconvertibleErrorCode());
  return std::unique_ptr<RedirectableSymbolManager>(
      new JITLinkRedirectableSymbolManager(ObjLinkingLayer, JD,
      AnonymousPtrCreator, PtrJumpStubCreator));
}

@redstar
Copy link
Member Author

redstar commented Oct 24, 2024

Changed the code according to your proposal. Looks much better now, thanks!

Comment on lines 36 to 39
auto RM = std::unique_ptr<RedirectableSymbolManager>(
new JITLinkRedirectableSymbolManager(ObjLinkingLayer, JD, Err));
if (Err)
return Err;
new JITLinkRedirectableSymbolManager(
ObjLinkingLayer, JD, AnonymousPtrCreator, PtrJumpStubCreator));
return std::move(RM);
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to just return the object here, rather than naming it then returning it, but otherwise LGTM. Thanks @redstar!

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. yw.

The unit tests `ReOptimizeLayerTest.BasicReOptimization` and `JITLinkRedirectionManagerTest.BasicRedirectionOperation` are failing for me with the error:

```
Program aborted due to an unhandled Error:
Error value was Success. (Note: Success values must still be checked prior to being destroyed).
```

The error is raised when a value is assigned to `Err`, due to the the missing `ErrorAsOutParameter`.

The fix is to move the error handling out of the constructor.
@redstar redstar force-pushed the knacke/fixorctestcrash branch from 1d36a77 to 6ab1d2b Compare October 25, 2024 15:10
@redstar
Copy link
Member Author

redstar commented Oct 25, 2024

Hm. Why does the PR not close after manually merging?

@lhames lhames merged commit 9d9b1ba into llvm:main Oct 28, 2024
8 checks passed
@lhames
Copy link
Contributor

lhames commented Oct 28, 2024

Weird -- I thought it had been merged too, but it doesn't look like it. Should be fixed now.

NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
llvm#113492)

Check `AnonymousPtrCreator` and `PtrJumpStubCreator` before creating the
JITLinkRedirectableSymbolManager object. This simplifies construction, and
avoids premature registration as a resource manager in the failure case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants