Mercurial > hg > graal-jvmci-8
comparison src/share/vm/memory/allocation.cpp @ 10271:f9be75d21404
8012902: remove use of global operator new - take 2
Summary: The fix of 8010992, disable use of global operator new and new[] which caused failure on some tests. This takes two of the bugs also add ALLOW_OPERATOR_NEW_USAGE to prevent crash for third party code calling operator new of jvm on certain platforms.
Reviewed-by: coleenp, dholmes, zgu
Contributed-by: yumin.qi@oracle.com
author | minqi |
---|---|
date | Tue, 14 May 2013 09:41:12 -0700 |
parents | 746b070f5022 |
children | a1ebd310d5c1 ce9ecec70f99 |
comparison
equal
deleted
inserted
replaced
10269:a9270d9ecb13 | 10271:f9be75d21404 |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. | 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | 4 * |
5 * This code is free software; you can redistribute it and/or modify it | 5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as | 6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
47 #endif | 47 #endif |
48 #ifdef TARGET_OS_FAMILY_bsd | 48 #ifdef TARGET_OS_FAMILY_bsd |
49 # include "os_bsd.inline.hpp" | 49 # include "os_bsd.inline.hpp" |
50 #endif | 50 #endif |
51 | 51 |
52 void* StackObj::operator new(size_t size) { ShouldNotCallThis(); return 0; }; | 52 void* StackObj::operator new(size_t size) { ShouldNotCallThis(); return 0; } |
53 void StackObj::operator delete(void* p) { ShouldNotCallThis(); }; | 53 void StackObj::operator delete(void* p) { ShouldNotCallThis(); } |
54 void* _ValueObj::operator new(size_t size) { ShouldNotCallThis(); return 0; }; | 54 void* StackObj::operator new [](size_t size) { ShouldNotCallThis(); return 0; } |
55 void _ValueObj::operator delete(void* p) { ShouldNotCallThis(); }; | 55 void StackObj::operator delete [](void* p) { ShouldNotCallThis(); } |
56 | |
57 void* _ValueObj::operator new(size_t size) { ShouldNotCallThis(); return 0; } | |
58 void _ValueObj::operator delete(void* p) { ShouldNotCallThis(); } | |
59 void* _ValueObj::operator new [](size_t size) { ShouldNotCallThis(); return 0; } | |
60 void _ValueObj::operator delete [](void* p) { ShouldNotCallThis(); } | |
56 | 61 |
57 void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data, | 62 void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data, |
58 size_t word_size, bool read_only, TRAPS) { | 63 size_t word_size, bool read_only, TRAPS) { |
59 // Klass has it's own operator new | 64 // Klass has it's own operator new |
60 return Metaspace::allocate(loader_data, word_size, read_only, | 65 return Metaspace::allocate(loader_data, word_size, read_only, |
78 } | 83 } |
79 | 84 |
80 void MetaspaceObj::print_address_on(outputStream* st) const { | 85 void MetaspaceObj::print_address_on(outputStream* st) const { |
81 st->print(" {"INTPTR_FORMAT"}", this); | 86 st->print(" {"INTPTR_FORMAT"}", this); |
82 } | 87 } |
83 | |
84 | 88 |
85 void* ResourceObj::operator new(size_t size, allocation_type type, MEMFLAGS flags) { | 89 void* ResourceObj::operator new(size_t size, allocation_type type, MEMFLAGS flags) { |
86 address res; | 90 address res; |
87 switch (type) { | 91 switch (type) { |
88 case C_HEAP: | 92 case C_HEAP: |
95 break; | 99 break; |
96 default: | 100 default: |
97 ShouldNotReachHere(); | 101 ShouldNotReachHere(); |
98 } | 102 } |
99 return res; | 103 return res; |
104 } | |
105 | |
106 void* ResourceObj::operator new [](size_t size, allocation_type type, MEMFLAGS flags) { | |
107 return (address) operator new(size, type, flags); | |
100 } | 108 } |
101 | 109 |
102 void* ResourceObj::operator new(size_t size, const std::nothrow_t& nothrow_constant, | 110 void* ResourceObj::operator new(size_t size, const std::nothrow_t& nothrow_constant, |
103 allocation_type type, MEMFLAGS flags) { | 111 allocation_type type, MEMFLAGS flags) { |
104 //should only call this with std::nothrow, use other operator new() otherwise | 112 //should only call this with std::nothrow, use other operator new() otherwise |
116 ShouldNotReachHere(); | 124 ShouldNotReachHere(); |
117 } | 125 } |
118 return res; | 126 return res; |
119 } | 127 } |
120 | 128 |
129 void* ResourceObj::operator new [](size_t size, const std::nothrow_t& nothrow_constant, | |
130 allocation_type type, MEMFLAGS flags) { | |
131 return (address)operator new(size, nothrow_constant, type, flags); | |
132 } | |
121 | 133 |
122 void ResourceObj::operator delete(void* p) { | 134 void ResourceObj::operator delete(void* p) { |
123 assert(((ResourceObj *)p)->allocated_on_C_heap(), | 135 assert(((ResourceObj *)p)->allocated_on_C_heap(), |
124 "delete only allowed for C_HEAP objects"); | 136 "delete only allowed for C_HEAP objects"); |
125 DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = (uintptr_t)badHeapOopVal;) | 137 DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = (uintptr_t)badHeapOopVal;) |
126 FreeHeap(p); | 138 FreeHeap(p); |
139 } | |
140 | |
141 void ResourceObj::operator delete [](void* p) { | |
142 operator delete(p); | |
127 } | 143 } |
128 | 144 |
129 #ifdef ASSERT | 145 #ifdef ASSERT |
130 void ResourceObj::set_allocation_type(address res, allocation_type type) { | 146 void ResourceObj::set_allocation_type(address res, allocation_type type) { |
131 // Set allocation type in the resource object | 147 // Set allocation type in the resource object |
213 void trace_heap_free(void* p) { | 229 void trace_heap_free(void* p) { |
214 // A lock is not needed here - tty uses a lock internally | 230 // A lock is not needed here - tty uses a lock internally |
215 tty->print_cr("Heap free " INTPTR_FORMAT, p); | 231 tty->print_cr("Heap free " INTPTR_FORMAT, p); |
216 } | 232 } |
217 | 233 |
218 bool warn_new_operator = false; // see vm_main | |
219 | |
220 //-------------------------------------------------------------------------------------- | 234 //-------------------------------------------------------------------------------------- |
221 // ChunkPool implementation | 235 // ChunkPool implementation |
222 | 236 |
223 // MT-safe pool of chunks to reduce malloc/free thrashing | 237 // MT-safe pool of chunks to reduce malloc/free thrashing |
224 // NB: not using Mutex because pools are used before Threads are initialized | 238 // NB: not using Mutex because pools are used before Threads are initialized |
358 // Chunk implementation | 372 // Chunk implementation |
359 | 373 |
360 void* Chunk::operator new(size_t requested_size, size_t length) { | 374 void* Chunk::operator new(size_t requested_size, size_t length) { |
361 // requested_size is equal to sizeof(Chunk) but in order for the arena | 375 // requested_size is equal to sizeof(Chunk) but in order for the arena |
362 // allocations to come out aligned as expected the size must be aligned | 376 // allocations to come out aligned as expected the size must be aligned |
363 // to expected arean alignment. | 377 // to expected arena alignment. |
364 // expect requested_size but if sizeof(Chunk) doesn't match isn't proper size we must align it. | 378 // expect requested_size but if sizeof(Chunk) doesn't match isn't proper size we must align it. |
365 assert(ARENA_ALIGN(requested_size) == aligned_overhead_size(), "Bad alignment"); | 379 assert(ARENA_ALIGN(requested_size) == aligned_overhead_size(), "Bad alignment"); |
366 size_t bytes = ARENA_ALIGN(requested_size) + length; | 380 size_t bytes = ARENA_ALIGN(requested_size) + length; |
367 switch (length) { | 381 switch (length) { |
368 case Chunk::size: return ChunkPool::large_pool()->allocate(bytes); | 382 case Chunk::size: return ChunkPool::large_pool()->allocate(bytes); |
667 #ifndef PRODUCT | 681 #ifndef PRODUCT |
668 // The global operator new should never be called since it will usually indicate | 682 // The global operator new should never be called since it will usually indicate |
669 // a memory leak. Use CHeapObj as the base class of such objects to make it explicit | 683 // a memory leak. Use CHeapObj as the base class of such objects to make it explicit |
670 // that they're allocated on the C heap. | 684 // that they're allocated on the C heap. |
671 // Commented out in product version to avoid conflicts with third-party C++ native code. | 685 // Commented out in product version to avoid conflicts with third-party C++ native code. |
672 // %% note this is causing a problem on solaris debug build. the global | 686 // On certain platforms, such as Mac OS X (Darwin), in debug version, new is being called |
673 // new is being called from jdk source and causing data corruption. | 687 // from jdk source and causing data corruption. Such as |
674 // src/share/native/sun/awt/font/fontmanager/textcache/hsMemory.cpp::hsSoftNew | 688 // Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair |
675 // define CATCH_OPERATOR_NEW_USAGE if you want to use this. | 689 // define ALLOW_OPERATOR_NEW_USAGE for platform on which global operator new allowed. |
676 #ifdef CATCH_OPERATOR_NEW_USAGE | 690 // |
691 #ifndef ALLOW_OPERATOR_NEW_USAGE | |
677 void* operator new(size_t size){ | 692 void* operator new(size_t size){ |
678 static bool warned = false; | 693 assert(false, "Should not call global operator new"); |
679 if (!warned && warn_new_operator) | 694 return 0; |
680 warning("should not call global (default) operator new"); | 695 } |
681 warned = true; | 696 |
682 return (void *) AllocateHeap(size, "global operator new"); | 697 void* operator new [](size_t size){ |
683 } | 698 assert(false, "Should not call global operator new[]"); |
684 #endif | 699 return 0; |
700 } | |
701 | |
702 void* operator new(size_t size, const std::nothrow_t& nothrow_constant){ | |
703 assert(false, "Should not call global operator new"); | |
704 return 0; | |
705 } | |
706 | |
707 void* operator new [](size_t size, std::nothrow_t& nothrow_constant){ | |
708 assert(false, "Should not call global operator new[]"); | |
709 return 0; | |
710 } | |
711 | |
712 void operator delete(void* p) { | |
713 assert(false, "Should not call global delete"); | |
714 } | |
715 | |
716 void operator delete [](void* p) { | |
717 assert(false, "Should not call global delete []"); | |
718 } | |
719 #endif // ALLOW_OPERATOR_NEW_USAGE | |
685 | 720 |
686 void AllocatedObj::print() const { print_on(tty); } | 721 void AllocatedObj::print() const { print_on(tty); } |
687 void AllocatedObj::print_value() const { print_value_on(tty); } | 722 void AllocatedObj::print_value() const { print_value_on(tty); } |
688 | 723 |
689 void AllocatedObj::print_on(outputStream* st) const { | 724 void AllocatedObj::print_on(outputStream* st) const { |