(mksh) tighten 32-bit requirements; (lksh) switch to long; allow any bitness

This commit is contained in:
tg 2012-06-28 20:17:39 +00:00
parent ae26f45417
commit c1f821d4e5
6 changed files with 158 additions and 28 deletions

View File

@ -1,5 +1,5 @@
#!/bin/sh
srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.576 2012/06/26 19:33:29 tg Exp $'
srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.577 2012/06/28 20:17:33 tg Exp $'
#-
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012
@ -409,7 +409,7 @@ SRCS="$SRCS lex.c main.c misc.c shf.c syn.c tree.c var.c"
if test $legacy = 0; then
SRCS="$SRCS edit.c"
check_categories="$check_categories shell:legacy-no"
check_categories="$check_categories shell:legacy-no int:32"
else
check_categories="$check_categories shell:legacy-yes"
add_cppflags -DMKSH_LEGACY_MODE
@ -1485,7 +1485,7 @@ else
#define EXTERN
#define MKSH_INCLUDES_ONLY
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/Build.sh,v 1.576 2012/06/26 19:33:29 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/Build.sh,v 1.577 2012/06/28 20:17:33 tg Exp $");
int main(void) { printf("Hello, World!\n"); return (0); }
EOF
case $cm in
@ -1752,11 +1752,15 @@ test x1 = x$HAVE_CAN_WNOOVERFLOW && CFLAGS="$CFLAGS -Wno-overflow"
ac_testn compile_time_asserts_$$ '' 'whether compile-time assertions pass' <<-'EOF'
#define MKSH_INCLUDES_ONLY
#include "sh.h"
#ifndef CHAR_BIT
#define CHAR_BIT 8 /* defuse this test on really legacy systems */
#endif
struct ctasserts {
#define cta(name, assertion) char name[(assertion) ? 1 : -1]
/* this one should be defined by the standard */
cta(char_is_1_char, (sizeof(char) == 1) && (sizeof(signed char) == 1) &&
(sizeof(unsigned char) == 1));
cta(char_is_8_bits, (CHAR_BIT) == 8);
/* the next assertion is probably not really needed */
cta(short_is_2_char, sizeof(short) == 2);
cta(short_size_no_matter_of_signedness, sizeof(short) == sizeof(unsigned short));
@ -1765,24 +1769,28 @@ cta(int_is_4_char, sizeof(int) == 4);
cta(int_size_no_matter_of_signedness, sizeof(int) == sizeof(unsigned int));
cta(long_ge_int, sizeof(long) >= sizeof(int));
cta(long_size_no_matter_of_signedness, sizeof(long) == sizeof(unsigned long));
#ifndef MKSH_LEGACY_MODE
/* the next assertion is probably not really needed */
cta(ari_is_4_char, sizeof(mksh_ari_t) == 4);
/* but the next three are; we REQUIRE signed integer wraparound */
cta(ari_is_signed, (mksh_ari_t)-1 < (mksh_ari_t)0);
/* but the next two are; we REQUIRE signed integer wraparound */
cta(ari_has_31_bit, 0 < (mksh_ari_t)(((((mksh_ari_t)1 << 15) << 15) - 1) * 2 + 1));
cta(ari_sign_32_bit_and_wrap,
(mksh_ari_t)(((((mksh_ari_t)1 << 15) << 15) - 1) * 2 + 1) >
(mksh_ari_t)(((((mksh_ari_t)1 << 15) << 15) - 1) * 2 + 2));
/* the next assertion is probably not really needed */
cta(uari_is_4_char, sizeof(mksh_uari_t) == 4);
/* but the next four are; we REQUIRE unsigned integer wraparound */
cta(uari_is_unsigned, (mksh_uari_t)-1 > (mksh_uari_t)0);
/* but the next three are; we REQUIRE unsigned integer wraparound */
cta(uari_has_31_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 2 + 1));
cta(uari_has_32_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3));
cta(uari_wrap_32_bit,
(mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3) >
(mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 4));
#endif
/* these are always required */
cta(ari_is_signed, (mksh_ari_t)-1 < (mksh_ari_t)0);
cta(uari_is_unsigned, (mksh_uari_t)-1 > (mksh_uari_t)0);
cta(sizet_size_no_matter_of_signedness, sizeof(ssize_t) == sizeof(size_t));
cta(ptrdifft_sizet_same_size, sizeof(ptrdiff_t) == sizeof(size_t));
@ -1796,6 +1804,45 @@ EOF
CFLAGS=$save_CFLAGS
eval test 1 = \$HAVE_COMPILE_TIME_ASSERTS_$$ || exit 1
#
# extra checks for legacy mksh
#
if test $legacy = 1; then
ac_test long_32bit '' 'whether long is 32 bit wide' <<-'EOF'
#define MKSH_INCLUDES_ONLY
#include "sh.h"
#ifndef CHAR_BIT
#define CHAR_BIT 0
#endif
struct ctasserts {
#define cta(name, assertion) char name[(assertion) ? 1 : -1]
cta(char_is_8_bits, (CHAR_BIT) == 8);
cta(long_is_32_bits, sizeof(long) == 4);
};
int main(void) { return (sizeof(struct ctasserts)); }
EOF
ac_test long_64bit '!' long_32bit 0 'whether long is 64 bit wide' <<-'EOF'
#define MKSH_INCLUDES_ONLY
#include "sh.h"
#ifndef CHAR_BIT
#define CHAR_BIT 0
#endif
struct ctasserts {
#define cta(name, assertion) char name[(assertion) ? 1 : -1]
cta(char_is_8_bits, (CHAR_BIT) == 8);
cta(long_is_64_bits, sizeof(long) == 8);
};
int main(void) { return (sizeof(struct ctasserts)); }
EOF
case $HAVE_LONG_32BIT$HAVE_LONG_64BIT in
10) check_categories="$check_categories int:32" ;;
01) check_categories="$check_categories int:64" ;;
*) check_categories="$check_categories int:u" ;;
esac
fi
#
# runtime checks
# once this is more than one, check if we can do runtime
@ -1806,6 +1853,13 @@ $e "${bi}run-time checks follow$ao, please ignore any weird errors"
if ac_testnnd silent_idivwrapv '' '(run-time) whether signed integer division overflows wrap silently' <<-'EOF'
#define MKSH_INCLUDES_ONLY
#include "sh.h"
#if !defined(MKSH_LEGACY_MODE) || HAVE_LONG_32BIT
#define IDIVWRAPV_VL (mksh_uari_t)0x80000000UL
#elif HAVE_LONG_64BIT
#define IDIVWRAPV_VL (mksh_uari_t)0x8000000000000000UL
#else
# error "cannot check this"
#endif
#ifdef SIGFPE
static void fpe_catcher(int) MKSH_A_NORETURN;
#endif
@ -1815,7 +1869,7 @@ if ac_testnnd silent_idivwrapv '' '(run-time) whether signed integer division ov
#ifdef SIGFPE
signal(SIGFPE, fpe_catcher);
#endif
o1 = ((mksh_ari_t)1 << 31);
o1 = (mksh_ari_t)IDIVWRAPV_VL;
o2 = -ac;
r1 = o1 / o2;
r2 = o1 % o2;
@ -2031,10 +2085,12 @@ cat >test.sh <<-EOF
break
fi
done
(( Sflag )) || echo + \$perli "\${args[@]}" -s "\$sflag" "\$@"
(( Sflag )) || exec \$perli "\${args[@]}" -s "\$sflag" "\$@"$tsts
# use of the -S option for check.t split into multiple chunks
rv=0
for s in "\$sflag".*; do
echo + \$perli "\${args[@]}" -s "\$s" "\$@"
\$perli "\${args[@]}" -s "\$s" "\$@"$tsts
rc=\$?
(( rv = rv ? rv : rc ))

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/Makefile,v 1.99 2012/05/04 22:44:31 tg Exp $
# $MirOS: src/bin/mksh/Makefile,v 1.100 2012/06/28 20:17:34 tg Exp $
#-
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012
@ -78,7 +78,8 @@ regress: ${PROG} check.pl check.t
mkdir -p regress-dir
echo export FNORD=666 >regress-dir/.mkshrc
HOME=$$(realpath regress-dir) perl ${.CURDIR}/check.pl \
-s ${.CURDIR}/check.t -v -p ./${PROG} -C fastbox,nodeprecated
-s ${.CURDIR}/check.t -v -p ./${PROG} \
-C shell:legacy-no,int:32,nodeprecated,fastbox
test-build: .PHONY
-rm -rf build-dir

60
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.548 2012/06/28 20:14:15 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.549 2012/06/28 20:17:35 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -29,7 +29,7 @@
# http://www.freebsd.org/cgi/cvsweb.cgi/src/tools/regression/bin/test/regress.sh?rev=HEAD
expected-stdout:
@(#)MIRBSD KSH R40 2012/06/26
@(#)MIRBSD KSH R40 2012/06/28
description:
Check version of shell.
stdin:
@ -38,7 +38,7 @@ name: KSH_VERSION
category: shell:legacy-no
---
expected-stdout:
@(#)LEGACY KSH R40 2012/06/26
@(#)LEGACY KSH R40 2012/06/28
description:
Check version of legacy shell.
stdin:
@ -324,6 +324,7 @@ expected-stderr-pattern:
name: arith-div-intmin-by-minusone
description:
Check division overflow wraps around silently
category: int:32
stdin:
echo signed:$((-2147483648 / -1))r$((-2147483648 % -1)).
echo unsigned:$((# -2147483648 / -1))r$((# -2147483648 % -1)).
@ -331,6 +332,17 @@ expected-stdout:
signed:-2147483648r0.
unsigned:0r2147483648.
---
name: arith-div-intmin-by-minusone-64
description:
Check division overflow wraps around silently
category: int:64
stdin:
echo signed:$((-9223372036854775808 / -1))r$((-9223372036854775808 % -1)).
echo unsigned:$((# -9223372036854775808 / -1))r$((# -9223372036854775808 % -1)).
expected-stdout:
signed:-9223372036854775808r0.
unsigned:0r9223372036854775808.
---
name: arith-assop-assoc-1
description:
Check associativity of assignment-operator operator
@ -345,6 +357,7 @@ expected-stdout:
name: arith-unsigned-1
description:
Check if unsigned arithmetics work
category: int:32
stdin:
# signed vs unsigned
echo x1 $((-1)) $((#-1))
@ -382,6 +395,7 @@ expected-stdout:
name: arith-limit32-1
description:
Check if arithmetics are 32 bit
category: int:32
stdin:
# signed vs unsigned
echo x1 $((-1)) $((#-1))
@ -406,6 +420,34 @@ expected-stdout:
x5 0 0
x6 1 1
---
name: arith-limit64-1
description:
Check if arithmetics are 64 bit
category: int:64
stdin:
# signed vs unsigned
echo x1 $((-1)) $((#-1))
# calculating
typeset -i vs
typeset -Ui vu
vs=9223372036854775807; vu=9223372036854775807
echo x2 $vs $vu
let vs++ vu++
echo x3 $vs $vu
vs=18446744073709551615; vu=18446744073709551615
echo x4 $vs $vu
let vs++ vu++
echo x5 $vs $vu
let vs++ vu++
echo x6 $vs $vu
expected-stdout:
x1 -1 18446744073709551615
x2 9223372036854775807 9223372036854775807
x3 -9223372036854775808 9223372036854775808
x4 -1 18446744073709551615
x5 0 0
x6 1 1
---
name: bksl-nl-ign-1
description:
Check that \newline is not collapsed after #
@ -3790,6 +3832,7 @@ expected-stderr-pattern:
name: integer-arithmetic-span
description:
Check wraparound and size that is defined in mksh
category: int:32
stdin:
echo s:$((2147483647+1)).$(((2147483647*2)+1)).$(((2147483647*2)+2)).
echo u:$((#2147483647+1)).$((#(2147483647*2)+1)).$((#(2147483647*2)+2)).
@ -3797,6 +3840,17 @@ expected-stdout:
s:-2147483648.-1.0.
u:2147483648.4294967295.0.
---
name: integer-arithmetic-span-64
description:
Check wraparound and size that is defined in mksh
category: int:64
stdin:
echo s:$((9223372036854775807+1)).$(((9223372036854775807*2)+1)).$(((9223372036854775807*2)+2)).
echo u:$((#9223372036854775807+1)).$((#(9223372036854775807*2)+1)).$((#(9223372036854775807*2)+2)).
expected-stdout:
s:-9223372036854775808.-1.0.
u:9223372036854775808.18446744073709551615.0.
---
name: lineno-stdin
description:
See if $LINENO is updated and can be modified.

26
expr.c
View File

@ -23,7 +23,21 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.57 2012/06/28 20:02:27 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.58 2012/06/28 20:17:36 tg Exp $");
#if !HAVE_SILENT_IDIVWRAPV
#if !defined(MKSH_LEGACY_MODE) || HAVE_LONG_32BIT
#define IDIVWRAPV_VL (mksh_uari_t)0x80000000UL
#define IDIVWRAPV_VR (mksh_uari_t)0xFFFFFFFFUL
#elif HAVE_LONG_64BIT
#define IDIVWRAPV_VL (mksh_uari_t)0x8000000000000000UL
#define IDIVWRAPV_VR (mksh_uari_t)0xFFFFFFFFFFFFFFFFUL
#else
# warning "cannot guarantee integer division wraparound"
#undef HAVE_SILENT_IDIVWRAPV
#define HAVE_SILENT_IDIVWRAPV 1
#endif
#endif
/* The order of these enums is constrained by the order of opinfo[] */
enum token {
@ -374,11 +388,11 @@ evalexpr(Expr_state *es, int prec)
* several compilers bitch around otherwise
*/
if (!es->natural &&
vl->val.u == (mksh_uari_t)0x80000000UL &&
vr->val.u == (mksh_uari_t)0xFFFFFFFFUL) {
vl->val.u == IDIVWRAPV_VL &&
vr->val.u == IDIVWRAPV_VR) {
/* -2147483648 / -1 = 2147483648 */
/* this ^ is really (1 << 31) though */
res = (mksh_ari_t)(mksh_uari_t)0x80000000UL;
res = (mksh_ari_t)IDIVWRAPV_VL;
} else
#endif
res = bivui(vl, /, vr);
@ -388,8 +402,8 @@ evalexpr(Expr_state *es, int prec)
#if !HAVE_SILENT_IDIVWRAPV
/* see O_DIV / O_DIVASN for the reason behind this */
if (!es->natural &&
vl->val.u == (mksh_uari_t)0x80000000UL &&
vr->val.u == (mksh_uari_t)0xFFFFFFFFUL) {
vl->val.u == IDIVWRAPV_VL &&
vr->val.u == IDIVWRAPV_VR) {
/* -2147483648 % -1 = 0 */
res = 0;
} else

21
sh.h
View File

@ -157,9 +157,9 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.567 2012/06/28 20:05:11 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.568 2012/06/28 20:17:37 tg Exp $");
#endif
#define MKSH_VERSION "R40 2012/06/26"
#define MKSH_VERSION "R40 2012/06/28"
/* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES
@ -172,16 +172,21 @@ typedef u_int32_t uint32_t;
#endif
/* arithmetic types: shell arithmetics */
#ifdef MKSH_LEGACY_MODE
/*
* NOTE: these are currently hard-coded to exactly 32 bit, do not change
*
* TODO: make these configurable, or add 64-bit arithmetic, somehow
* (on some operating environments, 64-bit types are needed for some
* things, such as the ulimit builtin, to work proper), except mksh
* must still be able to run on systems with no native 64-bit integers
* POSIX demands these to be the C environment's long type
*/
typedef long mksh_ari_t;
typedef unsigned long mksh_uari_t;
#else
/*
* These types are exactly 32 bit wide; signed and unsigned
* integer wraparound, even across division and modulo, for
* any shell code using them, is guaranteed.
*/
typedef int32_t mksh_ari_t;
typedef uint32_t mksh_uari_t;
#endif
/* boolean type (no <stdbool.h> deliberately) */
typedef unsigned char mksh_bool;

2
tree.c
View File

@ -23,7 +23,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.60 2012/06/28 20:01:01 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.61 2012/06/28 20:17:39 tg Exp $");
#define INDENT 8