Skip to content

Commit e91a12c

Browse files
committed
Separate class salts for separate classes
1 parent 302fcb8 commit e91a12c

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

spec/truffle/launcher_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def should_print_full_java_command(options, env: {})
315315
out = ruby_exe("puts 14.hash", options: "--experimental-options --hashing-deterministic", args: "2>&1")
316316
check_status_or_print(out)
317317
out.should include("SEVERE: deterministic hashing is enabled - this may make you vulnerable to denial of service attacks")
318-
out.should include("7141275149799654099")
318+
out.should include("3412061130696242594")
319319
end
320320

321321
it "prints help containing runtime options" do

src/main/java/org/truffleruby/core/hash/HashOperations.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,25 @@ public static boolean verifyStore(RubyContext context, RubyHash hash) {
133133
return true;
134134
}
135135

136-
public static final int CLASS_SALT = 55927484; // random number, stops hashes for similar values but different classes being the same, static because we want deterministic hashes
136+
// random number, stops hashes for similar values but different classes being the same, static because we want deterministic hashes
137+
public static final int BOOLEAN_CLASS_SALT = 55927484;
138+
public static final int INTEGER_CLASS_SALT = 1028093337;
139+
public static final int DOUBLE_CLASS_SALT = -1611229937;
137140

138141
public static long hashBoolean(boolean value, RubyContext context, RubyBaseNode node) {
139-
return context.getHashing(node).hash(CLASS_SALT, Boolean.hashCode(value));
142+
return context.getHashing(node).hash(BOOLEAN_CLASS_SALT, Boolean.hashCode(value));
140143
}
141144

142145
public static long hashLong(long value, RubyContext context, RubyBaseNode node) {
143-
return context.getHashing(node).hash(CLASS_SALT, value);
146+
return context.getHashing(node).hash(INTEGER_CLASS_SALT, value);
144147
}
145148

146149
public static long hashDouble(double value, RubyContext context, RubyBaseNode node) {
147-
return context.getHashing(node).hash(CLASS_SALT, Double.doubleToRawLongBits(value));
150+
return context.getHashing(node).hash(DOUBLE_CLASS_SALT, Double.doubleToRawLongBits(value));
148151
}
149152

150153
public static long hashBignum(RubyBignum value, RubyContext context, RubyBaseNode node) {
151-
return context.getHashing(node).hash(CLASS_SALT, BigIntegerOps.hashCode(value));
154+
return context.getHashing(node).hash(INTEGER_CLASS_SALT, BigIntegerOps.hashCode(value));
152155
}
153156

154157
}

0 commit comments

Comments
 (0)