Aliases for standard C functions (memset/memcpy/memcmp)#654
Conversation
There was a problem hiding this comment.
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, andmemcmp. - 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,--defsymlinker flags within theMakefile.
Changelog
- Makefile
- Added a line to the
LDFLAGSvariable to include linker flags (-Wl,--defsym=...) that create aliases formemcpy,memcmp, andmemset, pointing them tohal_memcpy,hal_memcmp, andhal_memsetrespectively. This change is added around line 32.
- Added a line to the
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
-
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. ↩
|
Nice solution! Does it work? 😄 Have you tried e.g. assigning struct to force use of memcpy? |
There was a problem hiding this comment.
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:
memcpyOverlap Safety: Thehal_memcpyimplementation foria32(and potentiallyriscv64) isn't safe for overlapping memory regions. Aliasingmemcpyto it can lead to data corruption. Either make allhal_memcpyinstances overlap-safe or provide and alias a separate, overlap-safememmove(and itshal_memmoveimplementation).
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.
| 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 |
There was a problem hiding this comment.
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
memcpyhas undefined behavior for overlapping source/destination regions.memmoveshould be used when overlap is possible. - The
aarch64hal_memcpy(inhal/aarch64/_memcpy.S) handles overlaps, acting likememmove. However, theia32hal_memcpy(inhal/ia32/string.c) uses a forward-only copy (rep movsl/movsbwithcld), which is not safe for all overlap scenarios (e.g.,dst > src). Theriscv64hal_memcpyalso doesn't explicitly guarantee overlap safety. - Aliasing
memcpyto ahal_memcpythat isn't universally overlap-safe (especially onia32) introduces a high risk of data corruption if the compiler generatesmemcpycalls or if developers usememcpyfor operations that might overlap.
To address this, consider these options:
-
Option A (Make
hal_memcpyuniversally overlap-safe):
Update allhal_memcpyimplementations (especially foria32andriscv64, if affected) to handle overlaps correctly, similar tomemmove. If this is done, consider aliasingmemmoveto this now-safehal_memcpy:
LDFLAGS += -Wl,--defsym=memcpy=hal_memcpy,--defsym=memcmp=hal_memcmp,--defsym=memset=hal_memset,--defsym=memmove=hal_memcpy
(Assuminghal_memcpybecomes fully overlap-safe on all platforms). -
Option B (Provide a separate, safe
hal_memmove- Recommended):
Keephal_memcpynon-overlap-safe. Provide a separate, overlap-safehal_memmovefunction for all architectures, and aliasmemmoveto it. This is crucial because code might rely onmemmovefor 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 requireshal_memmoveto 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
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 :) |
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
How Has This Been Tested?
Checklist:
Special treatment