This is an archive of the discontinued LLVM Phabricator instance.

[pseudo] Share the underly payload when stripping comments for a token stream
ClosedPublic

Authored by hokein on May 10 2022, 5:53 AM.

Details

Summary

stripComments(cook(...)) is a common pattern being written.
Without this patch, this has a use-after-free issue (cook returns a temporary
TokenStream object which has its own payload, but the payload is not
shared with the one returned by stripComments).

Diff Detail

Event Timeline

hokein created this revision.May 10 2022, 5:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2022, 5:53 AM
hokein requested review of this revision.May 10 2022, 5:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2022, 5:53 AM

This looks good, but if that's the idiom we should add it to cook() also.

This looks good, but if that's the idiom we should add it to cook() also.

cook is more tricky, it takes a TokenStream, and returns a new one with an allocator payload:

  • the input TokenStream doesn't have a payload, this is OK
  • the input TokenStream has a payload, but what if the type of payload is not an allocator?

Possible solution: 1) always assert the input TokenStream doesn't have a payload or the payload is allocator type 2) merge the lex and cook function, it looks like we always use the cook one.

This looks good, but if that's the idiom we should add it to cook() also.

cook is more tricky, it takes a TokenStream, and returns a new one with an allocator payload:

  • the input TokenStream doesn't have a payload, this is OK
  • the input TokenStream has a payload, but what if the type of payload is not an allocator?

Payload is opaque, it can be a pair of {new payload, inner payload}. e.g.

void TokenStream::addPayload(shared_ptr<void> P) {
  Payload = make_shared<pair<shared_ptr<void>, shared_ptr<void>>>(std::move(P), std::move(Payload));
}
hokein updated this revision to Diff 430646.May 19 2022, 5:54 AM

add addPayload method, and share payload in cook as well

This looks good, but if that's the idiom we should add it to cook() also.

cook is more tricky, it takes a TokenStream, and returns a new one with an allocator payload:

  • the input TokenStream doesn't have a payload, this is OK
  • the input TokenStream has a payload, but what if the type of payload is not an allocator?

Payload is opaque, it can be a pair of {new payload, inner payload}. e.g.

void TokenStream::addPayload(shared_ptr<void> P) {
  Payload = make_shared<pair<shared_ptr<void>, shared_ptr<void>>>(std::move(P), std::move(Payload));
}

Ohh, right, this is clever!

sammccall accepted this revision.Jul 13 2022, 6:08 AM
This revision is now accepted and ready to land.Jul 13 2022, 6:08 AM