Exemple #1
0
static void patch_aarch64(char *writeEntry, const char *execEntry,
        int stubSize, void *incrementPtr)
{
#if defined(__aarch64__)
    const uint32_t tmpl[] = {
        // ldr x0, 1f
        0x580000a0,
        // ldr x1, [x0]
        0xf9400001,
        // add x1, x1, #1
        0x91000421,
        // str x1, [x0]
        0xf9000001,
        // br x30
        0xd61f03c0,
        // 1:
        0x00000000, 0x00000000,
    };

    static const int offsetAddr = sizeof(tmpl) - 8;

    if (stubSize < sizeof(tmpl)) {
        return;
    }

    memcpy(writeEntry, tmpl, sizeof(tmpl));
    *((uint64_t *)(writeEntry + offsetAddr)) = (uint64_t) incrementPtr;

    __builtin___clear_cache((char *) execEntry, (char *) (execEntry + sizeof(tmpl)));
#else
    assert(0); // Should not be calling this
#endif
}
void SetDispatchFuncPointer(GLVNDGenEntrypoint *entry,
        GLVNDentrypointStub dispatch)
{
    uint8_t *code = entry->entrypointWrite;

#if defined(USE_X86_ASM)
    // For x86, we use a JMP instruction with a PC-relative offset. Figure out
    // the offset from the generated entrypoint to the dispatch function.
    intptr_t offset = ((intptr_t) dispatch) - ((intptr_t) entry->entrypointExec) - DISPATCH_FUNC_OFFSET_REL;
    *((intptr_t *) (code + DISPATCH_FUNC_OFFSET)) = offset;

#elif defined(USE_X86_64_ASM)
    // For x86_64, we have to use a movabs instruction, which needs the
    // absolute address of the dispatch function.
    *((GLVNDentrypointStub *) (code + DISPATCH_FUNC_OFFSET)) = dispatch;

#elif defined(USE_ARMV7_ASM)
    *((uint32_t *)(code + DISPATCH_FUNC_OFFSET)) = (uint32_t)dispatch;

    // Make sure the base address has the Thumb mode bit
    assert((uintptr_t)entry->entrypointExec & (uintptr_t)0x1);

    // See http://community.arm.com/groups/processors/blog/2010/02/17/caches-and-self-modifying-code
    __builtin___clear_cache((char *)entry->entrypointExec - 1,
                            (char *)entry->entrypointExec + sizeof(STUB_TEMPLATE));
#else
#error "Can't happen -- not implemented"
#endif
}
Exemple #3
0
int CodeCache::cache(  const AssemblyKeyBase& keyBase,
                            const sp<Assembly>& assembly)
{
    pthread_mutex_lock(&mLock);

    const ssize_t assemblySize = assembly->size();
    while (mCacheInUse + assemblySize > mCacheSize) {
        // evict the LRU
        size_t lru = 0;
        size_t count = mCacheData.size();
        for (size_t i=0 ; i<count ; i++) {
            const cache_entry_t& e = mCacheData.valueAt(i);
            if (e.when < mCacheData.valueAt(lru).when) {
                lru = i;
            }
        }
        const cache_entry_t& e = mCacheData.valueAt(lru);
        mCacheInUse -= e.entry->size();
        mCacheData.removeItemsAt(lru);
    }

    ssize_t err = mCacheData.add(key_t(keyBase), cache_entry_t(assembly, mWhen));
    if (err >= 0) {
        mCacheInUse += assemblySize;
        mWhen++;
        // synchronize caches...
        char* base = reinterpret_cast<char*>(assembly->base());
        char* curr = reinterpret_cast<char*>(base + assembly->size());
        __builtin___clear_cache(base, curr);
    }

    pthread_mutex_unlock(&mLock);
    return err;
}
Exemple #4
0
static void patch_armv7_thumb(char *writeEntry, const char *execEntry,
        int stubSize, void *incrementPtr)
{
#if defined(__arm__)
    // Thumb bytecode
    const uint16_t tmpl[] = {
        0x4802,         // ldr r0, 1f
        0x6801,         // ldr r1, [r0]
        0xf101, 0x0101, // add r1, r1, #1
        0x6001,         // str r1, [r0]
        0x4770,         // bx lr
        // 1:
        0x0000, 0x0000,
    };

    static int offsetAddr = sizeof(tmpl) - 4;
    if (stubSize < sizeof(tmpl)) {
        return;
    }

    memcpy(writeEntry, tmpl, sizeof(tmpl));
    *((uint32_t *)(writeEntry + offsetAddr)) = (uint32_t)incrementPtr;

    __builtin___clear_cache((char *) execEntry, (char *) (execEntry + sizeof(tmpl)));
#else
    assert(0); // Should not be calling this
#endif
}
Exemple #5
0
void MIPSEmitter::FlushIcacheSection(u8 *start, u8 *end) {
#if defined(MIPS)
#ifdef __clang__
	__clear_cache(start, end);
#else
	__builtin___clear_cache(start, end);
#endif
#endif
}
int v4_generate_JIT_code(const struct V4_Instruction* code, v4_random_math_JIT_func buf, const size_t buf_size)
{
#if defined __i386 || defined __x86_64__
	uint8_t* JIT_code = (uint8_t*) buf;
	const uint8_t* JIT_code_end = JIT_code + buf_size;

	APPEND_CODE(prologue, sizeof(prologue));

	uint32_t prev_rot_src = 0xFFFFFFFFU;

	for (int i = 0;; ++i)
	{
		const struct V4_Instruction inst = code[i];
		if (inst.opcode == RET)
			break;

		const uint8_t opcode = (inst.opcode == MUL) ? inst.opcode : (inst.opcode + 2);

		const uint32_t a = inst.dst_index;
		const uint32_t b = inst.src_index;
		const uint8_t c = opcode | (inst.dst_index << V4_OPCODE_BITS) | (((inst.src_index == 8) ? inst.dst_index : inst.src_index) << (V4_OPCODE_BITS + V4_DST_INDEX_BITS));

		switch (inst.opcode)
		{
		case ROR:
		case ROL:
			if (b != prev_rot_src)
			{
				prev_rot_src = b;
				const uint8_t* p1 = (const uint8_t*) instructions_mov[c];
				const uint8_t* p2 = (const uint8_t*) instructions_mov[c + 1];
				APPEND_CODE(p1, p2 - p1);
			}
			break;
		}

		if (a == prev_rot_src)
			prev_rot_src = 0xFFFFFFFFU;

		const uint8_t* p1 = (const uint8_t*) instructions[c];
		const uint8_t* p2 = (const uint8_t*) instructions[c + 1];
		APPEND_CODE(p1, p2 - p1);

		if (inst.opcode == ADD)
			*(uint32_t*)(JIT_code - 4) = inst.C;
	}

	APPEND_CODE(epilogue, sizeof(epilogue));

	__builtin___clear_cache((char*)buf, (char*)JIT_code);

	return 0;
#else
	return 1;
#endif
}
Exemple #7
0
/*++
Function:
  DBG_FlushInstructionCache: processor-specific portion of 
  FlushInstructionCache

See MSDN doc.
--*/
BOOL
DBG_FlushInstructionCache(
                          IN LPCVOID lpBaseAddress,
                          IN SIZE_T dwSize)
{
    // Intrinsic should do the right thing across all platforms
    __builtin___clear_cache((char *)lpBaseAddress, (char *)((INT_PTR)lpBaseAddress + dwSize));

    return TRUE;
}
Exemple #8
0
static inline void
ffi_clear_cache (void *start, void *end)
{
#if defined (__clang__) && defined (__APPLE__)
	sys_icache_invalidate (start, (char *)end - (char *)start);
#elif defined (__GNUC__)
	__builtin___clear_cache (start, end);
#else
#error "Missing builtin to flush instruction cache"
#endif
}
Exemple #9
0
TCA emitCallToExit(CodeBlock& cb, DataBlock& data, const UniqueStubs& us) {
  vixl::MacroAssembler a { cb };
  vixl::Label target_data;
  auto const start = cb.frontier();

  // Jump to enterTCExit
  a.Ldr(rAsm, &target_data);
  a.Br(rAsm);
  a.bind(&target_data);
  a.dc64(us.enterTCExit);

  __builtin___clear_cache(reinterpret_cast<char*>(start),
                          reinterpret_cast<char*>(cb.frontier()));
  return start;
}
int cacheflush(long start, long end, long /*flags*/) {
  if (end < start) {
    // It looks like this is really a MIPS-style cacheflush call.
    static bool warned = false;
    if (!warned) {
      __libc_format_log(ANDROID_LOG_WARN, "libc", "cacheflush called with (start,len) instead of (start,end)");
      warned = true;
    }
    end += start;
  }

  // Use the GCC builtin. This will generate inline synci instructions if available,
  // or call _flush_cache(start, len, BCACHE) directly.
  __builtin___clear_cache(reinterpret_cast<char*>(start), reinterpret_cast<char*>(end));
  return 0;
}
Exemple #11
0
static void patch_armv7_thumb_tsd(char *writeEntry,
                                  const char *execEntry,
                                  int stubSize)
{
#if defined(__arm__)
    char *pSawVertex3fv = (char *)&__glXSawVertex3fv;

    // Thumb bytecode
    char tmpl[] = {
        // ldr r0, 1f
        0x48, 0x02,
        // ldr r1, [r0]
        0x68, 0x01,
        // add r1, r1, #1
        0xf1, 0x01, 0x01, 0x01,
        // str r1, [r0]
        0x60, 0x01,
        // bx lr
        0x47, 0x70,
        // 1:
        0x00, 0x00, 0x00, 0x00,
    };

    int offsetAddr = sizeof(tmpl) - 4;

    if (stubSize < sizeof(tmpl)) {
        return;
    }

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
    glvnd_byte_swap16((uint16_t *)tmpl, offsetAddr);
#endif

    *((uint32_t *)(tmpl + offsetAddr)) = (uint32_t)pSawVertex3fv;

    memcpy(writeEntry, tmpl, sizeof(tmpl));

    __builtin___clear_cache((char *) execEntry, (char *) (execEntry + sizeof(tmpl)));
#else
    assert(0); // Should not be calling this
#endif
}
Exemple #12
0
void entry_generate_default_code(char *entry, int slot)
{
    char *writeEntry;

    // Make sure the base address has the Thumb mode bit
    assert((uintptr_t)entry & (uintptr_t)0x1);

    // Get the pointer to the writable mapping.
    writeEntry = (char *) u_execmem_get_writable(entry - 1);

    memcpy(writeEntry, BYTECODE_TEMPLATE, ARMV7_BYTECODE_SIZE);

    *((uint32_t *)(writeEntry + TEMPLATE_OFFSET_SLOT)) = slot;
    *((uint32_t *)(writeEntry + TEMPLATE_OFFSET_CURRENT_TABLE)) =
        (uint32_t)_glapi_Current;
    *((uint32_t *)(writeEntry + TEMPLATE_OFFSET_CURRENT_TABLE_GET)) =
        (uint32_t)_glapi_get_current;

    // See http://community.arm.com/groups/processors/blog/2010/02/17/caches-and-self-modifying-code
    __builtin___clear_cache(writeEntry, writeEntry + ARMV7_BYTECODE_SIZE);
}
Exemple #13
0
void* EmitJumpInstruction(void* from_, const void* to_)
{
    BYTE *base, *from, *to;
    base = from = (BYTE*)from_;
    to = (BYTE*)to_;

    BYTE* jump_from = from + 5;
    size_t distance = jump_from > to ? jump_from - to : to - jump_from;

    // emit 5 byte jmp (0xe9 RVA) if relative address is less than 32 bit.
    // otherwise emit 14 byte long jmp (0xff 0x25 [memory] + target)
    if (distance <= 0x7fff0000) {
        from[0] = 0xe9;
        from += 1;
        *((DWORD*)from) = (DWORD)((size_t)to - (size_t)jump_from);
        from += 4;
    }
    else {
        from[0] = 0xff;
        from[1] = 0x25;
        from += 2;
#if defined(__Arch_x86_64__)
        *((DWORD*)from) = (DWORD)((size_t)from + 4);
#elif defined(__Arch_x86__)
        *((DWORD*)from) = (DWORD)0;
#endif
        from += 4;
        *((DWORD_PTR*)from) = (DWORD_PTR)(to);
        from += 8;
    }

#ifdef _WIN32
    ::FlushInstructionCache(nullptr, base, size_t(from - base));
#else
    __builtin___clear_cache((char*)from, (char*)base);
#endif
    return from;
}
Exemple #14
0
static void sys_cacheflush(void *start, void *end)
{
	//__android_log_print(ANDROID_LOG_INFO,"nds4droid","sys_cacheflush %x %x", (unsigned long)start, (unsigned long)end);
	__builtin___clear_cache(start,end);
/*
#ifdef __ARM_EABI__
	// EABI version *
	int num = __ARM_NR_cacheflush;
	__asm__("mov  r0, %0 ;"
		"mov  r1, %1 ;"
		"mov  r2, #0 ;"
		"mov  r7, %2 ;"
		"swi  0" : : "r" (start), "r" (end), "r" (num)
			: "r0", "r1", "r2", "r3", "r7");
#else
	// OABI
	__asm__("mov  r0, %0 ;"
		"mov  r1, %1 ;"
		"mov  r2, #0 ;"
		"swi  %2" : : "r" (start), "r" (end), "i" __ARM_NR_cacheflush
			: "r0", "r1", "r2", "r3");
#endif
*/
}
Exemple #15
0
/*
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
 * endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
int main() {
  char a[100];
  __builtin___clear_cache(a, a + 100);
  return 0;
}
Exemple #16
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;

    entry *hash_table[1000] = {NULL},*hash_head[1000]= {NULL};


#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
#if defined(OPT)
        unsigned int key;
        key = hash(line);
        key = key % 1000;

        if(hash_head[key] == NULL) {
            hash_head[key] = (entry*)malloc(sizeof(entry));

            hash_table[key] = hash_head[key];


        }
        hash_table[key] = append(line,hash_table[key]);

#else

        e = append(line, e);
#endif
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    e = pHead;

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";

#if defined(OPT)

    unsigned int k = hash(input) % 1000;
    e = hash_head[k];




#else
    e = pHead;
#endif
    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel"));


#if defined(OPT)

    __builtin___clear_cache((char *) hash_head, (char *) hash_head + 1000 * sizeof(entry));
#endif

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    FILE *output;
#if defined(OPT)
    output = fopen("opt.txt", "a");
#else
    output = fopen("orig.txt", "a");
#endif
    fprintf(output, "append() findName() %lf %lf\n", cpu_time1, cpu_time2);
    fclose(output);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

    if (pHead->pNext) free(pHead->pNext);
    free(pHead);

    return 0;
}
Exemple #17
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }
#ifndef PHONEBOOKOPT
    /* build the entry */
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;
#else
    threadArg *threadData = malloc(sizeof(struct __THREAD_ARG));
    void *status;
    hash *e;
    hash *pHead = malloc(26 * sizeof(hash));
    for ( int i = 0 ; i < 26 ; i++ )
        pHead[i].pNamelist = NULL;
    printf("size of entry : %lu bytes\n", sizeof(pHead[0]));
    e = pHead;
    threadData ->fp = fp;
    threadData ->e = e;
#endif

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif

    clock_gettime(CLOCK_REALTIME, &start);
#ifndef PHONEBOOKOPT
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        e = append(line, e);
    }

#else
    pthread_attr_t attr;
    pthread_mutex_init(&mutexsum, NULL);

    /* Create threads */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for (long t = 0 ; t < NUMTHREADS ; t++) {
        pthread_create(&callThd[t], &attr, import, (void *)threadData);
    }

    pthread_attr_destroy(&attr);

    for (long t = 0 ; t < NUMTHREADS ; t++) {
        pthread_join(callThd[t], &status);
    }

#endif
    /* close file as soon as possible */
    fclose(fp);

    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    e = pHead;

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
    e = pHead;

    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel"));
#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);
    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);
    /* FIXME: release all allocated entries */
    free(pHead);
    pthread_mutex_destroy(&mutexsum);
    pthread_exit(NULL);
    return 0;
}
void f()
{
  int size = 40;
  char *memory = __builtin_alloca(size);
  __builtin___clear_cache(memory, memory + size);
}
Exemple #19
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* setting mempool and build the entry*/
#if defined(OPT)
    initMemPool();
    printf("size of entry : %lu bytes\n", sizeof(entry));
#else
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;
#endif

#if defined(OPT)
#if defined(__GNUC__)
    __builtin___clear_cache((char *) HashTable, (char *) HashTable + sizeof(entry));
#endif
#else
#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
#endif

#if defined(OPT)
#if defined(SLOT)
    int hash_slot[HASH_TABLE_SIZE] = {0};
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
#if defined(SLOT)
        hash_slot[hash(line)]++;
#endif
        if(append(line, HashTable)==NULL)
            puts("apped failed, the lastName is not valid.");
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);


    /* close file as soon as possible */
    fclose(fp);

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
    assert(findName(input, HashTable) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, HashTable)->lastName, "zyxel"));
#else

    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        if((e = append(line, e))==NULL)
            puts("apped failed, the lastName is not valid.");
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    e = pHead;

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
    e = pHead;

    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel"));
#endif

#if defined(OPT)
#if defined(__GNUC__)
    __builtin___clear_cache((char *) HashTable, (char *) HashTable + sizeof(entry));
#endif
#else
#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
#endif
    /* compute the execution time */
#if defined(OPT)
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, HashTable);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);
#else
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);
#endif
    FILE *output;
#if defined(OPT)
    output = fopen("opt.txt", "a");
#if defined(SLOT)
    FILE *output2;
    output2 = fopen("slot.txt", "a");
    for(int i = 0; i < HASH_TABLE_SIZE; i++) {
        fprintf(output2, "%d %d\n", i, hash_slot[i]);
    }
    fclose(output2);
#endif

#else
    output = fopen("orig.txt", "a");
#endif
    fprintf(output, "append() findName() %lf %.9lf\n", cpu_time1, cpu_time2);
    fclose(output);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %.9lf sec\n", cpu_time2);

#if defined(OPT)
    mempoolfree();
#else
    if (pHead->pNext) free(pHead->pNext);
    free(pHead);
#endif
    return 0;
}
Exemple #20
0
int main() {
  __builtin___clear_cache(buffer, buffer+32);
// CHECK: @llvm.clear_cache(i8* getelementptr inbounds ({{.*}}, i8* getelementptr inbounds (i8, i8* getelementptr inbounds ({{.*}} 32))
  return 0;
}
TEST(sys_mman, mmap_bug_27265969) {
  char* base = reinterpret_cast<char*>(mmap(nullptr, PAGE_SIZE * 2, PROT_EXEC | PROT_READ,
                                            MAP_ANONYMOUS | MAP_PRIVATE, 0, 0));
  // Some kernels had bugs that would cause segfaults here...
  __builtin___clear_cache(base, base + (PAGE_SIZE * 2));
}
Exemple #22
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead; // two pointer point to the same address(e points to the address that pHead points to)
    e->pNext = NULL;

#if defined(OPT) // define hash table
#define MAX_TABLE_SIZE 1000
    entry *hash_pHead[MAX_TABLE_SIZE], *hash_e[MAX_TABLE_SIZE];
    unsigned long j;
    for(j = 0; j < MAX_TABLE_SIZE; j++) {
        hash_pHead[j] = (entry *) malloc(sizeof(entry));
        hash_e[j] = hash_pHead[j];
        hash_e[j]->pNext = NULL;
    }
#endif


#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) { // read each line in words.txt and store it to the array variable 'line'
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0'; // "aLastName\n\0" ==> "aLastName\0\0"
        i = 0;
#if defined(OPT)
        unsigned long hash_addr = getHash_addr(line,MAX_TABLE_SIZE);
        hash_e[hash_addr] = append(line, hash_e[hash_addr]);
#else
        e = append(line, e); // when use strcpy "aLastName\0\0" ==> "aLastName\0"
#endif
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
#if defined(OPT)
    unsigned long inputHash_addr = getHash_addr(input,MAX_TABLE_SIZE);
    e = hash_pHead[inputHash_addr];
#else
    e = pHead;
#endif

    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?"); // the string "Did you ..." will be print with assert
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel")); // if find "zyxel" then continue, otherwise the program terminates

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    FILE *output;
#if OPT==1
    output = fopen("opt.txt", "a"); //append text to the original file
#else
    output = fopen("orig.txt", "a");
#endif
    fprintf(output, "append() findName() %lf %lf\n", cpu_time1, cpu_time2);
    fclose(output);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

    if (pHead->pNext) free(pHead->pNext);
    free(pHead);

    return 0;
}
Exemple #23
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2=0;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    PHONEBOOK.initialize();

    printf("size of entry : %lu bytes\n", sizeof(entry));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        PHONEBOOK.addNode(line,e);
    }

    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    /* the givn last name to find */
    char input[INPUT_SIZE][MAX_LAST_NAME_SIZE] = {"uninvolved","zyxel","whiteshank",
                                                  "odontomous", "pungoteague",
                                                  "reweighted", "xiphisternal", "yakattalo"
                                                 };

    for( i = 0; i < INPUT_SIZE; i++ ) {

        char in[MAX_LAST_NAME_SIZE];
        PHONEBOOK.get_cmp_buf(input[i],in);
        PHONEBOOK.moveNode();

        /*Get compress input value*/
        assert(PHONEBOOK.findName(input[i], e) &&
               "Did you implement findName() in " IMPL "?");

#ifndef FUZZY_SEARCH
        assert(0 == strcmp(PHONEBOOK.findName(input[i], e)->lastName, in));
#endif

#if defined(__GNUC__)
        __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
        /* compute the execution time */
        clock_gettime(CLOCK_REALTIME, &start);
        entry *result = PHONEBOOK.findName(input[i], e);

#ifdef SHOW_RESULT
        if(0 == strcmp(result -> lastName,in)) {
            //printf("Find %s\n",input[i]);
        } else {
            char output[MAX_LAST_NAME_SIZE];
            int count = 0;
            int input_len = strlen(in);
            int result_len,max_len,r;
            float radio;
            while (result != NULL) {
                decompress(result->lastName,output);
                result_len = strlen(output);
                if(abs(result_len - input_len) <= STRING_COMPARISON_LENGTH) {
                    max_len = (result_len < input_len) ? input_len : result_len;
                    r = Levenshtein(output,in);
                    radio = 1.0 - ((float)r) / max_len;
                    if(radio > STRING_COMPARISON_THRESHOLD) {
                        //printf("Simular Result : %s \t result : %d \t radio : %.2f \n",result->lastName,r,radio);
                        count++;
                    }
                }
                result = result -> pNext;
            }
            //printf("Candidate count is %d \n",count);
        }
#endif
        clock_gettime(CLOCK_REALTIME, &end);
        cpu_time2 += diff_in_second(start, end);
    }
    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

    /* FIXME: release all allocated entries */
    PHONEBOOK.release();

    return 0;
}
Exemple #24
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;
#if defined(OPT)
    hashTable *ht = createHashTable(TABLE_SIZE);
    printf("Hash Table Size: %d\n", TABLE_SIZE);
#endif

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
#if defined(OPT)
        e = append(line, ht);
#else
        e = append(line, e);
#endif
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_milisecond(start, end);

    /* close file as soon as possible */
    fclose(fp);

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = FIND_NAME;
    void *findPtr = NULL;
#if defined(OPT)
    findPtr = ht;
#else
    findPtr = pHead;
#endif
    assert(findName(input, findPtr) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, findPtr)->lastName, "zyxel"));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, findPtr);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_milisecond(start, end);

    printf("execution time of append() : %lf ms\n", cpu_time1);
    printf("execution time of findName() : %lf ms\n", cpu_time2);

    /* FIXME: release all allocated entries */
    free(pHead);

    return 0;
}
Exemple #25
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    entry *pHead;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));


    //create hashtable
    hashtable *hashtable = ht_create(65536); 
        

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        
        //add data
        ht_append(hashtable , line);
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);
    /* the givn last name to find */
   char input[MAX_LAST_NAME_SIZE] = "zyxel";


#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    //find data
    ht_findName(hashtable,input);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

    /* FIXME: release all allocated entries */
    free(pHead);

    return 0;
}
Exemple #26
0
void Mem::flushInstructionCache(void *p, size_t size)
{
#   ifndef __FreeBSD__
    __builtin___clear_cache(reinterpret_cast<char*>(p), reinterpret_cast<char*>(p) + size);
#   endif
}
Exemple #27
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    PHONE_BOOK.initialize();
    printf("size of entry : %u bytes\n", sizeof(entry));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif

    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        PHONE_BOOK.new_node(line, e);
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    PHONE_BOOK.move_node();

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";


    //assert(PHONE_BOOK.findName(input, e) &&
    //       "Did you implement findName() in " IMPL "?");
    //assert(0 == strcmp(PHONE_BOOK.findName(input, e) -> lastName, "zyxel"));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    PHONE_BOOK.findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

    /* FIXME: release all allocated entries */
    PHONE_BOOK.release();
    return 0;
}
Exemple #28
0
int main(int argc, char *argv[])
{
    FILE **fps;
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    int threadnum = 10;
    /*
    	printf("Specify number of worker threads: ");
    	scanf("%d", &threadnum);
    */
    /* check file opening */
    int k;
    fps = (FILE **)malloc(threadnum * sizeof(FILE *));
    for(k = 0; k < threadnum; k++) {
        fps[k] = fopen(DICT_FILE, "r");
        if (fps[k] == NULL) {
            printf("cannot open the file\n");
            return -1;
        }
    }

    /* build the entry */
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif


    pthread_t *threads = (pthread_t *)malloc(threadnum * sizeof(pthread_t));
    thread_data **param = (thread_data **)malloc(threadnum * sizeof(thread_data *));

    for(int k=0; k < TABLE_SIZE; k++) {
        if (pthread_mutex_init(&locks[k], NULL) != 0) {
            printf("\n mutex init failed\n");
            return 1;
        }
    }

    clock_gettime(CLOCK_REALTIME, &start);

    int err;
    for(k = 0; k < threadnum; k++) {
        param[k] = pack_param(fps[k], k, threadnum);
        err = pthread_create(&threads[k], NULL, (void *) &thread_work, (void *) param[k]);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
    }

    for(k = 0; k < threadnum; k++) {
        pthread_join(threads[k], NULL);
    }

    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    for(k = 0; k < threadnum; k++) {
        fclose(fps[k]);
    }

    for(int k=0; k < TABLE_SIZE; k++) {
        // printf("%d\n", hash_stats[k]);
        pthread_mutex_destroy(&locks[k]);
    }
    e = pHead;

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
    e = pHead;

    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel"));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    FILE *output;
#if defined(OPT)
    output = fopen("opt.txt", "a");
#else
    output = fopen("orig.txt", "a");
#endif
    fprintf(output, "append() findName() %lf %lf\n", cpu_time1, cpu_time2);
    fclose(output);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

    if (pHead->pNext) free(pHead->pNext);
    free(pHead);

    return 0;
}
Exemple #29
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        e = append(line, e);
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    e = pHead;

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
    e = pHead;

    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel"));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

	free_struct(e);

    return 0;
}
Exemple #30
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
# if defined(OPT)
    entry **pHead, **e;
    pHead = (entry **) malloc(sizeof(entry*) * TABLESIZE);
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    for(i = 0; i < TABLESIZE; i++) {
        e[i] = (entry*) malloc(sizeof(entry));
        e[i]->pNext = NULL;
    }
    i = 0;
#else
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
#endif

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        e = append(line, e);
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    e = pHead;

    /* the givn last name to find */
    char input[8][MAX_LAST_NAME_SIZE];
    strcpy(input[0], "zyxel");
    strcpy(input[1], "uninvolved");
    strcpy(input[2], "whiteshank");
    strcpy(input[3], "odontomous");
    strcpy(input[4], "pungoteague");
    strcpy(input[5], "reweighted");
    strcpy(input[6], "xiphisternal");
    strcpy(input[7], "yakattalo");

    e = pHead;

    assert(findName(input[0], e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input[0], e)->lastName, "zyxel"));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    for(i = 0; i < 8; i++) {
        findName(input[i], e);
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    FILE *output;
#if defined(OPT)
    output = fopen("opt.txt", "a");
#else
    output = fopen("orig.txt", "a");
#endif
    fprintf(output, "append() findName() %lf %lf\n", cpu_time1, cpu_time2);
    fclose(output);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);
    printf("total time elapsed : %lf sec\n", cpu_time1 + cpu_time2);

#if defined(OPT)
    for(i = 0; i < TABLESIZE; i++) {
        if (pHead[i]->pNext) free(pHead[i]->pNext);
    }
#else
    if (pHead->pNext) free(pHead->pNext);
#endif
    free(pHead);

    return 0;
}