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

Use valid address in pthread_setspecific to silence warnings #162

Merged
merged 3 commits into from
Sep 5, 2024
Merged

Conversation

qmuntal
Copy link
Collaborator

@qmuntal qmuntal commented Sep 4, 2024

Building with a newer glibc (probably since 2.34) causes the following warning:

# vendor/github.com/golang-fips/openssl/v2
thread_setup_unix.c: In function 'thread_id':
thread_setup_unix.c:32:12: warning: 'pthread_setspecific' expecting 1 byte in a region of size 0 [-Wstringop-overread]
   32 |     (void) pthread_setspecific(destructor_key, (void*)1);
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from thread_setup_unix.c:5:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/include-fixed/pthread.h:1317:12: note: in a call to function 'pthread_setspecific' declared with attribute 'access (none, 2)'
 1317 | extern int pthread_setspecific (pthread_key_t __key,
      |            ^~~~~~~~~~~~~~~~~~~

The is caused by the access attribute annotation of pthread_setspecific, which was recently added.

GCC defines the none mode of access as:

The access mode none specifies that the pointer to which it applies is not used to access the referenced object at all. Unless the pointer is null the pointed-to object must exist and have at least the size as denoted by the size-index argument. The object need not be initialized. The mode is intended to be used as a means to help validate the expected object size, for example in functions that call __builtin_object_size. See Object Size Checking.

We are passing (void*)1 as a NULL pointer, which doesn't meet the none access requirements. This PR fixes that by using a static stub char pointer.

Fixes microsoft/go#1305.

thread_setup_unix.c Show resolved Hide resolved
Copy link
Contributor

@derekparker derekparker left a comment

Choose a reason for hiding this comment

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

LGTM

thread_setup_unix.c Outdated Show resolved Hide resolved
@@ -9,6 +9,9 @@ static pthread_mutex_t *mutex_buf = NULL;

static pthread_key_t destructor_key;

/* Used in pthread_setspecific. See https://github.com/microsoft/go/issues/1305. */
static char stub;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this need to exist at the global scope, or could static char stub; or char stub; be declared just before the call?

If there's no difference, I think putting the declaration inside thread_id would be a bit clearer. And IMO putting it on the stack (if possible) raises the fewest questions from a reader's perspective. 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looks putting the variable in the function scope also works, and it does look clearer. I'll keep the static keyword, though, I would rather not pass a pointer to a stack-allocated variable that can be used outside the function frame, just in case it is used somewhere 😅.

qmuntal and others added 2 commits September 5, 2024 10:18
Co-authored-by: Davis Goodin <dagood@users.noreply.github.com>
@qmuntal qmuntal merged commit 6f4d87d into v2 Sep 5, 2024
26 checks passed
@qmuntal qmuntal deleted the fixthread branch September 5, 2024 08:38
qmuntal added a commit that referenced this pull request Sep 5, 2024
* use valid address in pthread_setspecific to silence warnings

* Update thread_setup_unix.c

Co-authored-by: Davis Goodin <dagood@users.noreply.github.com>

* use local variable instead of global

---------

Co-authored-by: Davis Goodin <dagood@users.noreply.github.com>
(cherry picked from commit 6f4d87d)
dagood pushed a commit that referenced this pull request Sep 5, 2024
…168)

* use valid address in pthread_setspecific to silence warnings

* Update thread_setup_unix.c

Co-authored-by: Davis Goodin <dagood@users.noreply.github.com>

* use local variable instead of global

---------

Co-authored-by: Davis Goodin <dagood@users.noreply.github.com>
(cherry picked from commit 6f4d87d)
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.

Building app with Go 1.23 OpenSSL backend emits a build warning
4 participants