Override operator new and delete in tests

This is necessary to ensure errno is not updated while allocating.

Allocators are allowed to update errno, even in case of success. As gvisor
uses matchers to check the value of errno, the tests might fail if errno is
overriden by an allocation done while building the matcher. Using a custom
implementation of new and delete ensures this is not the case.

PiperOrigin-RevId: 619238390
This commit is contained in:
gVisor bot
2024-03-26 10:39:52 -07:00
parent cc37e536cb
commit edbc2af9f7
5 changed files with 282 additions and 21 deletions
+1 -1
View File
@@ -1,8 +1,8 @@
load("@rules_license//rules:license.bzl", "license")
load("//tools:defs.bzl", "build_test", "gazelle", "go_path")
load("//tools/nogo:defs.bzl", "nogo_config")
load("//tools/yamltest:defs.bzl", "yaml_test")
load("//website:defs.bzl", "doc")
load("@rules_license//rules:license.bzl", "license")
package(
default_applicable_licenses = ["//:license"],
File diff suppressed because it is too large Load Diff
+8
View File
@@ -439,3 +439,11 @@ cc_library(
"@com_google_absl//absl/types:optional",
],
)
cc_library(
name = "errno_safe_allocator",
testonly = 1,
srcs = [
"errno_safe_allocator.cc",
],
)
+40
View File
@@ -0,0 +1,40 @@
// Copyright 2024 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// new and delete that preserves errno in case of success.
#include <errno.h>
#include <stdlib.h>
namespace {
void* errno_safe_malloc(size_t size) {
int original_errno = errno;
void* result = malloc(size);
if (result != nullptr) {
errno = original_errno;
}
return result;
}
void errno_safe_free(void* p) {
int original_errno = errno;
free(p);
errno = original_errno;
}
} // namespace
void* operator new(size_t size) { return errno_safe_malloc(size); }
void* operator new[](size_t size) { return errno_safe_malloc(size); }
void operator delete(void* p) { errno_safe_free(p); }
void operator delete[](void* p) { errno_safe_free(p); }
-18
View File
@@ -292,20 +292,6 @@ inline uint64_t ms_elapsed(const struct timespec& begin,
namespace internal {
// RAII class to restore errno. Used because malloc/new are not guaranteed to
// preserve errno.
class ErrnoRestorer {
public:
ErrnoRestorer() : saved_errno_(errno) {}
~ErrnoRestorer() { errno = saved_errno_; }
ErrnoRestorer(const ErrnoRestorer&) = delete;
ErrnoRestorer& operator=(const ErrnoRestorer&) = delete;
private:
int saved_errno_;
};
template <typename Container>
class ElementOfMatcher {
public:
@@ -351,8 +337,6 @@ class SyscallSuccessMatcher {
template <typename T>
operator ::testing::Matcher<T>() const {
ErrnoRestorer errno_restorer;
// E is one of three things:
// - T, or a type losslessly and implicitly convertible to T.
// - A monomorphic Matcher<T>.
@@ -409,7 +393,6 @@ class AnySuccessValueMatcher {
public:
template <typename T>
operator ::testing::Matcher<T>() const {
ErrnoRestorer errno_restorer;
return ::testing::MakeMatcher(new Impl<T>());
}
@@ -490,7 +473,6 @@ class SpecificErrnoMatcher : public ::testing::MatcherInterface<int> {
};
inline ::testing::Matcher<int> SpecificErrno(int const expected) {
ErrnoRestorer errno_restorer;
return ::testing::MakeMatcher(new SpecificErrnoMatcher(expected));
}