Skip to content

AIX related improvements (discussed in #810) - #819

Open
karel-m wants to merge 2 commits into
developfrom
pr/aix-failures
Open

karel-m wants to merge 2 commits into
developfrom
pr/aix-failures

Conversation

@karel-m

@karel-m karel-m commented Nov 17, 2026

Copy link
Copy Markdown
Member

alternative fix for #810

@karel-m

karel-m commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

@rainer-tammer Could you please check whether the fix in this PR works for you?

@karel-m
karel-m requested a review from sjaeckel November 17, 2026 14:11

@sjaeckel sjaeckel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we simply re-define XMALLOC on AIX to malloc((len) + 1) or if we're worried this might overflow to

({
void *LTC_TMPVAR(p);
size_t LTC_TMPVAR(tlen) = (len);
LTC_TMPVAR(p) = malloc(LTC_TMPVAR(tlen) ? LTC_TMPVAR(tlen) : 1);
LTC_TMPVAR(p);
})

instead of doing this for each architecture?

@karel-m

karel-m commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

I don't have a strong opinion here. I just don't want to add #ifdef AIX ... as IMO this is not specific to AIX.

AFAIK, it's "legal" for malloc(0) to return either NULL or a valid pointer and our code should handle both correctly.

@sjaeckel

Copy link
Copy Markdown
Member

I don't have a strong opinion here. I just don't want to add #ifdef AIX ... as IMO this is not specific to AIX.

AFAIK, it's "legal" for malloc(0) to return either NULL or a valid pointer and our code should handle both correctly.

I also wouldn't want to make this arch specific, since it's toolchain and C standard version specific ... reading the malloc manpage is real fun ... m(

The question becomes: Should we try to fix this in a future-proof way or simply slap this patch on and fix any potential wrong usages in the future when they happened?

After looking at the realloc(3P) manpage I'm tempted to create wrappers for the entire malloc class of functions that behave everywhere the same ...

RETURN VALUE
       Upon successful completion, realloc() shall return a pointer to the (possibly moved) allocated space. If size is 0, either:

        *  A null pointer shall be returned and, if ptr is not a null pointer, errno shall be set to an implementation-defined value.

        *  A pointer to the allocated space shall be returned, and the memory object pointed to by ptr shall be freed. The application shall ensure that the pointer is not used to access an object.

"either do this or do something entirely different."

@rainer-tammer

Copy link
Copy Markdown

With the following patch the malloc patch (#810, pr/aix-fixes) does work with perl-CryptX (no AIX #ifdef).

  • complete replacement of src/ltc/xxx with the files from pr/aix-fixes
  • no changes to the perl-CryptX ltm.
diff --git a/src/ltc/math/ltm_desc.c b/src/ltc/math/ltm_desc.c
index 99c9f14..89642fc 100644
--- a/src/ltc/math/ltm_desc.c
+++ b/src/ltc/math/ltm_desc.c
@@ -7,10 +7,31 @@
 #ifdef LTM_DESC
 
 #include <tommath.h>
-#if !defined(PRIVATE_MP_WARRAY) && !defined(BN_MP_PRIME_IS_PRIME_C)
-#include <stdbool.h>
+
+/*
+ * mp_prime_is_prime() returns its result through a pointer whose type
+ * depends on the libtommath flavour we are built against:
+ *   - libtommath 1.x:                                int
+ *   - libtommath 2.x (C99, tommath.h includes <stdbool.h>): bool
+ *   - libtommath 2.x converted with `make c89`
+ *     (tommath_c89.h, no <stdbool.h>):              mp_bool (an enum)
+ * Passing a `bool` where an `int`/`mp_bool` is expected only works by
+ * accident on little-endian targets; on big-endian the result is always
+ * read as 0 and rand_prime() never terminates.
+ * <stdbool.h> is required to define __bool_true_false_are_defined, so
+ * its presence tells the C99 and the C89 build of libtommath apart.
+ */
+#if defined(PRIVATE_MP_WARRAY) || defined(BN_MP_PRIME_IS_PRIME_C)
+typedef int ltm_bool;
+#elif defined(__bool_true_false_are_defined)
+typedef bool ltm_bool;
+#else
+typedef mp_bool ltm_bool;
 #endif
 
+/* SIZE_MAX needs <stdint.h>, which the C89 tommath.h does not include */
+#define LTM_SIZE_MAX ((size_t)-1)
+
 static const struct {
     mp_err mpi_code;
     int ltc_code;
@@ -196,7 +217,7 @@ static int write_radix(const void *a, char *b, int radix)
 #ifdef BN_MP_TORADIX_C
    return mpi_to_ltc_error(mp_toradix(a, b, radix));
 #else
-   return mpi_to_ltc_error(mp_to_radix(a, b, SIZE_MAX, NULL, radix));
+   return mpi_to_ltc_error(mp_to_radix(a, b, LTM_SIZE_MAX, NULL, radix));
 #endif
 }
 
@@ -219,7 +240,7 @@ static int unsigned_write(const void *a, unsigned char *b)
 #ifdef BN_MP_TO_UNSIGNED_BIN_C
    return mpi_to_ltc_error(mp_to_unsigned_bin(a, b));
 #else
-   return mpi_to_ltc_error(mp_to_ubin(a, b, SIZE_MAX, NULL));
+   return mpi_to_ltc_error(mp_to_ubin(a, b, LTM_SIZE_MAX, NULL));
 #endif
 }
 
@@ -444,11 +465,7 @@ static int exptmod(const void *a, const void *b, const void *c, void *d)
 static int isprime(const void *a, int b, int *c)
 {
    int err;
-#if defined(PRIVATE_MP_WARRAY) || defined(BN_MP_PRIME_IS_PRIME_C)
-   int res;
-#else
-   bool res;
-#endif
+   ltm_bool res;
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(c != NULL);
    b = mp_prime_rabin_miller_trials(mp_count_bits(a));

@sjaeckel

Copy link
Copy Markdown
Member

[...] the malloc patch (#810, pr/aix-fixes) does work [...].

Just to be sure: you meant pr/aix-failures, not pr/aix-fixes right?

@rainer-tammer

Copy link
Copy Markdown

Sorry, yes I meant pr/aix-failures.
I have used this branch for perl-CryptX + the src/ltc/math/ltm_desc.c patch.
After the changes a make test did work without issues.
No changes to the ltm.

@karel-m

karel-m commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

I am not sure about implementing future-proof wrappers; we have three occurrences here.

Maybe improved comment:

buf = XMALLOC(buf_len ? buf_len : 1); /* 0 is valid for buf_len; XMALLOC(0) may return NULL */

Or simpler:

buf = XMALLOC(buf_len + 1); /* 0 is valid for buf_len; XMALLOC(0) may return NULL */

@rainer-tammer

Copy link
Copy Markdown

Looks good.
And what about the src/ltc/math/ltm_desc.c ?

@sjaeckel

Copy link
Copy Markdown
Member

And what about the src/ltc/math/ltm_desc.c ?

I'll have a look after this is decided upon.

I am not sure about implementing future-proof wrappers; we have three occurrences here.

I'm also not so sure whether I'd want them, but I don't know whether we can solve this properly without ...

Or simpler:

buf = XMALLOC(buf_len + 1); /* 0 is valid for buf_len; XMALLOC(0) may return NULL */

which could overflow to 0 in theory!?

@rainer-tammer

rainer-tammer commented Nov 18, 2026 •

Copy link
Copy Markdown

Good.
Note: The ltm_desc.c fix is independent of the malloc() stuff.
It fixes an unrelated hang on AIX (in the latest pr/aix-failures).

@rainer-tammer

Copy link
Copy Markdown

Any news regarding perl-CryptX src/ltc/math/ltm_desc.c (src/math/ltm_desc.c, libtomcrypt)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants