diff --git a/code/mps.c b/code/mps.c index 41c8d076af..c1719e2112 100644 --- a/code/mps.c +++ b/code/mps.c @@ -104,11 +104,11 @@ #include "lockan.c" /* generic locks */ #include "than.c" /* generic threads manager */ -#include "vman.c" /* malloc-based pseudo memory mapping */ #include "protan.c" /* generic memory protection */ #include "prmcan.c" /* generic operating system mutator context */ #include "prmcanan.c" /* generic architecture mutator context */ #include "span.c" /* generic stack probe */ +#include "vman.c" /* malloc-based pseudo memory mapping */ /* macOS on ARM64 built with Clang */ diff --git a/code/mpslib.h b/code/mpslib.h index f7f1eac6fc..70ca75cc62 100644 --- a/code/mpslib.h +++ b/code/mpslib.h @@ -55,6 +55,13 @@ typedef void (*mps_lib_assert_fail_t)(const char *, unsigned, const char *); extern mps_lib_assert_fail_t mps_lib_assert_fail_install(mps_lib_assert_fail_t); +/* Allocate or free memory. Analogous to `malloc` and `free` from + stdlib.h. NOTE: These must not be used outside the ANSI VM + (vman.c) to ensure the MPS remains able to bootstrap itself from + raw memory. */ +extern void *mps_lib_malloc(size_t); +extern void mps_lib_free(void *); + /* Set, copy, or compare memory. Analagous to `memset`, `memcpy`, and `memcmp` from string.h. */ extern void *(mps_lib_memset)(void *, int, size_t); diff --git a/code/mpsliban.c b/code/mpsliban.c index a7927c80cd..b0b1887545 100644 --- a/code/mpsliban.c +++ b/code/mpsliban.c @@ -95,6 +95,16 @@ mps_lib_assert_fail_t mps_lib_assert_fail_install(mps_lib_assert_fail_t handler) } +void *mps_lib_malloc(size_t size) +{ + return malloc(size); +} + +void mps_lib_free(void *p) +{ + free(p); +} + void *(mps_lib_memset)(void *s, int c, size_t n) { return memset(s, c, n); diff --git a/code/mpslibfs.c b/code/mpslibfs.c new file mode 100644 index 0000000000..354213d3f2 --- /dev/null +++ b/code/mpslibfs.c @@ -0,0 +1,156 @@ +/* mpslibfs.c: FREESTANDING LIBRARY INTERFACE STUB + * + * $Id$ + * Copyright (c) 2001-2020 Ravenbrook Limited. See end of file for license. + * Portions copyright (c) 2002 Global Graphics Software. + * + * .purpose: The purpose of this code is to test at compile time + * whether the code MPS can be compiled as freestanding + * (design.mps.exec-env.req) with a command like:: + * + * gcc -nostdlib -DCONFIG_PLINTH_NONE -DCONFIG_PF_ANSI \ + * --entry mps_lib_assert_fail mps.c mpslibfs.c + * + * .readership: For MPS client application developers and MPS developers. + * .sources: + */ + +#include "mpslib.h" +#include "mpsio.h" + +#include "mpstd.h" +#include "event.h" + +int mps_lib_get_EOF(void) +{ + NOTREACHED; +} + +mps_lib_FILE *mps_lib_get_stderr(void) +{ + NOTREACHED; +} + +mps_lib_FILE *mps_lib_get_stdout(void) +{ + NOTREACHED; +} + +int mps_lib_fputc(int c, mps_lib_FILE *stream) +{ + NOTREACHED; +} + +int mps_lib_fputs(const char *s, mps_lib_FILE *stream) +{ + NOTREACHED; +} + +void mps_lib_assert_fail(const char *file, + unsigned line, + const char *condition) +{ + for (;;) + NOOP; +} + +mps_lib_assert_fail_t mps_lib_assert_fail_install(mps_lib_assert_fail_t handler) +{ + NOTREACHED; +} + + +void *mps_lib_malloc(size_t size) +{ + NOTREACHED; +} + +void mps_lib_free(void *p) +{ + NOTREACHED; +} + +void *(mps_lib_memset)(void *s, int c, size_t n) +{ + NOTREACHED; +} + +void *(mps_lib_memcpy)(void *s1, const void *s2, size_t n) +{ + NOTREACHED; +} + +int (mps_lib_memcmp)(const void *s1, const void *s2, size_t n) +{ + NOTREACHED; +} + +mps_clock_t mps_clock(void) +{ + NOTREACHED; +} + + +mps_clock_t mps_clocks_per_sec(void) +{ + NOTREACHED; +} + +unsigned long mps_lib_telemetry_control(void) +{ + NOTREACHED; +} + + +mps_res_t mps_io_create(mps_io_t *mps_io_r) +{ + NOTREACHED; +} + + +void mps_io_destroy(mps_io_t mps_io) +{ + NOTREACHED; +} + + +mps_res_t mps_io_write(mps_io_t mps_io, void *buf, size_t size) +{ + NOTREACHED; +} + + +mps_res_t mps_io_flush(mps_io_t mps_io) +{ + NOTREACHED; +} + + +/* C. COPYRIGHT AND LICENSE + * + * Copyright (C) 2001-2023 Ravenbrook Limited . + * + * 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. + * + * 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. + */ diff --git a/code/root.c b/code/root.c index 42336bbe75..606bd5c3d0 100644 --- a/code/root.c +++ b/code/root.c @@ -201,7 +201,7 @@ static Res rootCreate(Root *rootReturn, Arena arena, root->arena = arena; root->rank = rank; root->var = type; - root->the = *theUnionP; + mps_lib_memcpy(&root->the, theUnionP, sizeof(root->the)); root->grey = TraceSetEMPTY; root->summary = RefSetUNIV; root->mode = mode; diff --git a/code/vman.c b/code/vman.c index fba4afe7d5..4dc916b507 100644 --- a/code/vman.c +++ b/code/vman.c @@ -6,8 +6,7 @@ #include "mpm.h" #include "vm.h" - -#include /* for malloc and free */ +#include "mpslib.h" SRCID(vman, "$Id$"); @@ -58,7 +57,7 @@ Res VMInit(VM vm, Size size, Size grainSize, void *params) if (reserved < grainSize || reserved > (Size)(size_t)-1) return ResRESOURCE; - vbase = malloc((size_t)reserved); + vbase = mps_lib_malloc((size_t)reserved); if (vbase == NULL) return ResMEMORY; (void)mps_lib_memset(vbase, VMJunkBYTE, reserved); @@ -96,7 +95,7 @@ void VMFinish(VM vm) vm->sig = SigInvalid; (void)mps_lib_memset(vm->block, VMJunkBYTE, vm->reserved); - free(vm->block); + mps_lib_free(vm->block); }