[PATCH] D90714: [clang]Fix length threshold for MicrosoftMangle md5 hash

David Blaikie via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 14:30:10 PST 2020


dblaikie added a comment.

In D90714#2377231 <https://reviews.llvm.org/D90714#2377231>, @mibintc wrote:

> I'm sorry, I don't see how to build up an identifier with an arbitrary number of characters using the token pasting, for example an ident with 4095 characters is not a power of 2. I tried pasting together X2048 X1024 X512 etc but that doesn't work
>
>   fu.cpp:12:18: error: pasting ")" and "X16" does not give a valid preprocessing token
>    #define FUN X32(x) ## X16(x)
>                     ^
>   fu.cpp:13:1: note: in expansion of macro ‘FUN’

Ah, yeah, that's the awkward thing where you have to indirect macro expansions before concatenation (some discussion here <https://wiki.sei.cmu.edu/confluence/display/c/PRE05-C.+Understand+macro+replacement+when+concatenating+tokens+or+performing+stringification>, though couldn't find the perfect reference/discussion on the issue).

Essentially you've got to add an extra level of macro indirection to be able to concatenate like that, eg:

  #define FUN_HELPER(x, y) x ## y
  #define FUN FUN_HELPER(X32(x), X16(x))



> I want the test case to show the transition between when md5 is not needed and when it's needed, i want to build strings that might not be power of root string.  I could add more comments to the test case, perhaps, "the strlen of the Microsoft mangled name is X" for the one case, and "the strlen of ... is X+1" for the other.

They're really non-obvious that those particular mangled names produce names that are exactly a certain length, even with a comment that seems pretty subtle compared to a more direct construction.

Let's see how to build a 4095 length function name... bit more code, but I think fairly legible: https://godbolt.org/z/Tbv6zz

  #define STR_(X) #X
  #define STR(X) STR_(X)
  
  #define C_(P1, P2) P1##P2
  #define C2(P1, P2) C_(P1, P2)
  #define C4(P1, P2, P3, P4) C2(C2(P1, P2), C2(P3, P4))
  
  #define X2(X) C2(X, X)
  #define X4(X) X2(X2(X))
  #define X8(X) X2(X4(X))
  #define X16(X) X2(X8(X))
  #define X32(X) X2(X16(X))
  #define X64(X) X2(X32(X))
  #define X128(X) X2(X64(X))
  #define X256(X) X2(X128(X))
  #define X512(X) X2(X256(X))
  #define X1024(X) X2(X512(X))
  #define X2048(X) X2(X1024(X))
  #define X4096(X) X2(X2048(X))
  
  #define X4095(X)                              \
    C2(C2(                                      \
      C4(X,       X2(X),   X4(X),    X8(X)),    \
      C4(X16(X),  X32(X),  X64(X),   X128(X))), \
      C4(X256(X), X512(X), X1024(X), X2048(X)))
  
  void X4096(a)() {}
  
  extern int a[4096];
  int a[sizeof(STR(X4096(a))) - 1];
  
  void X4095(b)() {}
  
  extern int b[4095];
  int b[sizeof(STR(X4095(b))) - 1];


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90714/new/

https://reviews.llvm.org/D90714



More information about the cfe-commits mailing list