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

Refactor SessionHandle: use variant for secure&unauthenticated, secure session only stores local session id in the handle. #11266

Closed
wants to merge 1 commit into from

Conversation

kghost
Copy link
Contributor

@kghost kghost commented Nov 1, 2021

Problem

SessionHandle is too big, it contains to name unnecessary fields.

Change overview

  • Currently 2 types of session is supported: Secure, Unauthenticated. Using Variant for each supported session
  • For SecureSession, only store LocalSessionId in the session handle, because it is unique, and can be used to look up a session.

Testing

Verifed by unit-tests.

@andy31415 andy31415 changed the title Refactor SessionHandle Refactor SessionHandle: use variant for secure&unauthenticated, secure session only stores local session id in the handle. Nov 1, 2021
@kghost kghost changed the title Refactor SessionHandle: use variant for secure&unauthenticated, secure session only stores local session id in the handle. Refactor SessionHandle Nov 1, 2021
@github-actions
Copy link

github-actions bot commented Nov 1, 2021

PR #11266: Size comparison from 1073bb2 to 1036a8e

Increases (21 builds for esp32, k32w, mbed, nrfconnect, p6, qpg)
platform target config section 1073bb2 1036a8e change % change
esp32 all-clusters-app c3devkit (read only) 879954 880006 52 0.0
.flash.rodata 199056 199096 40 0.0
.flash.text 879954 880006 52 0.0
m5stack (read only) 910915 910963 48 0.0
.flash.rodata 207784 207824 40 0.0
.flash.text 910915 910963 48 0.0
k32w lighting-app k32w061+se05x+release .text 612628 612668 40 0.0
lock-app k32w061+debug .text 514468 514508 40 0.0
shell k32w061+debug .text 359332 359336 4 0.0
mbed all-clusters-app CY8CPROTO_062_4343W+release .heap 851432 851656 224 0.0
lighting-app CY8CPROTO_062_4343W+release .heap 859040 859264 224 0.0
lock-app CY8CPROTO_062_4343W+release .heap 860152 860376 224 0.0
shell CY8CPROTO_062_4343W+release .heap 875248 875368 120 0.0
nrfconnect lighting-app nrf52840dk_nrf52840 rodata 96304 96344 40 0.0
text 577100 577108 8 0.0
nrf52840dk_nrf52840+rpc rodata 87080 87136 56 0.1
text 550276 550280 4 0.0
nrf5340dk_nrf5340_cpuapp rodata 91548 91584 36 0.0
text 506568 506576 8 0.0
lock-app nrf52840dk_nrf52840 rodata 92660 92700 40 0.0
text 558580 558588 8 0.0
nrf5340dk_nrf5340_cpuapp rodata 87964 88004 40 0.0
text 488140 488148 8 0.0
pump-app nrf52840dk_nrf52840 rodata 93908 93944 36 0.0
text 561792 561796 4 0.0
pump-controller-app nrf52840dk_nrf52840 rodata 92684 92720 36 0.0
text 558424 558428 4 0.0
shell nrf52840dk_nrf52840 rodata 72536 72576 40 0.1
nrf5340dk_nrf5340_cpuapp rodata 67180 67216 36 0.1
p6 lock-app default .heap 964744 965192 448 0.0
qpg lighting-app qpg6100+debug (read only) 489412 489424 12 0.0
.text 484092 484104 12 0.0
lock-app qpg6100+debug (read only) 465752 465788 36 0.0
.text 460432 460468 36 0.0
Decreases (26 builds for efr32, esp32, k32w, mbed, nrfconnect, p6, qpg, telink)
platform target config section 1073bb2 1036a8e change % change
efr32 lighting-app BRD4161A (read only) 735256 734052 -1204 -0.2
(read/write) 114444 114220 -224 -0.2
.bss 112692 112468 -224 -0.2
.text 735248 734044 -1204 -0.2
BRD4161A+rpc (read only) 722680 721476 -1204 -0.2
(read/write) 131052 130824 -228 -0.2
.bss 129196 128972 -224 -0.2
.text 722672 721468 -1204 -0.2
lock-app BRD4161A (read only) 714552 713332 -1220 -0.2
(read/write) 112260 112036 -224 -0.2
.bss 110548 110324 -224 -0.2
.text 714544 713324 -1220 -0.2
window-app BRD4161A (read only) 715440 714252 -1188 -0.2
(read/write) 112588 112360 -228 -0.2
.bss 110868 110644 -224 -0.2
.text 715432 714244 -1188 -0.2
esp32 all-clusters-app c3devkit (read/write) 1307184 1307000 -184 -0.0
.dram0.bss 58424 58200 -224 -0.4
m5stack (read/write) 426968 426784 -184 -0.0
.dram0.bss 60920 60696 -224 -0.4
k32w lighting-app k32w061+se05x+release (read/write) 698016 697832 -184 -0.0
.bss 77688 77464 -224 -0.3
lock-app k32w061+debug (read/write) 590320 590136 -184 -0.0
.bss 68188 67964 -224 -0.3
shell k32w061+debug (read/write) 424988 424872 -116 -0.0
.bss 63280 63160 -120 -0.2
mbed all-clusters-app CY8CPROTO_062_4343W+release (read/write) 2293808 2293720 -88 -0.0
.bss 179796 179572 -224 -0.1
.text 1256408 1256320 -88 -0.0
lighting-app CY8CPROTO_062_4343W+release (read/write) 2273256 2273168 -88 -0.0
.bss 171836 171612 -224 -0.1
.text 1235856 1235768 -88 -0.0
lock-app CY8CPROTO_062_4343W+release (read/write) 2250912 2250824 -88 -0.0
.bss 170740 170516 -224 -0.1
.text 1213512 1213424 -88 -0.0
shell CY8CPROTO_062_4343W+release (read/write) 2048656 2048496 -160 -0.0
.bss 156232 156112 -120 -0.1
.text 1011256 1011096 -160 -0.0
nrfconnect lighting-app nrf52840dk_nrf52840 (read/write) 860167 859999 -168 -0.0
bss 111148 110924 -224 -0.2
nrf52840dk_nrf52840+rpc (read/write) 820991 820839 -152 -0.0
bss 107392 107168 -224 -0.2
nrf5340dk_nrf5340_cpuapp (read/write) 785210 785022 -188 -0.0
bss 112524 112300 -224 -0.2
lock-app nrf52840dk_nrf52840 (read/write) 836875 836691 -184 -0.0
bss 110184 109960 -224 -0.2
nrf5340dk_nrf5340_cpuapp (read/write) 762170 762002 -168 -0.0
bss 111596 111372 -224 -0.2
pump-app nrf52840dk_nrf52840 (read/write) 841547 841359 -188 -0.0
bss 110320 110096 -224 -0.2
pump-controller-app nrf52840dk_nrf52840 (read/write) 836867 836679 -188 -0.0
bss 110220 109996 -224 -0.2
shell nrf52840dk_nrf52840 (read/write) 776179 776043 -136 -0.0
bss 109096 108976 -120 -0.1
text 519936 519900 -36 -0.0
nrf5340dk_nrf5340_cpuapp (read/write) 691202 691094 -108 -0.0
bss 110080 109960 -120 -0.1
text 440548 440512 -36 -0.0
p6 lock-app default (read/write) 2166464 2165272 -1192 -0.1
.bss 66184 65736 -448 -0.7
.text 1124728 1123536 -1192 -0.1
qpg lighting-app qpg6100+debug .bss 50320 50128 -192 -0.4
lock-app qpg6100+debug .bss 49272 49080 -192 -0.4
telink lighting-app tlsr9518adk80d (read/write) 661646 661458 -188 -0.0
bss 68960 68736 -224 -0.3
text 457394 457386 -8 -0.0
Full report (29 builds for efr32, esp32, k32w, mbed, nrfconnect, p6, qpg, telink)
platform target config section 1073bb2 1036a8e change % change
efr32 lighting-app BRD4161A (read only) 735256 734052 -1204 -0.2
(read/write) 114444 114220 -224 -0.2
.bss 112692 112468 -224 -0.2
.data 1752 1752 0 0.0
.text 735248 734044 -1204 -0.2
BRD4161A+rpc (read only) 722680 721476 -1204 -0.2
(read/write) 131052 130824 -228 -0.2
.bss 129196 128972 -224 -0.2
.data 1852 1852 0 0.0
.text 722672 721468 -1204 -0.2
lock-app BRD4161A (read only) 714552 713332 -1220 -0.2
(read/write) 112260 112036 -224 -0.2
.bss 110548 110324 -224 -0.2
.data 1712 1712 0 0.0
.text 714544 713324 -1220 -0.2
window-app BRD4161A (read only) 715440 714252 -1188 -0.2
(read/write) 112588 112360 -228 -0.2
.bss 110868 110644 -224 -0.2
.data 1716 1716 0 0.0
.text 715432 714244 -1188 -0.2
esp32 all-clusters-app c3devkit (read only) 879954 880006 52 0.0
(read/write) 1307184 1307000 -184 -0.0
.dram0.bss 58424 58200 -224 -0.4
.dram0.data 16464 16464 0 0.0
.flash.rodata 199056 199096 40 0.0
.flash.text 879954 880006 52 0.0
.iram0.text 57554 57554 0 0.0
m5stack (read only) 910915 910963 48 0.0
(read/write) 426968 426784 -184 -0.0
.dram0.bss 60920 60696 -224 -0.4
.dram0.data 32100 32100 0 0.0
.flash.rodata 207784 207824 40 0.0
.flash.text 910915 910963 48 0.0
.iram0.text 125115 125115 0 0.0
k32w lighting-app k32w061+se05x+release (read/write) 698016 697832 -184 -0.0
.bss 77688 77464 -224 -0.3
.data 1900 1900 0 0.0
.text 612628 612668 40 0.0
lock-app k32w061+debug (read/write) 590320 590136 -184 -0.0
.bss 68188 67964 -224 -0.3
.data 1864 1864 0 0.0
.text 514468 514508 40 0.0
shell k32w061+debug (read/write) 424988 424872 -116 -0.0
.bss 63280 63160 -120 -0.2
.data 672 672 0 0.0
.text 359332 359336 4 0.0
mbed all-clusters-app CY8CPROTO_062_4343W+release (read only) 6224 6224 0 0.0
(read/write) 2293808 2293720 -88 -0.0
.bss 179796 179572 -224 -0.1
.data 5216 5216 0 0.0
.heap 851432 851656 224 0.0
.text 1256408 1256320 -88 -0.0
lighting-app CY8CPROTO_062_4343W+release (read only) 6224 6224 0 0.0
(read/write) 2273256 2273168 -88 -0.0
.bss 171836 171612 -224 -0.1
.data 5568 5568 0 0.0
.heap 859040 859264 224 0.0
.text 1235856 1235768 -88 -0.0
lock-app CY8CPROTO_062_4343W+release (read only) 6224 6224 0 0.0
(read/write) 2250912 2250824 -88 -0.0
.bss 170740 170516 -224 -0.1
.data 5552 5552 0 0.0
.heap 860152 860376 224 0.0
.text 1213512 1213424 -88 -0.0
pigweed-app CY8CPROTO_062_4343W+release (read only) 6224 6224 0 0.0
(read/write) 1139744 1139744 0 0.0
.bss 11752 11752 0 0.0
.data 4368 4368 0 0.0
.heap 1020328 1020328 0 0.0
.text 103128 103128 0 0.0
shell CY8CPROTO_062_4343W+release (read only) 6224 6224 0 0.0
(read/write) 2048656 2048496 -160 -0.0
.bss 156232 156112 -120 -0.1
.data 4968 4968 0 0.0
.heap 875248 875368 120 0.0
.text 1011256 1011096 -160 -0.0
nrfconnect lighting-app nrf52840dk_nrf52840 (read/write) 860167 859999 -168 -0.0
bss 111148 110924 -224 -0.2
rodata 96304 96344 40 0.0
text 577100 577108 8 0.0
nrf52840dk_nrf52840+rpc (read/write) 820991 820839 -152 -0.0
bss 107392 107168 -224 -0.2
rodata 87080 87136 56 0.1
text 550276 550280 4 0.0
nrf5340dk_nrf5340_cpuapp (read/write) 785210 785022 -188 -0.0
bss 112524 112300 -224 -0.2
rodata 91548 91584 36 0.0
text 506568 506576 8 0.0
lock-app nrf52840dk_nrf52840 (read/write) 836875 836691 -184 -0.0
bss 110184 109960 -224 -0.2
rodata 92660 92700 40 0.0
text 558580 558588 8 0.0
nrf5340dk_nrf5340_cpuapp (read/write) 762170 762002 -168 -0.0
bss 111596 111372 -224 -0.2
rodata 87964 88004 40 0.0
text 488140 488148 8 0.0
pigweed-app nrf52840dk_nrf52840 (read/write) 497323 497323 0 0.0
bss 51824 51824 0 0.0
rodata 45776 45776 0 0.0
text 339436 339436 0 0.0
pump-app nrf52840dk_nrf52840 (read/write) 841547 841359 -188 -0.0
bss 110320 110096 -224 -0.2
rodata 93908 93944 36 0.0
text 561792 561796 4 0.0
pump-controller-app nrf52840dk_nrf52840 (read/write) 836867 836679 -188 -0.0
bss 110220 109996 -224 -0.2
rodata 92684 92720 36 0.0
text 558424 558428 4 0.0
shell nrf52840dk_nrf52840 (read/write) 776179 776043 -136 -0.0
bss 109096 108976 -120 -0.1
rodata 72536 72576 40 0.1
text 519936 519900 -36 -0.0
nrf5340dk_nrf5340_cpuapp (read/write) 691202 691094 -108 -0.0
bss 110080 109960 -120 -0.1
rodata 67180 67216 36 0.1
text 440548 440512 -36 -0.0
p6 lock-app default (read/write) 2166464 2165272 -1192 -0.1
.bss 66184 65736 -448 -0.7
.data 2416 2416 0 0.0
.heap 964744 965192 448 0.0
.text 1124728 1123536 -1192 -0.1
qpg lighting-app qpg6100+debug (read only) 489412 489424 12 0.0
(read/write) 114144 114144 0 0.0
.bss 50320 50128 -192 -0.4
.data 1000 1000 0 0.0
.text 484092 484104 12 0.0
lock-app qpg6100+debug (read only) 465752 465788 36 0.0
(read/write) 114140 114140 0 0.0
.bss 49272 49080 -192 -0.4
.data 956 956 0 0.0
.text 460432 460468 36 0.0
persistent-storage-app qpg6100+debug (read only) 155820 155820 0 0.0
(read/write) 114140 114140 0 0.0
.bss 27752 27752 0 0.0
.data 372 372 0 0.0
.text 150500 150500 0 0.0
telink lighting-app tlsr9518adk80d (read/write) 661646 661458 -188 -0.0
bss 68960 68736 -224 -0.3
noinit 33216 33216 0 0.0
text 457394 457386 -8 -0.0

@kghost kghost changed the title Refactor SessionHandle Refactor SessionHandle: use variant for secure&unauthenticated, secure session only stores local session id in the handle. Nov 1, 2021
Transport::SecureSession * SessionHandle::AsSecureSession() const
{
const VariantSecureSession & session = mSession.Get<VariantSecureSession>();
return session.mSessionManager.GetSecureSession(session.mLocalSessionId);
Copy link
Contributor

Choose a reason for hiding this comment

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

So this can still return null, right? But some of the callsites that use this (possibly indirectly now that it's hidden under operator-> had their null-checks removed? Why?

/*
* @brief A handle to all types of sessions
*
* A SessionHandle always referring to a valid session, it will never dangling.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is true given the mSessionManager.GetSecureSession(session.mLocalSessionId); bit. This won't dangle, but it can return null, not a session.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose the assertion is that given correct code, we expect the session handle to refer to a session that will have existed at one time. It can still definitely be the case that the session has since been evicted.

All of this makes me a bit nervous. Correct operation here is predicated on the session ID space not wrapping over dangling session handles. But you can't actually guarantee this with the session ID allocator because nothing in the system can cleanup outstanding session handles when underlying sessions are evicted.

In short, while hiding the underlying session pointers prevents dereference of an invalid pointer, it does NOT prevent this session ID wrap problem. I bet I could devise a pathological test case where a session is evicted, 65535 subsequent sessions are opened and then closed, one more session is opened and then a dangling session handle from the first, evicted session becomes an incorrect match to the newest session.

To be clear, this isn't a new problem with this PR. This PR just makes this more obvious by stripping everything but the session ID out of the session handle.

I don't know how this can be fixed unless the session manager can reach into the system and explicitly invalidate outstanding session handles. The most obvious design would change the session handles into pointers to actual, allocated memory in the session manager.

But we also need to be able to forcibly evict sessions. This implies a need to be able to execute a failure callback chain for any session that's being forcibly evicted. This might be possible with addition of a CancelExchange(session ID) interface for the exchange manager.

Complicated, but probably doable...

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we perhaps do something similar to file descriptors? I think it could be workable for the session manager to forcibly close sessions (as I think it does now), but only for the holders of session handles to release session IDs for reuse by the session ID allocator.

/*
* @brief A handle to all types of sessions
*
* A SessionHandle always referring to a valid session, it will never dangling.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* A SessionHandle always referring to a valid session, it will never dangling.
* A SessionHandle always refers to a valid session, it is never dangling.

{}
bool operator==(const VariantSecureSession & that) const { return mLocalSessionId == that.mLocalSessionId; }

// Include a ref to session manager to make the handler easy to use.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Include a ref to session manager to make the handler easy to use.
// Include a ref to session manager to make the handle easy to use.

bool operator==(const VariantSecureSession & that) const { return mLocalSessionId == that.mLocalSessionId; }

// Include a ref to session manager to make the handler easy to use.
// Replace these field with a raw pointer to the actual secure session, once we have figured out how to prevent dangling
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Replace these field with a raw pointer to the actual secure session, once we have figured out how to prevent dangling
// Replace these fields with a raw pointer to the actual secure session, once we have figured out how to prevent dangling

Comment on lines +66 to +67
SessionManager & mSessionManager;
uint16_t mLocalSessionId;
Copy link
Contributor

Choose a reason for hiding this comment

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

So after these changes a SessionHandle is still two words, right?

@jepenven-silabs
Copy link
Contributor

@kghost as discussed on PR #11390 can you make sure that we can still access the GroupId value and the FabricId from the sessionHandle after the refactor? Those two are vital for sending group messages.

@kghost
Copy link
Contributor Author

kghost commented Nov 10, 2021

@jepenven-silabs we can access these field inside SessionManager, they are not visible for applications.

@stale
Copy link

stale bot commented Nov 17, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale Stale issue or PR label Nov 17, 2021
@stale
Copy link

stale bot commented Nov 24, 2021

This stale pull request has been automatically closed. Thank you for your contributions.

@stale stale bot closed this Nov 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants