Skip to content

Aliases for standard C functions (memset/memcpy/memcmp)#654

Draft
rmikielis wants to merge 1 commit into
masterfrom
rmikielis/RTOS-1041
Draft

Aliases for standard C functions (memset/memcpy/memcmp)#654
rmikielis wants to merge 1 commit into
masterfrom
rmikielis/RTOS-1041

Conversation

@rmikielis
Copy link
Copy Markdown
Contributor

JIRA: RTOS-1041

Description

GCC requires free standing environments to provide few standard C functions (ex. memset). These aliases redirect such calls to HAL library functions (hal_memset)

Motivation and Context

Required by GCC

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: (ia32 qemu, arm qemu).

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-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.

Hello @rmikielis, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

As gemini-code-assist, I've reviewed this pull request. The primary goal, as stated in the title and description, is to satisfy a requirement from GCC for freestanding environments to provide implementations of standard C functions like memset, memcpy, and memcmp. This pull request addresses this by adding linker flags to the build process that create symbol aliases. These aliases redirect calls to the standard function names (memset, memcpy, memcmp) to their corresponding implementations provided elsewhere, specifically hal_memset, hal_memcpy, and hal_memcmp. This change is categorized as a chore and has been hand-tested on ia32 and arm qemu.

Highlights

  • Standard C Function Aliases: Introduces linker-level aliases for the standard C library functions memset, memcpy, and memcmp.
  • GCC Freestanding Requirement: Satisfies a requirement from the GCC compiler when building for freestanding environments, ensuring necessary standard functions are available via expected names.
  • Build System Change: Implements the aliasing by adding -Wl,--defsym linker flags within the Makefile.

Changelog

  • Makefile
    • Added a line to the LDFLAGS variable to include linker flags (-Wl,--defsym=...) that create aliases for memcpy, memcmp, and memset, pointing them to hal_memcpy, hal_memcmp, and hal_memset respectively. This change is added around line 32.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Symbols link the code,
Aliases point the way now,
Functions find their home.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@agkaminski
Copy link
Copy Markdown
Member

Nice solution! Does it work? 😄 Have you tried e.g. assigning struct to force use of memcpy?

Copy link
Copy Markdown

@gemini-code-assist gemini-code-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.

Code Review

This pull request introduces aliases for standard C functions (memset, memcpy, memcmp) to their HAL counterparts, which is required by GCC in freestanding environments. The use of LDFLAGS with -Wl,--defsym is an appropriate approach. However, there's a critical concern regarding the overlap safety of memcpy and the different hal_memcpy implementations across architectures.

Summary of Findings

  • Critical Risk: memcpy Overlap Safety: The hal_memcpy implementation for ia32 (and potentially riscv64) isn't safe for overlapping memory regions. Aliasing memcpy to it can lead to data corruption. Either make all hal_memcpy instances overlap-safe or provide and alias a separate, overlap-safe memmove (and its hal_memmove implementation).

Merge Readiness

Due to the critical concern regarding memcpy's behavior with overlapping memory regions and the current hal_memcpy implementation on ia32 (and potentially other platforms), I recommend that this pull request not be merged until this issue is addressed. Resolving this is essential to prevent potential data corruption bugs.

I am not authorized to approve pull requests, but based on this review, changes are requested before this PR can be considered safe for merging. Please ensure further review and approval from authorized maintainers after addressing the concerns.

Comment thread Makefile
CPPFLAGS += -DVERSION=\"$(VERSION)\" -DRELEASE=\"$(RELEASE)\" -DTARGET_FAMILY=\"$(TARGET_FAMILY)\"

# Add aliases to standard C library functions as required by GCC
LDFLAGS += -Wl,--defsym=memcpy=hal_memcpy,--defsym=memcmp=hal_memcmp,--defsym=memset=hal_memset
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

While aliasing is necessary, there's a critical issue concerning memcpy's behavior with overlapping memory regions and the current hal_memcpy implementations.

  • The C standard memcpy has undefined behavior for overlapping source/destination regions. memmove should be used when overlap is possible.
  • The aarch64 hal_memcpy (in hal/aarch64/_memcpy.S) handles overlaps, acting like memmove. However, the ia32 hal_memcpy (in hal/ia32/string.c) uses a forward-only copy (rep movsl/movsb with cld), which is not safe for all overlap scenarios (e.g., dst > src). The riscv64 hal_memcpy also doesn't explicitly guarantee overlap safety.
  • Aliasing memcpy to a hal_memcpy that isn't universally overlap-safe (especially on ia32) introduces a high risk of data corruption if the compiler generates memcpy calls or if developers use memcpy for operations that might overlap.

To address this, consider these options:

  1. Option A (Make hal_memcpy universally overlap-safe):
    Update all hal_memcpy implementations (especially for ia32 and riscv64, if affected) to handle overlaps correctly, similar to memmove. If this is done, consider aliasing memmove to this now-safe hal_memcpy:
    LDFLAGS += -Wl,--defsym=memcpy=hal_memcpy,--defsym=memcmp=hal_memcmp,--defsym=memset=hal_memset,--defsym=memmove=hal_memcpy
    (Assuming hal_memcpy becomes fully overlap-safe on all platforms).

  2. Option B (Provide a separate, safe hal_memmove - Recommended):
    Keep hal_memcpy non-overlap-safe. Provide a separate, overlap-safe hal_memmove function for all architectures, and alias memmove to it. This is crucial because code might rely on memmove for overlap safety. The change would be:
    LDFLAGS += -Wl,--defsym=memcpy=hal_memcpy,--defsym=memcmp=hal_memcmp,--defsym=memset=hal_memset,--defsym=memmove=hal_memmove
    (This requires hal_memmove to be implemented correctly and be overlap-safe across all platforms).

Clarify the intended strategy for memory operation safety and ensure a mechanism for safe overlapping memory copies (memmove semantics) is reliably available across all target architectures. Without this, the change poses a significant risk of data integrity issues.

LDFLAGS += -Wl,--defsym=memcpy=hal_memcpy,--defsym=memcmp=hal_memcmp,--defsym=memset=hal_memset,--defsym=memmove=hal_memmove

@github-actions
Copy link
Copy Markdown

Unit Test Results

7 958 tests  ±0   7 434 ✅ ±0   40m 48s ⏱️ +4s
  470 suites ±0     524 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit ce38458. ± Comparison against base commit 4584c03.

@rmikielis
Copy link
Copy Markdown
Contributor Author

Nice solution! Does it work? 😄 Have you tried e.g. assigning struct to force use of memcpy?

I was able to force GCC to submit memset and memcpy, and aliases worked fine in final kernel .elf (redirections to hal_* can be seen). I added bit more under https://confluence.phoenix-rtos.com/display/RTOS/Standard+C+library+calls :)

@agkaminski
Copy link
Copy Markdown
Member

@Darchiv @nalajcie I vote for merging this. Pls fix commit message though (and add JIRA ID)

@agkaminski agkaminski requested review from Darchiv and nalajcie June 17, 2025 17:48
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