[compiler-rt] [compiler-rt][ASan] Add function copying annotations (PR #91702)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 16 00:54:11 PDT 2024


================
@@ -0,0 +1,232 @@
+// RUN: %clangxx_asan -fexceptions -O %s -o %t && %env_asan_opts=detect_stack_use_after_return=0 %run %t
+//
+// Test __sanitizer_copy_contiguous_container_annotations.
+
+#include <algorithm>
+#include <deque>
+#include <numeric>
+
+#include <assert.h>
+#include <sanitizer/asan_interface.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static constexpr size_t kGranularity = 8;
+
+template <class T> static constexpr T RoundDown(T x) {
+  return reinterpret_cast<T>(reinterpret_cast<uintptr_t>(x) &
+                             ~(kGranularity - 1));
+}
+template <class T> static constexpr T RoundUp(T x) {
+  return (x == RoundDown(x))
+             ? x
+             : reinterpret_cast<T>(reinterpret_cast<uintptr_t>(RoundDown(x)) +
+                                   kGranularity);
+}
+
+static std::deque<int> GetPoisonedState(char *begin, char *end) {
+  std::deque<int> result;
+  for (char *ptr = begin; ptr != end; ++ptr) {
+    result.push_back(__asan_address_is_poisoned(ptr));
+  }
+  return result;
+}
+
+static void RandomPoison(char *beg, char *end) {
+  if (beg != RoundDown(beg) && RoundDown(beg) != RoundDown(end) &&
+      rand() % 2 == 1) {
+    __asan_poison_memory_region(beg, RoundUp(beg) - beg);
+    __asan_unpoison_memory_region(beg, rand() % (RoundUp(beg) - beg + 1));
+  }
+  for (beg = RoundUp(beg); beg + kGranularity <= end; beg += kGranularity) {
+    __asan_poison_memory_region(beg, kGranularity);
+    __asan_unpoison_memory_region(beg, rand() % (kGranularity + 1));
+  }
+  if (end > beg && __asan_address_is_poisoned(end)) {
+    __asan_poison_memory_region(beg, kGranularity);
+    __asan_unpoison_memory_region(beg, rand() % (end - beg + 1));
+  }
+}
+
+static size_t count_unpoisoned(std::deque<int> &poison_states, size_t n) {
+  size_t result = 0;
+  for (size_t i = 0; i < n && !poison_states.empty(); ++i) {
+    if (!poison_states.front()) {
+      result = i + 1;
+    }
+    poison_states.pop_front();
+  }
+
+  return result;
+}
+
+void TestNonOverlappingContainers(size_t capacity, size_t off_old,
+                                  size_t off_new, int poison_buffers) {
+  size_t old_buffer_size = capacity + off_old + kGranularity * 2;
+  size_t new_buffer_size = capacity + off_new + kGranularity * 2;
+  char *old_buffer = new char[old_buffer_size];
----------------
AdvenamTacet wrote:

Does it look good now? Or should I change more?

https://github.com/llvm/llvm-project/pull/91702


More information about the llvm-commits mailing list