void jvm_StackInit(JVMStack *stack, uint32 max) {
  stack->max = max;
  stack->pos = 0;
  max += 3;
  stack->data = (uint64*)jvm_malloc(sizeof(uint64) * max);
  stack->flags = (uint32*)jvm_malloc(sizeof(uint32) * max);
}
 void  BranchTable::init()
  {
    _b_items = 
      (BranchItem*) jvm_malloc(BRANCH_TABLE_LENGTH *  sizeof(BranchItem));
    GUARANTEE(_b_items, "sanity");
    jvm_memset(_b_items, 0, BRANCH_TABLE_LENGTH * sizeof(BranchItem));
    _item_index = 0;
  }
Exemplo n.º 3
0
uint8* jvm_ReadWholeFile(const char *path, uint32 *size) {
  uint8         *buf;
  FILE          *fp;
  
  fp = fopen(path, "rb");
  if (!fp) {
    debugf("error could not open file %s", path);
    jvm_exit(-4);
  }
  fseek(fp, 0, 2);
  *size = ftell(fp);
  fseek(fp, 0, 0);
  buf = (uint8*)jvm_malloc(*size);
  debugf("here %llx\n", buf);
  fread(buf, 1, *size, fp);
  debugf("here\n");
  fclose(fp);
  
  return buf;
}
Exemplo n.º 4
0
/*****************************************************************************
 * FUNCTION
 *  jam_listmem_malloc
 * DESCRIPTION
 *  
 * PARAMETERS
 *  size        [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void *jam_listmem_malloc(kal_int32 size)
{
#ifndef J2ME_SLIM_REMOVE_LIST_MEMORY_SUPPORT
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 allocate_size;
    void *loc = NULL;
    listmem_hdr tmp_header = NULL;
    char *temp = NULL, *mem_ptr;
    char *mem_pool_start = g_jam_midslist_mem_start_p;
    char *mem_pool_end = g_jam_midslist_mem_end_p;
    listmem_hdr header;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (size <= 0)
    {
        return NULL;
    }

    /* 4bytes alignment */
    allocate_size = (size + ALIGNMENT) & 0xfffffffc;

    /* find a free slot */
    for (mem_ptr = mem_pool_start; mem_ptr < mem_pool_end; mem_ptr += header->size + sizeof(listmem_struct))
    {

        header = (listmem_hdr) mem_ptr;
        if (header->magic != MAGIC)
        {
            kal_trace(TRACE_INFO, FUNC_JAM_LIST_MALLOC_CORRUPTION,(kal_int32) mem_ptr);
            break;
        }
        else
        {
            while (1)
            {
                /* coalescing */
                if (header->free == 1)
                {
                    /* if current block is free */
                    temp = (char*)header;
                    temp += header->size + sizeof(listmem_struct);
                    if (temp >= mem_pool_end)
                    {
                        break;
                    }
                    tmp_header = (listmem_hdr) temp;

                    if ((tmp_header->free == 1) && (tmp_header->magic == MAGIC) && (temp < mem_pool_end))
                    {
                        /* and the next block is free too */
                        /* then coalesce */
                        header->size += tmp_header->size + sizeof(listmem_struct);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            if ((header->free == 1) && (header->size >= (unsigned)allocate_size))
            {

                if (header->size > (allocate_size + sizeof(listmem_struct) + 4))
                {
                    listmem_hdr next_header;

                    /* split block */
                    next_header = (listmem_hdr) ((char*)mem_ptr + allocate_size + sizeof(listmem_struct));
                    next_header->magic = MAGIC;
                    next_header->free = 1;
                    next_header->size = header->size - allocate_size - sizeof(listmem_struct);
                    header->size = allocate_size;
                }
                header->free = 0;
                loc = (void*)((char*)header + sizeof(listmem_struct));
                return (loc);
            }
        }
    }
    kal_trace(TRACE_INFO, FUNC_JAM_LIST_MALLOC_MEM_NOT_AVAIL,(kal_int32) mem_ptr);
    return 0;
#else
    kal_prompt_trace(MOD_JAM, "Here used to be list memory malloc.");
    jvm_malloc(size);     
#endif
}
Exemplo n.º 5
0
void *OsMemory_allocate(size_t size) {
  return jvm_malloc(size);
}
Exemplo n.º 6
0
OsFile_MappedImageHandle OsFile_MapImage(const PathChar* name,
                                         address preferrable_destination,
                                         int length, int rw_offset,
                                         int rw_length) {
  GUARANTEE(length == rw_offset + rw_length, "assumption");
  GUARANTEE((int(preferrable_destination) % ASSUMED_PAGE_SIZE) == 0,
            "page aligned");
#if USE_UNICODE_FOR_FILENAMES
  char ascii_name[256];
  int len = fn_strlen(name);
  if (len > 255) {
    len = 255;
  }
  for (int i=0; i<len; i++) {
    ascii_name[i] = (char)name[i];
  }
  ascii_name[len] = 0;
#else
  const char *ascii_name = name;
#endif
  Linux_MappedImage *img = 
      (Linux_MappedImage*)jvm_malloc(sizeof(Linux_MappedImage));

  if (img == NULL) {
    return NULL;
  }

  if (Verbose) {
    TTY_TRACE_CR(("Map image desired = 0x%x",int(preferrable_destination))); 
  }
  
  int open_flags = O_RDONLY;
  int fd = jvm_open(ascii_name, open_flags);

  // Align the RW region down so that the RW region starts at
  // a page boundary.
  int ro_length = length - rw_length;
  ro_length = (int)align_size_down(ro_length, ASSUMED_PAGE_SIZE);
  rw_offset = ro_length;
  rw_length = length - ro_length;

  if (ForceImageRelocation) {
    // For testing purposes, check relocations
    preferrable_destination += ASSUMED_PAGE_SIZE * 17;
  }

  address ro_addr = (address)-1;
  address rw_addr = (address)-1;
  address ro_preferred = preferrable_destination;
  address rw_preferred = preferrable_destination + rw_offset;

  if (fd == -1) {
    goto error;
  }


  if (ro_length > 0 && LoadXIPImage && !ForceImageRelocation) {
    ro_addr = (address)jvm_mmap(ro_preferred, ro_length, 
                                PROT_READ, MAP_PRIVATE, fd, 0);
    rw_addr = (address)jvm_mmap(rw_preferred, rw_length, 
                                PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,
                                rw_offset);
  } else {
    // The whole image needs to be mapped R/W.
  }

  if (ro_addr == ro_preferred && rw_preferred == rw_preferred) {
    if (Verbose) {
      TTY_TRACE_CR(("Map image actual  = 0x%x [RO] size=%d", int(ro_addr),
                                                             ro_length));
      TTY_TRACE_CR(("Map image actual  = 0x%x [RW] size=%d", int(rw_addr),
                                                             rw_length));
    }
  
    img->mapped_address    = ro_addr;
    img->ro_length         = ro_length;
    img->rw_mapped_address = rw_addr;
    img->rw_length         = rw_length;
  } else {
    // Can't get to our preferred location. Relocation of the image content
    // is needed, so we need to remap the whole thing using RW mapping
    if (ro_addr != (address) -1) {
      jvm_munmap(ro_addr, ro_length);
    }
    if (rw_addr != (address) -1) {
      jvm_munmap(rw_addr, rw_length);
    }
    ro_addr = (address)jvm_mmap(ro_preferred, length,
                                PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
    if (ro_addr == (address)-1) {
      goto error;
    }

    if (Verbose) {
      TTY_TRACE_CR(("Map image actual  = 0x%x [RW] size=%d", int(ro_addr),
                                                             length));
    }
  
    img->mapped_address    = ro_addr;
    img->ro_length         = length;
    img->rw_mapped_address = (address)-1;
    img->rw_length         = 0;
  }

  // we don't need fd anymore, mapping is still preserved
  jvm_close(fd);
  return img;

error:
  if (img) {
    jvm_free(img);
  }
  if (fd >= 0) {
    jvm_close(fd);
  }
  return NULL;
}
Exemplo n.º 7
0
/*****************************************************************************
 * FUNCTION
 *  jbt_cmd_handle_restore
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void jbt_cmd_handle_restore(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint8 i = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_trace(TRACE_FUNC, FUNC_JBT_RESULT, 0xA1);

    /* TODO the check range may be furthur optimized. we only need to check specific non-concurrent commands */
    for (i = JBT_CMDCODE_GET_LOCAL_NAME; i < JBT_CMDCODE_DEVICE_DISCOVERY_CMD_CANCEL; i++)
    {
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_HANDLE_RESTORE_INDEXx02X, i);
        jbt_cmd_check_device_in_queue(i);
    }
    kal_trace(
        TRACE_JBT_GROUP,
        JBT_JBT_CMD_HANDLE_RESTORE_JBT_CMD_QUEUE_CONTEXTCMD_RUN_NOx02X,
        jbt_cmd_queue_context.cmd_run_no);

    if (jbt_cmd_queue_context.cmd_run_no == 0)
    {
        jbt_restore_procedure(JBT_CMD_SUCCESS);
    }
    else
    {
        /* Add check: If JBT is in POWER_OTHERS state (caused by VM finalize), if there is an inquiry ongoing, just cancels it */
        if (jbt_gap_power_state() == JBT_POWER_OTHERS)
        {
            jbt_op_info *discovery_operation = jbt_gap_obtain_operation(JBT_CMDCODE_DEVICE_DISCOVERY_CMD);

            if (discovery_operation->operation_state == JBT_OPERATION_DISCOVERY_SEARCHING)
            {
                /* Existing an ongoing discovery operation, sends discovery cancel cmd to btstack to speed the process of VM termination */
                jbt_gap_device_discovery_cancel *parms;
                kal_uint32 jbt_gap_device_discovery_cancel_size = sizeof(jbt_gap_device_discovery_cancel);
                kal_uint8 block_inx = 0;
                
                /*
                 * DONT invoke the function jbt_device_discovery_cancel_cmd() directly:
                 * Because JBT is not power_on status during VM termination, if invoking this function will return failure
                 */
                block_inx = jbt_malloc_block(jbt_gap_device_discovery_cancel_size);

#ifndef JBT_SUPPORT_ADM_MEM
                parms = (jbt_gap_device_discovery_cancel*) jvm_malloc(jbt_gap_device_discovery_cancel_size);
#else                
                parms = (jbt_gap_device_discovery_cancel*) kal_adm_alloc(jbt_get_mem_pool_id(),jbt_gap_device_discovery_cancel_size);
#endif
                if (parms != 0)
                {
                    kal_trace(TRACE_JBT_GROUP, FUNC_JBT_CANCEL_INQUIRY_DUE_TO_VM_TERMINATE);
                    jbt_malloc_record(block_inx, (kal_uint8*)parms, jbt_gap_device_discovery_cancel_size);
                    parms->transaction_id = discovery_operation->transaction_id;
                    kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_INSERT_QUEUE);
                    jbt_cmd_insert_queue(
                        JBT_CMDCODE_DEVICE_DISCOVERY_CMD_CANCEL,
                        JBT_STACK_CMD_CONCURRENT,
                        (kal_uint8*) parms);
                    
                    jbt_free_record((kal_uint8*) parms);
#ifndef JBT_SUPPORT_ADM_MEM
                    jvm_free((kal_uint8*) parms);
#else
                    kal_adm_free(jbt_get_mem_pool_id(),(kal_uint8*) parms);
#endif
                }
                else
                {
                    /* Failed of jvm_malloc() */
                    jbt_free_block(block_inx);
                    kal_trace(TRACE_JBT_GROUP, FUNC_JBT_CANCEL_INQUIRY_FAILED_WHILE_VM_TERMINATE);
                }
            }
        }
    }
}
int jvm_core_core_handler(struct _JVM *jvm, struct _JVMBundle *bundle, struct _JVMClass *jclass,
                               uint8 *method8, uint8 *type8, JVMLocal *locals,
                               int localCnt, JVMLocal *result) {
  int                   x;
  int                   c;
  int                   error;
  JVMBundleClass        *cbc;
  JVMObject             *jobject;
  JVMObject             *sobject;
  JVMObject             *pobject;
  uint8                 *cn;
  uint8                 lbuf[128];
  uint8                 *buf;
  FILE                  *fp;
  struct stat           _stat;
  JVMObject             *_jobject;
  
  debugf("success:%s:%s\n", method8, type8);
  // determine what is being called
  for (x = 0, c = 0; method8[x] != 0; ++x)
    c += method8[x];

  debugf("native:%s:%x\n", method8, c);
  //jvm_exit(-5);
  switch (c) {
    // Collect
    case 0x2c6:
      jvm_Collect(jvm);
      result->data = 0;
      result->flags = 0;
      break;
    // LoadResource
    case 0x4c8:
      sobject = (JVMObject*)locals[0].data;
      debugf("sobject %x\n", sobject);
      if (!sobject)
        break;
      pobject = (JVMObject*)sobject->_fields[0].value;
      for (c = 0; c < pobject->fieldCnt; ++c)
        lbuf[c] = ((uint8*)pobject->fields)[c];
      lbuf[c] = 0;

      fp = fopen(&lbuf[0], "rb");
      fstat(fileno(fp), &_stat);
      c = _stat.st_size;
      buf = (uint8*)jvm_malloc(sizeof(uint8) * c);
      fread(&buf[0], c, 1, fp);
      fclose(fp);

      error = jvm_CreatePrimArray(jvm, bundle, JVM_ATYPE_BYTE, c, &_jobject, buf);
      if (error)
        return error;
      result->data = (uint64)_jobject;
      result->flags = JVM_STACK_ISARRAYREF | JVM_STACK_ISOBJECTREF | JVM_STACK_ISBYTE;
      break;
    // Exit
    case 0x19a:
      jvm_exit(-1);
      break;
    // Print
    case 0x20d:
      sobject = (JVMObject*)locals[0].data;
      debugf("sobject %x\n", sobject);
      if (!sobject)
        break;
      pobject = (JVMObject*)sobject->_fields[0].value;
      debugf("pobject %x %u\n", pobject, sobject->fieldCnt);
      if (!pobject)
        break;

      debugf("##:printc[%u]:", pobject->fieldCnt);
      for (c = 0; c < pobject->fieldCnt; ++c) {
        jvm_printf("%c", ((uint16*)pobject->fields)[c]);
      }
      jvm_printf("\n");
      exit(-3);
      break;
    // EnumClasses
    case 0x463:
      // get count of classes
      for (c = 0, cbc = bundle->first; cbc != 0; cbc = cbc->next, ++c);
      error = jvm_CreateObjectArray(jvm, bundle, "java/lang/String", c, &jobject);
      if (error) {
        return error;
      }
      for (c = 0, cbc = bundle->first; cbc != 0; cbc = cbc->next, ++c) {
        // create string object
        error = jvm_CreateObject(jvm, bundle, "java/lang/String", &sobject);
        if (error)
          return error;
        // get string we need to put in it
        cn = jvm_GetClassNameFromClass(cbc->jclass);
        // get length of that string
        for (x = 0; cn[x] != 0; ++x);
        // create primitive array to hold string
        error = jvm_CreatePrimArray(jvm, bundle, JVM_ATYPE_BYTE, x, &pobject, 0);
        // fill primitive array
        for (x = 0; cn[x] != 0; ++x)
          ((uint8*)pobject->fields)[x] = cn[x];
        if (error)
           return error;
        // add primitive array to String field
        sobject->_fields[0].value = (uintptr)pobject;
        sobject->_fields[0].jclass = 0;
        sobject->_fields[0].aflags = JVM_STACK_ISOBJECTREF | JVM_STACK_ISARRAYREF;
        // add to object array of String
        jobject->fields[c] = (uint64)sobject;
        //jvm_exit(-5);
      }
      // return the object array of String
      result->data = (uintptr)jobject;
      result->flags = JVM_STACK_ISOBJECTREF | JVM_STACK_ISARRAYREF;
      return JVM_SUCCESS;
  }
  return JVM_SUCCESS;
}