News for hackers. Stuff that matters.
Re:CoreCLR is now open source by benreic on Tuesday February 03, 2015 @01:25PM (#8992139)
Quick link to what I think is the most interesting class in the CLR:

https://github.com/dotnet/coreclr/blob/master/src/mscorlib/s...

Re:CoreCLR is now open source by munificent on Tuesday February 03, 2015 @03:40PM (#8993202)
Since I wrote a string hash code function recently[1], I was interested to see what they use. Like many, they're using[2] the venerable djb hash. That sent me down a rabbit hole where I discovered that Bernstein was 19 when he wrote about it. Wow.

[1]: https://github.com/munificent/wren/blob/master/src/wren_valu...

[2]: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/s...

Re:CoreCLR is now open source by simfoo on Tuesday February 03, 2015 @01:30PM (#8992183)
From their GetHashCode():

// We want to ensure we can change our hash function daily.

// This is perfectly fine as long as you don't persist the

// value from GetHashCode to disk or count on String A

// hashing before string B. Those are bugs in your code.

hash1 ^= ThisAssembly.DailyBuildNumber;

I'd love to hear the story behind this one :D

Re:CoreCLR is now open source by daeken on Tuesday February 03, 2015 @01:37PM (#8992241)
I don't know the story, but the logic behind it is simple: If you want to guarantee no one depends on GetHashCode staying static between runs of an application, change it all the time.
Re:CoreCLR is now open source by evincarofautumn on Tuesday February 03, 2015 @04:53PM (#8993673)
But now I can circumvent this hack by xoring with ThisAssembly.DailyBuildNumber again. :)
Re:CoreCLR is now open source by rplnt on Wednesday February 04, 2015 @10:26AM (#8997297)
I think the story is even simpler than that as the code in question is prefaced with: #if DEBUG

The shipped product doesn't include this "randomness".

Re:CoreCLR is now open source by MichaelGG on Tuesday February 03, 2015 @02:08PM (#8992484)
Also is it not for hash collision protection, which can end up hurting the runtime of many algorithms, causing a DoS?
Re:CoreCLR is now open source by plorkyeran on Tuesday February 03, 2015 @02:17PM (#8992567)
No, it has no effect on hash collisions. Two hashes that are equal before being xored with the daily build number will still be equal afterwards.
Re:CoreCLR is now open source by porges on Tuesday February 03, 2015 @02:27PM (#8992657)
It won't help much there as the number is static for a particular build.
Re:CoreCLR is now open source by munificent on Tuesday February 03, 2015 @03:37PM (#8993172)
The hash randomization for security is above this part of the code.
Re:CoreCLR is now open source by gus_massa on Tuesday February 03, 2015 @02:15PM (#8992550)
IIRC, a few yeas ago appeared a denial of service attack, probably originally for Phyton, but it was ported son to other languages.

The idea is that the hash is good enough for normal list, but it's not a cryptographic hash and it's easy to find collisions. Then you can make a lot of requests with strings that has the same hash value. Now the hash operations are O(N) instead of O(~1) and everything is slower.

Using an unpredictable hash calculation makes this attack more difficult.

Re:CoreCLR is now open source by omaranto on Tuesday February 03, 2015 @03:28PM (#8993112)
Your typo can be easily fixed by a Python one-liner:

    >>> (lambda w:w[2:]+w[:2])(''.join(sorted("Phyton",key=lambda c:math.sin(ord(c)^50))))
    'Python'
Re:CoreCLR is now open source by euroclydon on Tuesday February 03, 2015 @01:39PM (#8992253)
It's in a #if DEBUG statement so it would not change. Historically, even if they shipped the debug symbols, the assembly would have been built in release. Now, I suppose you could build it in debug.
Re:CoreCLR is now open source by Aleman360 on Tuesday February 03, 2015 @01:56PM (#8992392)
The Reference Source site is much easier to navigate. Method names are hyperlinks:

http://referencesource.microsoft.com/#mscorlib/system/string...

Re:CoreCLR is now open source by sekasi on Tuesday February 03, 2015 @07:05PM (#8994459)
Admittedly, I've never been big in C#, but I'm AMAZED by how much repetition string comparison there is.

IE: Environment.GetResourceString("ArgumentOutOfRange_Index")

The string there is in multiple areas of that class, and the same behavior is displayed for all of them. Wouldn't logic suggest everything such as above would be moved in to a constant repository for clarity and also less potential human error for future additions?

Re:CoreCLR is now open source by Rapzid on Tuesday February 03, 2015 @08:06PM (#8994705)
It's pretty common in C# to use Constants to represent strings. Cuts down on the repetition as you said and you get the Intellisense too. I'd be curios to know the reasoning for using the literals as well.
Re:CoreCLR is now open source by mike_hearn on Wednesday February 04, 2015 @12:13PM (#8998041)
And for comparison:

http://grepcode.com/file/repository.grepcode.com/java/root/j...

It's sort of surprising to me how much huger the .NET version is, in terms of code. Virtually all the lines in the Java version are API docs. The .NET version doesn't seem to have them (they must be elsewhere?) but it does have a lot more code and that code is much lower level.

Not sure what that means, if anything, but it's interesting.

Re:CoreCLR is now open source by DoggettCK on Tuesday February 03, 2015 @03:09PM (#8992958)
That does answer an unanswered question I had on SO about string hashing. If strings are immutable, why isn't the hash code memoized? Seems like it would make HashSet/Dictionary lookups using string keys much faster.
Re:CoreCLR is now open source by munificent on Tuesday February 03, 2015 @03:38PM (#8993180)
Presumably because it's not worth the memory hit to store the hash.
Re:CoreCLR is now open source by DoggettCK on Tuesday February 03, 2015 @03:47PM (#8993237)
That was my assumption, too. They do memoize the length, but I'm sure those bytes add up, having run into OutOfMemoryExceptions building huge amounts of strings before.
Re:CoreCLR is now open source by reddiric on Tuesday February 03, 2015 @04:00PM (#8993327)
Strings know their length in the CLR because they are represented as BSTRs

http://blogs.msdn.com/b/ericlippert/archive/2011/07/19/strin...

This lets them interoperate with OLE Automation.

Re:CoreCLR is now open source by porges on Tuesday February 03, 2015 @08:39PM (#8994837)
You have to store the length, C# strings can contain '\0' (although the hashing code doesn't take this into account!)