LCOV - code coverage report
Current view: top level - ports/unix - alloc.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-740-gbf49a087b.info Lines: 18 30 60.0 %
Date: 2022-12-09 11:55:04 Functions: 3 5 60.0 %
Branches: 3 8 37.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2014 Fabian Vogt
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include <stdio.h>
      28                 :            : #include <stdint.h>
      29                 :            : #include <stdlib.h>
      30                 :            : #include <string.h>
      31                 :            : #include <sys/mman.h>
      32                 :            : 
      33                 :            : #include "py/mpstate.h"
      34                 :            : #include "py/gc.h"
      35                 :            : 
      36                 :            : #if MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)
      37                 :            : 
      38                 :            : #if defined(__OpenBSD__) || defined(__MACH__)
      39                 :            : #define MAP_ANONYMOUS MAP_ANON
      40                 :            : #endif
      41                 :            : 
      42                 :            : // The memory allocated here is not on the GC heap (and it may contain pointers
      43                 :            : // that need to be GC'd) so we must somehow trace this memory.  We do it by
      44                 :            : // keeping a linked list of all mmap'd regions, and tracing them explicitly.
      45                 :            : 
      46                 :            : typedef struct _mmap_region_t {
      47                 :            :     void *ptr;
      48                 :            :     size_t len;
      49                 :            :     struct _mmap_region_t *next;
      50                 :            : } mmap_region_t;
      51                 :            : 
      52                 :       5857 : void mp_unix_alloc_exec(size_t min_size, void **ptr, size_t *size) {
      53                 :            :     // size needs to be a multiple of the page size
      54                 :       5857 :     *size = (min_size + 0xfff) & (~0xfff);
      55                 :       5857 :     *ptr = mmap(NULL, *size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
      56         [ -  + ]:       5857 :     if (*ptr == MAP_FAILED) {
      57                 :          0 :         *ptr = NULL;
      58                 :            :     }
      59                 :            : 
      60                 :            :     // add new link to the list of mmap'd regions
      61                 :       5857 :     mmap_region_t *rg = m_new_obj(mmap_region_t);
      62                 :       5857 :     rg->ptr = *ptr;
      63                 :       5857 :     rg->len = min_size;
      64                 :       5857 :     rg->next = MP_STATE_VM(mmap_region_head);
      65                 :       5857 :     MP_STATE_VM(mmap_region_head) = rg;
      66                 :       5857 : }
      67                 :            : 
      68                 :          0 : void mp_unix_free_exec(void *ptr, size_t size) {
      69                 :          0 :     munmap(ptr, size);
      70                 :            : 
      71                 :            :     // unlink the mmap'd region from the list
      72         [ #  # ]:          0 :     for (mmap_region_t **rg = (mmap_region_t **)&MP_STATE_VM(mmap_region_head); *rg != NULL; *rg = (*rg)->next) {
      73         [ #  # ]:          0 :         if ((*rg)->ptr == ptr) {
      74                 :          0 :             mmap_region_t *next = (*rg)->next;
      75                 :          0 :             m_del_obj(mmap_region_t, *rg);
      76                 :          0 :             *rg = next;
      77                 :          0 :             return;
      78                 :            :         }
      79                 :            :     }
      80                 :            : }
      81                 :            : 
      82                 :      12709 : void mp_unix_mark_exec(void) {
      83         [ +  + ]:      19467 :     for (mmap_region_t *rg = MP_STATE_VM(mmap_region_head); rg != NULL; rg = rg->next) {
      84                 :       6758 :         gc_collect_root(rg->ptr, rg->len / sizeof(mp_uint_t));
      85                 :            :     }
      86                 :      12709 : }
      87                 :            : 
      88                 :            : #if MICROPY_FORCE_PLAT_ALLOC_EXEC
      89                 :            : // Provide implementation of libffi ffi_closure_* functions in terms
      90                 :            : // of the functions above. On a normal Linux system, this save a lot
      91                 :            : // of code size.
      92                 :            : void *ffi_closure_alloc(size_t size, void **code);
      93                 :            : void ffi_closure_free(void *ptr);
      94                 :            : 
      95                 :          2 : void *ffi_closure_alloc(size_t size, void **code) {
      96                 :          2 :     size_t dummy;
      97                 :          2 :     mp_unix_alloc_exec(size, code, &dummy);
      98                 :          2 :     return *code;
      99                 :            : }
     100                 :            : 
     101                 :          0 : void ffi_closure_free(void *ptr) {
     102                 :          0 :     (void)ptr;
     103                 :            :     // TODO
     104                 :          0 : }
     105                 :            : #endif
     106                 :            : 
     107                 :            : MP_REGISTER_ROOT_POINTER(void *mmap_region_head);
     108                 :            : 
     109                 :            : #endif // MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)

Generated by: LCOV version 1.15-5-g462f71d