From 3878208a8b789a8f608ef33dc621e34931b8448a Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 7 Mar 2022 14:54:18 +0800 Subject: [PATCH] found the problem...mem* functions are provided in Rust the libc implementation was overriding key Rust symbols by re-implementing mem* calls. "oops". --- services/ffi-test/src/bindings.rs | 6 +- services/ffi-test/sys/build.rs | 1 + services/ffi-test/sys/ffi/ffi.c | 35 ++ services/ffi-test/sys/ffi/libc_split.c | 651 +++++++++++++++++++++++++ 4 files changed, 690 insertions(+), 3 deletions(-) create mode 100644 services/ffi-test/sys/ffi/libc_split.c diff --git a/services/ffi-test/src/bindings.rs b/services/ffi-test/src/bindings.rs index 782ca964c..062ca59ed 100644 --- a/services/ffi-test/src/bindings.rs +++ b/services/ffi-test/src/bindings.rs @@ -105,7 +105,6 @@ pub unsafe extern "C" fn realloc( Some(index) => { log::info!("realloc/free success: {:x}", ptr as usize); let mut old = C_HEAP.remove(index); - old.clear(); let checked_size = if size == 0 { 1 // at least 1 element so we have a pointer we can pass back } else { @@ -113,9 +112,10 @@ pub unsafe extern "C" fn realloc( }; let mut alloc: Vec:: = Vec::with_capacity(checked_size as usize); let ret_ptr = alloc.as_mut_ptr(); - for (&src, dst) in old.iter().zip(alloc.iter_mut()) { - *dst = src; + for &src in old.iter() { + alloc.push(src); } + old.clear(); C_HEAP.push(alloc); log::info!("realloc/allocated: {:x}", ret_ptr as usize); diff --git a/services/ffi-test/sys/build.rs b/services/ffi-test/sys/build.rs index 22379f901..8cb10a09f 100644 --- a/services/ffi-test/sys/build.rs +++ b/services/ffi-test/sys/build.rs @@ -8,6 +8,7 @@ fn main() { let espeak_srcs = vec![ "ffi/ffi.c", "ffi/libc.c", + "ffi/libc_split.c", "ffi/scanf.c", ]; let espeak_includes = vec![ diff --git a/services/ffi-test/sys/ffi/ffi.c b/services/ffi-test/sys/ffi/ffi.c index 3fbe36289..4821c0ea6 100644 --- a/services/ffi-test/sys/ffi/ffi.c +++ b/services/ffi-test/sys/ffi/ffi.c @@ -51,6 +51,41 @@ int malloc_test() { for (i = size-1; i >= 0; i--) { free(p[i]); } + + char *foo = malloc(200); + for (i = 0; i < 200; i++) { + foo[i] = (char)i; + } + char *bar = malloc(200); + memcpy(bar, foo, 200); + if (memcmp(foo, bar, 200) != 0) { + printf("fail on alloc and copy\n"); + } else { + printf("pass on alloc and copy\n"); + } + char *baz = realloc(foo, 300); + int mresult = memcmp(baz, bar, 200); + if (mresult != 0) { + printf("fail on realloc copy: %d\n", mresult); + for (i = 0; i < 200; i++) { + if (bar[i] != baz[i]) { + printf(" fail bar[%d](%d) != baz[%d](%d)\n", i, bar[i], i, baz[i]); + } + } + } else { + printf("pass on realloc copy \n"); + } + memset(baz, 42, 300); + int pass = 1; + for (i = 0; i < 300; i++) { + if (baz[i] != 42) { + printf("fail on memset\n"); + pass = 0; + } + } + if (pass == 1) { + printf("memset passed\n"); + } /* int x = 6; char *** matrix = alloc_matrix(x, x); diff --git a/services/ffi-test/sys/ffi/libc_split.c b/services/ffi-test/sys/ffi/libc_split.c new file mode 100644 index 000000000..389132df3 --- /dev/null +++ b/services/ffi-test/sys/ffi/libc_split.c @@ -0,0 +1,651 @@ +#ifdef LOCAL_TEST +#include "../config.h" +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libc.h" + +/* +void * +memcpy (void *dest, const void *src, size_t len) +{ + char *d = dest; + const char *s = src; + while (len--) + *d++ = *s++; + return dest; +} + +void * +memset (void *dest, int val, size_t len) +{ + unsigned char *ptr = dest; + while (len-- > 0) + *ptr++ = val; + return dest; +} + +int memcmp(const void *s1, const void *s2, size_t n) +{ + const unsigned char* p1 = s1; + const unsigned char* end1 = p1 + n; + const unsigned char* p2 = s2; + int d = 0; + for (;;) { + if (d || p1 >= end1) break; + d = (int)*p1++ - (int)*p2++; + if (d || p1 >= end1) break; + d = (int)*p1++ - (int)*p2++; + if (d || p1 >= end1) break; + d = (int)*p1++ - (int)*p2++; + if (d || p1 >= end1) break; + d = (int)*p1++ - (int)*p2++; + } + return d; +} +*/ + +wchar_t * +wcschr(const wchar_t *s, wchar_t c) +{ + while (*s != c && *s != L'\0') + s++; + if (*s == c) + return ((wchar_t *)s); + return (NULL); +} +size_t +wcslen(const wchar_t *s) +{ + const wchar_t *p; + p = s; + while (*p) + p++; + return p - s; +} +int +strncmp(const char *s1, const char *s2, size_t n) +{ + if (n == 0) + return (0); + do { + if (*s1 != *s2++) + return (*(unsigned char *)s1 - *(unsigned char *)--s2); + if (*s1++ == 0) + break; + } while (--n != 0); + return (0); +} +char * +strstr(const char *s, const char *find) +{ + char c, sc; + size_t len; + if ((c = *find++) != 0) { + len = strlen(find); + do { + do { + if ((sc = *s++) == 0) + return (NULL); + } while (sc != c); + } while (strncmp(s, find, len) != 0); + s--; + } + return ((char *)s); +} +char * +strchr (register const char *s, int c) +{ + do { + if (*s == c) + { + return (char*)s; + } + } while (*s++); + return (0); +} + +char * +strcpy(char *to, const char *from) +{ + char *save = to; + for (; (*to = *from) != '\0'; ++from, ++to); + return(save); +} + +int +strcmp(const char *s1, const char *s2) +{ + while (*s1 == *s2++) + if (*s1++ == 0) + return (0); + return (*(unsigned char *)s1 - *(unsigned char *)--s2); +} + +char * +strcat(char *s, const char *append) +{ + char *save = s; + for (; *s; ++s); + while ((*s++ = *append++) != '\0'); + return(save); +} + +size_t +strlen(const char *str) +{ + const char *s; + for (s = str; *s; ++s) + ; + return (s - str); +} + +char * +strdup(const char *str) +{ + size_t siz; + char *copy; + siz = strlen(str) + 1; + if ((copy = malloc(siz)) == NULL) + return(NULL); + (void)memcpy(copy, str, siz); + return(copy); +} + +char * +strncpy(char *dst, const char *src, size_t n) +{ + if (n != 0) { + char *d = dst; + const char *s = src; + do { + if ((*d++ = *s++) == 0) { + /* NUL pad the remaining n-1 bytes */ + while (--n != 0) + *d++ = 0; + break; + } + } while (--n != 0); + } + return (dst); +} + +long +strtol(const char *nptr, char **endptr, int base) +{ + const char *s; + long acc, cutoff; + int c; + int neg, any, cutlim; + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + s = nptr; + do { + c = (unsigned char) *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for longs is + * [-2147483648..2147483647] and the input base is 10, + * cutoff will be set to 214748364 and cutlim to either + * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated + * a value > 214748364, or equal but the next digit is > 7 (or 8), + * the number is too big, and we will return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + cutoff = neg ? LONG_MIN : LONG_MAX; + cutlim = cutoff % base; + cutoff /= base; + if (neg) { + if (cutlim > 0) { + cutlim -= base; + cutoff += 1; + } + cutlim = -cutlim; + } + for (acc = 0, any = 0;; c = (unsigned char) *s++) { + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0) + continue; + if (neg) { + if (acc < cutoff || (acc == cutoff && c > cutlim)) { + any = -1; + acc = LONG_MIN; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc -= c; + } + } else { + if (acc > cutoff || (acc == cutoff && c > cutlim)) { + any = -1; + acc = LONG_MAX; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc += c; + } + } + } + if (endptr != 0) + *endptr = (char *) (any ? s - 1 : nptr); + return (acc); +} + +unsigned long +strtoul(const char *nptr, char **endptr, int base) +{ + const char *s; + unsigned long acc, cutoff; + int c; + int neg, any, cutlim; + /* + * See strtol for comments as to the logic used. + */ + s = nptr; + do { + c = (unsigned char) *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + cutoff = ULONG_MAX / (unsigned long)base; + cutlim = ULONG_MAX % (unsigned long)base; + for (acc = 0, any = 0;; c = (unsigned char) *s++) { + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0) + continue; + if (acc > cutoff || (acc == cutoff && c > cutlim)) { + any = -1; + acc = ULONG_MAX; + errno = ERANGE; + } else { + any = 1; + acc *= (unsigned long)base; + acc += c; + } + } + if (neg && any > 0) + acc = -acc; + if (endptr != 0) + *endptr = (char *) (any ? s - 1 : nptr); + return (acc); +} + +uintmax_t +strtoumax(const char *nptr, char **endptr, int base) +{ + const char *s; + uintmax_t acc, cutoff; + int c; + int neg, any, cutlim; + /* + * See strtoq for comments as to the logic used. + */ + s = nptr; + do { + c = (unsigned char) *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + /* BIONIC: avoid division and modulo for common cases */ +#define CASE_BASE(x) \ + case x: cutoff = UINTMAX_MAX / x; \ + cutlim = UINTMAX_MAX % x; \ + break + switch (base) { + CASE_BASE(8); + CASE_BASE(10); + CASE_BASE(16); + default: + cutoff = UINTMAX_MAX / base; + cutlim = UINTMAX_MAX % base; + } + for (acc = 0, any = 0;; c = (unsigned char) *s++) { + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0) + continue; + if (acc > cutoff || (acc == cutoff && c > cutlim)) { + any = -1; + acc = UINTMAX_MAX; + errno = ERANGE; + } else { + any = 1; + acc *= (uintmax_t)base; + acc += c; + } + } + if (neg && any > 0) + acc = -acc; + if (endptr != 0) + *endptr = (char *) (any ? s - 1 : nptr); + return (acc); +} +static inline int digitval(int ch) +{ + unsigned d; + d = (unsigned)(ch - '0'); + if (d < 10) return (int)d; + d = (unsigned)(ch - 'a'); + if (d < 6) return (int)(d+10); + d = (unsigned)(ch - 'A'); + if (d < 6) return (int)(d+10); + return -1; +} +uintmax_t +strntoumax(const char *nptr, char **endptr, int base, size_t n) +{ + const unsigned char* p = (const unsigned char *)nptr; + const unsigned char* end = p + n; + int minus = 0; + uintmax_t v = 0; + int d; + /* skip leading space */ + while (p < end && isspace(*p)) + p++; + /* Single optional + or - */ + if (p < end) { + char c = p[0]; + if ( c == '-' || c == '+' ) { + minus = (c == '-'); + p++; + } + } + if ( base == 0 ) { + if ( p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') ) { + p += 2; + base = 16; + } else if ( p+1 < end && p[0] == '0' ) { + p += 1; + base = 8; + } else { + base = 10; + } + } else if ( base == 16 ) { + if ( p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') ) { + p += 2; + } + } + while ( p < end && (d = digitval(*p)) >= 0 && d < base ) { + v = v*base + d; + p += 1; + } + if ( endptr ) + *endptr = (char *)p; + return minus ? -v : v; +} +intmax_t strntoimax(const char *nptr, char **endptr, int base, size_t n) +{ + return (intmax_t) strntoumax(nptr, endptr, base, n); +} +intmax_t +strtoimax(const char *nptr, char **endptr, int base) +{ + const char *s; + intmax_t acc, cutoff; + int c; + int neg, any, cutlim; + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + s = nptr; + do { + c = (unsigned char) *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for intmax_t is + * [-9223372036854775808..9223372036854775807] and the input base + * is 10, cutoff will be set to 922337203685477580 and cutlim to + * either 7 (neg==0) or 8 (neg==1), meaning that if we have + * accumulated a value > 922337203685477580, or equal but the + * next digit is > 7 (or 8), the number is too big, and we will + * return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + /* BIONIC: avoid division and module for common cases */ +#define CASE_BASE2(x) \ + case x: \ + if (neg) { \ + cutlim = INTMAX_MIN % x; \ + cutoff = INTMAX_MIN / x; \ + } else { \ + cutlim = INTMAX_MAX % x; \ + cutoff = INTMAX_MAX / x; \ + }; \ + break + + switch (base) { + case 4: + if (neg) { + cutlim = (int)(INTMAX_MIN % 4); + cutoff = INTMAX_MIN / 4; + } else { + cutlim = (int)(INTMAX_MAX % 4); + cutoff = INTMAX_MAX / 4; + } + break; + CASE_BASE2(8); + CASE_BASE2(10); + CASE_BASE2(16); + default: + cutoff = neg ? INTMAX_MIN : INTMAX_MAX; + cutlim = cutoff % base; + cutoff /= base; + } +#undef CASE_BASE + + if (neg) { + if (cutlim > 0) { + cutlim -= base; + cutoff += 1; + } + cutlim = -cutlim; + } + for (acc = 0, any = 0;; c = (unsigned char) *s++) { + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0) + continue; + if (neg) { + if (acc < cutoff || (acc == cutoff && c > cutlim)) { + any = -1; + acc = INTMAX_MIN; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc -= c; + } + } else { + if (acc > cutoff || (acc == cutoff && c > cutlim)) { + any = -1; + acc = INTMAX_MAX; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc += c; + } + } + } + if (endptr != 0) + *endptr = (char *) (any ? s - 1 : nptr); + return (acc); +} +int atoi(const char* s) +{ + return (int)strtoimax(s, NULL, 10); +} + +char * +strtok(char *s, const char *delim) +{ + static char *last; + return strtok_r(s, delim, &last); +} +char * +strtok_r(char *s, const char *delim, char **last) +{ + char *spanp; + int c, sc; + char *tok; + if (s == NULL && (s = *last) == NULL) + return (NULL); + /* + * Skip (span) leading delimiters (s += strspn(s, delim), sort of). + */ +cont: + c = *s++; + for (spanp = (char *)delim; (sc = *spanp++) != 0;) { + if (c == sc) + goto cont; + } + if (c == 0) { /* no non-delimiter characters */ + *last = NULL; + return (NULL); + } + tok = s - 1; + /* + * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). + * Note that delim must have one NUL; we stop if we see that, too. + */ + for (;;) { + c = *s++; + spanp = (char *)delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = 0; + *last = s; + return (tok); + } + } while (sc != 0); + } + /* NOTREACHED */ +}