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

[TySan] possible false positive with memcpy()? #122934

Open
seanm opened this issue Jan 14, 2025 · 0 comments
Open

[TySan] possible false positive with memcpy()? #122934

seanm opened this issue Jan 14, 2025 · 0 comments
Labels
compiler-rt:tysan Type sanitizer false-positive Warning fires when it should not

Comments

@seanm
Copy link

seanm commented Jan 14, 2025

I have a TySan report that I think/thought is a false positive. So I used creduce to try and minimize it. It reduced down to the following:

typedef struct {
  int a;
  int b;
  int c;
  int i;
  int d;
  int e;
  int f;
  int dim[8];
  long g
} h;
int k, m = __builtin_object_size(&k, 0);
h b;
void j();
void n(void *o) {
  __builtin___memcpy_chk(&k, o, sizeof(0), m);
  k; // •••• TySan complains here, line 17
}
void calloc();
h *c = calloc;
void l() {
  c->g;
  b = *c;
  j(0, b);
}
void j(int, void *p) { n(p + 64); }
void main() { l(); }

TySan reports:

==79752==ERROR: TypeSanitizer: type-aliasing-violation on address 0x000100c1c010 (pc 0x000100c168ec bp 0x00016f1eac30 sp 0x00016f1ea3b0 tid 32786625)
READ of size 4 at 0x000100c1c010 with type int accesses an existing object of type long (in <anonymous type> at offset 64)
    #0 0x000100c168e8 in n test-preprocessed.c:17

Line 17 looks like it does nothing, but indeed if I comment it out, TySan no longer warns.

This reduced code is pretty nonsensical of course, but in the real code, it's using memcpy() precisely to avoid strict aliasing violations. Isn't memcpy() supposed to be the kosher way to copy anything of any size/alignment to anything else?

Also, and maybe I should make another ticket, but it's a shame the report says:

  • (in <anonymous type> at offset 64)

instead of:

  • (in 'struct h' at offset 64, field 'g')
@llvmbot llvmbot added the false-positive Warning fires when it should not label Jan 14, 2025
@EugeneZelenko EugeneZelenko added the compiler-rt:tysan Type sanitizer label Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:tysan Type sanitizer false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

3 participants