On OSX using XCode 11.6 I'm building v8 as a static library (libv8_monolith.a). In one case I'm linking it into an Executable and everything is fine, in another case I'm linking it into a Bundle (dynamic library) and templated functions crash EXC_BAD_ACCESS:
For example AllocatePage():
memory-allocator.h
template <MemoryAllocator::AllocationMode alloc_mode = kRegular, typename SpaceType> EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
Page* AllocatePage(size_t size, SpaceType* owner, Executability executable);
extern template EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
Page* MemoryAllocator::AllocatePage<MemoryAllocator::kRegular, PagedSpace>(size_t size, PagedSpace* owner, Executability executable);
memory-allocator.cc
template <MemoryAllocator::AllocationMode alloc_mode, typename SpaceType>
Page* MemoryAllocator::AllocatePage(size_t size, SpaceType* owner, Executability executable) {
MemoryChunk* chunk = nullptr;
if (alloc_mode == kPooled) {
DCHECK_EQ(size, static_cast<size_t>(
MemoryChunkLayout::AllocatableMemoryInMemoryChunk(
owner->identity())));
DCHECK_EQ(executable, NOT_EXECUTABLE);
chunk = AllocatePagePooled(owner);
}
if (chunk == nullptr) {
chunk = AllocateChunk(size, size, executable, owner);
}
if (chunk == nullptr) return nullptr;
return owner->InitializePage(chunk);
}
template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
Page* MemoryAllocator::AllocatePage<MemoryAllocator::kRegular, PagedSpace>(size_t size, PagedSpace* owner, Executability executable);
If I instead write it as a non-templated version, and recompile libv8_monolith.a there's no crash:
Page* AllocatePage2(size_t size, PagedSpace* owner, Executability executable);
Page* MemoryAllocator::AllocatePage2(size_t size, PagedSpace* owner, Executability executable) {
MemoryChunk* chunk = nullptr;
if (chunk == nullptr) {
chunk = AllocateChunk(size, size, executable, owner);
}
if (chunk == nullptr) return nullptr;
return owner->InitializePage(chunk);
}
Note that none of these templated functions that crash are exposed externally (they're not part of the v8.h API), they're all code internal to v8 inside its "namespace internal".
Are there some compiler or linker flags I'm missing? Is this even something I should be doing? Do I have to compile v8 as a dynamic library to use it inside another dynamic library?