Skip to content
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
1 change: 1 addition & 0 deletions projects/rocthrust/test/hipstdpar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ set(ROCPRIM_LOCATION ${ROCPRIM_INCLUDE_DIR})
add_hipstdpar_test("algorithms" "compile" OFF)
add_hipstdpar_test("interpose" "compile" ON)
add_hipstdpar_test("realloc" "run" ON)
add_hipstdpar_test("zero_size_alloc" "run" ON)

# Restore global state
set(CMAKE_CXX_STANDARD ${ROCTHRUST_CMAKE_CXX_STANDARD})
74 changes: 74 additions & 0 deletions projects/rocthrust/test/hipstdpar/test_zero_size_alloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// MIT License
//
// Copyright (c) 2026 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Regression test for zero-size allocations under --hipstdpar-interpose-alloc.
//
// __hipstdpar_aligned_alloc used to call hipMemAdvise(r, n, ...) unconditionally
// after a successful underlying allocation. hipMemAdvise rejects zero-length
// ranges regardless of pointer validity, so the wrapper discarded an already
// valid pointer and returned nullptr for any n == 0 request. That, in turn, made
// __hipstdpar_operator_new_aligned throw std::runtime_error for operator new(0),
// even though the standard requires zero-size allocation to succeed and return a
// valid, distinct, non-null pointer ([expr.new], [new.delete.single]).
//
// This test exercises operator new(0), new T[0], malloc(0), and calloc(0, 0):
// none of them may throw or abort, and operator new/new[] must return a
// non-null pointer.

#include <cstdlib>
#include <new>

int main()
{
try
{
// operator new(0) must succeed and return a valid, non-null pointer.
void* p = ::operator new(0);
if (!p)
{
return EXIT_FAILURE;
}
::operator delete(p);

// new T[0] must succeed and return a valid, non-null pointer.
int* arr = new int[0];
if (!arr)
{
return EXIT_FAILURE;
}
delete[] arr;

// malloc(0)/calloc(0, 0) are legally allowed to return nullptr, but must
// never crash or abort the process.
void* m = std::malloc(0);
std::free(m);

void* c = std::calloc(0, 0);
std::free(c);
}
catch (...)
{
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ extern "C" {
auto r = __hipstdpar_hidden_memalign(a, n);

if (!hipstd::__initialised) return r;
if (n == 0) return r; // hipMemAdvise rejects zero-length ranges; nothing to advise.

hipDevice_t d{};
hipGetDevice(&d);
Expand Down
Loading