Skip to content

[RFC] trace: verify argument size - #3376

Closed
lyakh wants to merge 3 commits into
thesofproject:masterfrom
lyakh:trace64
Closed

lyakh wants to merge 3 commits into
thesofproject:masterfrom
lyakh:trace64

Conversation

@lyakh

@lyakh lyakh commented Sep 2, 2020 •

Copy link
Copy Markdown
Collaborator

It's useful for my debugging, and it's compile-time only, so we might merge it, but we don't have to, it's completely optional.

Currently SOF tracing only supports up to 32 bit arguments, using it with 64-bit arguments produces unpredictable results. Add compile-time checks to make sure all arguments are of correct size.

Oh, and yes, it currently breaks building, which is what it's supposed to do. So if we want to merge this we first have to fix those breakages (expect a patch soon).

@paulstelian97

Copy link
Copy Markdown
Collaborator

Are the errors actual locations where you're using 64-bit variables improperly in traces?

@lyakh

lyakh commented Sep 2, 2020

Copy link
Copy Markdown
Collaborator Author

Are the errors actual locations where you're using 64-bit variables improperly in traces?

@paulstelian97 yes, you should be able to see that in CI logs

@paulstelian97

Copy link
Copy Markdown
Collaborator

I assume you can add the fixes for those cases (in most cases I think a cast to (uint32_t) will suffice? ) in this same PR, or would it be a better idea to separate it?

@lyakh

lyakh commented Sep 2, 2020

Copy link
Copy Markdown
Collaborator Author

I assume you can add the fixes for those cases (in most cases I think a cast to (uint32_t) will suffice? ) in this same PR, or would it be a better idea to separate it?

@paulstelian97 exactly, let's see how many were not included in my test configuration

@lyakh

lyakh commented Sep 2, 2020

Copy link
Copy Markdown
Collaborator Author

Build failed for ARM (i.MX8*) in src/drivers/imx/interrupt.c::irq_handler() at

			tr_err(&irq_i_tr, "irq_handler(): IRQ storm, status "
			       PRIx64,
			       get_irqsteer_interrupts(line_index));

@dbaluta as far as I understand ARM doesn't have a magic bullet to make 64-bit tracing work, does it? So this has to be fixed too?

@dbaluta

dbaluta commented Sep 2, 2020

Copy link
Copy Markdown
Collaborator

@dbaluta as far as I understand ARM doesn't have a magic bullet to make 64-bit tracing work, does it? So this has to be fixed too?

Yes. @paulstelian97 care to help when you have some time. I'm currently in vacation and getting back on September, 14.

@paulstelian97

Copy link
Copy Markdown
Collaborator

Oh yeah, forgot about that one. I'll see it tomorrow, but basically I'd just fetch that into a 64-bit variable and print two hex parameters which are the 32-bit halves. It was broken (always showing zeros) before anyway.

@lyakh

lyakh commented Sep 3, 2020

Copy link
Copy Markdown
Collaborator Author

Oh yeah, forgot about that one. I'll see it tomorrow, but basically I'd just fetch that into a 64-bit variable and print two hex parameters which are the 32-bit halves. It was broken (always showing zeros) before anyway.

@dbaluta @paulstelian97 I've added a fix for that to the patch too

Comment thread src/drivers/imx/interrupt.c Outdated

@lgirdwood lgirdwood 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.

This is good - it lets developers know which trace types are supported (so they dont read back garbage in the trace logs)
@mrajwa btw - did you see the KPB change - you could be reading some garbage in parts of your logs....

Comment thread src/include/sof/trace/trace.h Outdated

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.

trace_check_size_uint32_1()

Comment thread src/include/sof/trace/trace.h Outdated

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.

sizeof(uint32_t)

Comment thread src/audio/host.c Outdated

@paulstelian97 paulstelian97 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What helps you to not get build errors when passing pointers to traces? I can't seem to identify the spot that fixes it...

SOF tracing can only handle up to 32-bit long values, attempts to
trace 64-bit values produce unpredictable results. Fix such cases
by making sure values are within range and casting them to 32 bits.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>

@paulstelian97 paulstelian97 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

From my point of view this looks good. Do handle Liam's comments as I agree.

@lyakh

lyakh commented Sep 4, 2020

Copy link
Copy Markdown
Collaborator Author

SOFCI TEST

Currently SOF tracing only supports up to 32 bit arguments, using
it with 64-bit arguments produces unpredictable results. Add compile-
time checks to make sure all arguments are of correct size.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
The PP_NARG() macro can only handle integer type arguments, use
META_COUNT_VARAGS_BEFORE_COMPILE() instead to avoid having to cast
pointer types to integers.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Comment on lines +173 to +200
#define trace_check_size_uint32_0() do { } while (0)

#define trace_check_size_uint32_1(a) do { \
STATIC_ASSERT(sizeof(a) <= sizeof(uint32_t), \
trace_parameter_too_large); \
} while (0)

#define trace_check_size_uint32_2(a, b) do { \
trace_check_size_uint32_1(a); \
trace_check_size_uint32_1(b); \
} while (0)

#define trace_check_size_uint32_3(a, b, c) do { \
trace_check_size_uint32_1(a); \
trace_check_size_uint32_1(b); \
trace_check_size_uint32_1(c); \
} while (0)

#define trace_check_size_uint32_4(a, b, c, d) do { \
trace_check_size_uint32_1(a); \
trace_check_size_uint32_1(b); \
trace_check_size_uint32_1(c); \
trace_check_size_uint32_1(d); \
} while (0)

#define trace_check_args(...) META_CONCAT(trace_check_size_uint32_, \
META_COUNT_VARAGS_BEFORE_COMPILE(__VA_ARGS__))(__VA_ARGS__)

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.

@lyakh maybe we would like to use META_MAP(), then trace_check_size_uint32_2, trace_check_size_uint32_3, trace_check_size_uint32_4 could be removed and solution will be more flexible when possible number of max number of trace arguments change.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@ktrzcinx you mean like

#define trace_check_size_uint32_2(a, b) META_MAP(1, trace_check_size_uint32_1, a, b)
#define trace_check_size_uint32_3(a, b, c) META_MAP(1, trace_check_size_uint32_1, a, b, c)
#define trace_check_size_uint32_4(a, b, c, d) META_MAP(2, trace_check_size_uint32_2, a, b, c, d)

yeah, sure, why not make it a bit more confusing yet :-D Or just make the above a bit more compact by

#define trace_check_size_uint32_3(a, b, c) do {				\
	  trace_check_size_uint32_2(a, b);				\
	  trace_check_size_uint32_1(c);					\
} while (0)

#define trace_check_size_uint32_4(a, b, c, d) do {			\
	  trace_check_size_uint32_2(a, b);				\
	  trace_check_size_uint32_2(c, d);				\
} while (0)

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.

I think rather about something like:

#define trace_check_size_uint32_1(a)
	STATIC_ASSERT(sizeof(a) <= sizeof(uint32_t), trace_parameter_too_large);

#define STATIC_CHECK_ARGS(...) do { META_RECURSE( \
	META_MAP(1, META_DEFER(trace_check_size_uint32_1), __VA_ARGS__)); \
	} while (0)

#define _log_message(atomic, lvl, comp_class, ctx, id_1, id_2,		\
		     format, ...)					\
do {									\
	_DECLARE_LOG_ENTRY(lvl, format, comp_class,			\
			   PP_NARG(__VA_ARGS__));			\
	STATIC_CHECK_ARGS(__VA_ARGS__);					\
	STATIC_ASSERT(
        ...

*not tested

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@ktrzcinx I tried that and it didn't compile. Maybe I'm using it wrongly, and I don't see any other uses of META_MAP() in the code, so, unless you volunteer to investigate, maybe it's better to stay with the current solution for now.

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.

Given that we are close to v1.6 lets merge this today unless @ktrzcinx can update the macro as it's better than what we have today. We can always revisit after v1.6. I would also like us to revisit META_MAP() if there are no users.

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.

It wasn't one line change, so I created separate PR covering this topic: #3393.

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.

Great - @lyakh shall we close this one ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@lgirdwood sure, would @ktrzcinx also pull the third patch from this series into his PR?

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.

done

@ktrzcinx

Copy link
Copy Markdown
Member

PR content moved to #3393

@ktrzcinx ktrzcinx closed this Sep 10, 2020
@lyakh
lyakh deleted the trace64 branch October 12, 2020 12:14
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.

5 participants