crypto: Mark pure allocating functions return values as restrict-like The returned pointers cannot possibly alias with any other pointers, we could use this fact to generate better code. With this annotation, we manage to drop unnecessary pointer aliasing checks which is around 12 KiB worth of dead code in libcrypto and libssl combined on x86_64 and around 6 KiB on AArch64. Signed-off-by: Xiangfei Ding <xfding@google.com> Change-Id: I9b6c60d56cb837f89584d6ca542160546a6a6964 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/105908 Reviewed-by: David Benjamin <davidben@google.com> Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/API-CONVENTIONS.md b/API-CONVENTIONS.md index c0cd64a..5c40c27 100644 --- a/API-CONVENTIONS.md +++ b/API-CONVENTIONS.md
@@ -78,6 +78,9 @@ - The bytes delimited from the beginning of the allocation pointed at by the pointer until `size` bytes after the pointer, exclusive, are valid for write. - `size` must have been no more than `PTRDIFF_MAX` bytes. +- Any non-`NULL` pointer returned is disjoint from every other live object, and + a zero-size allocation returns either `NULL` or a unique pointer that can be + passed to `OPENSSL_free`. ## Pointers and slices
diff --git a/crypto/mem.cc b/crypto/mem.cc index faedd53..0194438 100644 --- a/crypto/mem.cc +++ b/crypto/mem.cc
@@ -87,6 +87,17 @@ // `OPENSSL_memory_free` to zero out the memory before returning it to the // system. `OPENSSL_memory_free` will not be passed NULL pointers. // +// Because `OPENSSL_malloc` and related functions carry `OPENSSL_ATTR_MALLOC`, +// any non-NULL pointer returned by `OPENSSL_memory_alloc` or by a custom +// `OPENSSL_malloc` override must be disjoint from every other live allocation. +// Its contents are treated as uninitialized. Zero-size allocations must return +// a unique pointer distinct from all other live allocations so that it can be +// passed to `OPENSSL_memory_free` or `OPENSSL_free`, but never a shared +// non-NULL sentinel value. +// +// See https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-malloc +// for more details on the `malloc` protocol. +// // WARNING: These functions are called on every allocation and free in // BoringSSL across the entire process. They may be called by any code in the // process which calls BoringSSL, including in process initializers and thread
diff --git a/include/openssl/base.h b/include/openssl/base.h index 39afcde..60c599f 100644 --- a/include/openssl/base.h +++ b/include/openssl/base.h
@@ -180,6 +180,14 @@ #define OPENSSL_UNUSED #endif +// OPENSSL_ATTR_MALLOC marks allocation functions whose returned non-NULL +// pointers never alias existing objects. +#if defined(__GNUC__) || defined(__clang__) +#define OPENSSL_ATTR_MALLOC __attribute__((__malloc__)) +#else +#define OPENSSL_ATTR_MALLOC +#endif + #if defined(BORINGSSL_ALWAYS_USE_STATIC_INLINE) // Add OPENSSL_UNUSED so that, should an inline function be emitted via macro // (e.g. a `STACK_OF(T)` implementation) in a source file without tripping
diff --git a/include/openssl/mem.h b/include/openssl/mem.h index ca265d0..07d386f 100644 --- a/include/openssl/mem.h +++ b/include/openssl/mem.h
@@ -38,16 +38,17 @@ // private data. The resulting pointer must be freed with `OPENSSL_free`. In // the case of a malloc failure, prior to returning NULL `OPENSSL_malloc` will // push `ERR_R_MALLOC_FAILURE` onto the openssl error stack. -OPENSSL_EXPORT void *OPENSSL_malloc(size_t size); +OPENSSL_ATTR_MALLOC OPENSSL_EXPORT void *OPENSSL_malloc(size_t size); // OPENSSL_zalloc behaves like `OPENSSL_malloc` except it also initializes the // resulting memory to zero. -OPENSSL_EXPORT void *OPENSSL_zalloc(size_t size); +OPENSSL_ATTR_MALLOC OPENSSL_EXPORT void *OPENSSL_zalloc(size_t size); // OPENSSL_calloc is similar to a regular `calloc`, but allocates data with // `OPENSSL_malloc`. On overflow, it will push `ERR_R_OVERFLOW` onto the error // queue. -OPENSSL_EXPORT void *OPENSSL_calloc(size_t num, size_t size); +OPENSSL_ATTR_MALLOC OPENSSL_EXPORT void *OPENSSL_calloc(size_t num, + size_t size); // OPENSSL_realloc returns a pointer to a buffer of `new_size` bytes that // contains the contents of `ptr`. Unlike `realloc`, a new buffer is always @@ -79,7 +80,7 @@ OPENSSL_EXPORT uint32_t OPENSSL_strhash(const char *s); // OPENSSL_strdup has the same behaviour as strdup(3). -OPENSSL_EXPORT char *OPENSSL_strdup(const char *s); +OPENSSL_ATTR_MALLOC OPENSSL_EXPORT char *OPENSSL_strdup(const char *s); // OPENSSL_strnlen has the same behaviour as strnlen(3). OPENSSL_EXPORT size_t OPENSSL_strnlen(const char *s, size_t len); @@ -148,7 +149,8 @@ // OPENSSL_strndup returns an allocated, duplicate of `str`, which is, at most, // `size` bytes. The result is always NUL terminated. The memory allocated // must be freed with `OPENSSL_free`. -OPENSSL_EXPORT char *OPENSSL_strndup(const char *str, size_t size); +OPENSSL_ATTR_MALLOC OPENSSL_EXPORT char *OPENSSL_strndup(const char *str, + size_t size); // OPENSSL_memdup returns an allocated, duplicate of `size` bytes from `data` or // NULL on allocation failure. The memory allocated must be freed with @@ -167,7 +169,9 @@ // Deprecated functions. // CRYPTO_malloc calls `OPENSSL_malloc`. `file` and `line` are ignored. -OPENSSL_EXPORT void *CRYPTO_malloc(size_t size, const char *file, int line); +OPENSSL_ATTR_MALLOC OPENSSL_EXPORT void *CRYPTO_malloc(size_t size, + const char *file, + int line); // CRYPTO_realloc calls `OPENSSL_realloc`. `file` and `line` are ignored. OPENSSL_EXPORT void *CRYPTO_realloc(void *ptr, size_t new_size, @@ -190,7 +194,7 @@ OPENSSL_EXPORT size_t CRYPTO_secure_used(void); // OPENSSL_secure_malloc calls `OPENSSL_malloc`. -OPENSSL_EXPORT void *OPENSSL_secure_malloc(size_t size); +OPENSSL_ATTR_MALLOC OPENSSL_EXPORT void *OPENSSL_secure_malloc(size_t size); // OPENSSL_secure_clear_free calls `OPENSSL_clear_free`. OPENSSL_EXPORT void OPENSSL_secure_clear_free(void *ptr, size_t len);
diff --git a/util/idextractor/clang_ast_parser.go b/util/idextractor/clang_ast_parser.go index b7afc78..3f36ac6 100644 --- a/util/idextractor/clang_ast_parser.go +++ b/util/idextractor/clang_ast_parser.go
@@ -438,6 +438,7 @@ "FriendDecl", "NonTypeTemplateParmDecl", "ParmVarDecl", + "RestrictAttr", "StaticAssertDecl", "TemplateArgument", "TemplateTemplateParmDecl",