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

Linux: Fix kmsg f-strings #1502

Merged
merged 2 commits into from
Jan 2, 2025
Merged

Linux: Fix kmsg f-strings #1502

merged 2 commits into from
Jan 2, 2025

Conversation

ikelos
Copy link
Member

@ikelos ikelos commented Jan 1, 2025

Closes #1496

@@ -166,7 +166,7 @@ def get_caller(self, obj):

def get_caller_text(self, caller_id):
caller_name = "CPU" if caller_id & 0x80000000 else "Task"
caller = f"{caller_name}({caller_id & ~0x80000000:u})"
caller = f"{caller_name}({int(caller_id & ~0x80000000)})"
Copy link
Contributor

Choose a reason for hiding this comment

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

the int() is redundant here

@gcmoreira
Copy link
Contributor

hm this bug was introduced in this commit, in the context of use ruff for linting #1364.

@ikelos If you want to use an f-string, your changes look good. However, I'd recommend removing the unnecessary int() call for simplicity.
If you decide not to use an f-string, I recommend reverting these changes to the original version, which was carefully tested and verified.

@gcmoreira gcmoreira changed the title Linux: Fix kmsf f-strings Linux: Fix kmsg f-strings Jan 2, 2025
@gcmoreira
Copy link
Contributor

@c0rydoras I wonder if this bug was a ruff suggestion or you included this later?

@ikelos
Copy link
Member Author

ikelos commented Jan 2, 2025

Thanks for checking this! The unnecessary int cast was exactly my thought about the PR (essentially made by the initial report, #1496). I would like to keep the use of f-strings over %, I believe it's recommended (for reasons as put forward in the PEP rationale), I just couldn't find an example to try it out on. I only have a few linux samples and several are from AOMF...

@ikelos ikelos merged commit dce5ff1 into develop Jan 2, 2025
24 checks passed
@ikelos ikelos deleted the issues/issue1496 branch January 2, 2025 11:36
@gcmoreira
Copy link
Contributor

gcmoreira commented Jan 2, 2025

@ikelos sorry, hold on. I double-checked this using the example I used in that method comment, and it turns out this doesn't work as expected. Unfortunately, we introduced another bug here.

    def nsec_to_sec_str(self, nsec: int) -> str:
        # See kernel/printk/printk.c:print_time()
        #   Here, we could simply do:
        #       "%.6f" % (nsec / 1000000000.0)
        #   However, that will cause a roundoff error. For instance, using
        #   17110365556 as input, the above will result in 17.110366.
        #   While the kernel print_time function will result in 17.110365.
        #   This might seem insignificant but it could cause some issues
        #   when compared with userland tool results or when used in
        #   timelines.
>>> nsec=17110365556; "%lu.%06lu" % (nsec / 1000000000, (nsec % 1000000000) / 1000)
'17.110365'

>>> nsec=17110365556; f"{nsec / 1000000000}.{(nsec % 1000000000) / 1000:06}"
'17.110365556.110365.556'

Using // should get the same results and fix this issue:

>>> nsec=17110365556; f"{nsec // 1000000000}.{(nsec % 1000000000) // 1000:06}"
'17.110365'

Another test:

>>> nsec=17000365556; "%lu.%06lu" % (nsec / 1000000000, (nsec % 1000000000) / 1000)
'17.000365'

>>> nsec=17000365556; f"{nsec // 1000000000}.{(nsec % 1000000000) // 1000:06}"
'17.000365'

The only potential issue would be if nsec were not an int. However, this is not possible as it is always sourced from ts_nsec in log, printk_log, or printk_info, which is defined as a u64.

>>> nsec=17000365556.3; "%lu.%06lu" % (nsec / 1000000000, (nsec % 1000000000) / 1000)
'17.000365'

>>> nsec=17000365556.3; f"{nsec // 1000000000}.{(nsec % 1000000000) // 1000:06}"
'17.0.0365.0'

gcmoreira added a commit to gcmoreira/volatility3 that referenced this pull request Jan 2, 2025
ikelos added a commit that referenced this pull request Jan 3, 2025
linux: kmsg plugin: Fix f'string bug introduced in #1502
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.

Change to f-strings introduces ValueErrors in kmsg.py
2 participants