Skip to content

8299254: Support dealing with standard assert macro #2842

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

Closed
wants to merge 2 commits into from
Closed
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: 3 additions & 1 deletion make/hotspot/lib/JvmFlags.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ JVM_CFLAGS_TARGET_DEFINES += \
#

ifeq ($(DEBUG_LEVEL), release)
# release builds disable uses of assert macro from <assert.h>.
JVM_CFLAGS_DEBUGLEVEL := -DNDEBUG
# For hotspot, release builds differ internally between "optimized" and "product"
# in that "optimize" does not define PRODUCT.
ifneq ($(HOTSPOT_DEBUG_LEVEL), optimized)
JVM_CFLAGS_DEBUGLEVEL := -DPRODUCT
JVM_CFLAGS_DEBUGLEVEL += -DPRODUCT
endif
else ifeq ($(DEBUG_LEVEL), fastdebug)
JVM_CFLAGS_DEBUGLEVEL := -DASSERT
Expand Down
36 changes: 36 additions & 0 deletions src/hotspot/share/utilities/vmassert_reinstall.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

// Intentionally no #include guard. May be included multiple times for effect.

// See vmassert_uninstall.hpp for usage.

// Remove possible stdlib assert macro (or any others, for that matter).
#undef assert

// Reinstall HotSpot's assert macro, if previously defined.
#ifdef vmassert
#define assert(p, ...) vmassert(p, __VA_ARGS__)
#endif

45 changes: 45 additions & 0 deletions src/hotspot/share/utilities/vmassert_uninstall.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

// Intentionally no #include guard. May be included multiple times for effect.

// The files vmassert_uninstall.hpp and vmassert_reinstall.hpp provide a
// workaround for the name collision between HotSpot's assert macro and the
// Standard Library's assert macro. When including a 3rd-party header that
// uses (and so includes) the standard assert macro, wrap that inclusion with
// includes of these two files, e.g.
//
// #include "utilities/vmassert_uninstall.hpp"
// #include <header including standard assert macro>
// #include "utilities/vmassert_reinstall.hpp"
//
// This removes the HotSpot macro definition while pre-processing the
// 3rd-party header, then reinstates the HotSpot macro (if previously defined)
// for following code.

// Remove HotSpot's assert macro, if present.
#ifdef vmassert
#undef assert
#endif // vmassert

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,10 +24,13 @@
#include "precompiled.hpp"
#include "gc/shared/memset_with_concurrent_readers.hpp"
#include "utilities/globalDefinitions.hpp"
#include "unittest.hpp"

#include "utilities/vmassert_uninstall.hpp"
#include <string.h>
#include <sstream>
#include "utilities/vmassert_reinstall.hpp"

#include "unittest.hpp"

static unsigned line_byte(const char* line, size_t i) {
return unsigned(line[i]) & 0xFF;
Expand Down
8 changes: 5 additions & 3 deletions test/hotspot/gtest/jfr/test_networkUtilization.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -42,11 +42,13 @@
#include "utilities/globalDefinitions.hpp"
#include "utilities/growableArray.hpp"

#include "unittest.hpp"

#include "utilities/vmassert_uninstall.hpp"
#include <vector>
#include <list>
#include <map>
#include "utilities/vmassert_reinstall.hpp"

#include "unittest.hpp"

namespace {

Expand Down
14 changes: 2 additions & 12 deletions test/hotspot/gtest/unittest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,9 @@
#include <stdio.h>

#define GTEST_DONT_DEFINE_TEST 1
#include "utilities/vmassert_uninstall.hpp"
#include "gtest/gtest.h"

// gtest/gtest.h includes assert.h which will define the assert macro, but hotspot has its
// own standards incompatible assert macro that takes two parameters.
// The workaround is to undef assert and then re-define it. The re-definition
// must unfortunately be copied since debug.hpp might already have been
// included and a second include wouldn't work due to the header guards in debug.hpp.
#ifdef assert
#undef assert
#ifdef vmassert
#define assert(p, ...) vmassert(p, __VA_ARGS__)
#endif
#endif
#include "utilities/vmassert_reinstall.hpp"

#define CONCAT(a, b) a ## b

Expand Down