1*f65a228fSRobert Mustacchi# 2*f65a228fSRobert Mustacchi# This file and its contents are supplied under the terms of the 3*f65a228fSRobert Mustacchi# Common Development and Distribution License ("CDDL"), version 1.0. 4*f65a228fSRobert Mustacchi# You may only use this file in accordance with the terms of version 5*f65a228fSRobert Mustacchi# 1.0 of the CDDL. 6*f65a228fSRobert Mustacchi# 7*f65a228fSRobert Mustacchi# A full copy of the text of the CDDL should have accompanied this 8*f65a228fSRobert Mustacchi# source. A copy of the CDDL is also available via the Internet at 9*f65a228fSRobert Mustacchi# http://www.illumos.org/license/CDDL. 10*f65a228fSRobert Mustacchi# 11*f65a228fSRobert Mustacchi 12*f65a228fSRobert Mustacchi# 13*f65a228fSRobert Mustacchi# Copyright 2020 Robert Mustacchi 14*f65a228fSRobert Mustacchi# 15*f65a228fSRobert Mustacchi _ _ _ 16*f65a228fSRobert Mustacchi ___| |_ __| (_) ___ 17*f65a228fSRobert Mustacchi/ __| __/ _` | |/ _ \ 18*f65a228fSRobert Mustacchi\__ \ || (_| | | (_) | 19*f65a228fSRobert Mustacchi|___/\__\__,_|_|\___/ 20*f65a228fSRobert Mustacchi 21*f65a228fSRobert MustacchiNotes on the design of stdio. 22*f65a228fSRobert Mustacchi 23*f65a228fSRobert Mustacchi------------ 24*f65a228fSRobert MustacchiFile Streams 25*f65a228fSRobert Mustacchi------------ 26*f65a228fSRobert Mustacchi 27*f65a228fSRobert MustacchiAt the heart of the stdio is the 'FILE *'. The 'FILE *' represents a 28*f65a228fSRobert Mustacchistream that can be read, written, and seeked. The streams traditionally 29*f65a228fSRobert Mustacchirefer to a file descriptor, when created by fopen(3C), or may refer to 30*f65a228fSRobert Mustacchimemory, when created by open_memstream(3C) or fmopen(3C). This document 31*f65a228fSRobert Mustacchifocuses on the implementation of streams. Other misc. functions in stdio 32*f65a228fSRobert Mustacchiare not discussed. 33*f65a228fSRobert Mustacchi 34*f65a228fSRobert Mustacchi------------ 35*f65a228fSRobert MustacchiOrganization 36*f65a228fSRobert Mustacchi------------ 37*f65a228fSRobert Mustacchi 38*f65a228fSRobert MustacchiMost functions exist in a file with the same name. When adding new 39*f65a228fSRobert Mustacchifiles to stdio the file name should match the primary function name. 40*f65a228fSRobert MustacchiThere are a few exceptions. Almost all of the logic related to both 41*f65a228fSRobert Mustacchiflushing and knowledge of how to handle the 32-bit ABI issues (described 42*f65a228fSRobert Mustacchiin the next section) can be found in flush.c. 43*f65a228fSRobert Mustacchi 44*f65a228fSRobert Mustacchi----------------------------- 45*f65a228fSRobert Mustacchistruct __FILE_TAG and the ABI 46*f65a228fSRobert Mustacchi----------------------------- 47*f65a228fSRobert Mustacchi 48*f65a228fSRobert MustacchiThe definition of the 'FILE *' is a pointer to a 'struct __FILE_TAG'. 49*f65a228fSRobert MustacchiThe 'struct __FILE_TAG' structure has a long history that dates back to 50*f65a228fSRobert Mustacchihistorical UNIX. For better or for worse, we have inherited some of the 51*f65a228fSRobert Mustacchidesign decisions of the past, it's important to understand what those 52*f65a228fSRobert Mustacchiare as they have profound impact on the stdio design and serve as a good 53*f65a228fSRobert Mustacchicautionary tale for future ABI decisions. 54*f65a228fSRobert Mustacchi 55*f65a228fSRobert MustacchiIn the original UNIX designs, the 'struct __FILE_TAG' was exposed as a 56*f65a228fSRobert Mustacchinon-opaque structure. This was also true on other platforms. This had a 57*f65a228fSRobert Mustacchicouple of challenges: 58*f65a228fSRobert Mustacchi 59*f65a228fSRobert Mustacchi* It meant the size of the 'struct __FILE_TAG' was part of the ABI 60*f65a228fSRobert Mustacchi* Consumers would access the members directly. You can find examples of 61*f65a228fSRobert Mustacchi this in our public headers where things like getc() are inlined in 62*f65a228fSRobert Mustacchi terms of the implementation. Various 3rd-party software that has 63*f65a228fSRobert Mustacchi existed for quite some time knows the offset of members and directly 64*f65a228fSRobert Mustacchi manipulates them. This is still true as of 2020. 65*f65a228fSRobert Mustacchi* The 'struct __FILE_TAG' only used an unsigned char (uint8_t) for the 66*f65a228fSRobert Mustacchi file descriptor in the 32-bit version. Other systems used a short, so 67*f65a228fSRobert Mustacchi they were in better shape. This was changed in the 64-bit version to 68*f65a228fSRobert Mustacchi use an int. 69*f65a228fSRobert Mustacchi* The main C stdio symbols 'stdin', 'stdout', and 'stderr', were (and 70*f65a228fSRobert Mustacchi still are) exposed as an array. This means that while the 64-bit 71*f65a228fSRobert Mustacchi structure is opaque, its size is actually part of the ABI. 72*f65a228fSRobert Mustacchi 73*f65a228fSRobert MustacchiAll of these issues have been dealt with in different ways in the 74*f65a228fSRobert Mustacchisystem. The first thing that is a little confusing is where to find the 75*f65a228fSRobert Mustacchidefinitions of the actual implementation. The 32-bit 'struct __FILE_BUF' 76*f65a228fSRobert Mustacchiis split into two different pieces, the part that is public and a 77*f65a228fSRobert Mustacchisecondary, private part. 78*f65a228fSRobert Mustacchi 79*f65a228fSRobert MustacchiThe public definition of the 'struct __FILE_TAG' for 32-bit code and the 80*f65a228fSRobert Mustacchiopaque definition for 64-bit code may be found in 81*f65a228fSRobert Mustacchi'usr/src/head/stdio_impl.h.'. The actual definition of the 64-bit 82*f65a228fSRobert Mustacchistructure and the 32-bit additions are all found in 83*f65a228fSRobert Mustacchi'usr/src/lib/libc/inc/file64.h.' 84*f65a228fSRobert Mustacchi 85*f65a228fSRobert MustacchiIn file64.h, one will find the 'struct xFILEdata' (extended FILE * data). 86*f65a228fSRobert MustacchiThis represents all of the data that has been added to stdio that is 87*f65a228fSRobert Mustacchimissing from the public structure. Whenever a 'FILE *' is allocated, 88*f65a228fSRobert Mustacchi32-bit code always ensures that there is a corresponding 'struct 89*f65a228fSRobert MustacchixFILEdata' that exists. Currently, we still have plenty of padding left 90*f65a228fSRobert Mustacchiin the 64-bit version of the structure for at least 3 pointers. 91*f65a228fSRobert Mustacchi 92*f65a228fSRobert MustacchiTo add a member to the structure, one has to add data to the structures 93*f65a228fSRobert Mustacchiin 'lib/libc/inc/file64.h'. If for some reason, all the padding would be 94*f65a228fSRobert Mustacchiused up, then you must stop. The size of the 64-bit structure _cannot_ 95*f65a228fSRobert Mustacchibe extended, as noted earlier it is part of the ABI. If we hit this 96*f65a228fSRobert Mustacchicase, then one must introduce the struct xFILEdata for the lp64 97*f65a228fSRobert Mustacchienvironment. 98*f65a228fSRobert Mustacchi 99*f65a228fSRobert Mustacchi-------------------------- 100*f65a228fSRobert MustacchiAllocating FILE Structures 101*f65a228fSRobert Mustacchi-------------------------- 102*f65a228fSRobert Mustacchi 103*f65a228fSRobert Mustacchilibc defines a number of 'FILE *' structures by default. These can all 104*f65a228fSRobert Mustacchibe found in 'data.c'. The first _NFILE (20 or 60 depending on the 105*f65a228fSRobert Mustacchiplatform) are defined statically. In the 32-bit case, the corresponding 106*f65a228fSRobert Mustacchi'struct _xFILEdata' is allocated along with it. 107*f65a228fSRobert Mustacchi 108*f65a228fSRobert MustacchiTo determine if a structure is free or not in the array, the `_flag` 109*f65a228fSRobert Mustacchimember is consulted. If the flag has been set to zero, then the STREAM 110*f65a228fSRobert Mustacchiis considered free and can be allocated. All of the allocated (whether 111*f65a228fSRobert Mustacchiused or not) 'FILE *' structures are present on a linked list which is 112*f65a228fSRobert Mustacchifound in 'flush.c' rooted at the symbol '__first_link'. This list is 113*f65a228fSRobert Mustacchialways scanned to try and reuse an existing 'FILE *' structure before 114*f65a228fSRobert Mustacchiallocating a new one. If all of the existing ones are in use, then one 115*f65a228fSRobert Mustacchiwill be allocated. 116*f65a228fSRobert Mustacchi 117*f65a228fSRobert MustacchiAn important thing to understand is that once allocated, a 'FILE *' will 118*f65a228fSRobert Mustacchinever be freed by libc. It will always exist on the global list of 119*f65a228fSRobert Mustacchistructures to be reused. 120*f65a228fSRobert Mustacchi 121*f65a228fSRobert Mustacchi--------- 122*f65a228fSRobert MustacchiBuffering 123*f65a228fSRobert Mustacchi--------- 124*f65a228fSRobert Mustacchi 125*f65a228fSRobert MustacchiEvery stream in stdio starts out as buffered. Buffering can be changed 126*f65a228fSRobert Mustacchiby calling either setbuf(3C) or setvbuf(3C). This buffer is stored in 127*f65a228fSRobert Mustacchithe `_base` member of the 'struct __FILE_TAG'. The amount of valid data 128*f65a228fSRobert Mustacchiin the buffer is maintained in the '_cnt' member of the structure. By 129*f65a228fSRobert Mustacchidefault, there is no associated buffer with a stream. When the stream is 130*f65a228fSRobert Mustacchifirst used, the buffer will be assigned by a call to _findbuf() in 131*f65a228fSRobert Mustacchi_findbuf.c. 132*f65a228fSRobert Mustacchi 133*f65a228fSRobert MustacchiThere are pre-allocated buffers that exist. There are two specifically 134*f65a228fSRobert Mustacchifor stdin and stdout (stderr is unbuffered). These include space for 135*f65a228fSRobert Mustacchiboth the buffer and the pushback buffer. The pushback buffer is used so 136*f65a228fSRobert Mustacchisomeone can call fungetc(3C) regardless of whether a buffering mode is 137*f65a228fSRobert Mustacchienabled or not. Characters that we 'unget' are placed on the pushback 138*f65a228fSRobert Mustacchibuffer. 139*f65a228fSRobert Mustacchi 140*f65a228fSRobert MustacchiFor other buffering modes, we'll try and allocate an appropriate sized 141*f65a228fSRobert Mustacchibuffer. The buffer size defaults to BUFSIZ, but if the stream is backed 142*f65a228fSRobert Mustacchiby a file descriptor, we'll use fstat() to determine the appropriate 143*f65a228fSRobert Mustacchisize to use and match the file system block size. If we cannot allocate 144*f65a228fSRobert Mustacchithat, we'll fall back to trying to allocate a pushback buffer. 145*f65a228fSRobert Mustacchi 146*f65a228fSRobert Mustacchilibc defines static data for _NFILE worth of pushback buffers which are 147*f65a228fSRobert Mustacchiindexed based on the underlying file descriptor. This and the stdin and 148*f65a228fSRobert Mustacchistdout buffers are all found in 'data.c' in _smbuf, _sibuf, and _sobuf 149*f65a228fSRobert Mustacchirespectively. 150*f65a228fSRobert Mustacchi 151*f65a228fSRobert Mustacchi------------------------------ 152*f65a228fSRobert MustacchiReading, Writing, and Flushing 153*f65a228fSRobert Mustacchi------------------------------ 154*f65a228fSRobert Mustacchi 155*f65a228fSRobert MustacchiBy default, reads and writes on a stream, whether backed by a 156*f65a228fSRobert Mustacchifile-descriptor or not, go through the buffer described in the previous 157*f65a228fSRobert Mustacchisection. If a read or write can be satisfied by the buffer, then no 158*f65a228fSRobert Mustacchiunderlying I/O will occur, unless buffering has been disabled. 159*f65a228fSRobert Mustacchi 160*f65a228fSRobert MustacchiThe various function entry points that read such as fread(3C) or 161*f65a228fSRobert Mustacchifgetc(3C) will not call read() directly but will instead try to fill the 162*f65a228fSRobert Mustacchibuffer, which will cause a read if required. This is centralized in 163*f65a228fSRobert Mustacchi_filbuf(). When a read is required from the underlying file, it will 164*f65a228fSRobert Mustacchicall _xread() in flush.c. For more on _xread() see the operations vector 165*f65a228fSRobert Mustacchisection further along. 166*f65a228fSRobert Mustacchi 167*f65a228fSRobert MustacchiUnlike reads, writes are much less centralized and each of the main 168*f65a228fSRobert Mustacchiwriting entry points has reimplemented the path of writing to the buffer 169*f65a228fSRobert Mustacchiand flushing it. It would be good in the future to consolidate them. In 170*f65a228fSRobert Mustacchigeneral, data will be written directly to the stdio buffer. When that 171*f65a228fSRobert Mustacchibuffer needs to be flushed either the _flsbuf() or _xflsbuf() functions 172*f65a228fSRobert Mustacchiwill be called to actually flush out the buffer. 173*f65a228fSRobert Mustacchi 174*f65a228fSRobert MustacchiWhen data needs to be flushed from a buffer to its underlying file 175*f65a228fSRobert Mustacchidescriptor (or other backing store), all of the write family functions 176*f65a228fSRobert Mustacchiultimately call _xwrite(). 177*f65a228fSRobert Mustacchi 178*f65a228fSRobert MustacchiFlushes can occur in a few different ways: 179*f65a228fSRobert Mustacchi 180*f65a228fSRobert Mustacchi1. A write has filled up the buffer. 181*f65a228fSRobert Mustacchi2. A new line ('\n') is written and new-line buffering is used. 182*f65a228fSRobert Mustacchi3. fflush(3C) or a similar function has been called. 183*f65a228fSRobert Mustacchi4. A read occurs on a buffer that has unflushed writes. 184*f65a228fSRobert Mustacchi5. The stream is being closed. 185*f65a228fSRobert Mustacchi 186*f65a228fSRobert MustacchiMost of these methods are fairly similar; however, the fflush(3C) case 187*f65a228fSRobert Mustacchiis a little different. fflush() may be asked to flush all of the streams 188*f65a228fSRobert Mustacchiwhen it is passed a NULL stream. Even when that happens it will still 189*f65a228fSRobert Mustacchiutilize the same underlying mechanism via _xflsbuf() or _flsbuf(). 190*f65a228fSRobert Mustacchi 191*f65a228fSRobert Mustacchi----------- 192*f65a228fSRobert MustacchiOrientation 193*f65a228fSRobert Mustacchi----------- 194*f65a228fSRobert Mustacchi 195*f65a228fSRobert MustacchiStreams handle both wide characters and narrow characters. There is an 196*f65a228fSRobert Mustacchiinternal multi-byte conversion state buffer that is included with every 197*f65a228fSRobert Mustacchistream. A stream may exist in one of three modes: 198*f65a228fSRobert Mustacchi 199*f65a228fSRobert Mustacchi1. It may have an explicit narrow orientation 200*f65a228fSRobert Mustacchi2. It may have an explicit wide orientation 201*f65a228fSRobert Mustacchi3. It may have no orientation 202*f65a228fSRobert Mustacchi 203*f65a228fSRobert MustacchiWhen most streams are created, they have no orientation. The orientation 204*f65a228fSRobert Mustacchican then be explicitly set by calling fwide(3C). Some streams are also 205*f65a228fSRobert Mustacchicreated with an explicit orientation, for example, open_wmemstream(3C) 206*f65a228fSRobert Mustacchialways sets the stream to be wide. 207*f65a228fSRobert Mustacchi 208*f65a228fSRobert MustacchiThe C standard dictates that certain operations will actually cause a 209*f65a228fSRobert Mustacchistream with no orientation to have an explicit orientation set. Calling 210*f65a228fSRobert Mustacchia narrow or wide related character function, such as 'fgetc(3C)' or 211*f65a228fSRobert Mustacchi'fgetwc(3C)' respectively will then cause the orientation to be set if 212*f65a228fSRobert Mustacchiit has not been. Once an orientation for a stream has been set, it 213*f65a228fSRobert Mustacchicannot be changed until the stream has been closed or it is reset by 214*f65a228fSRobert Mustacchicalling freopen(3C). 215*f65a228fSRobert Mustacchi 216*f65a228fSRobert MustacchiThere are a few functions that don't change this today. One example is 217*f65a228fSRobert Mustacchiungetc(3C). Often this isn't indicative of whether it should or 218*f65a228fSRobert Mustacchishouldn't change the orientation, but is a side effect of the history of 219*f65a228fSRobert Mustacchithe stdio implementation. 220*f65a228fSRobert Mustacchi 221*f65a228fSRobert Mustacchi------------------------------------- 222*f65a228fSRobert MustacchiOperations Vectors and Memory Streams 223*f65a228fSRobert Mustacchi------------------------------------- 224*f65a228fSRobert Mustacchi 225*f65a228fSRobert MustacchiTraditionally, stdio streams were always backed by a file descriptor of 226*f65a228fSRobert Mustacchisome kind and therefore always called out into functions like read(2), 227*f65a228fSRobert Mustacchiwrite(2), lseek(2), and close(2) directly. A series of new functions 228*f65a228fSRobert Mustacchiwere introduced in POSIX 2008 that add support for streams backed by 229*f65a228fSRobert Mustacchimemory in the form of fmemopen(3C), open_memstream(3C), and 230*f65a228fSRobert Mustacchiopen_wmemstream(3C). 231*f65a228fSRobert Mustacchi 232*f65a228fSRobert MustacchiTo deal with this and other possible designs, an operations vector was 233*f65a228fSRobert Mustacchiadded to the stream represented by the 'stdio_ops_t' structure. This is 234*f65a228fSRobert Mustacchistored in the '_ops' member of the 'struct __FILE_BUF'. For a normal 235*f65a228fSRobert Mustacchistream backed by a file descriptor, this member will be NULL. 236*f65a228fSRobert Mustacchi 237*f65a228fSRobert MustacchiIn places where a normal system call would have been made there is now a 238*f65a228fSRobert Mustacchicall to a corresponding function such as _xread(), _xwrite(), xseek(), 239*f65a228fSRobert Mustacchi_xseek64(), and _xclose(). If an operations vector is defined, it will 240*f65a228fSRobert Mustacchicall into the corresponding operation vector. If not, it will perform 241*f65a228fSRobert Mustacchithe traditional system call. This design choice consolidates all of the 242*f65a228fSRobert Mustacchiwork required to implement non-file descriptor backed streams. 243*f65a228fSRobert Mustacchi 244*f65a228fSRobert MustacchiWhen creating a non-file backed stream there are several expectations in 245*f65a228fSRobert Mustacchithe system: 246*f65a228fSRobert Mustacchi 247*f65a228fSRobert Mustacchi* The stream code should obtain a stream normally through a call to 248*f65a228fSRobert Mustacchi _findiop(). 249*f65a228fSRobert Mustacchi* If one needs to translate the normal fopen(3C) arguments, they should 250*f65a228fSRobert Mustacchi use the _stdio_flags() function. This will also construct the 251*f65a228fSRobert Mustacchi appropriate internal stdio flags for the stream. 252*f65a228fSRobert Mustacchi* The stream code must call _xassoc() to set the file operations vector 253*f65a228fSRobert Mustacchi before return a 'FILE *' out of libc. 254*f65a228fSRobert Mustacchi* All of the operations vectors must be implemented. 255*f65a228fSRobert Mustacchi* If the stream is seekable, it must explicitly use the SET_SEEKABLE() 256*f65a228fSRobert Mustacchi macro before return the stream. 257*f65a228fSRobert Mustacchi* If the stream is supposed to have a default orientation, it must set 258*f65a228fSRobert Mustacchi it by calling _setorientation(). Not all streams have a default 259*f65a228fSRobert Mustacchi orientation. 260*f65a228fSRobert Mustacchi* In the stream's close entry point it should call _xunassoc(). 261*f65a228fSRobert Mustacchi 262*f65a228fSRobert Mustacchi-------------------------- 263*f65a228fSRobert MustacchiExtended File and fileno() 264*f65a228fSRobert Mustacchi-------------------------- 265*f65a228fSRobert Mustacchi 266*f65a228fSRobert MustacchiThe 32-bit libc has historically been limited to 255 open streams 267*f65a228fSRobert Mustacchibecause of the use of an unsigned char. This problem does not impact the 268*f65a228fSRobert Mustacchi64-bit libc. To deal with this, libc uses a series of techniques which 269*f65a228fSRobert Mustacchiare summarized for users in extendedFILE(5). The usage of extendedFILE 270*f65a228fSRobert Mustacchican also be enabled by passing the special 'F' character to fopen(3C). 271*f65a228fSRobert Mustacchi 272*f65a228fSRobert MustacchiThe '_magic' member in the 32-bit 'struct __FILE_TAG' contains what used 273*f65a228fSRobert Mustacchito be the file descriptor. When extended file is not in use, the 274*f65a228fSRobert Mustacchi_magic member still does contain the file descriptor. However, when 275*f65a228fSRobert MustacchiextendedFILE is enabled, then the _magic member contains a sentinel 276*f65a228fSRobert Mustacchivalue and the actual value is stored in the 'struct xFILEdata' _magic 277*f65a228fSRobert Mustacchimember. 278*f65a228fSRobert Mustacchi 279*f65a228fSRobert MustacchiThe act of getting the correct file descriptor has been centralized in a 280*f65a228fSRobert Mustacchifunction called _get_fd(). This function knows how to handle the special 281*f65a228fSRobert Mustacchi32-bit case and the normal case. It also centralizes the logic of 282*f65a228fSRobert Mustacchichecking for a non-file backed stream. There are many cases in libc 283*f65a228fSRobert Mustacchiwhere we want to know the file descriptor to perform some operation; 284*f65a228fSRobert Mustacchihowever, non-file backed streams do not have a corresponding file 285*f65a228fSRobert Mustacchidescriptor. When such a stream is detected, we will explicitly return 286*f65a228fSRobert Mustacchi-1. This ensures that a bad file descriptor will be used if someone 287*f65a228fSRobert Mustacchimistakenly calls a system call. Functions like _fileno() call this 288*f65a228fSRobert Mustacchidirectly. 289*f65a228fSRobert Mustacchi 290*f65a228fSRobert Mustacchi------- 291*f65a228fSRobert MustacchiTesting 292*f65a228fSRobert Mustacchi------- 293*f65a228fSRobert Mustacchi 294*f65a228fSRobert MustacchiThere is a burgeoning test suite for stdio in 295*f65a228fSRobert Mustacchiusr/src/test/libc-tests/tests/stdio. If working in stdio (or libc more 296*f65a228fSRobert Mustacchigenerally) it is recommended that you run this test suite and add new 297*f65a228fSRobert Mustacchitests to it where appropriate. For most new functionality it is 298*f65a228fSRobert Mustacchiencouraged that you both import test suites that may already exist and 299*f65a228fSRobert Mustacchithat you also write your own test suites to properly cover a number of 300*f65a228fSRobert Mustacchierror and corner cases. 301*f65a228fSRobert Mustacchi 302*f65a228fSRobert MustacchiTests should also be written against libumem(3LIB), and umem debugging 303*f65a228fSRobert Mustacchishould be explicitly enabled in the program. Enabling umem debugging can 304*f65a228fSRobert Mustacchicatch a number of common memory usage errors. It also makes it easier to 305*f65a228fSRobert Mustacchitest for memory leaks by taking a core file and used the mdb 306*f65a228fSRobert Mustacchi'::findleaks' dcmd. A good starting point is to place the following in 307*f65a228fSRobert Mustacchithe program: 308*f65a228fSRobert Mustacchi 309*f65a228fSRobert Mustacchiconst char * 310*f65a228fSRobert Mustacchi_umem_debug_init(void) 311*f65a228fSRobert Mustacchi{ 312*f65a228fSRobert Mustacchi return ("default,verbose"); 313*f65a228fSRobert Mustacchi} 314*f65a228fSRobert Mustacchi 315*f65a228fSRobert Mustacchiconst char * 316*f65a228fSRobert Mustacchi_umem_logging_init(void) 317*f65a228fSRobert Mustacchi{ 318*f65a228fSRobert Mustacchi return ("fail,contents"); 319*f65a228fSRobert Mustacchi} 320*f65a228fSRobert Mustacchi 321*f65a228fSRobert MustacchiFor the definition of these flags, see umem_debug(3MALLOC). 322*f65a228fSRobert Mustacchi 323*f65a228fSRobert MustacchiIn addition, by leveraging umem debugging it becomes very easy to 324*f65a228fSRobert Mustacchisimulate malloc failure when required. This can be enabled by calling 325*f65a228fSRobert Mustacchiumem_setmtbf(1), which ensures that any subsequent memory requests 326*f65a228fSRobert Mustacchithrough malloc(), including those made indirectly by libc, will fail. To 327*f65a228fSRobert Mustacchirestore the behavior after a test, one can simply call umem_setmtbf(0). 328