Support ASAN detection on clang (#1424)

Clang and GCC differ on how they detect Address Sanitizer. Support both.

Closes #1424

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/upb/pull/1424 from protocolbuffers:asan-clang 491a5ee4cfd24c8eb281f894de0cf4384525c46a
PiperOrigin-RevId: 553805994
pull/13675/head^2
Jason Lunn 1 year ago committed by Copybara-Service
parent ad2a557d96
commit 1b0011dfef
  1. 5
      .github/workflows/bazel_tests.yml
  2. 19
      upb/mem/arena.h
  3. 18
      upb/port/def.inc
  4. 2
      upb/port/undef.inc
  5. 3
      upb/reflection/oneof_def.c

@ -75,6 +75,11 @@ jobs:
run: echo "CC=${{ matrix.CC }}" >> $GITHUB_ENV
- name: Run tests
run: cd ${{ github.workspace }} && ${{ matrix.BAZEL }} ${{ matrix.startup-flags }} ${{ matrix.BAZEL_CMD || 'test' }} --test_output=errors ${{ steps.bazel-cache.outputs.cache_args }} ${{ matrix.targets || '...' }} ${{ matrix.flags }}
- uses: actions/upload-artifact@v3
with:
name: logs
path: |
**/*.log
no-python:
runs-on: ubuntu-20-large

@ -76,7 +76,8 @@ UPB_INLINE size_t _upb_ArenaHas(upb_Arena* a) {
UPB_API_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) {
size = UPB_ALIGN_MALLOC(size);
if (UPB_UNLIKELY(_upb_ArenaHas(a) < size)) {
size_t span = size + UPB_ASAN_GUARD_SIZE;
if (UPB_UNLIKELY(_upb_ArenaHas(a) < span)) {
return _upb_Arena_SlowMalloc(a, size);
}
@ -87,18 +88,7 @@ UPB_API_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) {
UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
UPB_UNPOISON_MEMORY_REGION(ret, size);
h->ptr += size;
#if UPB_ASAN
{
size_t guard_size = 32;
if (_upb_ArenaHas(a) >= guard_size) {
h->ptr += guard_size;
} else {
h->ptr = h->end;
}
}
#endif
h->ptr += span;
return ret;
}
@ -112,7 +102,8 @@ UPB_API_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr,
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
UPB_ASSERT((char*)ptr + oldsize == h->ptr); // Must be the last alloc.
// Must be the last alloc.
UPB_ASSERT((char*)ptr + oldsize == h->ptr - UPB_ASAN_GUARD_SIZE);
UPB_ASSERT(size <= oldsize);
h->ptr = (char*)ptr + size;
}

@ -273,8 +273,23 @@
* expected behavior.
*/
#if defined(__SANITIZE_ADDRESS__)
/* Due to preprocessor limitations, the conditional logic for setting
* UPN_CLANG_ASAN below cannot be consolidated into a portable one-liner.
* See https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html.
*/
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define UPB_CLANG_ASAN 1
#else
#define UPB_CLANG_ASAN 0
#endif
#else
#define UPB_CLANG_ASAN 0
#endif
#if defined(__SANITIZE_ADDRESS__) || UPB_CLANG_ASAN
#define UPB_ASAN 1
#define UPB_ASAN_GUARD_SIZE 32
#ifdef __cplusplus
extern "C" {
#endif
@ -289,6 +304,7 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
__asan_unpoison_memory_region((addr), (size))
#else
#define UPB_ASAN 0
#define UPB_ASAN_GUARD_SIZE 0
#define UPB_POISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
#define UPB_UNPOISON_MEMORY_REGION(addr, size) \

@ -62,6 +62,8 @@
#undef UPB_POISON_MEMORY_REGION
#undef UPB_UNPOISON_MEMORY_REGION
#undef UPB_ASAN
#undef UPB_ASAN_GUARD_SIZE
#undef UPB_CLANG_ASAN
#undef UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3
#undef UPB_DEPRECATED
#undef UPB_GNUC_MIN

@ -136,7 +136,8 @@ void _upb_OneofDef_Insert(upb_DefBuilder* ctx, upb_OneofDef* o,
// TODO(salo): More redundant work happening here.
const bool name_exists = upb_strtable_lookup2(&o->ntof, name, size, NULL);
if (UPB_UNLIKELY(name_exists)) {
_upb_DefBuilder_Errf(ctx, "oneof fields have the same name (%s)", name);
_upb_DefBuilder_Errf(ctx, "oneof fields have the same name (%.*s)",
(int)size, name);
}
const bool ok = upb_inttable_insert(&o->itof, number, v, ctx->arena) &&

Loading…
Cancel
Save