Skip to content

Zephyr misses strict aliasing disabling #44466

@evgeniy-paltsev

Description

@evgeniy-paltsev

While debugging issue I've bumped when I was using new ARC GCC11 based toolchain I've surprisingly found that we don't disable strict aliasing (don't pass -fno-strict-aliasing option for gcc/llvm based toolchains).

I've seen the places where we violate strict aliasing rule in many places in Zephyr.


Here is an example:
In posix threads layer we do cast between k_tid_t (which is defined as typedef struct k_thread* k_tid_t)
and the pointer to posix_thread structure, which is defined as

struct posix_thread {
	struct k_thread thread;

    // *other fields* //
};

Here is an example of code where we do assignment which violates the strict aliasing rule (located in pthread.c)

int pthread_getschedparam(pthread_t pthread, int *policy,
			  struct sched_param *param)
{
	struct posix_thread *thread = (struct posix_thread *) pthread;
	uint32_t priority;

	if ((thread == NULL) || (thread->state == PTHREAD_TERMINATED)) {
		return ESRCH;
	}

	priority = k_thread_priority_get((k_tid_t) thread);       // <<<<---- incorrect!

	param->sched_priority = zephyr_to_posix_priority(priority, policy);
	return 0;
}

So, given that

  • initial GCC11 issue goes away when disable strict aliasing
  • we have places in code which violates strict aliasing rule
    I guess we need do disable strict aliasing.

I'm going to do that for ARC, however I guess it's worth to do it for all architectures.

Any ideas why it isn't disabled now?

Metadata

Metadata

Assignees

Labels

area: PortabilityStandard compliant code, toolchain abstractionarea: ToolchainsToolchainsbugThe issue is a bug, or the PR is fixing a bugpriority: mediumMedium impact/importance bug

Type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions