Fix bugs and make code tinier

- Fixed bug where stdio eof wasn't being sticky
- Fixed bug where fseeko() wasn't clearing eof state
- Removed assert() usage from libc favoring _unassert() / _npassert()
This commit is contained in:
Justine Tunney 2022-10-09 22:38:28 -07:00
parent 9b7c8db846
commit d5910e2673
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
115 changed files with 510 additions and 290 deletions

View file

@ -15,14 +15,10 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "libc/assert.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/bswap.h"
#include "libc/log/backtrace.internal.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/intrin/bsf.h"
#include "libc/intrin/bswap.h"
#include "libc/macros.internal.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/runtime.h"

View file

@ -15,8 +15,6 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "libc/assert.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/bignum.h"

View file

@ -16,10 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/log/check.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/mem/gc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/bignum_internal.h"
@ -261,7 +258,7 @@ mbedtls_p256_add( uint64_t X[5],
ADC( X[3], A[3], B[3], c, X[4] );
#endif
mbedtls_p256_rum( X );
DCHECK_EQ( 0, X[4] );
MBEDTLS_ASSERT( 0 == X[4] );
}
static void
@ -298,7 +295,7 @@ mbedtls_p256_sub( uint64_t X[5],
#endif
while( (int64_t)X[4] < 0 )
mbedtls_p256_gro( X );
DCHECK_EQ( 0, X[4] );
MBEDTLS_ASSERT( 0 == X[4] );
}
static void
@ -322,7 +319,7 @@ mbedtls_p256_hub( uint64_t A[5],
: "rax", "rcx", "memory", "cc");
while( (int64_t)A[4] < 0 )
mbedtls_p256_gro( A );
DCHECK_EQ( 0, A[4] );
MBEDTLS_ASSERT( 0 == A[4] );
#else
mbedtls_p256_sub( A, A, B );
#endif
@ -368,9 +365,9 @@ int mbedtls_p256_double_jac( const mbedtls_ecp_group *G,
s.Xn = mbedtls_mpi_limbs( &P->X );
s.Yn = mbedtls_mpi_limbs( &P->Y );
s.Zn = mbedtls_mpi_limbs( &P->Z );
CHECK_LE( s.Xn, 4 );
CHECK_LE( s.Yn, 4 );
CHECK_LE( s.Zn, 4 );
MBEDTLS_ASSERT( s.Xn <= 4 );
MBEDTLS_ASSERT( s.Yn <= 4 );
MBEDTLS_ASSERT( s.Zn <= 4 );
memcpy( s.X, P->X.p, s.Xn * 8 );
memcpy( s.Y, P->Y.p, s.Yn * 8 );
memcpy( s.Z, P->Z.p, s.Zn * 8 );
@ -424,11 +421,11 @@ int mbedtls_p256_add_mixed( const mbedtls_ecp_group *G,
s.Zn = mbedtls_mpi_limbs( &P->Z );
s.QXn = mbedtls_mpi_limbs( &Q->X );
s.QYn = mbedtls_mpi_limbs( &Q->Y );
CHECK_LE( s.Xn, 4 );
CHECK_LE( s.Yn, 4 );
CHECK_LE( s.Zn, 4 );
CHECK_LE( s.QXn, 4 );
CHECK_LE( s.QYn, 4 );
MBEDTLS_ASSERT( s.Xn <= 4 );
MBEDTLS_ASSERT( s.Yn <= 4 );
MBEDTLS_ASSERT( s.Zn <= 4 );
MBEDTLS_ASSERT( s.QXn <= 4 );
MBEDTLS_ASSERT( s.QYn <= 4 );
memcpy( s.X, P->X.p, s.Xn * 8 );
memcpy( s.Y, P->Y.p, s.Yn * 8 );
memcpy( s.Z, P->Z.p, s.Zn * 8 );

View file

@ -16,11 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/log/check.h"
#include "libc/mem/gc.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/mem/gc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/bignum_internal.h"
@ -189,8 +187,8 @@ mbedtls_p384_mul( uint64_t X[12],
}
else
{
if( A == X ) A = gc( memcpy( malloc( 6 * 8 ), A, 6 * 8 ) );
if( B == X ) B = gc( memcpy( malloc( 6 * 8 ), B, 6 * 8 ) );
if( A == X ) A = _gc( memcpy( malloc( 6 * 8 ), A, 6 * 8 ) );
if( B == X ) B = _gc( memcpy( malloc( 6 * 8 ), B, 6 * 8 ) );
Mul( X, A, n, B, m );
mbedtls_platform_zeroize( X + n + m, (12 - n - m) * 8 );
}
@ -305,7 +303,7 @@ mbedtls_p384_add( uint64_t X[7],
ADC( X[5], A[5], B[5], c, X[6] );
#endif
mbedtls_p384_rum( X );
DCHECK_EQ(0, X[6]);
MBEDTLS_ASSERT(0 == X[6]);
}
static void
@ -350,7 +348,7 @@ mbedtls_p384_sub( uint64_t X[7],
#endif
while( (int64_t)X[6] < 0 )
mbedtls_p384_gro( X );
DCHECK_EQ(0, X[6]);
MBEDTLS_ASSERT(0 == X[6]);
}
static void
@ -378,7 +376,7 @@ mbedtls_p384_hub( uint64_t A[7],
: "rax", "rcx", "memory", "cc");
while( (int64_t)A[6] < 0 )
mbedtls_p384_gro( A );
DCHECK_EQ(0, A[6]);
MBEDTLS_ASSERT(0 == A[6]);
#else
mbedtls_p384_sub(A, A, B);
#endif
@ -460,11 +458,11 @@ int mbedtls_p384_add_mixed( const mbedtls_ecp_group *G,
s.Zn = mbedtls_mpi_limbs( &P->Z );
s.QXn = mbedtls_mpi_limbs( &Q->X );
s.QYn = mbedtls_mpi_limbs( &Q->Y );
CHECK_LE( s.Xn, 6 );
CHECK_LE( s.Yn, 6 );
CHECK_LE( s.Zn, 6 );
CHECK_LE( s.QXn, 6 );
CHECK_LE( s.QYn, 6 );
MBEDTLS_ASSERT( s.Xn <= 6 );
MBEDTLS_ASSERT( s.Yn <= 6 );
MBEDTLS_ASSERT( s.Zn <= 6 );
MBEDTLS_ASSERT( s.QXn <= 6 );
MBEDTLS_ASSERT( s.QYn <= 6 );
memcpy( s.X, P->X.p, s.Xn * 8 );
memcpy( s.Y, P->Y.p, s.Yn * 8 );
memcpy( s.Z, P->Z.p, s.Zn * 8 );

View file

@ -16,8 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/log/check.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/bignum_internal.h"

View file

@ -51,7 +51,7 @@ COSMOPOLITAN_C_START_
#define MBEDTLS_ASSERT(EXPR) \
((void)((EXPR) || (__assert_fail(#EXPR, __FILE__, __LINE__), 0)))
#else
#define MBEDTLS_ASSERT(EXPR) (void)0
#define MBEDTLS_ASSERT(EXPR) _unassert(EXPR)
#endif
typedef struct mbedtls_platform_context {

View file

@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/fmt/conv.h"
@ -77,14 +76,6 @@ jmp_buf jmp_tmp;
int option_verbose = 1;
mbedtls_test_info_t mbedtls_test_info;
static uint64_t Rando(void) {
static uint64_t x = 0x18abac12f3191aed;
uint64_t z = (x += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
int mbedtls_test_platform_setup(void) {
char *p;
int ret = 0;
@ -128,7 +119,7 @@ int mbedtls_hardware_poll(void *wut, unsigned char *p, size_t n, size_t *olen) {
size_t i, j;
unsigned char b[8];
for (i = 0; i < n; ++i) {
x = Rando();
x = lemur64();
WRITE64LE(b, x);
for (j = 0; j < 8 && i + j < n; ++j) {
p[i + j] = b[j];