Esempio n. 1
0
  // Create and initialize a global char* constant in the instrumented
  // code.
  GlobalVariable* BytesFlops::create_global_constant(Module& module,
                                                     const char* name,
                                                     const char* value) {
    // First, create a _local_ array of characters.
    LLVMContext& globctx = module.getContext();
    size_t num_bytes = strlen(value) + 1;   // Number of characters including the trailing '\0'
    ArrayType* array_type = ArrayType::get(Type::getInt8Ty(globctx), num_bytes);
    Constant *local_string = ConstantDataArray::getString(globctx, value, true);
    GlobalVariable* string_contents =
      new GlobalVariable(module, array_type, true, GlobalValue::PrivateLinkage,
                         local_string, string(name)+string(".data"));
    string_contents->setAlignment(8);

    // Next, create a global pointer to the local array of characters.
    std::vector<Constant*> getelementptr_indexes;
    getelementptr_indexes.push_back(zero);
    getelementptr_indexes.push_back(zero);
    Constant* array_pointer =
      ConstantExpr::getGetElementPtr(string_contents, getelementptr_indexes);
    PointerType* pointer_type = PointerType::get(Type::getInt8Ty(globctx), 0);
    GlobalVariable* new_constant =
      new GlobalVariable(module, pointer_type, true,
                         GlobalValue::LinkOnceODRLinkage, array_pointer, name);
    mark_as_used(module, new_constant);
    return new_constant;
  }
Esempio n. 2
0
static void *_kmalloc(struct chunk *chunks, size_t size, int flags) {

    size = ROUND_UP(size, 8);
    mutex_lock(&kmalloc_lock);

    for (struct chunk *chunk = chunks; CHUNK_ADDR(chunk); chunk = GET_NEXT_CHUNK(chunk)) {
        if (IS_AVAILABLE_CHUNK(chunk) && size <= chunk->size) {
            // We've found an unused chunk with enough space!
            if (size + sizeof(*chunk) < chunk->size) {
                // Split the memory block.
                struct chunk *next_chunk = (void *) ((uintptr_t) chunk +
                                                     sizeof(*chunk) + size);
                next_chunk->next = chunk->next;
                next_chunk->size = chunk->size - sizeof(*chunk) - size;
                chunk->next      = next_chunk;
            }

            mark_as_used(chunk);
            chunk->size = size;
            used += size;
            INFO("kmalloc: allocate %dB", size);
            mutex_unlock(&kmalloc_lock);
            return (void *) ((uintptr_t) chunk + sizeof(*chunk));
        }
    }

    WARN("kmalloc: failed to allcoate %dB", size);
    mutex_unlock(&kmalloc_lock);
    return (void *) NULL;
}
Esempio n. 3
0
 // Create and initialize a global bool constant in the instrumented
 // code.
 GlobalVariable* BytesFlops::create_global_constant(Module& module,
                                                    const char* name,
                                                    bool value) {
   LLVMContext& globctx = module.getContext();
   IntegerType* booltype = Type::getInt1Ty(globctx);
   ConstantInt* const_value = ConstantInt::get(globctx, APInt(1, value));
   GlobalVariable* new_constant =
     new GlobalVariable(module, booltype, true, GlobalValue::LinkOnceODRLinkage,
                        const_value, name);
   mark_as_used(module, new_constant);
   return new_constant;
 }