1

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:

XCode Crash Callstack

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?

DougTanner
  • 41
  • 1
  • 6
  • Do you have the implementation of the function templates in your `.cpp` file? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Ted Lyngmo Aug 24 '20 at 14:27
  • 1
    Do you have a debugger? Can you describe how and *exactly where* the crash happens? (you should have a call stack and the like) What do you mean by "exposed externally" **exactly**? Where is `ConcurrentBitmap<>` defined? What code calls `marking_bitmap`? What is done with the return value? – Yakk - Adam Nevraumont Aug 24 '20 at 14:33
  • @Yakk-AdamNevraumont I've updated the question with a better example and a screenshot of the crash. – DougTanner Aug 25 '20 at 14:39

1 Answers1

0

It turns out After Effects has its own v8 dynamic library inside it, so when After Effects was loading our dynamic library there was a name clash between some of the functions. My solution for now will be to recompile v8 with changed namespaces to avoid the conflict.

DougTanner
  • 41
  • 1
  • 6