From e83aeba59505597122a1aa123c0fb5b80887bdd3 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 16 Aug 2021 12:29:01 -0700 Subject: [PATCH 1/3] Align arena initial block to ensure allocations are aligned. --- tests/test_generated_code.c | 11 +++++++++++ upb/upb.c | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/tests/test_generated_code.c b/tests/test_generated_code.c index 2afdefd02c..92dce0b44b 100644 --- a/tests/test_generated_code.c +++ b/tests/test_generated_code.c @@ -532,6 +532,16 @@ void test_arena_decode(void) { upb_arena_free(tmp); } +void test_arena_unaligned(void) { + char buf1[1024]; + // Force the pointer to be unaligned. + char *unaligned_buf_ptr = (char*)((uintptr_t)buf1 | 7); + upb_arena *arena = upb_arena_init( + unaligned_buf_ptr, &buf1[sizeof(buf1)] - unaligned_buf_ptr, NULL); + char *mem = upb_arena_malloc(arena, 5); + ASSERT(((uintptr_t)mem & 15) == 0); +} + int run_tests(int argc, char *argv[]) { test_scalars(); test_utf8(); @@ -544,5 +554,6 @@ int run_tests(int argc, char *argv[]) { test_arena_fuse(); test_arena_fuse_with_initial_block(); test_arena_decode(); + test_arena_unaligned(); return 0; } diff --git a/upb/upb.c b/upb/upb.c index fd0290b3b1..2238411b54 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -210,6 +210,11 @@ upb_arena *arena_initslow(void *mem, size_t n, upb_alloc *alloc) { upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { upb_arena *a; + /* Align initial pointer up so that we return properly-aligned pointers. */ + void *aligned = (void*)UPB_ALIGN_UP((uintptr_t)mem, 16); + n -= (uintptr_t)aligned - (uintptr_t)mem; + mem = aligned; + /* Round block size down to alignof(*a) since we will allocate the arena * itself at the end. */ n = UPB_ALIGN_DOWN(n, UPB_ALIGN_OF(upb_arena)); From a669587817810725ead06cbbb2ffe37fc0ccdda2 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 16 Aug 2021 13:05:28 -0700 Subject: [PATCH 2/3] Fixed the edge case where rounding up causes overflow. --- tests/test_generated_code.c | 7 +++++++ upb/upb.c | 11 +++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/test_generated_code.c b/tests/test_generated_code.c index 92dce0b44b..f4983b484e 100644 --- a/tests/test_generated_code.c +++ b/tests/test_generated_code.c @@ -540,6 +540,13 @@ void test_arena_unaligned(void) { unaligned_buf_ptr, &buf1[sizeof(buf1)] - unaligned_buf_ptr, NULL); char *mem = upb_arena_malloc(arena, 5); ASSERT(((uintptr_t)mem & 15) == 0); + upb_arena_free(arena); + + // Try the same, but with a size so small that aligning up will overflow. + arena = upb_arena_init(unaligned_buf_ptr, 5, &upb_alloc_global); + mem = upb_arena_malloc(arena, 5); + ASSERT(((uintptr_t)mem & 15) == 0); + upb_arena_free(arena); } int run_tests(int argc, char *argv[]) { diff --git a/upb/upb.c b/upb/upb.c index 2238411b54..48621b9f6e 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -210,10 +210,13 @@ upb_arena *arena_initslow(void *mem, size_t n, upb_alloc *alloc) { upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { upb_arena *a; - /* Align initial pointer up so that we return properly-aligned pointers. */ - void *aligned = (void*)UPB_ALIGN_UP((uintptr_t)mem, 16); - n -= (uintptr_t)aligned - (uintptr_t)mem; - mem = aligned; + if (n) { + /* Align initial pointer up so that we return properly-aligned pointers. */ + void *aligned = (void*)UPB_ALIGN_UP((uintptr_t)mem, 16); + size_t delta = (uintptr_t)aligned - (uintptr_t)mem; + n = delta <= n ? n - delta : 0; + mem = aligned; + } /* Round block size down to alignof(*a) since we will allocate the arena * itself at the end. */ From eabb77458ab0f8059a5021e009a99301102b3b37 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 16 Aug 2021 16:44:27 -0700 Subject: [PATCH 3/3] Fixes to make upb's tests compatible with a minimal Docker container. --- WORKSPACE | 7 +++---- bazel/workspace_deps.bzl | 8 +++----- benchmarks/gen_protobuf_binary_cc.py | 2 +- benchmarks/gen_synthetic_protos.py | 2 +- benchmarks/gen_upb_binary_c.py | 2 +- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 72c5d53804..2b03f3e927 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,7 +1,6 @@ workspace(name = "upb") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") load("//bazel:workspace_deps.bzl", "upb_deps") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") @@ -32,11 +31,11 @@ http_archive( sha256 = "59f918c8ccd4d74b6ac43484467b500f1d64b40cc1010daa055375b322a43ba3", ) -new_git_repository( +http_archive( name = "com_google_googleapis", - remote = "https://github.com/googleapis/googleapis.git", - branch = "master", + urls = ["https://github.com/googleapis/googleapis/archive/refs/heads/master.zip"], build_file = "//benchmarks:BUILD.googleapis", + strip_prefix = "googleapis-master", patch_cmds = ["find google -type f -name BUILD.bazel -delete"], ) diff --git a/bazel/workspace_deps.bzl b/bazel/workspace_deps.bzl index 23b81dc5f9..7ac050207c 100644 --- a/bazel/workspace_deps.bzl +++ b/bazel/workspace_deps.bzl @@ -1,14 +1,12 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") def upb_deps(): maybe( - git_repository, + http_archive, name = "com_google_absl", - commit = "998805a4c79d5d7a771f7e5a8ee3cbbbcba04f94", - remote = "https://github.com/abseil/abseil-cpp.git", - shallow_since = "1583355457 -0500", + url = "https://github.com/abseil/abseil-cpp/archive/refs/heads/master.zip", + strip_prefix = "abseil-cpp-master", ) maybe( diff --git a/benchmarks/gen_protobuf_binary_cc.py b/benchmarks/gen_protobuf_binary_cc.py index 66c2df603d..41fb100ed5 100644 --- a/benchmarks/gen_protobuf_binary_cc.py +++ b/benchmarks/gen_protobuf_binary_cc.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # # Copyright (c) 2009-2021, Google LLC # All rights reserved. diff --git a/benchmarks/gen_synthetic_protos.py b/benchmarks/gen_synthetic_protos.py index 54003d2b2d..99f83c15e1 100644 --- a/benchmarks/gen_synthetic_protos.py +++ b/benchmarks/gen_synthetic_protos.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # # Copyright (c) 2009-2021, Google LLC # All rights reserved. diff --git a/benchmarks/gen_upb_binary_c.py b/benchmarks/gen_upb_binary_c.py index d5c0d1db85..912d82ac98 100644 --- a/benchmarks/gen_upb_binary_c.py +++ b/benchmarks/gen_upb_binary_c.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # # Copyright (c) 2009-2021, Google LLC # All rights reserved.