Skip to content
Open
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
2 changes: 0 additions & 2 deletions clang/docs/TypeSanitizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ brief dictionary of these terms.

* ``omnipotent char``: This is a special type which can alias with anything. Its name comes from the C/C++
type ``char``.
* ``type p[x]``: This signifies pointers to the type. ``x`` is the number of indirections to reach the final value.
Copy link
Contributor

Choose a reason for hiding this comment

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

If we use "print tbaa name" flag then this terminology is still used, so maybe we want to keep it in the docs? Adding a note before/ after that this is no longer default behaviour would maybe be better

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we need the flag. Given that we print the C type name for other cases, I think it would make sense to always print the pointer in C style, and remove the flag

Copy link
Contributor

Choose a reason for hiding this comment

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

If we are always doing C style maybe it would make sense then to change "omnipotent char" to char as well? That or change the docs to state the name is permanent

As an example, a pointer to a pointer to an integer would be ``type p2 int``.

TypeSanitizer is still experimental. User-facing error messages should be improved in the future to remove
references to LLVM IR specific terms.
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/sanitize-type-globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// CHECK: @__tysan_v1_omnipotent_20char = linkonce_odr constant { i64, i64, ptr, i64, [16 x i8] } { i64 2, i64 1, ptr @__tysan_v1_Simple_20C_2b_2b_20TBAA, i64 0, [16 x i8] c"omnipotent char\00" }, comdat
// CHECK: @__tysan_v1_int = linkonce_odr constant { i64, i64, ptr, i64, [4 x i8] } { i64 2, i64 1, ptr @__tysan_v1_omnipotent_20char, i64 0, [4 x i8] c"int\00" }, comdat
// CHECK: @__tysan_v1_any_20pointer = linkonce_odr constant { i64, i64, ptr, i64, [12 x i8] } { i64 2, i64 1, ptr @__tysan_v1_omnipotent_20char, i64 0, [12 x i8] c"any pointer\00" }, comdat
// CHECK: @__tysan_v1_p1_20int = linkonce_odr constant { i64, i64, ptr, i64, [7 x i8] } { i64 2, i64 1, ptr @__tysan_v1_any_20pointer, i64 0, [7 x i8] c"p1 int\00" }, comdat
// CHECK: @__tysan_v1_p1_20int = linkonce_odr constant { i64, i64, ptr, i64, [5 x i8] } { i64 2, i64 1, ptr @__tysan_v1_any_20pointer, i64 0, [5 x i8] c"int*\00" }, comdat
// CHECK: @__tysan_v1___ZTS9CompleteS = linkonce_odr constant { i64, i64, ptr, i64, ptr, i64, [15 x i8] } { i64 2, i64 2, ptr @__tysan_v1_int, i64 0, ptr @__tysan_v1_p1_20int, i64 8, [15 x i8] c"_ZTS9CompleteS\00" }, comdat
// CHECK: @__tysan_v1___ZTS1b = linkonce_odr constant { i64, i64, [7 x i8] } { i64 2, i64 0, [7 x i8] c"_ZTS1b\00" }, comdat
// CHECK: @llvm.used = appending global [8 x ptr] [ptr @tysan.module_ctor, ptr @__tysan_v1_Simple_20C_2b_2b_20TBAA, ptr @__tysan_v1_omnipotent_20char, ptr @__tysan_v1_int, ptr @__tysan_v1_any_20pointer, ptr @__tysan_v1_p1_20int, ptr @__tysan_v1___ZTS9CompleteS, ptr @__tysan_v1___ZTS1b], section "llvm.metadata"
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/tysan/print_stacktrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void zero_array() {
for (i = 0; i < 1; ++i)
P[i] = 0.0f;
// CHECK: ERROR: TypeSanitizer: type-aliasing-violation
// CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type p1 float
// CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type float*
// CHECK: {{#0 0x.* in zero_array .*print_stacktrace.c:}}[[@LINE-3]]
// CHECK-SHORT-NOT: {{#1 0x.* in main .*print_stacktrace.c}}
// CHECK-LONG-NEXT: {{#1 0x.* in main .*print_stacktrace.c}}
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/tysan/ptr-float.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void zero_array() {
for (i = 0; i < 1; ++i)
P[i] = 0.0f;
// CHECK: ERROR: TypeSanitizer: type-aliasing-violation
// CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type p1 float
// CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type float*
// CHECK: {{#0 0x.* in zero_array .*ptr-float.c:}}[[@LINE-3]]
}

Expand Down
34 changes: 33 additions & 1 deletion llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ static std::string encodeName(StringRef Name) {
return Output;
}

/// Converts pointer type names from TBAA "p2 int" style to C style ("int**").
/// Currently leaves "omnipotent char" unchanged - not sure of a user-friendly
/// name for this type. If the type name was changed, returns true and stores
/// the new type name in `Dest`. Otherwise, returns false (`Dest` is unchanged).
static bool convertTBAAStyleTypeNamesToCStyle(StringRef TypeName,
std::string &Dest) {
if (!TypeName.consume_front("p"))
return false;

int Indirection;
if (TypeName.consumeInteger(10, Indirection))
return false;

if (!TypeName.consume_front(" "))
return false;

Dest.clear();
Dest.reserve(TypeName.size() + Indirection); // One * per indirection
Dest.append(TypeName);
Dest.append(Indirection, '*');

return true;
}

std::string
TypeSanitizer::getAnonymousStructIdentifier(const MDNode *MD,
TypeNameMapTy &TypeNames) {
Expand Down Expand Up @@ -355,7 +379,15 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
// [2, member count, [type pointer, offset]..., name]

LLVMContext &C = MD->getContext();
Constant *NameData = ConstantDataArray::getString(C, NameNode->getString());
StringRef TypeName = NameNode->getString();

// Convert LLVM-internal TBAA-style type names to C-style type names
// (more user-friendly)
std::string CStyleTypeName;
if (convertTBAAStyleTypeNamesToCStyle(TypeName, CStyleTypeName))
TypeName = CStyleTypeName;

Constant *NameData = ConstantDataArray::getString(C, TypeName);
SmallVector<Type *> TDSubTys;
SmallVector<Constant *> TDSubData;

Expand Down