Skip to content

Support digest plugins #3822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions lib/cext/include/ruby/digest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/************************************************

digest.h - header file for ruby digest modules

$Author$
created at: Fri May 25 08:54:56 JST 2001


Copyright (C) 2001-2006 Akinori MUSHA

$RoughId: digest.h,v 1.3 2001/07/13 15:38:27 knu Exp $
$Id$

************************************************/

#include "ruby.h"

#define RUBY_DIGEST_API_VERSION 3

typedef int (*rb_digest_hash_init_func_t)(void *);
typedef void (*rb_digest_hash_update_func_t)(void *, unsigned char *, size_t);
typedef int (*rb_digest_hash_finish_func_t)(void *, unsigned char *);

typedef struct {
int api_version;
size_t digest_len;
size_t block_len;
size_t ctx_size;
rb_digest_hash_init_func_t init_func;
rb_digest_hash_update_func_t update_func;
rb_digest_hash_finish_func_t finish_func;
} rb_digest_metadata_t;

#define DEFINE_UPDATE_FUNC_FOR_UINT(name) \
void \
rb_digest_##name##_update(void *ctx, unsigned char *ptr, size_t size) \
{ \
const unsigned int stride = 16384; \
\
for (; size > stride; size -= stride, ptr += stride) { \
name##_Update(ctx, ptr, stride); \
} \
/* Since size <= stride, size should fit into an unsigned int */ \
if (size > 0) name##_Update(ctx, ptr, (unsigned int)size); \
}

#define DEFINE_FINISH_FUNC_FROM_FINAL(name) \
int \
rb_digest_##name##_finish(void *ctx, unsigned char *ptr) \
{ \
return name##_Final(ptr, ctx); \
}

static inline VALUE
rb_digest_namespace(void)
{
rb_require("digest");
return rb_path2class("Digest");
}

static inline ID
rb_id_metadata(void)
{
return rb_intern_const("metadata");
}

static inline VALUE
rb_digest_make_metadata(const rb_digest_metadata_t *meta)
{
#undef RUBY_UNTYPED_DATA_WARNING
#define RUBY_UNTYPED_DATA_WARNING 0
return rb_obj_freeze(Data_Wrap_Struct(0, 0, 0, (void *)meta));
}
2 changes: 1 addition & 1 deletion lib/cext/include/truffleruby/truffleruby-abi-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
// $RUBY_VERSION must be the same as TruffleRuby.LANGUAGE_VERSION.
// $ABI_NUMBER starts at 1 and is incremented for every ABI-incompatible change.

#define TRUFFLERUBY_ABI_VERSION "3.3.7.2"
#define TRUFFLERUBY_ABI_VERSION "3.3.7.3"

#endif
80 changes: 80 additions & 0 deletions lib/truffle/digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
# under LICENSE.RUBY as it is derived from lib/ruby/stdlib/digest.rb.

require_relative 'digest/version'
require_relative 'ffi'

module Truffle
class Digest
module Foreign
class RbDigestMetadata < ::FFI::Struct
layout :api_version, :int,
:digest_len, :size_t,
:block_len, :size_t,
:ctx_size, :size_t,
:init_func, ::FFI::FunctionType.new(:int, [:pointer]),
:update_func, ::FFI::FunctionType.new(:void, [:pointer, :pointer, :size_t]),
:finish_func, ::FFI::FunctionType.new(:int, [:pointer, :pointer])
end
end
end
end


module Digest

Expand Down Expand Up @@ -170,6 +188,12 @@ def self.hexdigest(str, *args)
end

class Base < ::Digest::Class
def self.inherited(klass)
unless %w[Digest::MD5 Digest::SHA1 Digest::SHA2 Digest::SHA256 Digest::SHA384 Digest::SHA512].include?(klass.name)
klass.include(Digest::Plugin)
end
end

def block_length
Truffle::Digest.digest_block_length @digest
end
Expand All @@ -186,9 +210,65 @@ def reset
def update(str)
str = StringValue(str)
Truffle::Digest.update(@digest, str)

self
end
alias_method :<<, :update
end

module Plugin
attr_reader :metadata

def initialize
super
metadata_wrapped = Primitive.object_ivar_get(Primitive.class(self), :metadata)
metadata_pointer = Primitive.object_hidden_var_get(metadata_wrapped, Truffle::CExt::DATA_STRUCT).data
@metadata = Truffle::Digest::Foreign::RbDigestMetadata.new(FFI::Pointer.new(metadata_pointer))

reset
end

def initialize_copy(from)
@metadata = from.metadata
@context = from.context.clone

self
end

def context
@context ||= Truffle::FFI::MemoryPointer.new(:uchar, metadata[:ctx_size])
end

def block_length
metadata[:block_len]
end

def digest_length
metadata[:digest_len]
end

def update(str)
metadata[:update_func].call(context, Truffle::CExt.string_to_ffi_pointer_inplace(str), str.bytesize)

self
end
alias_method :<<, :update

def finish
str = Truffle::FFI::MemoryPointer.new(:uchar, metadata[:digest_len], false)
metadata[:finish_func].call(context, str)

reset

Primitive.pointer_read_bytes str.address, metadata[:digest_len]
end

def reset
@context = nil
if metadata[:init_func].call(context) != 1
raise RuntimeError, 'Digest initialization failed'
end
end
end

class MD5 < Base
Expand Down
2 changes: 1 addition & 1 deletion lib/truffle/truffle/cext_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Truffle::CExt
# Methods defined in this file are not considered as Ruby code implementing MRI C parts,
# see org.truffleruby.cext.CExtNodes.BlockProcNode

# methods defined with rb_define_method are normal Ruby methods therefore they cannot be defined in the cext.rb file
# methods defined with rb_define_method are normal Ruby methods therefore they cannot be defined in the cext.rb
# file because blocks passed as arguments would be skipped by org.truffleruby.cext.CExtNodes.BlockProcNode
def rb_define_method(mod, name, function, argc)
if argc < -2 or 15 < argc
Expand Down
99 changes: 99 additions & 0 deletions spec/ruby/optional/capi/digest_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require_relative 'spec_helper'

require 'fiddle'

load_extension('digest')

describe "C-API Digest functions" do
before :each do
@s = CApiDigestSpecs.new
end

describe "rb_digest_make_metadata" do
before :each do
@metadata = @s.rb_digest_make_metadata
end

it "should store the block length" do
@s.block_length(@metadata).should == 40
end

it "should store the digest length" do
@s.digest_length(@metadata).should == 20
end

it "should store the context size" do
@s.context_size(@metadata).should == 129
end
end

describe "digest plugin" do
before :each do
@s = CApiDigestSpecs.new
@digest = Digest::TestDigest.new

# A pointer to the CTX type defined in the extension for this spec. Digest does not make the context directly
# accessible as part of its API. However, to ensure we are properly loading the plugin, it's useful to have
# direct access to the context pointer to verify its contents.
@context = Fiddle::Pointer.new(@s.context(@digest))
end

it "should report the block length" do
@digest.block_length.should == 40
end

it "should report the digest length" do
@digest.digest_length.should == 20
end

it "should initialize the context" do
# Our test plugin always writes the string "Initialized\n" when its init function is called.
verify_context("Initialized\n")
end

it "should update the digest" do
@digest.update("hello world")

# Our test plugin always writes the string "Updated: <data>\n" when its update function is called.
current = "Initialized\nUpdated: hello world"
verify_context(current)

@digest << "blah"

current = "Initialized\nUpdated: hello worldUpdated: blah"
verify_context(current)
end

it "should finalize the digest" do
@digest.update("")

finish_string = @digest.instance_eval { finish }

# We expect the plugin to write out the last `@digest.digest_length` bytes, followed by the string "Finished\n".
#
finish_string.should == "d\nUpdated: Finished\n"
finish_string.encoding.should == Encoding::ASCII_8BIT
end

it "should reset the context" do
@digest.update("foo")
verify_context("Initialized\nUpdated: foo")

@digest.reset

# The context will be recreated as a result of the `reset` so we must fetch the latest context pointer.
@context = Fiddle::Pointer.new(@s.context(@digest))

verify_context("Initialized\n")
end

def verify_context(current_body)
# In the CTX type, the length of the current context contents is stored in the first byte.
byte_count = @context[0]
byte_count.should == current_body.bytesize

# After the size byte follows a string.
@context[1, byte_count].should == current_body
end
end
end
Loading
Loading