Exemple #1
0
static void
SpecializeToMemory(uint8_t* prevMemoryBase, CodeSegment& cs, const Metadata& metadata,
                   ArrayBufferObjectMaybeShared& buffer)
{
#ifdef WASM_HUGE_MEMORY
    MOZ_RELEASE_ASSERT(metadata.boundsChecks.empty());
#else
    uint32_t limit = buffer.wasmBoundsCheckLimit();
    MOZ_RELEASE_ASSERT(IsValidBoundsCheckImmediate(limit));

    for (const BoundsCheck& check : metadata.boundsChecks)
        MacroAssembler::wasmPatchBoundsCheck(check.patchAt(cs.base()), limit);
#endif

#if defined(JS_CODEGEN_X86)
    uint8_t* memoryBase = buffer.dataPointerEither().unwrap(/* code patching */);
    if (prevMemoryBase != memoryBase) {
        for (MemoryPatch patch : metadata.memoryPatches) {
            void* patchAt = cs.base() + patch.offset;

            uint8_t* prevImm = (uint8_t*)X86Encoding::GetPointer(patchAt);
            MOZ_ASSERT(prevImm >= prevMemoryBase);

            uint32_t offset = prevImm - prevMemoryBase;
            MOZ_ASSERT(offset <= INT32_MAX);

            X86Encoding::SetPointer(patchAt, memoryBase + offset);
        }
    }
#else
    MOZ_RELEASE_ASSERT(metadata.memoryPatches.empty());
#endif
}
Exemple #2
0
static void
StaticallyLink(CodeSegment& cs, const LinkData& linkData, ExclusiveContext* cx)
{
    for (LinkData::InternalLink link : linkData.internalLinks) {
        uint8_t* patchAt = cs.base() + link.patchAtOffset;
        void* target = cs.base() + link.targetOffset;
        if (link.isRawPointerPatch())
            *(void**)(patchAt) = target;
        else
            Assembler::PatchInstructionImmediate(patchAt, PatchedImmPtr(target));
    }

    for (auto imm : MakeEnumeratedRange(SymbolicAddress::Limit)) {
        const Uint32Vector& offsets = linkData.symbolicLinks[imm];
        for (size_t i = 0; i < offsets.length(); i++) {
            uint8_t* patchAt = cs.base() + offsets[i];
            void* target = AddressOf(imm, cx);
            Assembler::PatchDataWithValueCheck(CodeLocationLabel(patchAt),
                                               PatchedImmPtr(target),
                                               PatchedImmPtr((void*)-1));
        }
    }

    // These constants are logically part of the code:

    *(double*)(cs.globalData() + NaN64GlobalDataOffset) = GenericNaN();
    *(float*)(cs.globalData() + NaN32GlobalDataOffset) = GenericNaN();
}
Exemple #3
0
bool A5InitLoader::isSupported(const CodeSegment &code, const uint32 offset, const uint32 size) throw() {
	// Check whether the name matches
	if (code.getName() != "%A5Init")
		return false;

	const byte *memory = _executable.getMemory();
	const uint32 memorySize = _executable.getMemorySize();

	// Check whether it only exports one function
	if (!code.is32BitSegment() && READ_UINT16_BE(memory + offset + 2) != 0x0001)
		return false;
	else if (READ_UINT32_BE(memory + offset + 8) != 0x00000001)
		return false;

	const uint32 internalOffset = (code.is32BitSegment() ? 46 : 10);
	const uint32 infoOffset = READ_UINT16_BE(memory + offset + internalOffset) + internalOffset;

	// Check whether the information area is still inside the memory dump
	if (offset + infoOffset + 16 >= memorySize)
		return false;

	const uint32 dataOffset = READ_UINT32_BE(memory + offset + infoOffset + 8);
	const uint32 relocationDataOffset = READ_UINT32_BE(memory + offset + infoOffset + 12);

	// Check whether the compressed data is still in the memory dump
	if (offset + dataOffset >= memorySize)
		return false;
	// Check whether the relocation data is still in the memory dump
	if (offset + relocationDataOffset >= memorySize)
		return false;

	// Looks like it is an %A5Init segment
	return true;
}
Exemple #4
0
void A5InitLoader::load(const CodeSegment &code, const uint32 offset, const uint32 size, std::ostream &out) throw(std::exception) {
	byte *memory = _executable.getMemory();

	const uint32 internalOffset = (code.is32BitSegment() ? 46 : 10);
	const uint32 infoOffset = READ_UINT16_BE(memory + offset + internalOffset) + internalOffset;
	const uint32 dataSize = READ_UINT32_BE(memory + offset + infoOffset + 0);
	const uint16 needLoadBit = READ_UINT16_BE(memory + offset + infoOffset + 4);
	const uint32 dataOffset = READ_UINT32_BE(memory + offset + infoOffset + 8);
	const uint32 relocationDataOffset = READ_UINT32_BE(memory + offset + infoOffset + 12);

	// Output various information about the %A5Init segment
	out << "%A5Init info data:\n"
	       "\tData size: " << dataSize << "\n"
	       "\tNeed to load: " << needLoadBit << "\n"
	       "\tData offset: " << dataOffset << "\n"
	       "\tRelocation offset: " << relocationDataOffset << std::endl;

	// Check whether we actually have to do some work
	if (needLoadBit != 1) {
		out << "A5 data does not need any initialization" << std::endl;
		return;
	}

	const Code0Segment &code0 = _executable.getCode0Segment();
	uint8 *dst = memory + code0.getApplicationGlobalsSize() - dataSize;

	// uncompress the world
	uncompressA5World(dst, memory + offset + infoOffset + dataOffset);

	// relocate the world
	relocateWorld(code0.getApplicationGlobalsSize(), dst, memory + offset + infoOffset + relocationDataOffset, out);

	// Mark segment as initialized
	WRITE_UINT16_BE(memory + offset + infoOffset + 4, 0);
}
Exemple #5
0
static void
SpecializeToMemory(CodeSegment& cs, const Metadata& metadata, HandleWasmMemoryObject memory)
{
    for (const BoundsCheck& check : metadata.boundsChecks)
        Assembler::UpdateBoundsCheck(check.patchAt(cs.code()), memory->buffer().byteLength());

#if defined(JS_CODEGEN_X86)
    uint8_t* base = memory->buffer().dataPointerEither().unwrap();
    for (const MemoryAccess& access : metadata.memoryAccesses) {
        // Patch memory pointer immediate.
        void* addr = access.patchMemoryPtrImmAt(cs.code());
        uint32_t disp = reinterpret_cast<uint32_t>(X86Encoding::GetPointer(addr));
        MOZ_ASSERT(disp <= INT32_MAX);
        X86Encoding::SetPointer(addr, (void*)(base + disp));
    }
#endif
}
Exemple #6
0
bool
Module::instantiateTable(JSContext* cx, const CodeSegment& cs, SharedTableVector* tables) const
{
    for (const TableDesc& tableDesc : metadata_->tables) {
        SharedTable table = Table::create(cx, tableDesc.kind, tableDesc.length);
        if (!table || !tables->emplaceBack(table))
            return false;

        for (size_t i = 0; i < table->length(); i++)
            table->array()[i] = cs.badIndirectCallCode();
    }

    for (const ElemSegment& seg : elemSegments_) {
        SharedTable& table = (*tables)[seg.tableIndex];
        MOZ_ASSERT(seg.offset + seg.elems.length() <= table->length());
        for (size_t i = 0; i < seg.elems.length(); i++)
            table->array()[seg.offset + i] = cs.code() + seg.elems[i];
    }

    return true;
}
Exemple #7
0
static void
SendCodeRangesToProfiler(CodeSegment& cs, const Bytes& bytecode, const Metadata& metadata)
{
    bool enabled = false;
#ifdef JS_ION_PERF
    enabled |= PerfFuncEnabled();
#endif
#ifdef MOZ_VTUNE
    enabled |= IsVTuneProfilingActive();
#endif
    if (!enabled)
        return;

    for (const CodeRange& codeRange : metadata.codeRanges) {
        if (!codeRange.isFunction())
            continue;

        uintptr_t start = uintptr_t(cs.base() + codeRange.begin());
        uintptr_t end = uintptr_t(cs.base() + codeRange.end());
        uintptr_t size = end - start;

        UTF8Bytes name;
        if (!metadata.getFuncName(&bytecode, codeRange.funcIndex(), &name))
            return;
        if (!name.append('\0'))
            return;

        // Avoid "unused" warnings
        (void)start;
        (void)size;

#ifdef JS_ION_PERF
        if (PerfFuncEnabled()) {
            const char* file = metadata.filename.get();
            unsigned line = codeRange.funcLineOrBytecode();
            unsigned column = 0;
            writePerfSpewerAsmJSFunctionMap(start, size, file, line, column, name.begin());
        }
#endif
#ifdef MOZ_VTUNE
        if (IsVTuneProfilingActive()) {
            unsigned method_id = iJIT_GetNewMethodID();
            if (method_id == 0)
                return;
            iJIT_Method_Load method;
            method.method_id = method_id;
            method.method_name = name.begin();
            method.method_load_address = (void*)start;
            method.method_size = size;
            method.line_number_size = 0;
            method.line_number_table = nullptr;
            method.class_id = 0;
            method.class_file_name = nullptr;
            method.source_file_name = nullptr;
            iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, (void*)&method);
        }
#endif
    }

    return;
}