diff --git a/.github/workflows/bazel_tests.yml b/.github/workflows/bazel_tests.yml index 2cf83dfa11..caa5289645 100644 --- a/.github/workflows/bazel_tests.yml +++ b/.github/workflows/bazel_tests.yml @@ -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 diff --git a/upb/mem/arena.h b/upb/mem/arena.h index 4586b077e4..1ae0d37d17 100644 --- a/upb/mem/arena.h +++ b/upb/mem/arena.h @@ -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; } diff --git a/upb/port/def.inc b/upb/port/def.inc index c1cf36f287..e9a4ceeaff 100644 --- a/upb/port/def.inc +++ b/upb/port/def.inc @@ -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) \ diff --git a/upb/port/undef.inc b/upb/port/undef.inc index e1752f72f6..2a88977987 100644 --- a/upb/port/undef.inc +++ b/upb/port/undef.inc @@ -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 diff --git a/upb/reflection/oneof_def.c b/upb/reflection/oneof_def.c index e33dba953f..8ac974bab2 100644 --- a/upb/reflection/oneof_def.c +++ b/upb/reflection/oneof_def.c @@ -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) &&