sim: Use executable memory for the heap

On modern environments, bss is not executable.

Alternatively we can use the ARCH_HAVE_MODULE_TEXT mechanism.
But it's considered overkill for the sim, which is mainly
for development.
This commit is contained in:
YAMAMOTO Takashi 2020-03-26 13:58:26 +09:00 committed by Xiang Xiao
parent 748777e3f5
commit a493c92826
4 changed files with 83 additions and 2 deletions

View File

@ -76,6 +76,12 @@ DEPPATH = $(patsubst %,--dep-path %,$(subst :, ,$(VPATH)))
HOSTSRCS = up_hosttime.c
ifeq ($(CONFIG_LIBC_MODLIB),y)
HOSTSRCS += up_hostmemory.c
else ifeq ($(CONFIG_BINFMT_LOADABLE),y)
HOSTSRCS += up_hostmemory.c
endif
ifeq ($(CONFIG_STACK_COLORATION),y)
CSRCS += up_checkstack.c
endif

View File

@ -52,8 +52,6 @@
* Private Data
****************************************************************************/
static uint8_t sim_heap[SIM_HEAP_SIZE];
/****************************************************************************
* Public Functions
****************************************************************************/
@ -71,6 +69,24 @@ static uint8_t sim_heap[SIM_HEAP_SIZE];
void up_allocate_heap(void **heap_start, size_t *heap_size)
{
/* Note: Some subsystems like modlib and binfmt need to allocate
* executable memory.
*/
#if defined(CONFIG_LIBC_MODLIB) || defined(CONFIG_BINFMT_LOADABLE)
/* We make the entire heap executable here to keep
* the sim simpler. If it turns out to be a problem, the
* ARCH_HAVE_MODULE_TEXT mechanism can be an alternative.
*/
uint8_t *sim_heap = host_alloc_heap(SIM_HEAP_SIZE);
#else
/* This sim_heap would be placed in BSS, which is often not
* executable on modern environments.
*/
static uint8_t sim_heap[SIM_HEAP_SIZE];
#endif
*heap_start = sim_heap;
*heap_size = SIM_HEAP_SIZE;
}

View File

@ -0,0 +1,55 @@
/****************************************************************************
* arch/sim/src/sim/up_hostmemory.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: host_alloc_heap
*
* Description:
* Allocate executable memory for heap.
*
****************************************************************************/
void *host_alloc_heap(size_t sz)
{
void *p;
p = mmap(NULL, sz, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANON | MAP_PRIVATE, -1, 0);
if (p == MAP_FAILED)
{
perror("Failed to allocate heap with mmap");
exit(EXIT_FAILURE);
}
return p;
}

View File

@ -224,6 +224,10 @@ volatile spinlock_t g_cpu_paused[CONFIG_SMP_NCPUS] SP_SECTION;
int up_setjmp(xcpt_reg_t *jb);
void up_longjmp(xcpt_reg_t *jb, int val) noreturn_function;
/* up_hostmemory.c **********************************************************/
void *host_alloc_heap(size_t sz);
/* up_hosttime.c ************************************************************/
uint64_t host_gettime(bool rtc);