Skip to content

[libc++] Fix noexcept behaviour of operator new helper functions #74337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libcxx/src/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// in this shared library, so that they can be overridden by programs
// that define non-weak copies of the functions.

static void* operator_new_impl(std::size_t size) noexcept {
static void* operator_new_impl(std::size_t size) {
if (size == 0)
size = 1;
void* p;
Expand Down Expand Up @@ -87,7 +87,7 @@ _LIBCPP_WEAK void operator delete[](void* ptr, size_t) noexcept { ::operator del

# if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)

static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignment) noexcept {
static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignment) {
if (size == 0)
size = 1;
if (static_cast<size_t>(alignment) < sizeof(void*))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-exceptions
// UNSUPPORTED: sanitizer-new-delete

#include <new>
#include <cassert>
#include <limits>
#include <cstdlib>

struct construction_key {};
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_alloc(construction_key) : self(this) {}
const my_bad_alloc* const self;
};

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw my_bad_alloc(construction_key());
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new[](std::numeric_limits<std::size_t>::max());
(void)x;
assert(false);
} catch (my_bad_alloc const& e) {
assert(new_handler_called == 1);
assert(e.self == &e);
} catch (...) {
assert(false);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-exceptions
// UNSUPPORTED: sanitizer-new-delete

#include <new>
#include <cassert>
#include <limits>
#include <cstdlib>

struct construction_key {};
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_alloc(construction_key) : self(this) {}
const my_bad_alloc* const self;
};

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw my_bad_alloc(construction_key());
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new[](std::numeric_limits<std::size_t>::max(), static_cast<std::align_val_t>(32));
(void)x;
assert(false);
} catch (my_bad_alloc const& e) {
assert(new_handler_called == 1);
assert(e.self == &e);
} catch (...) {
assert(false);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-exceptions
// UNSUPPORTED: sanitizer-new-delete

#include <new>
#include <cassert>
#include <limits>
#include <cstdlib>

struct construction_key {};
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_alloc(construction_key) : self(this) {}
const my_bad_alloc* const self;
};

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw my_bad_alloc(construction_key());
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new[](std::numeric_limits<std::size_t>::max(), static_cast<std::align_val_t>(32), std::nothrow);
(void)x;
assert(x == NULL);
} catch (my_bad_alloc const& e) {
assert(new_handler_called == 1);
assert(e.self == &e);
} catch (...) {
assert(false);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-exceptions
// UNSUPPORTED: sanitizer-new-delete

#include <new>
#include <cassert>
#include <limits>
#include <cstdlib>

struct construction_key {};
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_alloc(construction_key) : self(this) {}
const my_bad_alloc* const self;
};

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw my_bad_alloc(construction_key());
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new[](std::numeric_limits<std::size_t>::max(), std::nothrow);
(void)x;
assert(x == NULL);
} catch (my_bad_alloc const& e) {
assert(new_handler_called == 1);
assert(e.self == &e);
} catch (...) {
assert(false);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-exceptions
// UNSUPPORTED: sanitizer-new-delete

#include <new>
#include <cassert>
#include <limits>
#include <cstdlib>

struct construction_key {};
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_alloc(construction_key) : self(this) {}
const my_bad_alloc* const self;
};

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw my_bad_alloc(construction_key());
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new(std::numeric_limits<std::size_t>::max());
(void)x;
assert(false);
} catch (my_bad_alloc const& e) {
assert(new_handler_called == 1);
assert(e.self == &e);
} catch (...) {
assert(false);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-exceptions
// UNSUPPORTED: sanitizer-new-delete

#include <new>
#include <cassert>
#include <limits>
#include <cstdlib>

struct construction_key {};
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_alloc(construction_key) : self(this) {}
const my_bad_alloc* const self;
};

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw my_bad_alloc(construction_key());
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new(std::numeric_limits<std::size_t>::max(), static_cast<std::align_val_t>(32));
(void)x;
assert(false);
} catch (my_bad_alloc const& e) {
assert(new_handler_called == 1);
assert(e.self == &e);
} catch (...) {
assert(false);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-exceptions
// UNSUPPORTED: sanitizer-new-delete

#include <new>
#include <cassert>
#include <limits>
#include <cstdlib>

struct construction_key {};
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_alloc(construction_key) : self(this) {}
const my_bad_alloc* const self;
};

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw my_bad_alloc(construction_key());
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new(std::numeric_limits<std::size_t>::max(), static_cast<std::align_val_t>(32), std::nothrow);
(void)x;
assert(x == NULL);
} catch (my_bad_alloc const& e) {
assert(new_handler_called == 1);
assert(e.self == &e);
} catch (...) {
assert(false);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-exceptions
// UNSUPPORTED: sanitizer-new-delete

#include <new>
#include <cassert>
#include <limits>
#include <cstdlib>

struct construction_key {};
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
my_bad_alloc(construction_key) : self(this) {}
const my_bad_alloc* const self;
};

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw my_bad_alloc(construction_key());
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new(std::numeric_limits<std::size_t>::max(), std::nothrow);
(void)x;
assert(x == NULL);
} catch (my_bad_alloc const& e) {
assert(new_handler_called == 1);
assert(e.self == &e);
} catch (...) {
assert(false);
}
return 0;
}
4 changes: 2 additions & 2 deletions libcxxabi/src/stdlib_new_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// in this shared library, so that they can be overridden by programs
// that define non-weak copies of the functions.

static void* operator_new_impl(std::size_t size) noexcept {
static void* operator_new_impl(std::size_t size) {
if (size == 0)
size = 1;
void* p;
Expand Down Expand Up @@ -107,7 +107,7 @@ void operator delete[](void* ptr, size_t) noexcept { ::operator delete[](ptr); }

#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)

static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignment) noexcept {
static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignment) {
if (size == 0)
size = 1;
if (static_cast<size_t>(alignment) < sizeof(void*))
Expand Down