Skip to content

fix: set correct argc=0 in _start() function#1211

Merged
xushiwei merged 1 commit intogoplus:mainfrom
luoliwoshang:emb/start
Aug 19, 2025
Merged

fix: set correct argc=0 in _start() function#1211
xushiwei merged 1 commit intogoplus:mainfrom
luoliwoshang:emb/start

Conversation

@luoliwoshang
Copy link
Member

@luoliwoshang luoliwoshang commented Aug 12, 2025

The original code used icmp eq i32 0, 0 which always evaluates to true, resulting in argc=1:

 declare i32 @printf(i8*, ...)
  @fmt = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1

  define i32 @main() {
  entry:
    %argc_val = icmp eq i32 0, 0
    %argc = zext i1 %argc_val to i32
    %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @fmt, i64 0, i64 0), i32 %argc)
    ret i32 0
  }

Output: 1 (incorrect)

After fix with add i32 0, 0, argc correctly equals 0.

@codecov
Copy link

codecov bot commented Aug 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.94%. Comparing base (f43e2c0) to head (b9fdc2a).
⚠️ Report is 19 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1211   +/-   ##
=======================================
  Coverage   87.93%   87.94%           
=======================================
  Files          32       32           
  Lines        7653     7657    +4     
=======================================
+ Hits         6730     6734    +4     
  Misses        844      844           
  Partials       79       79           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cpunion
Copy link
Collaborator

cpunion commented Aug 12, 2025

Fixing argv instead of argc. argc should be greater than or equal to 1, and argv[0] should be the executable path.

@luoliwoshang
Copy link
Member Author

argc should be greater than or equal to 1, and argv[0] should be the executable path.

I found a few challenges:

  1. Executable path cannot be determined at compile time
  2. LLVM cannot dynamically retrieve this path - it maybe requires OS-specific syscalls

So i think the most reasonable solution might be setting argc = 1 and argv[0] to an empty string to at least comply with the C standard minimum requirements, rather than the current argc = 0, argv = null.

@cpunion
Copy link
Collaborator

cpunion commented Aug 15, 2025

I found a few challenges:

  1. Executable path cannot be determined at compile time
  2. LLVM cannot dynamically retrieve this path - it maybe requires OS-specific syscalls

So i think the most reasonable solution might be setting argc = 1 and argv[0] to an empty string to at least comply with the C standard minimum requirements, rather than the current argc = 0, argv = null.

I agree with your solution.

@xushiwei xushiwei merged commit ff40a80 into goplus:main Aug 19, 2025
34 checks passed
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.

3 participants