Skip to content
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

Labels are not (fully) relocated if bound before used #53

Closed
Zeex opened this issue Sep 21, 2014 · 3 comments
Closed

Labels are not (fully) relocated if bound before used #53

Zeex opened this issue Sep 21, 2014 · 3 comments
Labels

Comments

@Zeex
Copy link
Contributor

Zeex commented Sep 21, 2014

I was recently upgrading a project of mine to the latest asmjit (previously used the one from Google Code) and made it to the point where everything compiles successfully but there's a problem at runtime, specifically with dword_ptr and labels.

I made a test program to demonstrate it:

bug.cpp:

#include <asmjit/asmjit.h>

#ifdef OLD_ASMJIT
  using namespace AsmJit;
#else
  using namespace asmjit;
  using namespace asmjit::x86;
#endif

int main() {
  #ifdef OLD_ASMJIT
    X86Assembler _;
  #else
    JitRuntime runtime;
    X86Assembler _(&runtime);
  #endif

  Label label = _.newLabel();

  _.mov(eax, 0xdeadbeef);
  _.ret();

  _.bind(label);
  _.mov(eax, 0x11223344);
  _.ret();

  size_t offset = _.getCodeSize();
  _.lea(eax, dword_ptr(label));
  _.jmp(eax);

  void *code = _.make();
  void *func = static_cast<uint8_t*>(code) + offset;

  // Should return 0x11223344
  int ret = asmjit_cast<int(*)()>(func)();

  #ifdef OLD_ASMJIT
    MemoryManager::getGlobal()->free(code);
  #else
    runtime.release(code);
  #endif

  return ret;
}

CMakeLists.txt:

project(bug)
cmake_minimum_required(VERSION 3.0)

option(OLD_ASMJIT TRUE)

add_definitions(-DASMJIT_API=)
if(OLD_ASMJIT)
  add_definitions(-DOLD_ASMJIT)
endif()

set(ASMJIT_STATIC TRUE)

if(OLD_ASMJIT)
  add_subdirectory(asmjit-old/asmjit)
else()
  add_subdirectory(asmjit)
endif()

if(OLD_ASMJIT)
  include_directories(asmjit-old/asmjit/src)
else()
  include_directories(asmjit/src)
endif()

add_executable(bug bug.cpp)
target_link_libraries(bug asmjit)

The program was designed to work with both old and new asmjit. As you can see, it makes a simple function with X86Assembler and then calls it and returns the result.

If you compile it with the old version of asmjit it will jump back to labeland return 0x11223344, as expected. However, with the new (current) version jumps to the start of code and therefore returns 0xdeadbeef.

If it helps, I've done some debugging and found that the offset variable in X86Assembler::_relocCode() is 0 instead of the real offset. I guess that means something is missing in X86Assembler_emit().

Update:

Found a fix, see PR #53.

@Zeex Zeex changed the title Labels are not relocated if bound before used Labels are not (fully) relocated if bound before used Sep 21, 2014
@kobalicek
Copy link
Member

Thanks! Affects 32-bit code generation.

@kobalicek kobalicek added the bug label Sep 21, 2014
Zeex added a commit to Zeex/asmjit that referenced this issue Sep 21, 2014
kobalicek added a commit that referenced this issue Sep 21, 2014
…the RelocData list).

Refactored slightly so RelocData always uses `rd` variable name and added a scope into the buggy locations.
@kobalicek
Copy link
Member

Hi Zeex,

I opened another pull related to this, fixing also the second place where the exactly same bug happens.

kobalicek added a commit that referenced this issue Sep 21, 2014
@kobalicek
Copy link
Member

Merged, Closing this one and the original pull. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants