Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address most implicit 64-to-32-bit conversion warnings. #525

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ucko
Copy link

@ucko ucko commented Oct 11, 2024

Visual Studio and Xcode both issue such warnings by default, and Clang also offers a -Wshorten-64-to-32 option to expose them elsewhere. To address them, harmonize types where practical and add casts as appropriate (first formally capping executables' read request sizes as necessary). NB: detect_repeat in pcre2_jit_compile.c still yields two such warnings that look plausibly legitimate but not so easy to fix. :-/

Copy link
Contributor

@carenas carenas left a comment

Choose a reason for hiding this comment

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

Presume from your commit message you were trying to remove warnings from the 8-bit library code when built in a 64bit system, could you elaborate on what is the driver behind that?

@@ -63,7 +63,7 @@ The INT64_OR_DOUBLE type is a 64-bit integer type when available,
otherwise double. */

BOOL
PRIV(ckd_smul)(PCRE2_SIZE *r, int a, int b)
PRIV(ckd_smul)(PCRE2_SIZE *r, ptrdiff_t a, ptrdiff_t b)
Copy link
Contributor

Choose a reason for hiding this comment

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

the whole point of this function is to make sure those 2 parameters are intso we can check for overflow

Copy link
Author

Choose a reason for hiding this comment

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

Ah, yeah, the hand-coded branch would need reworking. (__builtin_mul_overflow should DTRT regardless.) However, without this change, pcre2test's process_data could in principle pass in a truncated replen, yielding incorrect results. Should it instead bail if replen > INT_MAX?

Copy link
Contributor

@carenas carenas Oct 17, 2024

Choose a reason for hiding this comment

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

pcre2test is a test application, AFAIK replen was made a size_t just to avoid having to do a cast, and it is expected never to have a value larger than int either, so a truncation there shouldn't be possible, but feel free to poke at it and prove me wrong.

the call for ckd_smul was indeed how we are making sure that the value of the multiplication is always correct.

FWIW you could NOT build this test application if all you want is the library an want it to be warning free, there is a configure and cmake option for that. You haven't answered my original question so I am still not sure what the end goal is.

@@ -3302,7 +3302,7 @@ OP2(SLJIT_SUB | SLJIT_SET_Z, COUNT_MATCH, 0, COUNT_MATCH, 0, SLJIT_IMM, 1);
add_jump(compiler, &common->calllimit, JUMP(SLJIT_ZERO));
}

static SLJIT_INLINE void allocate_stack(compiler_common *common, int size)
static SLJIT_INLINE void allocate_stack(compiler_common *common, sljit_uw size)
Copy link
Contributor

Choose a reason for hiding this comment

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

probably should had been PCRE2_SIZE but were missed when PCRE2 was created from PCRE1 (which used int for all offsets)

Copy link
Author

Choose a reason for hiding this comment

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

PCRE2_SIZE substituted, thanks!

@@ -87,7 +87,7 @@ POSSIBILITY OF SUCH DAMAGE.
&& ((__clang_major__ == 3 && __clang_minor__ >= 3) || (__clang_major__ > 3)))
__attribute__((no_sanitize_address))
#endif
static sljit_u8* SLJIT_FUNC FF_FUN(sljit_u8 *str_end, sljit_u8 **str_ptr, sljit_uw offs1, sljit_uw offs2, sljit_uw chars)
static sljit_u8* SLJIT_FUNC FF_FUN(sljit_u8 *str_end, sljit_u8 **str_ptr, sljit_uw offs1, sljit_uw offs2, sljit_u32 chars)
Copy link
Contributor

Choose a reason for hiding this comment

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

this should break the 16bit library

Copy link
Author

@ucko ucko Oct 15, 2024

Choose a reason for hiding this comment

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

Not AFAICT, though I must admit I'd initially focused on the 8-bit one.

Copy link
Collaborator

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

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

Please be very careful when changing jit types.

@@ -2362,7 +2362,7 @@ SLJIT_ASSERT(stackpos == STACK(stacktop));
typedef struct delayed_mem_copy_status {
struct sljit_compiler *compiler;
int store_bases[RECURSE_TMP_REG_COUNT];
int store_offsets[RECURSE_TMP_REG_COUNT];
sljit_sw store_offsets[RECURSE_TMP_REG_COUNT];
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are intentionally small values.

Copy link
Author

Choose a reason for hiding this comment

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

Without this change, delayed_mem_copy_move's assignment into store_offsets can wind up truncated. Attempting to narrow the type of its store_offset argument instead reveals that three call sites pass in 64-bit values.

/* These values should never be negative, suggesting the use of sljit_u32.
However, they should also remain (far) below 1 << 31, and (would-be)
negative *differences* won't otherwise undergo correct sign extension. */
sljit_s32 c, charoffset, max = 256, min = READ_CHAR_MAX;
Copy link
Collaborator

Choose a reason for hiding this comment

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

c can be UINT32_MAX, so this is also a bad change.

Copy link
Author

Choose a reason for hiding this comment

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

Good point. I've scrapped all changes to compile_xclass_matchingpath in favor of formally making read_char's min and max sljit_uw.

Visual Studio and Xcode both issue such warnings by default, and Clang
also offers a -Wshorten-64-to-32 option to expose them elsewhere.  To
address them, harmonize types where practical and add casts as
appropriate (first formally capping executables' read request sizes as
necessary).  NB: detect_repeat in pcre2_jit_compile.c still yields two
such warnings that look plausibly legitimate but not so easy to fix. :-/

Signed-off-by: Aaron M. Ucko <[email protected]>
@@ -667,7 +667,7 @@ PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
pcre2_set_depth_limit(pcre2_match_context *, uint32_t); \
PCRE2_EXP_DECL int PCRE2_CALL_CONVENTION \
pcre2_set_heap_limit(pcre2_match_context *, uint32_t); \
pcre2_set_heap_limit(pcre2_match_context *, PCRE2_SIZE); \
Copy link
Collaborator

Choose a reason for hiding this comment

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

API functions cannot be changed, even if they are not the best.

Copy link
Author

Choose a reason for hiding this comment

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

Oops, yeah, I didn't mean to introduce an ABI break. :-/

Copy link
Author

Choose a reason for hiding this comment

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

Ah, right, I'll need to revert the POSIX API changes too.

@ucko
Copy link
Author

ucko commented Oct 15, 2024

Thanks for the quick feedback! I've force-pushed a largely corrected (and rebased) version and will reply on individual points shortly. AFAICT, some of these changes really should help in corner cases; although the remainder are formalities, the (configuration-dependent) warnings they address could bury other warnings (none here at present, I'm happy to report).

I acknowledge that this PR still needs more work.

@NWilson
Copy link
Member

NWilson commented Jan 12, 2025

Thank you Aaron (@ucko) for pushing us to fix these warnings.

I have addressed them in PR #655, hopefully. I took a slightly different direction to you on some of the fixes. We now pass with /W3 warnings on Windows (MSVC).

I have not addressed all the warnings generated by -Wshorten-64-to-32 (GCC, Clang) since that is far spammier than MSVC's equivalent.

Let me know if you are satisfied with the level of warnings suppression on master. I would be happy to fix any additional warnings that are causing you problems with your company's build policy, if you can point out where they are.

@ucko
Copy link
Author

ucko commented Jan 15, 2025

@NWilson Great, thank you very much! Your fixes are a big improvement, but I'd appreciate it if you could please take another look at pcre2_jit_neon_inc.h. I also still see a handful of warnings from pcre2grep.c and pcre2test.c, but they're less of a concern. I'll elaborate on your #655.

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.

4 participants