Skip to content
Permalink
Browse files

8548 want memset_s(3C)

Reviewed by: Robert Mustacchi <rm@joyent.com>
Approved by: Dan McDonald <danmcd@joyent.com>
  • Loading branch information...
yuripv authored and danmcd committed Aug 7, 2018
1 parent 3b5ccf6 commit 00efb5e1437d3912284689f1b0c13190e634e788
@@ -89,6 +89,19 @@ typedef _MAX_ALIGNMENT_TYPE max_align_t;
#endif /* !_STRICT_SYMBOLS || _STDC_C11 */
#endif /* _MAX_ALIGN_T */

#if __EXT1_VISIBLE
/* ISO/IEC 9899:2011 K.3.3.2 */
#ifndef _RSIZE_T_DEFINED
#define _RSIZE_T_DEFINED
typedef size_t rsize_t;
#endif
/* ISO/IEC 9899:2011 K.3.2.2 */
#ifndef _ERRNO_T_DEFINED
#define _ERRNO_T_DEFINED
typedef int errno_t;
#endif
#endif /* __EXT1_VISIBLE */

#ifdef __cplusplus
}
#endif
@@ -65,6 +65,26 @@ extern int at_quick_exit(void (*)(void));
extern _NORETURN_KYWD void quick_exit(int);
#endif /* !_STRICT_SYMBOLS || _STDC_C11 || __cplusplus >= 201103L */

#if __EXT1_VISIBLE

#ifndef _ERRNO_T_DEFINED
#define _ERRNO_T_DEFINED
typedef int errno_t;
#endif

/* K.3.6 */
typedef void (*constraint_handler_t)(const char *_RESTRICT_KYWD,
void *_RESTRICT_KYWD, errno_t);
/* K.3.6.1.1 */
extern constraint_handler_t set_constraint_handler_s(constraint_handler_t);
/* K.3.6.1.2 */
extern _NORETURN_KYWD void abort_handler_s(const char *_RESTRICT_KYWD,
void *_RESTRICT_KYWD, errno_t);
/* K3.6.1.3 */
extern void ignore_handler_s(const char *_RESTRICT_KYWD, void *_RESTRICT_KYWD,
errno_t);
#endif /* __EXT1_VISIBLE */

#if __cplusplus >= 199711L
}
#endif
@@ -143,6 +143,22 @@ extern char *strrchr(const char *, int);
extern char *strstr(const char *, const char *);
#endif /* __cplusplus >= 199711L */

#if __EXT1_VISIBLE

#ifndef _RSIZE_T_DEFINED
#define _RSIZE_T_DEFINED
typedef size_t rsize_t;
#endif

#ifndef _ERRNO_T_DEFINED
#define _ERRNO_T_DEFINED
typedef int errno_t;
#endif

/* ISO/IEC 9899:2011 K.3.7.4.1.1 */
extern errno_t memset_s(void *, rsize_t, int, rsize_t);
#endif /* __EXT1_VISIBLE */

#if __cplusplus >= 199711L
}
#endif /* end of namespace std */
@@ -35,4 +35,11 @@

#include <sys/stdint.h>

#if __EXT1_VISIBLE
/* ISO/IEC 9899:2011 K.3.4.4 */
#ifndef RSIZE_MAX
#define RSIZE_MAX (SIZE_MAX >> 1)
#endif
#endif /* __EXT1_VISIBLE */

#endif /* _STDINT_H */
@@ -467,6 +467,7 @@ PORTGEN= \
malloc.o \
memalign.o \
memmem.o \
memset_s.o \
mkdev.o \
mkdtemp.o \
mkfifo.o \
@@ -524,6 +525,7 @@ PORTGEN= \
scandir.o \
seekdir.o \
select.o \
set_constraint_handler_s.o \
setlabel.o \
setpriority.o \
settimeofday.o \
@@ -504,6 +504,7 @@ PORTGEN= \
malloc.o \
memalign.o \
memmem.o \
memset_s.o \
mkdev.o \
mkdtemp.o \
mkfifo.o \
@@ -561,6 +562,7 @@ PORTGEN= \
scandir.o \
seekdir.o \
select.o \
set_constraint_handler_s.o \
setlabel.o \
setpriority.o \
settimeofday.o \
@@ -316,6 +316,16 @@ extern wint_t __ungetwc_xpg5(wint_t, FILE *);
*/
extern char *current_locale(locale_t, int);

/*
* defined in set_constraint_handler_s.c.
*/
extern void __throw_constraint_handler_s(const char *_RESTRICT_KYWD, int);

/*
* defined in assfail.c.
*/
extern void common_panic(const char *, const char *);

#ifdef __cplusplus
}
#endif
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2017 Juniper Networks. All rights reserved.
*
* 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 REGENTS 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 REGENTS 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.
*/

/*
* Copyright 2018 Nexenta Systems, Inc.
*/

#include "lint.h"

#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include "libc.h"

/* __memset_vp is a volatile pointer to memset() function */
static void *(*const volatile __memset_vp)(void *, int, size_t) = (memset);

/* ISO/IEC 9899:2011 K.3.7.4.1 */
errno_t
memset_s(void *s, rsize_t smax, int c, rsize_t n)
{
errno_t ret;
rsize_t lim;
unsigned char v;

ret = EINVAL;
lim = n < smax ? n : smax;
v = (unsigned char)c;
if (s == NULL) {
__throw_constraint_handler_s("memset_s: s is NULL", ret);
} else if (smax > RSIZE_MAX) {
__throw_constraint_handler_s("memset_s: smax > RSIZE_MAX",
ret);
} else {
/*
* Both s and smax are valid, fill the memory buffer using
* memset() called through a volatile pointer to guarantee
* it will not be optimized away.
*/
(*__memset_vp)(s, v, lim);

if (n > RSIZE_MAX) {
__throw_constraint_handler_s("memset_s: n > RSIZE_MAX",
ret);
} else if (n > smax) {
__throw_constraint_handler_s("memset_s: n > smax", ret);
} else {
ret = 0;
}
}
return (ret);
}
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2017 Juniper Networks. All rights reserved.
*
* 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 REGENTS 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 REGENTS 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.
*/

/*
* Copyright 2018 Nexenta Systems, Inc.
*/

#include "lint.h"

#include <sys/types.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <synch.h>
#include <thread.h>
#include <unistd.h>

#include "libc.h"

/*
* Rationale recommends allocating new memory each time.
*/
static constraint_handler_t *_ch = NULL;
static mutex_t ch_lock = ERRORCHECKMUTEX;

constraint_handler_t
set_constraint_handler_s(constraint_handler_t handler)
{
constraint_handler_t *new, *old, ret;

new = malloc(sizeof (constraint_handler_t));
if (new == NULL)
return (NULL);
*new = handler;
mutex_enter(&ch_lock);
old = _ch;
_ch = new;
mutex_exit(&ch_lock);
if (old == NULL) {
ret = NULL;
} else {
ret = *old;
free(old);
}
return (ret);
}

/*ARGSUSED*/
void
abort_handler_s(const char *_RESTRICT_KYWD msg,
void *_RESTRICT_KYWD ptr, errno_t error)
{
common_panic("abort_handler_s: ", msg);
}

/*ARGSUSED*/
void
ignore_handler_s(const char *_RESTRICT_KYWD msg,
void *_RESTRICT_KYWD ptr, errno_t error)
{
}

void
__throw_constraint_handler_s(const char *_RESTRICT_KYWD msg, errno_t error)
{
constraint_handler_t ch;

mutex_enter(&ch_lock);
ch = (_ch != NULL) ? *_ch : NULL;
mutex_exit(&ch_lock);
if (ch != NULL) {
ch(msg, NULL, error);
} else {
/*
* If current handler is NULL (there were no calls to
* set_constraint_handler_s(), or it was called with NULL
* pointer handler argument), call default constraint handler
* per K.3.6.1.1 points 4 and 5.
*
* This implementation defines abort_handler_s() as default.
*/
abort_handler_s(msg, NULL, error);
}
}
@@ -77,6 +77,14 @@ $if _x86 && _ELF64
$add amd64
$endif

SYMBOL_VERSION ILLUMOS_0.27 { # memset_s(3C) and set_constraint_handler_s(3C)
protected:
abort_handler_s;
ignore_handler_s;
memset_s;
set_constraint_handler_s;
} ILLUMOS_0.26;

SYMBOL_VERSION ILLUMOS_0.26 { # fts(3) LFS
$if lf64
protected:
@@ -95,7 +95,7 @@ Abort(const char *msg)
* Write a panic message w/o grabbing any locks other than assert_lock.
* We have no idea what locks are held at this point.
*/
static void
void
common_panic(const char *head, const char *why)
{
char msg[400]; /* no panic() message in the library is this long */
@@ -530,6 +530,7 @@ PORTGEN= \
malloc.o \
memalign.o \
memmem.o \
memset_s.o \
mkdev.o \
mkdtemp.o \
mkfifo.o \
@@ -587,6 +588,7 @@ PORTGEN= \
scandir.o \
seekdir.o \
select.o \
set_constraint_handler_s.o \
setlabel.o \
setpriority.o \
settimeofday.o \
@@ -487,6 +487,7 @@ PORTGEN= \
malloc.o \
memalign.o \
memmem.o \
memset_s.o \
mkdev.o \
mkdtemp.o \
mkfifo.o \
@@ -544,6 +545,7 @@ PORTGEN= \
scandir.o \
seekdir.o \
select.o \
set_constraint_handler_s.o \
setlabel.o \
setpriority.o \
settimeofday.o \

0 comments on commit 00efb5e

Please sign in to comment.
You can’t perform that action at this time.