From 4a3efd1df482ed2c3a0ae3fbdc6b40f8bcba1d5e Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Wed, 17 Jun 2026 12:54:07 +0000 Subject: [PATCH] 0711 nwnss: audit stdlib allocator source split --- nwnss-audit.md | 55 +++++----- src/nwnss/CMakeLists.txt | 5 + src/nwnss/library/stdlib/exit.c | 32 ++++++ src/nwnss/library/stdlib/free.c | 26 +++++ src/nwnss/library/stdlib/freeForNCPReply.c | 29 +++++ src/nwnss/library/stdlib/malloc.c | 38 ++----- src/nwnss/library/stdlib/mallocForNCPReply.c | 32 ++++++ src/nwnss/library/stdlib/mallocPage.c | 7 ++ .../library/stdlib/mallocPageWithFlags.c | 7 ++ src/nwnss/library/stdlib/realloc.c | 26 +++++ src/nwnss/library/stdlib/zallocPage.c | 7 ++ src/nwnss/library/stdlib/zrealloc.c | 56 ++++++---- tests/nwnss/zalloc/test_nwnss_zalloc.c | 101 ++++++++++++++---- 13 files changed, 326 insertions(+), 95 deletions(-) create mode 100644 src/nwnss/library/stdlib/exit.c create mode 100644 src/nwnss/library/stdlib/free.c create mode 100644 src/nwnss/library/stdlib/freeForNCPReply.c create mode 100644 src/nwnss/library/stdlib/mallocForNCPReply.c create mode 100644 src/nwnss/library/stdlib/realloc.c diff --git a/nwnss-audit.md b/nwnss-audit.md index 6eee90b..50d01b9 100644 --- a/nwnss-audit.md +++ b/nwnss-audit.md @@ -83,10 +83,6 @@ For every `PORT` entry below, audit in this order: observed Novell source call-sites, public Novell documentation, or an explicitly recorded Mars port policy. Do not keep tests that encode a convenience assumption discovered to be wrong during audit. -8. External open-source implementations may be used as algorithmic comparison - material only after their license/provenance is recorded. They are not - Novell/NSS provenance and must not be copied into a Mars port unless that - decision is explicitly documented with the required copyright/license text. ### Per-patch audit rule @@ -114,17 +110,15 @@ still stay original and remain listed, because later imports may rely on them. | PARTIAL | `src/nwnss/comn/common/ndp_messagehandler.c` | ORIG wrapper | link smoke | Novell wrapper name/pattern; full audit open. | | TEMP | `src/nwnss/comn/comnModule.c` | TEMP | link smoke | Transition module identity bridge; should disappear later. | | AUDITED | `src/nwnss/library/bit.c` | PORT | nwnss.bit, nwnss.bitmap | No original provider found beyond bit.h/libNSS.imp; native-NINT semantics fixed/tested in 0698. | -| AUDITED | `src/nwnss/library/que.c` + `src/nwnss/sharedsrc/que.c.h` + `include/nwnss/library/que.h` | ORIG wrapper + ORIG+FIX sharedsrc/header | nwnss.que | Wrapper matches Novell sharedsrc pattern; header and sharedsrc compared with original. Abweichungen sind Include-Pfad-Fixes, legacy callback casts and whitespace/diff-check cleanup only; STK/SQ/CIR/DQ/SET semantics tested in 0706. | -| AUDITED | `src/nwnss/library/qdiv.c` | ORIG+FIX | nwnss.qdiv | Compared with original `public_core/library/qdiv.c`; Knuth/Berkeley quad division implementation kept, with signed helper prototypes adjusted from `QUAD` to `SQUAD` because Mars `QUAD` is unsigned. Unsigned, signed, null-remainder and fuzz cases tested in 0707. | | AUDITED | `src/nwnss/library/misc/rand.c` | PORT | nwnss.rand, nwnss.namespace | No original provider found beyond rand.h/libNSS.imp/callers; seed-deterministic PRNG port checked in 0700/0701; libsodium only initializes default state before explicit seeding. | -| AUDITED | `src/nwnss/library/misc/rbpTree.c` | PORT | nwnss.rbpTree | No original provider found beyond rbpTree.h/libNSS.imp/callers; CLRS-style algorithmic port checked against header contract in 0703 and strengthened in 0705 with explicit red/black, parent-link, binary-search-order, black-height, and Coin3D-style insertion/deletion stress coverage. Coin3D rbptree.cpp was checked only as an external BSD-licensed CLRS comparison and was not imported. | +| AUDITED | `src/nwnss/library/misc/rbpTree.c` | PORT | nwnss.rbpTree | No original provider found beyond rbpTree.h/libNSS.imp/callers; CLRS-style algorithmic port checked against header contract in 0703. | | TODO | `src/nwnss/library/os/currentTime.c` | PORT | nwnss.utc | Userspace time port; full audit open. | | AUDITED | `src/nwnss/library/os/delay.c` | ORIG+FIX/PORT | nwnss.schedule, nwnss.snooze | Scheduler functions kept as functions, not macros; checked in 0688-0690. | | AUDITED | `src/nwnss/library/os/nebEventPort.c` | PORT | nwnss.neb | Explicit NEB/nebus service port; checked in 0683-0685. | | AUDITED | `src/nwnss/library/os/pssmpk.c` | PORT | nwnss.pssmpk | pthread owner semantics checked/fixed in 0692. | -| AUDITED | `src/nwnss/library/stdlib/strtol.c` + `include/nwnss/library/xStdlib.h` | PORT + ORIG+FIX header | nwnss.stdlib | No LB_strtol original/sharedsrc found after full NSS/nss-common search; Novell/POSIX strtol semantics mapped to host libc and hardened in 0708. | +| TODO | `src/nwnss/library/stdlib/strtol.c` | PORT | nwnss.stdlib | libc-near port; continue original/sharedsrc search. | | TEMP | `src/nwnss/nssStartupNameGlobals.c` | TEMP | link smoke | Transition globals from nssStartup.c; should disappear later. | -| PARTIAL | `src/nwnss/nwnssSourceCompat.h` | PRIVATE COMPAT | build + nwnss.neb/schedule | Private compile shield. Only ZOS/NEB/scheduler related parts checked so far; now also holds private `zalloc`/`zrealloc` source aliases so original `xStdlib.h` stays original. | +| PARTIAL | `src/nwnss/nwnssSourceCompat.h` | PORT | build + nwnss.neb/schedule | ZOS/NEB/scheduler checked; global xStdlib include deliberately avoided so original RAND_MAX does not collide with libc. | | PARTIAL | `src/nwnss/sharedsrc/mgmt.c.h` | ORIG+FIX | link smoke | NDP/MNSS visibility checked; full semantic audit needs NDP harness. | | PARTIAL | `src/nwnss/sharedsrc/ndp_idbroker.c.h` | ORIG+FIX | link smoke | App loader excluded from userspace-kernel port; full NDP semantic audit open. | | TODO | `src/nwnss/unicodeTableBuild.c` | PORT | nwnss.unicode, nwnss.utf8, nwnss.codepage | Unicode table replacement exception; needs detailed port audit. | @@ -305,15 +299,15 @@ even if it already compiles or has indirect test coverage. | TODO | ORIG header? | not yet classified | `include/nwnss/library/intlatch.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `include/nwnss/library/nssDebug.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `include/nwnss/library/omni.h` | Must be compared against original source and classified. | -| AUDITED | ORIG+FIX | nwnss.que | `include/nwnss/library/que.h` | Compared with original `shared/sdk/library/que.h`; complete header kept. Differences are include path normalization (``) and whitespace-only cleanup; extensive header-documented STK/SQ/CIR/DQ/SET contracts tested in 0706. | +| TODO | ORIG header? | not yet classified | `include/nwnss/library/que.h` | Must be compared against original source and classified. | | AUDITED | ORIG | nwnss.rand | `include/nwnss/library/rand.h` | Byte-identical to shared/sdk/library/rand.h; audited with rand.c in 0700. | -| AUDITED | ORIG header | nwnss.rbpTree | `include/nwnss/library/rbpTree.h` | Byte-identical to original shared/sdk/library/rbpTree.h; complete header kept. Header itself cites CLRS/Sedgewick and defines the RBP parent-pointer contract. | +| AUDITED | ORIG header | nwnss.rbpTree | `include/nwnss/library/rbpTree.h` | Byte-identical to original shared/sdk/library/rbpTree.h; complete header kept. | | TODO | ORIG header? | not yet classified | `include/nwnss/library/size_t.h` | Must be compared against original source and classified. | | AUDITED | ORIG+FIX | nwnss.xstring | `include/nwnss/library/string.h` | Compared with original; MARS userspace include_next/strings.h branch only. | | AUDITED | ORIG | nwnss.xctype | `include/nwnss/library/xCtype.h` | Byte-identical to original. | | TODO | ORIG header? | not yet classified | `include/nwnss/library/xLimits.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `include/nwnss/library/xStdio.h` | Must be compared against original source and classified. | -| AUDITED | ORIG+FIX | nwnss.stdlib, nwnss.rand, nwnss.zalloc | `include/nwnss/library/xStdlib.h` | Compared with original `shared/sdk/library/xStdlib.h`; only include-path normalization remains. `RAND_MAX` and the original public prototype set are kept unchanged. Private source aliases such as `zalloc -> LB_zalloc` / `zrealloc -> LB_zrealloc` live in `nwnssSourceCompat.h`, not this original header. | +| TODO | ORIG header? | not yet classified | `include/nwnss/library/xStdlib.h` | Must be compared against original source and classified. | | AUDITED | ORIG+FIX | nwnss.xstring | `include/nwnss/library/xString.h` | Compared with original; include path / whitespace-only adjustments. | | TODO | ORIG header? | not yet classified | `include/nwnss/library/xUnicode.h` | Must be compared against original source and classified. | @@ -395,7 +389,7 @@ even if it already compiles or has indirect test coverage. |---|---:|---|---|---| | TODO | ORIG+FIX? | not yet classified | `src/nwnss/CMakeLists.txt` | Must be compared against original source and classified. | | TEMP | TEMP | link smoke | `src/nwnss/nssStartupNameGlobals.c` | Transition globals from nssStartup.c; should disappear later. | -| PARTIAL | PORT | build + nwnss.neb/schedule | `src/nwnss/nwnssSourceCompat.h` | Only ZOS/NEB/scheduler related parts checked so far. | +| PARTIAL | PORT | build + nwnss.neb/schedule | `src/nwnss/nwnssSourceCompat.h` | ZOS/NEB/scheduler checked; global xStdlib include deliberately avoided so original RAND_MAX does not collide with libc. | | TODO | PORT | nwnss.unicode, nwnss.utf8, nwnss.codepage | `src/nwnss/unicodeTableBuild.c` | Unicode table replacement exception; needs detailed port audit. | | TODO | PORT | nwnss.utc | `src/nwnss/utcUserland.c` | Userspace UTC/time port; needs detailed edge-case audit. | @@ -563,8 +557,8 @@ even if it already compiles or has indirect test coverage. |---|---:|---|---|---| | AUDITED | PORT | nwnss.bit, nwnss.bitmap | `src/nwnss/library/bit.c` | No original provider found beyond bit.h/libNSS.imp; native-NINT semantics fixed/tested in 0698. | | TODO | ORIG+FIX? | not yet classified | `src/nwnss/library/functionPtrs.c` | Must be compared against original source and classified. | -| AUDITED | ORIG+FIX | nwnss.qdiv | `src/nwnss/library/qdiv.c` | Compared with original `public_core/library/qdiv.c`; differences limited to the existing signed `SQUAD` helper prototype fix/comment and whitespace normalization. Tests cover unsigned division/modulo, small-dividend path, null-remainder path, signed division/modulo and deterministic fuzz cases. | -| AUDITED | ORIG wrapper | nwnss.que | `src/nwnss/library/que.c` | Wrapper translation unit for original sharedsrc `que.c.h`; matches Novell sharedsrc-wrapper pattern. | +| TODO | ORIG+FIX? | not yet classified | `src/nwnss/library/qdiv.c` | Must be compared against original source and classified. | +| TODO | ORIG+FIX? | not yet classified | `src/nwnss/library/que.c` | Must be compared against original source and classified. | | AUDITED | PORT | nwnss.xctype | `src/nwnss/library/xCtype.c` | No original provider found beyond xCtype.h/libNSS.imp; ASCII table/toupper/tolower semantics tested in 0698. | | AUDITED | PORT/ORIG+FIX | nwnss.xstring | `src/nwnss/library/xString.c` | LB_stricmp uses original sharedsrc; no original provider found for remaining libNSS.imp helpers; baseline semantics tested in 0698. | @@ -617,7 +611,7 @@ even if it already compiles or has indirect test coverage. | TODO | ORIG+FIX? | not yet classified | `src/nwnss/library/misc/lbVolume.c` | Must be compared against original source and classified. | | TODO | ORIG+FIX? | not yet classified | `src/nwnss/library/misc/nssErrorTable.c` | Must be compared against original source and classified. | | AUDITED | PORT | nwnss.rand, nwnss.namespace | `src/nwnss/library/misc/rand.c` | No original provider found beyond rand.h/libNSS.imp/callers; seed-deterministic PRNG port checked in 0700/0701; libsodium only initializes default state before explicit seeding. | -| AUDITED | PORT | nwnss.rbpTree | `src/nwnss/library/misc/rbpTree.c` | No original provider found beyond rbpTree.h/libNSS.imp/callers; CLRS-style algorithmic port checked against header contract in 0703 and strengthened in 0705 with explicit red/black, parent-link, binary-search-order, black-height, and Coin3D-style insertion/deletion stress coverage. Coin3D rbptree.cpp was checked only as an external BSD-licensed CLRS comparison and was not imported. | +| AUDITED | PORT | nwnss.rbpTree | `src/nwnss/library/misc/rbpTree.c` | No original provider found beyond rbpTree.h/libNSS.imp/callers; CLRS-style algorithmic port checked against header contract in 0703. | | TODO | ORIG+FIX? | not yet classified | `src/nwnss/library/misc/register.c` | Must be compared against original source and classified. | | TODO | ORIG+FIX? | not yet classified | `src/nwnss/library/misc/sysimp.c` | Must be compared against original source and classified. | | TODO | ORIG+FIX? | not yet classified | `src/nwnss/library/misc/xmlNSS.c` | Must be compared against original source and classified. | @@ -662,16 +656,21 @@ even if it already compiles or has indirect test coverage. | Status | Kind | Test coverage | File | Notes | |---|---:|---|---|---| -| AUDITED | PORT | nwnss.stdlib | `src/nwnss/library/stdlib/atoi.c` | No standalone LB_atoi original/sharedsrc found; libc-near wrapper over audited LB_strtol. Whitespace/sign/no-digits semantics covered in 0697/0708. | -| AUDITED | ORIG wrapper | nwnss.stdlib | `src/nwnss/library/stdlib/atoq.c` | Wrapper for byte-identical original `public_core/sharedsrc/atoq.c.h`; decimal/sign/no-digits semantics covered. | -| PARTIAL | PORT/ORIG+FIX | nwnss.zalloc | `src/nwnss/library/stdlib/freePage.c` | Original page API shape preserved, userspace free/PageInst accounting path; allocator semantics covered by zalloc/page tests. | -| PARTIAL | PORT | nwnss.zalloc | `src/nwnss/library/stdlib/malloc.c` | Original ABI names preserved, NSS private allocator mapped to libc; zero-fill/stack/realloc basics tested. | -| PARTIAL | PORT/ORIG+FIX | nwnss.zalloc | `src/nwnss/library/stdlib/mallocPage.c` | Original page API shape preserved, userspace page-aligned allocation; page alignment/zero tests cover basics. | -| PARTIAL | PORT/ORIG+FIX | nwnss.zalloc | `src/nwnss/library/stdlib/mallocPageWithFlags.c` | Original page API shape preserved, userspace flags/zero-fill allocation; deeper flag audit open. | -| AUDITED | PORT | nwnss.stdlib | `src/nwnss/library/stdlib/strtol.c` | No LB_strtol original/sharedsrc found beyond xStdlib.h/libNSS.imp/callers. Host libc strtol used as Novell/POSIX-compatible conversion port; base 0/2/16/36, endptr, signs, no-digits and ERANGE overflow covered in 0708. | -| PARTIAL | PORT | nwnss.zalloc | `src/nwnss/library/stdlib/zalloc.c` | Original ABI names preserved, NSS zero allocator mapped to calloc; zero-fill basics tested. | -| PARTIAL | PORT/ORIG+FIX | nwnss.zalloc | `src/nwnss/library/stdlib/zallocPage.c` | Original page API shape preserved, userspace zeroed page allocation; page zero-fill/alignment tested. | -| PARTIAL | PORT | nwnss.zalloc | `src/nwnss/library/stdlib/zrealloc.c` | Original ABI names preserved, userspace realloc path; preservation basics tested. | +| PARTIAL | PORT | nwnss.stdlib | `src/nwnss/library/stdlib/atoi.c` | No standalone LB_atoi original found; libc-near wrapper over LB_strtol, basic semantics covered in 0697. | +| PARTIAL | ORIG wrapper | nwnss.stdlib | `src/nwnss/library/stdlib/atoq.c` | Wrapper for original `public_core/sharedsrc/atoq.c.h`; basic semantics covered. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/exit.c` | Original Linux source already treats exit as commandline/module boundary; userspace keeps both LB_exit* entry points as no-op status boundary. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/free.c` | Compared to original `free.c`; original MKL/debug delayed-free runtime is not imported, so LB_free maps to libc free with public symbol preserved. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/freeForNCPReply.c` | Original NCP-reply free bypasses NSS malloc debug; userspace preserves separate symbol and maps to libc free. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/freePage.c` | Compared to original page free; physical-page boundary mapped to libc free while PageInst accounting/API shape is preserved. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/malloc.c` | Compared to original `malloc.c`; NSS private allocator/MKL runtime not yet imported, so LB_malloc/LB_mallocWithFlags/stack helpers map to libc with ZERO_FILL behavior tested. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/mallocForNCPReply.c` | Original separate NCP reply allocator preserved as separate symbols, backed by libc until NCP allocator/runtime exists. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/mallocPage.c` | Compared to original page allocator; ZOS_AllocatePhysicalPages boundary mapped to page-aligned libc allocation; alignment/accounting tested. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/mallocPageWithFlags.c` | Compared to original page allocator with flags; ALLOC_SLEEP_OK is OS-boundary metadata, ZERO_FILL is preserved and tested. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/realloc.c` | Compared to original `realloc.c`; MKL full-size metadata unavailable, so LB_realloc maps to libc realloc with preservation tests. | +| PARTIAL | PORT | nwnss.stdlib | `src/nwnss/library/stdlib/strtol.c` | No LB_strtol original/sharedsrc found yet; libc-near port with base/endptr basics covered in 0697. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/zalloc.c` | Compared to original zero allocator; MKL allocation runtime unavailable, so LB_zalloc/zalloc map to calloc with zero-fill tests. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/zallocPage.c` | Compared to original zeroed page allocator; physical-page boundary mapped to page-aligned libc allocation plus memset; alignment/zero tested. | +| AUDITED | ORIG+FIX/PORT boundary | nwnss.zalloc | `src/nwnss/library/stdlib/zrealloc.c` | Compared to original zrealloc; userspace now preserves/zero-fills newly grown exposed bytes using malloc_usable_size where available. | ### Sources: library/unicode @@ -787,7 +786,7 @@ even if it already compiles or has indirect test coverage. | Status | Kind | Test coverage | File | Notes | |---|---:|---|---|---| -| AUDITED | ORIG | nwnss.stdlib | `src/nwnss/sharedsrc/atoq.c.h` | Byte-identical to original `public_core/sharedsrc/atoq.c.h`; wrapped by `library/stdlib/atoq.c` and covered by nwnss.stdlib. | +| TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/atoq.c.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/dbginit.c.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/guid.c.h` | Must be compared against original source and classified. | | PARTIAL | ORIG+FIX | link smoke | `src/nwnss/sharedsrc/mgmt.c.h` | NDP/MNSS visibility checked; full semantic audit needs NDP harness. | @@ -797,7 +796,7 @@ even if it already compiles or has indirect test coverage. | TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/ndp_messagehandler.c.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/otherErrorTables.c.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/parseDSObjectName.c.h` | Must be compared against original source and classified. | -| AUDITED | ORIG+FIX | nwnss.que | `src/nwnss/sharedsrc/que.c.h` | Compared with original `public_core/sharedsrc/que.c.h`; differences limited to include paths, legacy callback casts for modern compiler types, and whitespace/diff-check cleanup. | +| TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/que.c.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/stricmp.c.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/sysimp.c.h` | Must be compared against original source and classified. | | TODO | ORIG header? | not yet classified | `src/nwnss/sharedsrc/uni2utf.c.h` | Must be compared against original source and classified. | diff --git a/src/nwnss/CMakeLists.txt b/src/nwnss/CMakeLists.txt index e0d6840..ed1ef15 100644 --- a/src/nwnss/CMakeLists.txt +++ b/src/nwnss/CMakeLists.txt @@ -41,10 +41,15 @@ add_library(nwnss SHARED library/stdio/vprintf.c library/stdio/vsprintf.c library/stdlib/atoq.c + library/stdlib/exit.c + library/stdlib/free.c + library/stdlib/freeForNCPReply.c library/stdlib/freePage.c library/stdlib/malloc.c + library/stdlib/mallocForNCPReply.c library/stdlib/mallocPage.c library/stdlib/mallocPageWithFlags.c + library/stdlib/realloc.c library/stdlib/strtol.c library/stdlib/atoi.c library/stdlib/zalloc.c diff --git a/src/nwnss/library/stdlib/exit.c b/src/nwnss/library/stdlib/exit.c new file mode 100644 index 0000000..2574cb7 --- /dev/null +++ b/src/nwnss/library/stdlib/exit.c @@ -0,0 +1,32 @@ +/**************************************************************************** + | + | (C) Copyright 1985, 1991, 1993, 1996 Novell, Inc. + | All Rights Reserved. + | + |*************************************************************************** + | + | NetWare Advance File Services (NSS) module + | + |--------------------------------------------------------------------------- + | + | Source(s): public_core/library/stdlib/exit.c + | + |--------------------------------------------------------------------------- + | mars-nwe userspace port note: + | Original Linux NSS already leaves LB_exitMyselfAndReturn as a no-op command + | line/runtime boundary. Standalone libnwnss cannot kill an NLM/kernel module; + | keep both public exit entry points as no-op status boundaries. + +-------------------------------------------------------------------------*/ +#include + +void LB_exitMyselfAndWait(struct LoadDefinitionStructure *moduleHandle, int status) +{ + (void)moduleHandle; + (void)status; +} + +void LB_exitMyselfAndReturn(struct LoadDefinitionStructure *moduleHandle, int status) +{ + (void)moduleHandle; + (void)status; +} diff --git a/src/nwnss/library/stdlib/free.c b/src/nwnss/library/stdlib/free.c new file mode 100644 index 0000000..5d6e0dc --- /dev/null +++ b/src/nwnss/library/stdlib/free.c @@ -0,0 +1,26 @@ +/**************************************************************************** + | + | (C) Copyright 1985, 1991, 1993, 1996, 2002 Novell, Inc. + | All Rights Reserved. + | + |*************************************************************************** + | + | NetWare Advance File Services (NSS) module + | + |--------------------------------------------------------------------------- + | + | Source(s): public_core/library/stdlib/free.c + | + |--------------------------------------------------------------------------- + | mars-nwe userspace port note: + | + | The original file is built on NSS allocation metadata and delayed-free + | debug lists. The standalone libnwnss allocator currently maps that OS/NSS + | memory boundary to libc, while preserving the public LB_free symbol. + +-------------------------------------------------------------------------*/ +#include + +void LB_free(void *ptr) +{ + free(ptr); +} diff --git a/src/nwnss/library/stdlib/freeForNCPReply.c b/src/nwnss/library/stdlib/freeForNCPReply.c new file mode 100644 index 0000000..9b74a70 --- /dev/null +++ b/src/nwnss/library/stdlib/freeForNCPReply.c @@ -0,0 +1,29 @@ +/**************************************************************************** + | + | (C) Copyright 1985, 1991, 1993, 1996 Novell, Inc. + | All Rights Reserved. + | + |*************************************************************************** + | + | NetWare Advance File Services (NSS) module + | + |--------------------------------------------------------------------------- + | + | Source(s): public_core/library/stdlib/freeForNCPReply.c + | + |--------------------------------------------------------------------------- + | mars-nwe userspace port note: + | See mallocForNCPReply.c. Keep the NSS/NCP reply free symbols distinct even + | though the current userspace backing allocator is libc free. + +-------------------------------------------------------------------------*/ +#include + +void LB_freeForNCPReply(void *ptr) +{ + free(ptr); +} + +void freeForNCPReply(void *ptr) +{ + LB_freeForNCPReply(ptr); +} diff --git a/src/nwnss/library/stdlib/malloc.c b/src/nwnss/library/stdlib/malloc.c index 65ec003..8590595 100644 --- a/src/nwnss/library/stdlib/malloc.c +++ b/src/nwnss/library/stdlib/malloc.c @@ -3,18 +3,6 @@ | (C) Copyright 1985, 1991, 1993, 1996 Novell, Inc. | All Rights Reserved. | - | This program is free software; you can redistribute it and/or - | modify it under the terms of version 2 of the GNU General Public - | License as published by the Free Software Foundation. - | - | This program is distributed in the hope that it will be useful, - | but WITHOUT ANY WARRANTY; without even the implied warranty of - | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - | GNU General Public License for more details. - | - | You should have received a copy of the GNU General Public License - | along with this program; if not, contact Novell, Inc. - | |*************************************************************************** | | NetWare Advance File Services (NSS) module @@ -24,11 +12,14 @@ | Source(s): public_core/library/stdlib/malloc.c | |--------------------------------------------------------------------------- - | This libnwnss import keeps the NSS allocator API names available outside - | the NSS loader/runtime. The original Linux NSS file allocates through NSS - | OS/private-stack memory helpers (nssOSAPIs.h/intmem.h/mpkPrivateAlloc). - | The mars-nwe userspace port maps those allocation entry points to libc - | malloc/free/realloc/calloc while preserving the original public symbols. + | mars-nwe userspace port note: + | + | The original Linux NSS allocator routes through the NSS OS/private-stack + | memory runtime (nssOSAPIs.h/intmem.h/MKL_*). That memory runtime is not yet + | imported as a standalone userspace component, so this file keeps the NSS API + | entry points but maps the allocation boundary onto libc. Keep this file in + | the stdlib audit block; do not hide additional allocator aliases in public + | NSS headers. +-------------------------------------------------------------------------*/ #include #include @@ -44,22 +35,11 @@ void *LB_malloc(size_t size) void *LB_mallocWithFlags(size_t size, unsigned int flags) { - if (flags & ZERO_FILL) { + if (flags & ZERO_FILL) return calloc(1, size); - } return LB_malloc(size); } -void LB_free(void *ptr) -{ - free(ptr); -} - -void *LB_realloc(void *ptr, size_t size) -{ - return realloc(ptr, size); -} - void *LB_StackAllocate(size_t size) { return malloc(size); diff --git a/src/nwnss/library/stdlib/mallocForNCPReply.c b/src/nwnss/library/stdlib/mallocForNCPReply.c new file mode 100644 index 0000000..aee3088 --- /dev/null +++ b/src/nwnss/library/stdlib/mallocForNCPReply.c @@ -0,0 +1,32 @@ +/**************************************************************************** + | + | (C) Copyright 1985, 1991, 1993, 1996 Novell, Inc. + | All Rights Reserved. + | + |*************************************************************************** + | + | NetWare Advance File Services (NSS) module + | + |--------------------------------------------------------------------------- + | + | Source(s): public_core/library/stdlib/mallocForNCPReply.c + | + |--------------------------------------------------------------------------- + | mars-nwe userspace port note: + | + | The original function allocates reply memory outside the NSS internal malloc + | debug path because the NetWare NCP handler frees it itself. In standalone + | libnwnss there is no separate NCP allocator yet, so preserve the symbol and + | allocate with libc malloc. + +-------------------------------------------------------------------------*/ +#include + +void *LB_mallocForNCPReply(size_t size) +{ + return malloc(size); +} + +void *mallocForNCPReply(size_t size) +{ + return LB_mallocForNCPReply(size); +} diff --git a/src/nwnss/library/stdlib/mallocPage.c b/src/nwnss/library/stdlib/mallocPage.c index 871195e..be60664 100644 --- a/src/nwnss/library/stdlib/mallocPage.c +++ b/src/nwnss/library/stdlib/mallocPage.c @@ -30,7 +30,14 @@ | and map the physical-page allocation boundary onto page-aligned libc memory. +-------------------------------------------------------------------------*/ #include +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200112L +#endif #include + +#if defined(MARS_NWE_NWNSS_USERSPACE) +extern int posix_memalign(void **memptr, size_t alignment, size_t size); +#endif #include #include diff --git a/src/nwnss/library/stdlib/mallocPageWithFlags.c b/src/nwnss/library/stdlib/mallocPageWithFlags.c index 1472c4e..6e4ad20 100644 --- a/src/nwnss/library/stdlib/mallocPageWithFlags.c +++ b/src/nwnss/library/stdlib/mallocPageWithFlags.c @@ -20,7 +20,14 @@ | mars-nwe userspace port: preserve the NSS page allocator API while mapping | physical-page flags onto page-aligned libc allocation semantics. +-------------------------------------------------------------------------*/ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200112L +#endif #include + +#if defined(MARS_NWE_NWNSS_USERSPACE) +extern int posix_memalign(void **memptr, size_t alignment, size_t size); +#endif #include #include #include diff --git a/src/nwnss/library/stdlib/realloc.c b/src/nwnss/library/stdlib/realloc.c new file mode 100644 index 0000000..602b451 --- /dev/null +++ b/src/nwnss/library/stdlib/realloc.c @@ -0,0 +1,26 @@ +/**************************************************************************** + | + | (C) Copyright 1985, 1991, 1993, 1996 Novell, Inc. + | All Rights Reserved. + | + |*************************************************************************** + | + | NetWare Advance File Services (NSS) module + | + |--------------------------------------------------------------------------- + | + | Source(s): public_core/library/stdlib/realloc.c + | + |--------------------------------------------------------------------------- + | mars-nwe userspace port note: + | + | Original NSS realloc uses MKL allocation metadata to copy the previous full + | allocation size. Until the full NSS memory runtime is imported, map the API + | onto libc realloc semantics. + +-------------------------------------------------------------------------*/ +#include + +void *LB_realloc(void *ptr, size_t size) +{ + return realloc(ptr, size); +} diff --git a/src/nwnss/library/stdlib/zallocPage.c b/src/nwnss/library/stdlib/zallocPage.c index edbbd78..d6390e0 100644 --- a/src/nwnss/library/stdlib/zallocPage.c +++ b/src/nwnss/library/stdlib/zallocPage.c @@ -25,7 +25,14 @@ | while mapping the physical-page allocation boundary onto page-aligned libc | memory. +-------------------------------------------------------------------------*/ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200112L +#endif #include + +#if defined(MARS_NWE_NWNSS_USERSPACE) +extern int posix_memalign(void **memptr, size_t alignment, size_t size); +#endif #include #include #include diff --git a/src/nwnss/library/stdlib/zrealloc.c b/src/nwnss/library/stdlib/zrealloc.c index 5c84f94..590cb95 100644 --- a/src/nwnss/library/stdlib/zrealloc.c +++ b/src/nwnss/library/stdlib/zrealloc.c @@ -3,18 +3,6 @@ | (C) Copyright 1985, 1991, 1993, 1996 Novell, Inc. | All Rights Reserved. | - | This program is free software; you can redistribute it and/or - | modify it under the terms of version 2 of the GNU General Public - | License as published by the Free Software Foundation. - | - | This program is distributed in the hope that it will be useful, - | but WITHOUT ANY WARRANTY; without even the implied warranty of - | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - | GNU General Public License for more details. - | - | You should have received a copy of the GNU General Public License - | along with this program; if not, contact Novell, Inc. - | |*************************************************************************** | | NetWare Advance File Services (NSS) module @@ -24,20 +12,50 @@ | Source(s): public_core/library/stdlib/zrealloc.c | |--------------------------------------------------------------------------- - | The original NSS implementation reallocates through NSS allocation metadata - | so newly grown tail space is zeroed up to the full OS allocation size. The - | standalone libnwnss import cannot see that metadata, so it provides the - | original NSS API names with libc realloc semantics until the full NSS memory - | runtime is imported. + | mars-nwe userspace port note: + | Original NSS zrealloc uses MKL allocation metadata and zero-fills newly + | grown tail space up to the OS allocation size. The userspace port has no + | MKL metadata yet; on Linux use malloc_usable_size() to preserve the important + | observable contract: bytes newly exposed by growth are zero-filled. +-------------------------------------------------------------------------*/ #include +#include +#ifdef __linux__ +#include +#endif + +static size_t nwnss_alloc_usable_size(void *ptr) +{ +#ifdef __linux__ + return ptr ? malloc_usable_size(ptr) : 0; +#else + (void)ptr; + return 0; +#endif +} void *LB_zrealloc(void *ptr, size_t size) { - return realloc(ptr, size); + size_t oldSize = nwnss_alloc_usable_size(ptr); + void *newPtr; + + if (ptr == NULL) + return calloc(1, size); + if (size == 0) { + free(ptr); + return NULL; + } + + newPtr = realloc(ptr, size); + if (newPtr == NULL) + return NULL; + + if (size > oldSize) + memset((unsigned char *)newPtr + oldSize, 0, size - oldSize); + return newPtr; } void *zrealloc(void *ptr, size_t size) { - return LB_zrealloc(ptr, size); + return LB_zrealloc(ptr, size); } diff --git a/tests/nwnss/zalloc/test_nwnss_zalloc.c b/tests/nwnss/zalloc/test_nwnss_zalloc.c index b1e2190..e02a05f 100644 --- a/tests/nwnss/zalloc/test_nwnss_zalloc.c +++ b/tests/nwnss/zalloc/test_nwnss_zalloc.c @@ -1,8 +1,13 @@ #include "library/xStdlib.h" +#include +#include #include -#include -#include + +void *zalloc(size_t size); +void *zrealloc(void *ptr, size_t size); +void *mallocForNCPReply(size_t size); +void freeForNCPReply(void *ptr); #define CHECK(expr) \ do { \ @@ -12,39 +17,67 @@ } \ } while (0) +static void raw_memset(unsigned char *dst, unsigned char value, size_t count) +{ + size_t i; + for (i = 0; i < count; ++i) { + dst[i] = value; + } +} + +static int is_zero_range(const unsigned char *p, size_t start, size_t end) +{ + size_t i; + for (i = start; i < end; ++i) { + if (p[i] != 0) + return 0; + } + return 1; +} + int main(void) { unsigned char *p = LB_zalloc(32); unsigned char *m; unsigned char *stack; + unsigned char *reply; + void *page; size_t i; + size_t total_before = PageInst.pgTotal; CHECK(p != NULL); - for (i = 0; i < 32; ++i) { - CHECK(p[i] == 0); - } + CHECK(is_zero_range(p, 0, 32)); - memset(p, 0x5a, 32); - p = LB_zrealloc(p, 64); + raw_memset(p, 0x5a, 32); + p = LB_zrealloc(p, 96); CHECK(p != NULL); for (i = 0; i < 32; ++i) { CHECK(p[i] == 0x5a); } - - free(p); + CHECK(is_zero_range(p, 32, 96)); + LB_free(p); p = zalloc(8); CHECK(p != NULL); - for (i = 0; i < 8; ++i) { - CHECK(p[i] == 0); - } - p = zrealloc(p, 16); + CHECK(is_zero_range(p, 0, 8)); + raw_memset(p, 0x33, 8); + p = zrealloc(p, 24); CHECK(p != NULL); - free(p); + for (i = 0; i < 8; ++i) { + CHECK(p[i] == 0x33); + } + CHECK(is_zero_range(p, 8, 24)); + LB_free(p); + + CHECK(LB_zrealloc(NULL, 16) != NULL); + p = LB_zrealloc(NULL, 16); + CHECK(p != NULL); + CHECK(is_zero_range(p, 0, 16)); + CHECK(LB_zrealloc(p, 0) == NULL); m = LB_malloc(16); CHECK(m != NULL); - memset(m, 0x33, 16); + raw_memset(m, 0x33, 16); m = LB_realloc(m, 32); CHECK(m != NULL); for (i = 0; i < 16; ++i) { @@ -54,14 +87,44 @@ int main(void) m = LB_mallocWithFlags(12, ZERO_FILL); CHECK(m != NULL); - for (i = 0; i < 12; ++i) { - CHECK(m[i] == 0); - } + CHECK(is_zero_range(m, 0, 12)); LB_free(m); + reply = LB_mallocForNCPReply(20); + CHECK(reply != NULL); + raw_memset(reply, 0x44, 20); + LB_freeForNCPReply(reply); + + reply = mallocForNCPReply(20); + CHECK(reply != NULL); + raw_memset(reply, 0x55, 20); + freeForNCPReply(reply); + stack = LB_StackAllocate(24); CHECK(stack != NULL); - memset(stack, 0x7e, 24); + raw_memset(stack, 0x7e, 24); LB_StackFree(stack, 24); + + page = LB_mallocPage(1); + CHECK(page != NULL); + CHECK(((ADDR)page & (PAGE_SIZE - 1)) == 0); + LB_freePage(page, 1); + + page = LB_mallocPageWithFlags(1, ZERO_FILL); + CHECK(page != NULL); + CHECK(((ADDR)page & (PAGE_SIZE - 1)) == 0); + CHECK(is_zero_range((const unsigned char *)page, 0, PAGE_SIZE)); + LB_freePage(page, 1); + + page = LB_zallocPage(1); + CHECK(page != NULL); + CHECK(((ADDR)page & (PAGE_SIZE - 1)) == 0); + CHECK(is_zero_range((const unsigned char *)page, 0, PAGE_SIZE)); + LB_freePage(page, 1); + + CHECK(PageInst.pgTotal == total_before); + + LB_exitMyselfAndReturn(NULL, 0); + LB_exitMyselfAndWait(NULL, 0); return 0; }