Skip to content

[TySan] Fix struct access with different bases #120412

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler-rt/lib/tysan/tysan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ static bool isAliasingLegalUp(tysan_type_descriptor *TDA,
break;
}

// This offset can't be negative. Therefore we must be accessing something
// before the current type (not legal) or partially inside the last type.
// In the latter case, we adjust Idx.
if (TDA->Struct.Members[Idx].Offset > OffsetA) {
// Trying to access something before the current type.
if (!Idx)
return false;

Idx -= 1;
}

OffsetA -= TDA->Struct.Members[Idx].Offset;
TDA = TDA->Struct.Members[Idx].Type;
} else {
Expand Down
49 changes: 49 additions & 0 deletions compiler-rt/test/tysan/struct-offset-different-base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: %clangxx_tysan -O0 %s -o %t && %run %t >%t.out 2>&1
// RUN: FileCheck %s --implicit-check-not ERROR < %t.out

// Modified reproducer from https://github.com/llvm/llvm-project/issues/105960

#include <stdio.h>

struct inner1 {
char buffer;
int i;
};

struct inner2 {
char buffer;
int i;
float endBuffer;
};

void init_inner1(inner1 *iPtr) { iPtr->i = 200; }
void init_inner2(inner2 *iPtr) {
iPtr->i = 400;
iPtr->endBuffer = 413.0f;
}

struct outer {
inner1 foo;
inner2 bar;
char buffer;
};

int main(void) {
outer *l = new outer();

init_inner1(&l->foo);
init_inner2(&l->bar);

int access = l->foo.i;
printf("Accessed value 1 is %d\n", access);
access = l->bar.i;
printf("Accessed value 2 is %d\n", access);
float fAccess = l->bar.endBuffer;
printf("Accessed value 3 is %f\n", fAccess);

return 0;
}

// CHECK: Accessed value 1 is 200
// CHECK: Accessed value 2 is 400
// CHECK: Accessed value 3 is 413.0
Loading