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

fix: restore debug output indentation in catch clause #2134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gremat
Copy link

@gremat gremat commented Jan 17, 2025

Verbose debug output (e.g., --with-debug-verbose=2) may run into an inconsistent indentation when nested function calls are disturbed by signals (SIGBUS or SIGSEGV): the closing statement is never executed, cf. #2133 (comment) . Thanks @secDre4mer for pointing this out.

This patch restores the indentation in this case. The resulting output is obviously incomplete because it misses the closing statement but the subsequently reported error should be a sufficient hint to assess the situation. For instance,

2.256372 11010464 + yr_scanner_scan_mem(buffer=a0000004e891000 buffer_size=1048576) {
2.256429 11010464   + yr_scanner_scan_mem_blocks() {
2.256468 11010464     - _yr_get_first_block() {} = 116503290 // default iterator; single memory block, blocking
2.257259 11010464     + _yr_scanner_scan_mem_block(block_data=a0000004e891000 block->base=0x0 block->size=1048576) {
2.274888 11010464     - _yr_scanner_clean_matches() {}
2.274940 11010464   } = 4 AKA ERROR_COULD_NOT_MAP_FILE // yr_scanner_scan_mem_blocks()
2.275003 11010464 } = 4 AKA ERROR_COULD_NOT_MAP_FILE // yr_scanner_scan_mem()

does not contain the closing _yr_scanner_scan_mem_block statement.

Copy link

google-cla bot commented Jan 17, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link

@code-review-assist code-review-assist bot left a comment

Choose a reason for hiding this comment

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

Hi team,

I'm currently reviewing this pull request and will provide my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly understand the changes:

This pull request addresses an indentation issue in verbose debug output when nested function calls are interrupted by signals (like SIGBUS or SIGSEGV). The problem, as noted in the description, resulted in inconsistent indentation because the closing statement wasn't executed. This was originally reported in issue #2133 (comment).

Here's a breakdown of the changes:

  • Intent: Restore correct indentation in verbose debug output even when interrupted by signals.
  • Changes:
    • Added a global variable yr_debug_indent to libyara/exception.h (lines 224-225). This variable is used to track the indentation level.
    • Modified the YR_TRYCATCH macro in libyara/exception.h (lines 248, 263):
      • Before the jump to the catch clause, the current indentation level is stored in yr_debug_indent_before_jump (line 248).
      • In the catch clause, the indentation level is restored to the value stored before the jump (line 263).
  • Files Affected: libyara/exception.h (multiple changes across lines 221-267)

The updated YR_TRYCATCH macro ensures that even if a signal interrupts execution, the debug output maintains consistent indentation. While the output will be incomplete, the error message should still be informative.

I'll post a more detailed review shortly.


Debugging's a quest,
Signals may cause a slight mess,
Indentation's key.

Copy link

@code-review-assist code-review-assist bot left a comment

Choose a reason for hiding this comment

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

This patch addresses an indentation inconsistency in verbose debug output when signals interrupt nested function calls. The approach of restoring the indentation before the catch clause is a good way to handle the incomplete output scenario. The example provided clearly illustrates the issue and how the patch resolves it. I have a few minor suggestions regarding code style and documentation.

@@ -221,6 +221,8 @@ static void exception_handler(int sig, siginfo_t * info, void *context)
}
}

extern YR_TLS int yr_debug_indent;

Choose a reason for hiding this comment

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

low

Consider adding a comment explaining the purpose of this variable, specifically how it's used for debug indentation. For example, you could say something like "Thread-local variable to track the indentation level for debug output."

Suggested change
extern YR_TLS int yr_debug_indent;
extern YR_TLS int yr_debug_indent; // Thread-local variable to track the indentation level for debug output.

Copy link
Author

Choose a reason for hiding this comment

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

The variable is declared elsewhere. No need to comment on it here.

@@ -243,6 +245,7 @@ typedef struct sigaction sa;
} \
exception_handler_usecount++; \
pthread_mutex_unlock(&exception_handler_mutex); \
int yr_debug_indent_before_jump = yr_debug_indent; \

Choose a reason for hiding this comment

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

low

For consistency with the YARA coding style, consider adding a space after the = in the initialization. Also, could you clarify the purpose of saving the current indentation level with a brief comment? For example: "Save the current debug indentation level before the jump."

Suggested change
int yr_debug_indent_before_jump = yr_debug_indent; \
int yr_debug_indent_before_jump = yr_debug_indent; // Save the current debug indentation level before the jump.

Copy link
Author

Choose a reason for hiding this comment

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

Added (without breaking function-like macro).

Copy link
Member

Choose a reason for hiding this comment

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

You can ignore these AI-generated suggestions, this only an experiment and the suggestions should be taken with a grain of salt.

Comment on lines 262 to 264
/* Restore debug output indentation in case of failure */ \
yr_debug_indent = yr_debug_indent_before_jump; \

Choose a reason for hiding this comment

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

low

The comment could be slightly improved for clarity. How about: "Restore debug indentation to the level before the try block."

Suggested change
/* Restore debug output indentation in case of failure */ \
yr_debug_indent = yr_debug_indent_before_jump; \
/* Restore debug indentation to the level before the try block. */
yr_debug_indent = yr_debug_indent_before_jump;

Copy link
Author

Choose a reason for hiding this comment

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

The suggested extra information is redundant in the context of the (local) variable's name.

@gremat gremat force-pushed the fix/restore-debug-output-indentation-in-catch-clause branch from 9988d9b to 3cf9194 Compare January 17, 2025 10:33
@plusvic
Copy link
Member

plusvic commented Jan 17, 2025

This doesn't build properly in some CI tests.

@gremat
Copy link
Author

gremat commented Jan 17, 2025

This doesn't build properly in some CI tests.

Saw it right now. My bad, I didn't run a local build without --enable-debug. Working on a fix.

@gremat gremat force-pushed the fix/restore-debug-output-indentation-in-catch-clause branch from 3cf9194 to 5748522 Compare January 17, 2025 11:44
@gremat
Copy link
Author

gremat commented Jan 17, 2025

There are two CI failures left, both of them not related to my changes.

Cygwin fails because flex is missing:
image

And the bazel-build fails.

@@ -243,6 +254,8 @@ typedef struct sigaction sa;
} \
exception_handler_usecount++; \
pthread_mutex_unlock(&exception_handler_mutex); \
/* Save the current debug indentation level before the jump. */ \
YR_DEBUG_INDENT_RESET_INIT \
Copy link
Member

Choose a reason for hiding this comment

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

Why not using ...

#if 0 == YR_DEBUG_VERBOSITY
int yr_debug_indent_before_jump = yr_debug_indent;
#endif 

Instead of declaring yet another macro. It's easier to follow the code this way.

Copy link
Author

Choose a reason for hiding this comment

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

Please correct me if I'm mistaken but I think you cannot do that within YR_TRYCATCH where both macros are used. YR_TRYCATCH is a function-like macro thus, effectively, a single line (also indicated by the trailing backslashes). There, you cannot insert a construct like #if ... #endif which requires the # directives to be the first non-whitespace characters on each line.

However, I pushed a slightly different approach which uses yet another function-like macro to set yr_debug_indent in the catch clause. It might be somewhat nicer to read, but please decide for yourself. One drawback, though: we get a compiler warning (per YR_TRYCATCH instance) about an unused variable (which is only cosmetic; the compiler should optimize it away).

@@ -256,6 +269,9 @@ typedef struct sigaction sa;
} \
else \
{ \
/* Restore debug output indentation in case of failure */ \
YR_DEBUG_INDENT_RESET_RESTORE \
Copy link
Member

Choose a reason for hiding this comment

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

Same here, instead of declaring YR_DEBUG_INDENT_RESET_RESTORE we could use..

#if 0 == YR_DEBUG_VERBOSITY
yr_debug_indent = yr_debug_indent_before_jump;
#endif 

Copy link
Author

Choose a reason for hiding this comment

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

(See comment above.)

Drawback of this solution is a compiler warning about the unused
variable for non-debug builds. But this should not be a performance
penalty because the unused variable should be optimized away by the
compiler.
@gremat gremat requested a review from plusvic January 29, 2025 09:17
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.

2 participants