문제

나는 생각하고 있었고,내 손안에서 일부 jit compilataion(다만을 위해서 학습)과 좋은 것이라 그것을 가지고 작업 플랫폼 난 이후에 실행 모든 주요 세 가지를 집에서(windows,os x,linux).그 마음에,알고 싶은 경우가 있을 얻을 수 있는 방법을 사용하여 가상 메모리 windows 기능을 할당하는 메모리를 실행 권한이 있습니다.좋은 것을 사용하는 malloc 거나 새로운 관점 프로세서에서 같은 블록입니다.

Any tips?

도움이 되었습니까?

해결책

한 가지 가능성은 프로그램을 실행하는 Windows 설치가 Dep Alwaysoff (나쁜 아이디어) 또는 DEP Optout (더 나은 아이디어)로 구성되어야하는 요구 사항을 만드는 것입니다.

Boot.ini 파일을 변경하여 설정을 변경하여 다음을 구성 할 수 있습니다 (winxp sp2+ 및 win2k3 sp1+).

/noexecute=OptOut

그런 다음 개별 프로그램을 선택하여 선택하여 (XP에 따라) :

Start button
    Control Panel
        System
            Advanced tab
                Performance Settings button
                    Data Execution Prevention tab

이를 통해 즉시 생성 된 프로그램 내에서 코드를 실행할 수 있습니다. malloc() 블록.

이로 인해 프로그램은 DEP가 예방할 수있는 공격에 더 취약 해집니다.

Windows 2008에서는 다음과 같은 명령이있는 것 같습니다.

bcdedit.exe /set {current} nx OptOut

그러나 솔직히 말해서 플랫폼 의존적 코드를 최소화하려면 코드를 단일 함수로 분리하는 것만으로는 쉽습니다.

void *MallocWithoutDep(size_t sz) {
    #if defined _IS_WINDOWS
        return VirtualMalloc(sz, OPT_DEP_OFF); // or whatever
    #elif defined IS_LINUX
        // Do linuxy thing
    #elif defined IS_MACOS
        // Do something almost certainly inexplicable
    #endif
}

모든 플랫폼 종속 함수를 자체 파일에 넣으면 나머지 코드는 자동으로 플랫폼 공연입니다.

다른 팁

DEP 은 꺼 실행 권한에서 모든 코드 페이지의 메모리입니다.코드 응용 프로그램의 로드 메모리에 있는 실행에 필요한 비용은 이용자가 부담합니다고 있는 많은 JITs 에서 작동하는 윈도우/리눅스/에서라도,경우에도 DEP 가 활성화됩니다.이 때문에 있을 동적으로 메모리를 할당으로 필요한 권한 설정합니다.

일반적으로,일반 malloc 사용하지 않아야 하기 때문에 권한 있는 당 페이지입니다.맞추의 malloced 메모리 페이지가 아직도 가능 가격에 몇 가지 오버헤드가 발생합니다.당신이 사용하지 않습니다 malloc,일부 사용자 정의 메모리 관리(단위한 실행 가능한 코드).사용자 정의 관리는 일반적인 방법 JIT.

가 있는 솔루션을 크롬에서 프로젝트,사용하는 JIT 에 대한 V8 자바 스크립트 VM 고는 크로스-플랫폼입니다.수 크로스-플랫폼으로,필요한 기능을 여러 파일에 구현하고 그들은 선택한 컴파일때 정해진다.

리눅스:(크롬 src/v8/src/플랫폼-linux.cc)플래그가 PROT_EXEC 의 mmap().

void* OS::Allocate(const size_t requested,
                   size_t* allocated,
                   bool is_executable) {
  const size_t msize = RoundUp(requested, AllocateAlignment());
  int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
  void* addr = OS::GetRandomMmapAddr();
  void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (mbase == MAP_FAILED) {
    /** handle error */
    return NULL;
  }
  *allocated = msize;
  UpdateAllocatedSpaceLimits(mbase, msize);
  return mbase;
}

Win32(src/v8/src/플랫폼-win32.cc):깃발이 PAGE_EXECUTE_READWRITE 의 VirtualAlloc

void* OS::Allocate(const size_t requested,
                   size_t* allocated,
                   bool is_executable) {
  // The address range used to randomize RWX allocations in OS::Allocate
  // Try not to map pages into the default range that windows loads DLLs
  // Use a multiple of 64k to prevent committing unused memory.
  // Note: This does not guarantee RWX regions will be within the
  // range kAllocationRandomAddressMin to kAllocationRandomAddressMax
#ifdef V8_HOST_ARCH_64_BIT
  static const intptr_t kAllocationRandomAddressMin = 0x0000000080000000;
  static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
#else
  static const intptr_t kAllocationRandomAddressMin = 0x04000000;
  static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000;
#endif

  // VirtualAlloc rounds allocated size to page size automatically.
  size_t msize = RoundUp(requested, static_cast<int>(GetPageSize()));
  intptr_t address = 0;

  // Windows XP SP2 allows Data Excution Prevention (DEP).
  int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;

  // For exectutable pages try and randomize the allocation address
  if (prot == PAGE_EXECUTE_READWRITE &&
      msize >= static_cast<size_t>(Page::kPageSize)) {
    address = (V8::RandomPrivate(Isolate::Current()) << kPageSizeBits)
      | kAllocationRandomAddressMin;
    address &= kAllocationRandomAddressMax;
  }

  LPVOID mbase = VirtualAlloc(reinterpret_cast<void *>(address),
                              msize,
                              MEM_COMMIT | MEM_RESERVE,
                              prot);
  if (mbase == NULL && address != 0)
    mbase = VirtualAlloc(NULL, msize, MEM_COMMIT | MEM_RESERVE, prot);

  if (mbase == NULL) {
    LOG(ISOLATE, StringEvent("OS::Allocate", "VirtualAlloc failed"));
    return NULL;
  }

  ASSERT(IsAligned(reinterpret_cast<size_t>(mbase), OS::AllocateAlignment()));

  *allocated = msize;
  UpdateAllocatedSpaceLimits(mbase, static_cast<int>(msize));
  return mbase;
}

MacOS(src/v8/src/플랫폼 맥오.cc):깃발이 PROT_EXEC 의 mmap 처럼,리눅스 또는 다른 posix.

void* OS::Allocate(const size_t requested,
                   size_t* allocated,
                   bool is_executable) {
  const size_t msize = RoundUp(requested, getpagesize());
  int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
  void* mbase = mmap(OS::GetRandomMmapAddr(),
                     msize,
                     prot,
                     MAP_PRIVATE | MAP_ANON,
                     kMmapFd,
                     kMmapFdOffset);
  if (mbase == MAP_FAILED) {
    LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed"));
    return NULL;
  }
  *allocated = msize;
  UpdateAllocatedSpaceLimits(mbase, msize);
  return mbase;
}

그리고 또트 bcdedit.exe과 같은 방법으로만 사용해야 합니다 아주 오래된 프로그램을 생성하는 새로운 실행 코드 메모리에,하지만 세트 Exec 속성에 이 페이지로 이동합니다.에 대한 새로운 프로그램,firefox 또는 크롬/크롬 또는 현대 JIT,DEP 활동해야하고,JIT 이 관리하는 메모리를 사용 권한 세분화된 방식이다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top