Giter Club home page Giter Club logo

Comments (13)

sjaeckel avatar sjaeckel commented on June 30, 2024

as far as I can see you're using 1.16
[edit]but you seem to have modified some files. e.g. the "attribute((always_inline))" isn't in the sources of the standard 1.16 tomcrypt_macros.c[/edit]

from libtomcrypt.

wolfman2000 avatar wolfman2000 commented on June 30, 2024

I wasn't the one that made those attribute changes. Still...good to see that there's something.

Does the latest version use those attributes as well?

from libtomcrypt.

donaldguy avatar donaldguy commented on June 30, 2024

I am presently attempting to get Stempania 3.9 to build on Lion (which is more of a challenge, I suspect) and am seeing this error. Though extra bizarrely I seem to only see it when I execute the build from the XCode GUI. If I use xcodebuild from the command line it seems to build cleanly (though I haven't gotten the rest to build enough to have any hope of testing linking yet).

from libtomcrypt.

donaldguy avatar donaldguy commented on June 30, 2024

It seems like the difference, at least for 3.9 may be that the GUI seems to be hitting the "Debug" configuration, whereas xcodebuild is using the "Native" configuration. I have't looked into it deeper than that.

from libtomcrypt.

sporksmith avatar sporksmith commented on June 30, 2024

I ran into this issue as well. The workaround I ended up doing was to add #define LTC_NO_ROLC to tomcrypt_custom.h.

I think the issue is that the ROLc and RORc functions attempt to emit rotate instructions with a hard-coded literal, from the parameter i. This is what the asm constraint "I" means. Since the functions are specified as inline, the compiler could end up replacing the (i) parameter in the asm with a literal, in which case it works fine.

ROLc is defined as:

static inline unsigned ROLc(unsigned word, const int i)
{
   asm ("roll %2,%0"
      :"=r" (word)
      :"0" (word),"I" (i));
   return word;
}

I believe the intent is for something like y = ROLc(x, 3) to get rewritten as follows before getting handed off to the assembler:

asm ("roll %2,%0"
  :"=r" (x)
  :"0" (x),"I" (3));
y = x;

However, if the function doesn't get inlined, or the compiler doesn't optimize (i) to a literal constant as a result of inlining, then the constraint "I" will indeed be invalid.

It sounds like someone tried to address this issue in your codebase by forcing the compiler to always inline. Unfortunately this won't work if the attribute gets ignored, or if the compiler doesn't do the further optimization of replacing (i) with the literal parameter.

I think that the most portable fix would be to make ROLc and RORc macros instead of functions.

from libtomcrypt.

sporksmith avatar sporksmith commented on June 30, 2024

I thought I'd go ahead and give a quick try for redefining ROLc and RORc as macros. The tricky part is that an asm block is a statement, not an expression. The only way I can think to do it is using statement expressions, which are supported by both gcc and clang, but are not ansi c.

We also need to copy the parameter to avoid changing its value and to handle the case where the parameter isn't an lvalue.

Anyways, the following compiles, but I haven't tested for correctness:

#define ROLc(word, i)                           \                                                            
  ({ unsigned __rotd;                           \                                                            
    __rotd = (word);                            \                                                            
    asm ("roll %2,%0"                           \                                                                     :"=r" (__rotd)                         \                                                            
         :"0" (__rotd),"I" (i));                \                                                            
    __rotd; })                                                                                               

#define RORc(word, i)                           \                                                            
  ({ unsigned __rotd;                           \                                                            
    __rotd = (word);                            \                                                            
    asm ("rorl %2,%0"                           \                                                            
         :"=r" (__rotd)                         \
         :"0" (__rotd),"I" (i));                \
    __rotd; })

from libtomcrypt.

rofl0r avatar rofl0r commented on June 30, 2024

how about using the old trick: do { code_here(); } while(0)

from libtomcrypt.

sporksmith avatar sporksmith commented on June 30, 2024

how about using the old trick: do { code_here(); } while(0)

That trick only works to replace a function that returns void. This one needs to return the rotated value. Here's an example call site from aes.c:

rk[12] = rk[ 4] ^ setup_mix(RORc(temp, 8));

(edited for clarification)

from libtomcrypt.

sporksmith avatar sporksmith commented on June 30, 2024

Incidentally, I think the reason I'm hitting this bug is because I'm compiling with -O0, causing gcc to skip the inlining. I'm including tomcrypt as part of a larger project build, from which the tomcrypt build inherits some CFLAGS. i.e., the bug can be replicated with gcc on the stock code base by:

make CFLAGS="-O0 -I./src/headers -DLTC_SOURCE"

from libtomcrypt.

sjaeckel avatar sjaeckel commented on June 30, 2024

@sporksmith did you try to define the function with __attribute__((always_inline))?

from libtomcrypt.

sporksmith avatar sporksmith commented on June 30, 2024

I don't remember whether we tried __attribute__((always_inline)) or not. I imagine it would work, if gcc honors that attribute even at -O0.

We ended up solving it by just not having the tomcrypt build inherit the CFLAGS from the project build. I.e., let tomcrypt compile at -O2, in which case the functions gets inlined and this bug doesn't surface.

from libtomcrypt.

MMI avatar MMI commented on June 30, 2024

Proposed solution in this pull request

from libtomcrypt.

karel-m avatar karel-m commented on June 30, 2024

pull/125 was closed, can we also close this issue?

from libtomcrypt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.