LADSoft / OrangeC

OrangeC Compiler And Tool Chain
http://ladsoft.tripod.com/orange_c_compiler.html
Other
284 stars 39 forks source link

Incorrect program behavior at /O2 vs. /O1 #953

Closed johnsonjh closed 10 months ago

johnsonjh commented 10 months ago

Greetings.

This is a rather complex issue, and I've not yet been able to make a minimal reproducer, and for that I apologise.

It also complicates things that my regular test environment is non-trivial the tests are normally executing in Wine on Linux, but I'm going to post what I know here now, because it is reproducible on a pure Windows environment as well.

Using libsir, commit a34764334b9c6cca5fe78c09ab201abdec0c1d60:

You'll see the test failure. This failure can be reproduced in isolation with the --only filesystem flag to our test harness:

libsir 2.2.3-dev (prerelease) running 1 test...

(1/1) 'filesystem'...

        Running under Wine: 8.14
        _sir_getcwd: 'Z:\home\user\libsir'
        _sir_getappfilename: 'Z:\home\user\libsir\build\bin\sirtests.exe'
        _sir_getbasename: 'sirtests.exe'
        _sir_getappdir: 'Z:\home\user\libsir\build\bin'
        _sir_getdirname: 'Z:\home\user\libsir\build\bin'
        _sir_getdirname('') = '.'
        _sir_getdirname('C:\foo') = 'C:\foo'
        _sir_getdirname('C:\foo') = 'C:\foo'
        _sir_getdirname('C:\foo\bar\bad:') = 'C:\foo\bar\bad:'
        _sir_getdirname('C:\') = 'C:\'
        _sir_getdirname('') = ''
        _sir_getdirname('') = '.'
        _sir_getbasename('foo\bar\file.with.many.full.stops') = 'file.with.many.full.stops'
        _sir_getbasename('C:\foo\bar\poorly-renamed.txt.pdf') = 'poorly-renamed.txt.pdf'
        _sir_getbasename('C:\foo\bar\illegal>filename.txt') = 'illegal>filename.txt'
        _sir_getbasename('C:\') = 'C:\'
        _sir_getbasename('\Program Files\foo.bar') = 'foo.bar'
        _sir_getbasename('') = '.'
        _sir_ispathrelative('this/is/relative') = true
        _sir_ispathrelative('relative') = true
        _sir_ispathrelative('./relative') = true
        _sir_ispathrelative('../../relative') = true
        _sir_ispathrelative('D:\absolute') = false
        _sir_ispathrelative('C:\Program Files\FooBar') = false
        _sir_ispathrelative('C:\') = false
        _sir_ispathrelative('\absolute') = false
        _sir_pathexists('../foobarbaz') = false
        _sir_pathexists('foobarbaz') = false
        _sir_pathexists('C:\Windows') = true
        _sir_pathexists('C:\Program Files') = true
        _sir_pathexists('\') = true
        _sir_pathexists('.\') = true
        _sir_pathexists('..\') = true
        _sir_pathexists('../../LICENSES/MIT.txt') = true
        _sir_pathexists('@B') = false
        _sir_pathexists('./') = true
        _sir_pathexists('') = false
        !! Unexpected (9, Error in __sir_validstr (include\sir\helpers.h:273): 'Invalid string argument')
        _sir_pathexists('file.exists') = true
        _sir_validfd(0) = false
        _sir_validfd(1) = false
        _sir_validfd(2) = false
        _sir_validfd(0) = false
        _sir_validfd(3) = true

(1/1) 'filesystem' finished: FAIL

done: 1 of 1 libsir test failed in 0.006sec

Failed test:

        - filesystem

Recompiling the source and using /O1 instead of /O2 resolve the issue:

(1/1) 'filesystem'...

    Running under Wine: 8.14
    _sir_getcwd: 'Z:\home\user\libsir'
    _sir_getappfilename: 'Z:\home\user\libsir\build\bin\sirtests.exe'
    _sir_getbasename: 'sirtests.exe'
    _sir_getappdir: 'Z:\home\user\libsir\build\bin'
    _sir_getdirname: 'Z:\home\user\libsir\build\bin'
    _sir_getdirname('C:\') = 'C:\'
    _sir_getdirname('C:\foo') = 'C:\foo'
    _sir_getdirname('C:\foo') = 'C:\foo'
    _sir_getdirname('C:\foo\bar\bad:') = 'C:\foo\bar\bad:'
    _sir_getdirname('C:\') = 'C:\'
    _sir_getdirname('') = ''
    _sir_getdirname('') = '.'
    _sir_getbasename('foo\bar\file.with.many.full.stops') = 'file.with.many.full.stops'
    _sir_getbasename('C:\foo\bar\poorly-renamed.txt.pdf') = 'poorly-renamed.txt.pdf'
    _sir_getbasename('C:\foo\bar\illegal>filename.txt') = 'illegal>filename.txt'
    _sir_getbasename('C:\') = 'C:\'
    _sir_getbasename('\Program Files\foo.bar') = 'foo.bar'
    _sir_getbasename('') = '.'
    _sir_ispathrelative('this/is/relative') = true
    _sir_ispathrelative('relative') = true
    _sir_ispathrelative('./relative') = true
    _sir_ispathrelative('../../relative') = true
    _sir_ispathrelative('D:\absolute') = false
    _sir_ispathrelative('C:\Program Files\FooBar') = false
    _sir_ispathrelative('C:\') = false
    _sir_ispathrelative('\absolute') = false
    _sir_pathexists('../foobarbaz') = false
    _sir_pathexists('foobarbaz') = false
    _sir_pathexists('C:\Windows') = true
    _sir_pathexists('C:\Program Files') = true
    _sir_pathexists('\') = true
    _sir_pathexists('.\') = true
    _sir_pathexists('..\') = true
    _sir_pathexists('../../LICENSES/MIT.txt') = true
    _sir_pathexists('../../msvs/libsir.sln') = true
    _sir_pathexists('./') = true
    _sir_pathexists('../') = true
    _sir_pathexists('file.exists') = true
    _sir_validfd(0) = false
    _sir_validfd(1) = false
    _sir_validfd(2) = false
    _sir_validfd(0) = false
    _sir_validfd(3) = true

(1/1) 'filesystem' finished: PASS

done: 1 libsir test passed in 0.006sec!



I must mention that the compiler flag difference of `/O2` vs. `/O1` is enough to reproduce. I hope I'm not misunderstanding the flags, but I would *think* I am *disabling* those individual optimizations against /O2 (*i.e.*, starting with /O2 and then removing optimizations), but it seems that I am either mistaken, or something else in play here.

This is using  `occ (OrangeC) Version 6.0.71.1`.  

I can reproduce this on Windows, so it's not a problem with the Linux environment.  I also should mention that I can change some (unrelated) code and the failing strings (like `'@B'` which comes from nowhere in our code ) *do* change, so something is getting stepped in program memory somewhere when using `/O2` builds vs. `/O1` or `/O0`.
LADSoft commented 10 months ago

this is just a little more difficult and I'm working on #603 tonite, will get to this within the next couple of days...

johnsonjh commented 10 months ago

Let me know if you need any help reproducing - we have OrangeC as part of our CI process (see https://gitlab.com/libsir/libsir/-/pipelines/994055215) so it gets exercised a lot :)

LADSoft commented 10 months ago

your instructions for reproducing it were on target so it was easy to reproduce... I 'm working on a fix.

That is cool you use OrangeC as part of your CI process :smile:

johnsonjh commented 10 months ago

@LADSoft Thanks for the quick fix and I'm glad it turned out to be something simple!

LADSoft commented 10 months ago

well actually I have some luck in that a lot of the fixes I do are easy... but it can sometime be difficult honing in on exactly what needs to be fixed lol...