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

tests: Bluetooth: Add some more tests for bt_le_create_conn() #78284

Merged

Conversation

rugeGerritsen
Copy link
Collaborator

Adds coverage for attempting to connect while already connecting or connecting to a device that already exists.


TEST_ASSERT(initial_refs >= 1, "Expect to have at least once reference");

err = bt_conn_le_create(&peer, &create_param, BT_LE_CONN_PARAM_DEFAULT, &conn);
Copy link
Collaborator Author

@rugeGerritsen rugeGerritsen Sep 11, 2024

Choose a reason for hiding this comment

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

Here the API doesn't protect the user from providing a connection object that still has references to it. I don't think this can be handled with todays implementation unless we change the API to require conn to point to NULL.

I believe this can be a source of subtle bugs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree. This is why testlib (which btw is made to work on target) does this. And I now think it's worth the API break to start enforcing this in core API.

* @note The reference variable @p connp is required be empty (i.e.
* NULL) on entry.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think it requires an API change.

Isn't to possible to just do something along the lines of

if (ret_conn != NULL && atomic_get((*ret_conn)->ref) != 0) {
    return -EALREADY;
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

isn't that -EALREADY an API change though?

Copy link
Collaborator

Choose a reason for hiding this comment

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

isn't that -EALREADY an API change though?

Yeah, but not a breaking API change IMO, although that is clearly debatable :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@hermabe , you are right. The code sample was wrong. I updated the code to declare conn as a pointer.

Copy link
Member

Choose a reason for hiding this comment

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

Your point still stands with the updated sample. struct bt_conn *conn; may point to garbage so **ret_conn cannot be used.

Copy link
Collaborator

Choose a reason for hiding this comment

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

When declaring struct bt_conn *conn;, it can happen that it points to something inside acl_conns, right?

Technically, yes if it's uninitialized. IS_ARRAY_ELEMENT should also check whether it's aligned properly, so even if it's within the array, if it's not aligned properly it should still work.

If it happens to actually match a connection object pointer directly, then it should also be OK as we can then treat it as a proper bt_conn pointer and check the reference counter

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it happens to actually match a connection object pointer directly, then it should also be OK as we can then treat it as a proper bt_conn pointer and check the reference counter

I would argue that we shouldn't rely on the undefined initialization value of a pointer.

Lets say the application is configured to support two connections.

int err;
struct bt_conn *conn1 = NULL;
err = bt_conn_le_create(..., &conn1);

/* ... establish connection 1 */

int err;
struct bt_conn *conn2; /* By chance this is initialized to conn1 */
err = bt_conn_le_create(..., &conn2); /* This call will now fail */ 

I assume that is not intended.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If it happens to actually match a connection object pointer directly, then it should also be OK as we can then treat it as a proper bt_conn pointer and check the reference counter

I would argue that we shouldn't rely on the undefined initialization value of a pointer.

Lets say the application is configured to support two connections.

int err;
struct bt_conn *conn1 = NULL;
err = bt_conn_le_create(..., &conn1);

/* ... establish connection 1 */

int err;
struct bt_conn *conn2; /* By chance this is initialized to conn1 */
err = bt_conn_le_create(..., &conn2); /* This call will now fail */ 

I assume that is not intended.

That is a fair point. Ideally it shouldn't be necessary to initialize an [out] pointer, and there's no way to check that at runtime I believe.

The above case would be a very rare instance though.

@rugeGerritsen rugeGerritsen force-pushed the bt-conn-create-tests branch 2 times, most recently from f6719b1 to 90c16c9 Compare September 11, 2024 12:21
@rugeGerritsen rugeGerritsen marked this pull request as ready for review September 11, 2024 14:01
@rugeGerritsen rugeGerritsen marked this pull request as draft September 11, 2024 14:42
@rugeGerritsen rugeGerritsen marked this pull request as ready for review September 12, 2024 09:28
tests/bsim/bluetooth/host/central/src/dummy_peripheral.c Outdated Show resolved Hide resolved
tests/bsim/bluetooth/host/central/src/main.c Outdated Show resolved Hide resolved
tests/bsim/bluetooth/host/central/src/main.c Outdated Show resolved Hide resolved
tests/bsim/bluetooth/host/central/src/main.c Show resolved Hide resolved
tests/bsim/bluetooth/host/central/src/main.c Outdated Show resolved Hide resolved
Adds coverage for attempting to connect while already connecting
or connecting to a device that already exists.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
@mmahadevan108 mmahadevan108 merged commit f6712c4 into zephyrproject-rtos:main Sep 17, 2024
25 checks passed
PavelVPV added a commit to PavelVPV/zephyr that referenced this pull request Oct 15, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos#78284 (comment)
for details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
PavelVPV added a commit to PavelVPV/zephyr that referenced this pull request Oct 15, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
PavelVPV added a commit to PavelVPV/zephyr that referenced this pull request Oct 16, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
PavelVPV added a commit to PavelVPV/zephyr that referenced this pull request Oct 16, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
PavelVPV added a commit to PavelVPV/zephyr that referenced this pull request Oct 17, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
PavelVPV added a commit to PavelVPV/zephyr that referenced this pull request Oct 17, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
PavelVPV added a commit to PavelVPV/zephyr that referenced this pull request Oct 17, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
dleach02 pushed a commit that referenced this pull request Oct 18, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
PavelVPV added a commit to PavelVPV/sdk-zephyr that referenced this pull request Oct 21, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos/zephyr#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
(cherry picked from commit 8acb1cc)
PavelVPV added a commit to PavelVPV/sdk-zephyr that referenced this pull request Oct 21, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos/zephyr#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
(cherry picked from commit 8acb1cc)
PavelVPV added a commit to PavelVPV/sdk-zephyr that referenced this pull request Oct 24, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos/zephyr#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
(cherry picked from commit 8acb1cc)
PavelVPV added a commit to PavelVPV/sdk-zephyr that referenced this pull request Oct 24, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos/zephyr#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
(cherry picked from commit 8acb1cc)
PavelVPV added a commit to PavelVPV/sdk-zephyr that referenced this pull request Oct 28, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos/zephyr#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
(cherry picked from commit 8acb1cc)
coreboot-org-bot pushed a commit to coreboot/zephyr-cros that referenced this pull request Oct 28, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos/zephyr#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

(cherry picked from commit 8acb1cc)

Original-Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
GitOrigin-RevId: 8acb1cc
Cr-Build-Id: 8733565196572304993
Cr-Build-Url: https://cr-buildbucket.appspot.com/build/8733565196572304993
Copybot-Job-Name: zephyr-main-copybot-downstream
Change-Id: I6987d4437fa20c66933aa467e78dc9a0bf234d0b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5944414
Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com>
Commit-Queue: Ting Shen <phoenixshen@chromium.org>
Tested-by: Ting Shen <phoenixshen@chromium.org>
Reviewed-by: Dawid Niedźwiecki <dawidn@google.com>
PavelVPV added a commit to PavelVPV/sdk-zephyr that referenced this pull request Oct 31, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos/zephyr#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
(cherry picked from commit 8acb1cc)
rlubos pushed a commit to nrfconnect/sdk-zephyr that referenced this pull request Oct 31, 2024
This commit adds a warning and a Kconfig option to `bt_conn_le_create`
and `bt_conn_le_create_synced` functions which are meant to warn a user
of a potential leakage of an active connection object.

This change is implemented due to frequent incorrect use of the
connection pointer where a pointer to an existing connection object
is overwritten by `bt_conn_le_create` and `bt_conn_le_create_synced`
functions which in turns leads to sporadic critical bugs. See
zephyrproject-rtos/zephyr#78284 (comment)
for more details.

The Kconfig option is introduced instead of always returning the error
to not affect current implementations. However, it is recommended to
keep this option enabled to avoid potential bugs.

Signed-off-by: Pavel Vasilyev <pavel.vasilyev@nordicsemi.no>
(cherry picked from commit 8acb1cc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants