Skip to content

Instantly share code, notes, and snippets.

@rickhull
Last active March 4, 2024 21:09
Show Gist options
  • Save rickhull/59774592dbaf12d47fa53d0c29c13e6d to your computer and use it in GitHub Desktop.
Save rickhull/59774592dbaf12d47fa53d0c29c13e6d to your computer and use it in GitHub Desktop.
reproduce Digest::MD5 thread-safe bug
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ ruby --version
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ ruby test_digest.rb
Run options: --seed 50097
# Running:
..........
Finished in 0.001157s, 8640.1824 runs/s, 0.0000 assertions/s.
10 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ COUNT=5 ruby test_digest.rb
Run options: --seed 64037
# Running:
.....
Finished in 0.001348s, 3708.1535 runs/s, 0.0000 assertions/s.
5 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ COUNT=55 ruby test_digest.rb
Run options: --seed 64079
# Running:
.......................................................
Finished in 0.002710s, 20291.7500 runs/s, 0.0000 assertions/s.
55 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 ruby test_digest.rb
Run options: --seed 18926
# Running:
EE.......E
Finished in 0.003147s, 3177.9789 runs/s, 0.0000 assertions/s.
1) Error:
Digest::MD5 misbehavior#test_0007_might raise Digest::Base cannot be directly inherited in Ruby:
RuntimeError: Digest::Base cannot be directly inherited in Ruby
test_digest.rb:23:in `digest'
test_digest.rb:23:in `hexdigest'
test_digest.rb:23:in `block (3 levels) in <main>'
2) Error:
Digest::MD5 misbehavior#test_0010_might raise Digest::Base cannot be directly inherited in Ruby:
RuntimeError: Digest::Base cannot be directly inherited in Ruby
test_digest.rb:23:in `digest'
test_digest.rb:23:in `hexdigest'
test_digest.rb:23:in `block (3 levels) in <main>'
3) Error:
Digest::MD5 misbehavior#test_0004_might raise Digest::Base cannot be directly inherited in Ruby:
RuntimeError: Digest::Base cannot be directly inherited in Ruby
test_digest.rb:23:in `digest'
test_digest.rb:23:in `hexdigest'
test_digest.rb:23:in `block (3 levels) in <main>'
10 runs, 0 assertions, 0 failures, 3 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 ruby test_digest.rb
Run options: --seed 3185
# Running:
E.........
Finished in 0.001733s, 5770.0903 runs/s, 0.0000 assertions/s.
1) Error:
Digest::MD5 misbehavior#test_0007_might raise Digest::Base cannot be directly inherited in Ruby:
RuntimeError: Digest::Base cannot be directly inherited in Ruby
test_digest.rb:23:in `digest'
test_digest.rb:23:in `hexdigest'
test_digest.rb:23:in `block (3 levels) in <main>'
10 runs, 0 assertions, 0 failures, 1 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 ruby test_digest.rb
Run options: --seed 1789
# Running:
E.........
Finished in 0.002380s, 4200.8600 runs/s, 0.0000 assertions/s.
1) Error:
Digest::MD5 misbehavior#test_0006_might raise Digest::Base cannot be directly inherited in Ruby:
RuntimeError: Digest::Base cannot be directly inherited in Ruby
test_digest.rb:23:in `digest'
test_digest.rb:23:in `hexdigest'
test_digest.rb:23:in `block (3 levels) in <main>'
10 runs, 0 assertions, 0 failures, 1 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 COUNT=1 ruby test_digest.rb
Run options: --seed 57501
# Running:
.
Finished in 0.001273s, 785.6187 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 COUNT=1 ruby test_digest.rb
Run options: --seed 53694
# Running:
.
Finished in 0.000906s, 1103.8905 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 COUNT=1 ruby test_digest.rb
Run options: --seed 9945
# Running:
.
Finished in 0.000945s, 1058.1049 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 COUNT=1 ruby test_digest.rb
Run options: --seed 39995
# Running:
.
Finished in 0.000885s, 1129.3208 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 COUNT=1 ruby test_digest.rb
Run options: --seed 18290
# Running:
.
Finished in 0.001245s, 802.9665 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 COUNT=2 ruby test_digest.rb
Run options: --seed 41680
# Running:
E.
Finished in 0.001340s, 1492.7812 runs/s, 0.0000 assertions/s.
1) Error:
Digest::MD5 misbehavior#test_0001_might raise Digest::Base cannot be directly inherited in Ruby:
RuntimeError: Digest::Base cannot be directly inherited in Ruby
test_digest.rb:23:in `digest'
test_digest.rb:23:in `hexdigest'
test_digest.rb:23:in `block (3 levels) in <main>'
2 runs, 0 assertions, 0 failures, 1 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 COUNT=2 ruby test_digest.rb
Run options: --seed 52566
# Running:
E.
Finished in 0.001202s, 1663.7787 runs/s, 0.0000 assertions/s.
1) Error:
Digest::MD5 misbehavior#test_0002_might raise Digest::Base cannot be directly inherited in Ruby:
RuntimeError: Digest::Base cannot be directly inherited in Ruby
test_digest.rb:23:in `digest'
test_digest.rb:23:in `hexdigest'
test_digest.rb:23:in `block (3 levels) in <main>'
2 runs, 0 assertions, 0 failures, 1 errors, 0 skips
[rwh@nixos:~/code/59774592dbaf12d47fa53d0c29c13e6d]$ PARME=1 COUNT=2 ruby test_digest.rb
Run options: --seed 20339
# Running:
E.
Finished in 0.001406s, 1422.0908 runs/s, 0.0000 assertions/s.
1) Error:
Digest::MD5 misbehavior#test_0002_might raise Digest::Base cannot be directly inherited in Ruby:
RuntimeError: Digest::Base cannot be directly inherited in Ruby
test_digest.rb:23:in `digest'
test_digest.rb:23:in `hexdigest'
test_digest.rb:23:in `block (3 levels) in <main>'
2 runs, 0 assertions, 0 failures, 1 errors, 0 skips
require 'minitest/autorun'
require 'digest'
# Everything below about MD5 applies equally to SHA1, RMD160, etc.
# There are two known ways to get a reference to Digest::MD5, safely:
# 1. require 'digest/md5' # (and not just 'digest')
# 2. klass = Digest('MD5') # (with require 'digest')
#
# On my Ruby 3.2.2, in the presence of threads, when only requiring 'digest'
# (and not 'digest/md5'), a RuntimeError may be raised:
RUNTIME_ERROR = 'Digest::Base cannot be directly inherited in Ruby'
parme = ENV['PARME'].to_s.match /1/
count = ENV['COUNT'].to_i
count = 10 if count <= 0
describe "Digest::MD5 misbehavior" do
parallelize_me! if parme
count.times {
it "might raise #{RUNTIME_ERROR}" do
Digest::MD5.hexdigest('asdf')
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment