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

unix.StdCLibraryFunctions analysis regression #116421

Open
avlouis opened this issue Nov 15, 2024 · 2 comments
Open

unix.StdCLibraryFunctions analysis regression #116421

avlouis opened this issue Nov 15, 2024 · 2 comments

Comments

@avlouis
Copy link

avlouis commented Nov 15, 2024

There seems to be a regression in the unix.StdCLibraryFunctions analysis check.
Found below is code for a small example. Running clang --analyze sendto.c produces warnings such as the following:

sendto.c:20:3: warning: The 1st argument to 'sendto' is -1 but should be >= 0 [unix.StdCLibraryFunctions]
   20 |   sendto(sockfd, NULL, 0, 0, NULL, 0);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This happens for versions 19.1.2 and 19.1.3 but not 18.1.8. All acquired via this Github project.

It seems the analysis pass believes some_function_outside_tu can set sockfd to -1 as removing the call to it or adding if(0 > sockfd) return 1; between it and the call to sendto resolves this warning. some_function_outside_tu should not be able to modify sockfd since the implementation is in a different translation unit and sockfd is static.

Here is the code for sendto.c:

#include <arpa/inet.h>
#include <sys/socket.h>
#include <stddef.h>

static int sockfd = -1;

void some_function_outside_tu(); // declared here, but implementation
				 // is outside this translation unit

int main(int argc, const char* argv[]) {
  (void)argc; (void)argv;

  sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if(0 > sockfd) {
    return 1;
  }

  some_function_outside_tu(); // this causes issues
  sendto(sockfd, NULL, 0, 0, NULL, 0);
}
@llvmbot
Copy link

llvmbot commented Nov 15, 2024

@llvm/issue-subscribers-clang-static-analyzer

Author: Andrew V. Louis (avlouis)

There seems to be a regression in the `unix.StdCLibraryFunctions` analysis check. Found below is code for a small example. Running `clang --analyze sendto.c` produces warnings such as the following: ``` sendto.c:20:3: warning: The 1st argument to 'sendto' is -1 but should be >= 0 [unix.StdCLibraryFunctions] 20 | sendto(sockfd, NULL, 0, 0, NULL, 0); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ```

This happens for versions 19.1.2 and 19.1.3 but not 18.1.8. All acquired via this Github project.

It seems the analysis pass believes some_function_outside_tu can set sockfd to -1 as removing the call to it or adding if(0 &gt; sockfd) return 1; between it and the call to sendto resolves this warning. some_function_outside_tu should not be able to modify sockfd since the implementation is in a different translation unit and sockfd is static.

Here is the code for sendto.c:

#include &lt;arpa/inet.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;stddef.h&gt;

static int sockfd = -1;

void some_function_outside_tu(); // declared here, but implementation
				 // is outside this translation unit

int main(int argc, const char* argv[]) {
  (void)argc; (void)argv;

  sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if(0 &gt; sockfd) {
    return 1;
  }

  some_function_outside_tu(); // this causes issues
  sendto(sockfd, NULL, 0, 0, NULL, 0);
}

@steakhal
Copy link
Contributor

Hi,
In this simplified case you are right. The address of that static global never escapes this translation unit. However, in general, if its address would have been taken and assigned to something, or passed to an opaque function call then even these globals should escape.

So in short, even static globals are subject to invalidation for opaque fn calls. Internally, I don't think we treat statics any differently, as we don't have a more sophisticated "address taken" analysis.

Speaking of the appearing issue, I'll have a look to see what commit it bisects to to have better context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants