Skip to content

Commit

Permalink
[compiler-rt][ASan] Add function moving annotations
Browse files Browse the repository at this point in the history
This PR adds a `__sanitizer_move_contiguous_container_annotations` function, which moves annotations from one memory area to another.
Old area is unpoisoned at the end. New area is annotated in the same way as the old region at the beginning (within limitations of ASan).

```cpp
void __sanitizer_move_contiguous_container_annotations(
    const void *old_storage_beg_p, const void *old_storage_end_p,
    const void *new_storage_beg_p, const void *new_storage_end_p) {
```

This function aims to help with short string annotations and similar container annotations.
Right now we change trait types of `std::basic_string` when compiling with ASan.

https://github.com/llvm/llvm-project/blob/87f3407856e61a73798af4e41b28bc33b5bf4ce6/libcxx/include/string#L738-L751

The goal is to not change `__trivially_relocatable` when compiling with ASan.
If this function is accpeted and upstreamed, the next step is creating function like `__memcpy_with_asan` moving memory with ASan.
And then using this function instead of `__builtin__memcpy` while moving trivially relocatable objects.

NOTICE: I did not test it yet, so it's probably not compiling, but I don't expect big changes. PR is WIP until I test it.

---

I'm thinking if there is a good way to address fact that in a container the new buffer is usually bigger than the previous one.
We may add two more arguments to the functions to address it (the beginning and the end of the whole buffer.

Another potential change is removing `new_storage_end_p` as it's redundant, because we require the same size.
  • Loading branch information
Advenam Tacet committed May 10, 2024
1 parent 95f208f commit 8ff3017
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 0 deletions.
24 changes: 24 additions & 0 deletions compiler-rt/include/sanitizer/common_interface_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,30 @@ void SANITIZER_CDECL __sanitizer_annotate_double_ended_contiguous_container(
const void *old_container_beg, const void *old_container_end,
const void *new_container_beg, const void *new_container_end);

/// Moves annotation from one storage to another.
/// At the end, new buffer is annotated in the same way as old buffer at
/// the very beginning. Old buffer is fully unpoisoned.
/// Main purpose of that function is use while moving trivially relocatable
/// objects, which memory may be poisoned (therefore not trivially with ASan).
///
/// A contiguous container is a container that keeps all of its elements
/// in a contiguous region of memory. The container owns the region of memory
/// <c>[old_storage_beg, old_storage_end)</c> and
/// <c>[new_storage_beg, new_storage_end)</c>;
/// There is no requirement where objects are kept.
/// Poisoned and non-poisoned memory areas can alternate,
/// there are no shadow memory restrictions.
///
/// Argument requirements:
/// New containert has to have the same size as the old container.
/// \param old_storage_beg Beginning of the old container region.
/// \param old_storage_end End of the old container region.
/// \param new_storage_beg Beginning of the new container region.
/// \param new_storage_end End of the new container region.
void SANITIZER_CDECL __sanitizer_move_contiguous_container_annotations(
const void *old_storage_beg, const void *old_storage_end,
const void *new_storage_beg, const void *new_storage_end);

/// Returns true if the contiguous container <c>[beg, end)</c> is properly
/// poisoned.
///
Expand Down
14 changes: 14 additions & 0 deletions compiler-rt/lib/asan/asan_errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,20 @@ void ErrorBadParamsToAnnotateDoubleEndedContiguousContainer::Print() {
ReportErrorSummary(scariness.GetDescription(), stack);
}

void ErrorBadParamsToMoveContiguousContainerAnnotations::Print() {
Report(
"ERROR: AddressSanitizer: bad parameters to "
"__sanitizer_move_contiguous_container_annotations:\n"
" old_storage_beg : %p\n"
" old_storage_end : %p\n"
" new_storage_beg : %p\n"
" new_storage_end : %p\n",
(void *)old_storage_beg, (void *)old_storage_end, (void *)new_storage_beg,
(void *)new_storage_end);
stack->Print();
ReportErrorSummary(scariness.GetDescription(), stack);
}

void ErrorODRViolation::Print() {
Decorator d;
Printf("%s", d.Error());
Expand Down
19 changes: 19 additions & 0 deletions compiler-rt/lib/asan/asan_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ struct ErrorBadParamsToAnnotateDoubleEndedContiguousContainer : ErrorBase {
void Print();
};

struct ErrorBadParamsToMoveContiguousContainerAnnotations : ErrorBase {
const BufferedStackTrace *stack;
uptr old_storage_beg, old_storage_end, new_storage_beg, new_storage_end;

ErrorBadParamsToMoveContiguousContainerAnnotations() = default; // (*)
ErrorBadParamsToMoveContiguousContainerAnnotations(
u32 tid, BufferedStackTrace *stack_, uptr old_storage_beg_,
uptr old_storage_end_, uptr new_storage_beg_, uptr new_storage_end_)
: ErrorBase(tid, 10,
"bad-__sanitizer_annotate_double_ended_contiguous_container"),
stack(stack_),
old_storage_beg(old_storage_beg_),
old_storage_end(old_storage_end_),
new_storage_beg(new_storage_beg_),
new_storage_end(new_storage_end_) {}
void Print();
};

struct ErrorODRViolation : ErrorBase {
__asan_global global1, global2;
u32 stack_id1, stack_id2;
Expand Down Expand Up @@ -421,6 +439,7 @@ struct ErrorGeneric : ErrorBase {
macro(StringFunctionSizeOverflow) \
macro(BadParamsToAnnotateContiguousContainer) \
macro(BadParamsToAnnotateDoubleEndedContiguousContainer) \
macro(BadParamsToMoveContiguousContainerAnnotations) \
macro(ODRViolation) \
macro(InvalidPointerPair) \
macro(Generic)
Expand Down
128 changes: 128 additions & 0 deletions compiler-rt/lib/asan/asan_poisoning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,134 @@ void __sanitizer_annotate_double_ended_contiguous_container(
}
}

// This function moves annotation from one buffer to another.
// Old buffer is unpoisoned at the end.
void __sanitizer_move_contiguous_container_annotations(
const void *old_storage_beg_p, const void *old_storage_end_p,
const void *new_storage_beg_p, const void *new_storage_end_p) {
if (!flags()->detect_container_overflow)
return;

VPrintf(2, "contiguous_container_old: %p %p\n", old_storage_beg_p,
old_storage_end_p);
VPrintf(2, "contiguous_container_new: %p %p\n", new_storage_beg_p,
new_storage_end_p);

uptr old_storage_beg = reinterpret_cast<uptr>(old_storage_beg_p);
uptr old_storage_end = reinterpret_cast<uptr>(old_storage_end_p);
uptr new_storage_beg = reinterpret_cast<uptr>(new_storage_beg_p);
uptr new_storage_end = reinterpret_cast<uptr>(new_storage_end_p);

constexpr uptr granularity = ASAN_SHADOW_GRANULARITY;

if (!(old_storage_beg <= old_storage_end) ||
!(new_storage_beg <= new_storage_end) ||
(old_storage_end - old_storage_beg) !=
(new_storage_end - new_storage_beg)) {
GET_STACK_TRACE_FATAL_HERE;
ReportBadParamsToMoveContiguousContainerAnnotations(
old_storage_beg, old_storage_end, new_storage_beg, new_storage_end,
&stack);
}

uptr new_internal_beg = RoundUpTo(new_storage_beg, granularity);
uptr old_internal_beg = RoundUpTo(old_storage_beg, granularity);
uptr new_external_beg = RoundDownTo(new_storage_beg, granularity);
uptr old_external_beg = RoundDownTo(old_storage_beg, granularity);
uptr new_internal_end = RoundDownTo(new_storage_end, granularity);
uptr old_internal_end = RoundDownTo(old_storage_end, granularity);

// At the very beginning we poison the whole buffer.
// Later we unpoison what is necessary.
PoisonShadow(new_internal_beg, new_internal_end - new_internal_beg,
kAsanContiguousContainerOOBMagic);
if (new_internal_beg != new_storage_beg) {
uptr new_unpoisoned = *(u8 *)MemToShadow(new_external_beg);
if (new_unpoisoned > (new_storage_beg - new_external_beg)) {
*(u8 *)MemToShadow(new_external_beg) =
static_cast<u8>(new_storage_beg - new_external_beg);
}
}
if (new_internal_end != new_storage_end) {
uptr new_unpoisoned = *(u8 *)MemToShadow(new_internal_end);
if (new_unpoisoned <= (new_storage_end - new_internal_end)) {
*(u8 *)MemToShadow(new_external_beg) =
static_cast<u8>(kAsanContiguousContainerOOBMagic);
}
}

// There are two cases.
// 1) Distance between buffers is granule-aligned.
// 2) It's not aligned, that case is slower.
if (old_storage_beg % granularity == new_storage_beg % granularity) {
// When buffers are aligned in the same way, we can just copy shadow memory,
// except first and last granule.
__builtin_memcpy((u8 *)MemToShadow(new_internal_beg),
(u8 *)MemToShadow(old_internal_beg),
(new_internal_end - new_internal_beg) / granularity);
// In first granule we cannot poison anything before beginning of the
// container.
if (new_internal_beg != new_storage_beg) {
uptr old_unpoisoned = *(u8 *)MemToShadow(old_external_beg);
uptr new_unpoisoned = *(u8 *)MemToShadow(new_external_beg);

if (old_unpoisoned > old_storage_beg - old_external_beg) {
*(u8 *)MemToShadow(new_external_beg) = old_unpoisoned;
} else if (new_unpoisoned > new_storage_beg - new_external_beg) {
*(u8 *)MemToShadow(new_external_beg) =
new_storage_beg - new_external_beg;
}
}
// In last granule we cannot poison anything after the end of the container.
if (new_internal_end != new_storage_end) {
uptr old_unpoisoned = *(u8 *)MemToShadow(old_internal_end);
uptr new_unpoisoned = *(u8 *)MemToShadow(new_internal_end);
if (new_unpoisoned <= new_storage_end - new_internal_end &&
old_unpoisoned < new_unpoisoned) {
*(u8 *)MemToShadow(new_internal_end) = old_unpoisoned;
}
}
} else {
// If buffers are not aligned, we have to go byte by byte.
uptr old_ptr = old_storage_beg;
uptr new_ptr = new_storage_beg;
uptr next_new;
for (; new_ptr + granularity <= new_storage_end;) {
next_new = RoundUpTo(new_ptr + 1, granularity);
uptr unpoison_to = 0;
for (; new_ptr != next_new; ++new_ptr, ++old_ptr) {
if (!AddressIsPoisoned(old_ptr)) {
unpoison_to = new_ptr + 1;
}
}
if (unpoison_to != 0) {
uptr granule_beg = new_ptr - granularity;
uptr value = unpoison_to - granule_beg;
*(u8 *)MemToShadow(granule_beg) = static_cast<u8>(value);
}
}
// Only case left is the end of the container in the middle of a granule.
// If memory after the end is unpoisoned, we cannot change anything.
// But if it's poisoned, we should unpoison as little as possible.
if (new_ptr != new_storage_end && AddressIsPoisoned(new_storage_end)) {
uptr unpoison_to = 0;
for (; new_ptr != new_storage_end; ++new_ptr, ++old_ptr) {
if (!AddressIsPoisoned(old_ptr)) {
unpoison_to = new_ptr + 1;
}
}
if (unpoison_to != 0) {
uptr granule_beg = RoundDownTo(new_storage_end, granularity);
uptr value = unpoison_to - granule_beg;
*(u8 *)MemToShadow(granule_beg) = static_cast<u8>(value);
}
}
}

__asan_unpoison_memory_region((void *)old_storage_beg,
old_storage_end - old_storage_beg);
}

static const void *FindBadAddress(uptr begin, uptr end, bool poisoned) {
CHECK_LE(begin, end);
constexpr uptr kMaxRangeToCheck = 32;
Expand Down
10 changes: 10 additions & 0 deletions compiler-rt/lib/asan/asan_report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ void ReportBadParamsToAnnotateDoubleEndedContiguousContainer(
in_report.ReportError(error);
}

void ReportBadParamsToMoveContiguousContainerAnnotations(
uptr old_storage_beg, uptr old_storage_end, uptr new_storage_beg,
uptr new_storage_end, BufferedStackTrace *stack) {
ScopedInErrorReport in_report;
ErrorBadParamsToMoveContiguousContainerAnnotations error(
GetCurrentTidOrInvalid(), stack, old_storage_beg, old_storage_end,
new_storage_beg, new_storage_end);
in_report.ReportError(error);
}

void ReportODRViolation(const __asan_global *g1, u32 stack_id1,
const __asan_global *g2, u32 stack_id2) {
ScopedInErrorReport in_report;
Expand Down
3 changes: 3 additions & 0 deletions compiler-rt/lib/asan/asan_report.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ void ReportBadParamsToAnnotateDoubleEndedContiguousContainer(
uptr storage_beg, uptr storage_end, uptr old_container_beg,
uptr old_container_end, uptr new_container_beg, uptr new_container_end,
BufferedStackTrace *stack);
void ReportBadParamsToMoveContiguousContainerAnnotations(
uptr old_storage_beg, uptr old_storage_end, uptr new_storage_beg,
uptr new_storage_end, BufferedStackTrace *stack);

void ReportODRViolation(const __asan_global *g1, u32 stack_id1,
const __asan_global *g2, u32 stack_id2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
INTERFACE_FUNCTION(__sanitizer_acquire_crash_state)
INTERFACE_FUNCTION(__sanitizer_annotate_contiguous_container)
INTERFACE_FUNCTION(__sanitizer_annotate_double_ended_contiguous_container)
INTERFACE_FUNCTION(__sanitizer_move_contiguous_container_annotations)
INTERFACE_FUNCTION(__sanitizer_contiguous_container_find_bad_address)
INTERFACE_FUNCTION(
__sanitizer_double_ended_contiguous_container_find_bad_address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ void __sanitizer_annotate_double_ended_contiguous_container(
const void *old_container_beg, const void *old_container_end,
const void *new_container_beg, const void *new_container_end);
SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_move_contiguous_container_annotations(
const void *old_storage_beg, const void *old_storage_end,
const void *new_storage_beg, const void *new_storage_end);
SANITIZER_INTERFACE_ATTRIBUTE
int __sanitizer_verify_contiguous_container(const void *beg, const void *mid,
const void *end);
SANITIZER_INTERFACE_ATTRIBUTE
Expand Down
Loading

0 comments on commit 8ff3017

Please sign in to comment.