liquid5-c 5.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a69a347ba8a222052fceb99748443908d8fa8c942f6aa7f8ca0c96644ef6695e
4
- data.tar.gz: c8b2ac1efa7a98d4784d6f162f31a2308459cd0b8480f5ca482e43db156bcc6e
3
+ metadata.gz: d0c754a3441b1a66c185b884b2176fd6d6be63271e34f198a84f686f3187ab05
4
+ data.tar.gz: 2d18c96ebc1c0a2d3ced25a92645dcdc274bb18ac4ba2c390493001e3f6e513e
5
5
  SHA512:
6
- metadata.gz: fbfc706a5447a127c06da77bc47031b7c81622dc286b37bebf4c1de33a9c77e157013ba56633e44110af6447ddca6b3314b8b19569f15b685fe8c6c11cdedf60
7
- data.tar.gz: cc0762143aac4603f77085a159446bd79f53589f8a256d6441baa345b20323dc7011c0fd9abf1c44517ac1353f22fd670f77b88a864e49664ac243bc8a9939c9
6
+ metadata.gz: f694bfad31bc63759450327b86b959e4cc9adc0ff80e2be8a68b9499bbb40c8b890065cdab058396951c2f408cfa0291d9f2601456de9aa0729c1cbf00147aa3
7
+ data.tar.gz: 11c0a0814f98ee38678df6450b8407362aaa2901d8e4090f0256985bb7f92d2627893a30b0601e8bbf163553217ede222e0f361de855f0b305452ee6ba850d1e
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # liquid5-c
2
2
 
3
3
  This fork of Shopify's liquid-c accelerates parsing and rendering with the Liquid 5.14.0 Ruby API.
4
- The native compiler depends on the internal layout of this Liquid release.
4
+ The extension depends on the internal layout of this Liquid release.
5
5
 
6
6
  ## Installation
7
7
 
8
- The gem builds a native extension and requires Ruby 3.0 or later, Ruby headers,
9
- a C compiler and make. After the first release, add this line to your Gemfile.
10
- Bundler installs Liquid 5.14.0 as a dependency.
8
+ The gem builds a native extension and requires Ruby 3.2 or later, Ruby headers,
9
+ a C compiler and make. Add [liquid5-c](https://rubygems.org/gems/liquid5-c)
10
+ to your Gemfile. Bundler installs Liquid 5.14.0 as a dependency.
11
11
 
12
12
  ```ruby
13
13
  gem "liquid5-c", "~> 5.0", require: "liquid/c"
@@ -27,38 +27,6 @@ template = Liquid::Template.parse("Hello, {{ customer.name }}")
27
27
  template.render!("customer" => { "name" => "Ada" }) # => "Hello, Ada"
28
28
  ```
29
29
 
30
- ## Publishing
31
-
32
- Before the first release, sign in to RubyGems.org with the account that will
33
- own the gem and create a [pending trusted publisher](https://rubygems.org/profile/oidc/pending_trusted_publishers).
34
- Enter the following values.
35
-
36
- | Field | Value |
37
- | --- | --- |
38
- | Gem name | `liquid5-c` |
39
- | GitHub owner | `VladimirIvanin` |
40
- | Repository | `liquid-c` |
41
- | Workflow filename | `release.yml` |
42
- | Environment | `release` |
43
-
44
- Copy the numeric account ID shown on that page into the GitHub Actions
45
- repository variable `RUBYGEMS_OWNER_ID` under Settings → Secrets and variables
46
- → Actions → Variables. The workflow uses OIDC for authentication.
47
-
48
- For each release, update `Liquid::C::VERSION` in `lib/liquid/c/version.rb`,
49
- run `bundle lock` and merge the changes into `main`. Once CI passes, tag
50
- the tested commit and push that tag. For the first release, run the following
51
- from the release commit.
52
-
53
- ```sh
54
- git tag -a v5.0.0 -m "Release liquid5-c 5.0.0"
55
- git push origin v5.0.0
56
- ```
57
-
58
- The Publish gem workflow checks the tag against the version, runs the unit
59
- and compatibility suites, then builds and installs the gem in isolation.
60
- After a successful native render it pushes that same package to RubyGems.org.
61
-
62
30
  ## Compatibility
63
31
 
64
32
  The C tokenizer takes UTF-8 input. Templates larger than 16,777,215 bytes
@@ -68,13 +36,12 @@ receive a tokenizer with a `shift` method. Profiling and the
68
36
  Set `Liquid::C.enabled` at application boot when choosing an engine for the
69
37
  whole process.
70
38
 
71
- The guarded native compiler for built-in tags requires MRI 3.2 and compatible
72
- Liquid methods. A registered subclass of `Liquid::Include` keeps its own parse
73
- and render behavior. Other Ruby versions can still use the C tokenizer, block
74
- body and rendering VM. To inspect variable parser decisions for a template,
39
+ The extension uses only the public C API of MRI: no private symbols are linked,
40
+ so it does not depend on the MRI patch version. All registered objects are GC
41
+ roots that survive heap compaction (`GC.compact`, `Process.warmup`). A registered
42
+ subclass of `Liquid::Include` keeps its own parse and render behavior. To inspect variable parser decisions for a template,
75
43
  pass a hash as the `native_parser_stats:` parse option. The parser populates
76
- it with counts such as `accepted` and `guard_cost`. Applications that require
77
- the guarded parser can check `Liquid::C::NativeParser::NATIVE_GUARDS` at boot.
44
+ it with counts such as `accepted` and `guard_cost`.
78
45
 
79
46
  Whitespace trimming follows Liquid 5.14.0 by default. Templates that depend on
80
47
  historical liquid-c whitespace trimming can opt in during parsing with
@@ -102,7 +69,7 @@ The runner checks each fixture's output.
102
69
 
103
70
  On MRI 3.3.13 (x86-64 Linux, YJIT off), the two render fixtures gave the
104
71
  following allocation counts, rounded to the nearest object per render. This Ruby
105
- uses the C rendering VM with the MRI 3.2 guarded compiler disabled.
72
+ uses the C tokenizer, block body and rendering VM.
106
73
 
107
74
  | Fixture | Plain Liquid | Liquid C |
108
75
  | --- | ---: | ---: |
@@ -127,7 +94,7 @@ mise run check
127
94
 
128
95
  Run `mise run test:insales` to check the current working tree on MRI 3.2.11,
129
96
  the InSales production version. The task uses rsync to create an isolated build
130
- under `tmp/`, verifies guarded native parsing is available, and runs the unit
97
+ under `tmp/`, and runs the unit
131
98
  tests and compatibility matrix. A failed run keeps its build and logs for
132
99
  inspection. The default `mise run test` uses MRI 3.3.13.
133
100
 
data/ext/liquid_c/block.c CHANGED
@@ -740,6 +740,8 @@ static VALUE block_body_render_to_output_buffer(VALUE self, VALUE context, VALUE
740
740
  document_body_ensure_compile_finished(entry->body);
741
741
 
742
742
  liquid_vm_render(document_body_get_block_body_header_ptr(entry), document_body_get_constants_ptr(entry), context, output, &body->as.compiled.nodelist);
743
+ // The VM holds raw pointers into the memory owned by self for the whole render.
744
+ RB_GC_GUARD(self);
743
745
  return output;
744
746
  }
745
747
 
@@ -17,11 +17,7 @@ typedef struct context {
17
17
 
18
18
  void liquid_define_context(void);
19
19
  void liquid_define_native_values(void);
20
- #ifdef HAVE_RB_CALLABLE_METHOD_ENTRY
21
- VALUE liquid_value_to_liquid(VALUE value);
22
- #else
23
20
  #define liquid_value_to_liquid(value) rb_funcall((value), id_to_liquid, 0)
24
- #endif
25
21
  void context_internal_init(VALUE context_obj, context_t *context);
26
22
  void context_refresh(context_t *context);
27
23
  void context_mark(context_t *context);
@@ -76,19 +76,17 @@ static VALUE expression_parse_pinned(VALUE opaque)
76
76
  return expr_obj;
77
77
  }
78
78
 
79
- static VALUE expression_unpin(VALUE opaque)
80
- {
81
- rb_gc_unregister_address((VALUE *)opaque);
82
- return Qnil;
83
- }
84
-
85
79
  static VALUE expression_strict_parse(VALUE klass, VALUE markup)
86
80
  {
87
81
  if (NIL_P(markup)) return Qnil;
88
82
  StringValue(markup);
89
83
  markup = rb_str_dup_frozen(markup);
90
- rb_gc_register_address(&markup);
91
- return rb_ensure(expression_parse_pinned, (VALUE)&markup, expression_unpin, (VALUE)&markup);
84
+ // The local is found by the conservative stack scan, which also pins the
85
+ // string. Registering a stack address as a global root would leave a
86
+ // dangling root if this fiber were abandoned before unwinding.
87
+ VALUE result = expression_parse_pinned((VALUE)&markup);
88
+ RB_GC_GUARD(markup);
89
+ return result;
92
90
  }
93
91
 
94
92
  VALUE expression_evaluate(VALUE self, VALUE context)
@@ -123,6 +121,7 @@ static VALUE expression_disassemble(VALUE self)
123
121
  void liquid_define_expression(void)
124
122
  {
125
123
  cLiquidCExpression = rb_define_class_under(mLiquidC, "Expression", rb_cObject);
124
+ rb_global_variable(&cLiquidCExpression);
126
125
  rb_undef_alloc_func(cLiquidCExpression);
127
126
  rb_define_singleton_method(cLiquidCExpression, "strict_parse", expression_strict_parse, 1);
128
127
  rb_define_method(cLiquidCExpression, "evaluate", expression_evaluate, 1);
@@ -17,6 +17,8 @@ if ENV["DEBUG"] == "true"
17
17
  # Ruby was compiled with -DRUBY_DEBUG.
18
18
  # https://github.com/ruby/ruby/blob/9e678cdbd054f78576a8f21b3f97cccc395ade22/include/ruby/assert.h#L36-L41
19
19
  $CFLAGS << (RUBY_ENGINE == "ruby" ? " -DRUBY_DEBUG" : " -DNDEBUG")
20
+ elsif ENV["LIQUID_C_CHECKED"] == "true"
21
+ # Optimized build that keeps assert() for CI and sanitizer runs.
20
22
  else
21
23
  $CFLAGS << " -DNDEBUG"
22
24
  end
@@ -45,16 +47,7 @@ rescue LoadError, Fiddle::DLError
45
47
  false
46
48
  end
47
49
 
48
- ["rb_callable_method_entry", "rb_hash_compare_by_id_p"].each do |function|
49
- have_func(function) if mri_exports?(function)
50
- end
51
- if mri_exports?("ruby_vm_event_flags") && try_link(<<~C)
52
- #define HAVE_RUBY_VM_EVENT_FLAGS 1
53
- #include "mri_compat.h"
54
- int main(void) { return liquid_vm_tracing(); }
55
- C
56
- $defs << "-DHAVE_RUBY_VM_EVENT_FLAGS"
57
- end
50
+ have_func("rb_hash_compare_by_id_p") if mri_exports?("rb_hash_compare_by_id_p")
58
51
 
59
52
  $warnflags&.gsub!("-Wdeclaration-after-statement", "")
60
53
  create_makefile("liquid_c")
@@ -14,6 +14,9 @@
14
14
  #include "vm_assembler_pool.h"
15
15
  #include "liquid_vm.h"
16
16
  #include "usage.h"
17
+ #include <ruby/version.h>
18
+ #include <string.h>
19
+ #include <stdio.h>
17
20
 
18
21
  ID id_evaluate;
19
22
  ID id_to_liquid;
@@ -113,8 +116,25 @@ __attribute__((noreturn)) void raise_non_utf8_encoding_error(VALUE string, const
113
116
  rb_raise(rb_eEncCompatError, "non-UTF8 encoded %s (%"PRIsVALUE") not supported", value_name, rb_obj_encoding(string));
114
117
  }
115
118
 
119
+ // The extension relies on the object layout of the Ruby it was compiled for.
120
+ // Loading a binary built for another Ruby (for example a stale lib/liquid_c.so
121
+ // after switching Ruby versions) would corrupt memory instead of failing.
122
+ static void check_abi(void)
123
+ {
124
+ const char *platform = RSTRING_PTR(rb_const_get(rb_cObject, rb_intern("RUBY_PLATFORM")));
125
+ const char *version = RSTRING_PTR(rb_const_get(rb_cObject, rb_intern("RUBY_VERSION")));
126
+ int major = 0, minor = 0;
127
+ sscanf(version, "%d.%d", &major, &minor);
128
+ if (major != RUBY_API_VERSION_MAJOR || minor != RUBY_API_VERSION_MINOR || strcmp(platform, RUBY_PLATFORM) != 0) {
129
+ rb_raise(rb_eLoadError,
130
+ "liquid_c was built for Ruby %d.%d (%s) but is loaded by Ruby %s (%s); recompile the extension",
131
+ RUBY_API_VERSION_MAJOR, RUBY_API_VERSION_MINOR, RUBY_PLATFORM, version, platform);
132
+ }
133
+ }
134
+
116
135
  RUBY_FUNC_EXPORTED void Init_liquid_c(void)
117
136
  {
137
+ check_abi();
118
138
  #ifndef HAVE_RB_GC_COUNT
119
139
  rb_global_variable(&portable_gc_count);
120
140
  portable_gc_count = liquid_method_handle(rb_obj_method(rb_mGC, ID2SYM(rb_intern("count"))));
@@ -39,9 +39,11 @@ static void vm_mark(void *ptr)
39
39
 
40
40
  c_buffer_rb_gc_mark(&vm->stack);
41
41
  context_mark(&vm->context);
42
- for (vm_variable_frame_t *frame = vm->variable_frame; frame; frame = frame->previous) {
43
- rb_gc_mark(frame->variable);
44
- }
42
+ // Variable frames are stack allocated and are kept alive by the
43
+ // conservative stack scan of their fiber. Walking them here would follow
44
+ // dangling pointers if a fiber were abandoned mid-render.
45
+ rb_gc_mark(vm->variable_frame_fiber);
46
+ rb_gc_mark(vm->stack_fiber);
45
47
  }
46
48
 
47
49
  static void vm_free(void *ptr)
@@ -70,6 +72,10 @@ static VALUE vm_internal_new(VALUE context)
70
72
  vm->stack = c_buffer_init();
71
73
 
72
74
  vm->invoking_filter = false;
75
+ vm->variable_frame = NULL;
76
+ vm->variable_frame_fiber = Qnil;
77
+ vm->stack_fiber = Qnil;
78
+ vm->stack_depth = 0;
73
79
 
74
80
  vm->context_generation = context_generation;
75
81
  context_internal_init(context, &vm->context);
@@ -358,6 +364,7 @@ static VALUE vm_restore_variable_frame(VALUE uncast_args)
358
364
  {
359
365
  variable_render_args_t *args = (void *)uncast_args;
360
366
  args->vm->variable_frame = args->frame.previous;
367
+ args->vm->variable_frame_fiber = args->frame.previous_fiber;
361
368
  return Qnil;
362
369
  }
363
370
 
@@ -465,8 +472,11 @@ static VALUE vm_render_until_error(VALUE uncast_args)
465
472
  VALUE hash = rb_hash_new();
466
473
 
467
474
  for (size_t i = 0; i < num_keys_and_values; i += 2) {
468
- VALUE *pair = vm_stack_peek_n(vm, num_keys_and_values - i);
469
- rb_hash_aset(hash, pair[0], pair[1]);
475
+ // Copy the pair: hashing a key may run Ruby code that
476
+ // re-enters the VM and reallocates the value stack.
477
+ VALUE *slot = vm_stack_peek_n(vm, num_keys_and_values - i);
478
+ VALUE key = slot[0], value = slot[1];
479
+ rb_hash_aset(hash, key, value);
470
480
  }
471
481
  vm_stack_pop_n(vm, num_keys_and_values);
472
482
 
@@ -590,6 +600,7 @@ static VALUE vm_render_until_error(VALUE uncast_args)
590
600
  .vm = vm,
591
601
  .frame = {
592
602
  .previous = vm->variable_frame,
603
+ .previous_fiber = vm->variable_frame_fiber,
593
604
  .variable = node,
594
605
  .instructions = ip,
595
606
  .constants = constants,
@@ -599,6 +610,7 @@ static VALUE vm_render_until_error(VALUE uncast_args)
599
610
  .output = output,
600
611
  };
601
612
  vm->variable_frame = &render_args.frame;
613
+ vm->variable_frame_fiber = rb_fiber_current();
602
614
  rb_ensure(vm_call_variable_render, (VALUE)&render_args, vm_restore_variable_frame, (VALUE)&render_args);
603
615
  ip = vm_skip_variable_instructions(ip);
604
616
  args->ip = NULL;
@@ -639,12 +651,28 @@ typedef struct vm_execution_args {
639
651
  size_t old_stack_byte_size;
640
652
  } vm_execution_args_t;
641
653
 
654
+ // Interleaving two fibers on one value stack would restore the stack to a size
655
+ // that cuts into the other fiber's live values. Reject it with an exception
656
+ // instead of corrupting memory. Call immediately before rb_ensure(vm_restore_stack).
657
+ static void vm_stack_enter(vm_t *vm)
658
+ {
659
+ VALUE fiber = rb_fiber_current();
660
+ if (vm->stack_depth == 0) {
661
+ vm->stack_fiber = fiber;
662
+ } else if (vm->stack_fiber != fiber) {
663
+ rb_raise(rb_eRuntimeError, "Liquid context is being rendered by another fiber or thread");
664
+ }
665
+ vm->stack_depth++;
666
+ }
667
+
642
668
  static VALUE vm_restore_stack(VALUE uncast_args)
643
669
  {
644
670
  vm_execution_args_t *args = (void *)uncast_args;
645
671
  vm_render_until_error_args_t *render_args = args->render_args;
646
672
  vm_t *vm = render_args->vm;
647
673
 
674
+ if (--vm->stack_depth == 0) vm->stack_fiber = Qnil;
675
+
648
676
  vm->stack.data_end = vm->stack.data ? vm->stack.data + args->old_stack_byte_size : NULL;
649
677
 
650
678
  return Qnil;
@@ -675,6 +703,7 @@ VALUE liquid_vm_evaluate(VALUE context, vm_assembler_t *code)
675
703
  .render_args = &args,
676
704
  .old_stack_byte_size = c_buffer_size(&vm->stack),
677
705
  };
706
+ vm_stack_enter(vm);
678
707
  return rb_ensure(vm_evaluate_body, (VALUE)&execution_args, vm_restore_stack, (VALUE)&execution_args);
679
708
  }
680
709
 
@@ -808,6 +837,7 @@ void liquid_vm_render(block_body_header_t *body, const VALUE *const_ptr, VALUE c
808
837
  .old_stack_byte_size = c_buffer_size(&vm->stack),
809
838
  };
810
839
 
840
+ vm_stack_enter(vm);
811
841
  rb_ensure(vm_render_loop, (VALUE)&execution_args, vm_restore_stack, (VALUE)&execution_args);
812
842
  }
813
843
 
@@ -819,7 +849,7 @@ static VALUE variable_render(VALUE self, VALUE context)
819
849
  }
820
850
  vm_t *vm = vm_from_context(context);
821
851
  vm_variable_frame_t *frame = vm->variable_frame;
822
- if (!frame || frame->variable != self || *frame->nodelist != Qundef) {
852
+ if (!frame || vm->variable_frame_fiber != rb_fiber_current() || frame->variable != self || *frame->nodelist != Qundef) {
823
853
  return rb_funcall(self, id_ruby_render, 1, context);
824
854
  }
825
855
 
@@ -834,6 +864,7 @@ static VALUE variable_render(VALUE self, VALUE context)
834
864
  .render_args = &render_args,
835
865
  .old_stack_byte_size = c_buffer_size(&vm->stack),
836
866
  };
867
+ vm_stack_enter(vm);
837
868
  VALUE result = rb_ensure(vm_evaluate_body, (VALUE)&execution_args, vm_restore_stack, (VALUE)&execution_args);
838
869
  vm_refresh_context(vm);
839
870
  if (!NIL_P(vm->context.global_filter)) {
@@ -8,6 +8,7 @@
8
8
 
9
9
  typedef struct vm_variable_frame {
10
10
  struct vm_variable_frame *previous;
11
+ VALUE previous_fiber;
11
12
  VALUE variable;
12
13
  const uint8_t *instructions;
13
14
  const VALUE *constants;
@@ -20,7 +21,15 @@ typedef struct vm {
20
21
  bool invoking_filter;
21
22
  unsigned long context_generation;
22
23
  unsigned long context_revision;
24
+ // Frames live on the machine stack of the fiber that created them. The
25
+ // owning fiber is recorded so a frame left behind by an abandoned fiber is
26
+ // never dereferenced from another fiber.
23
27
  vm_variable_frame_t *variable_frame;
28
+ VALUE variable_frame_fiber;
29
+ // The value stack is shared by every render of a context. Only the fiber
30
+ // that entered it first may use it until it has fully unwound.
31
+ VALUE stack_fiber;
32
+ unsigned long stack_depth;
24
33
  context_t context;
25
34
  } vm_t;
26
35
 
@@ -4,32 +4,9 @@
4
4
  #include <ruby.h>
5
5
  #include <stdbool.h>
6
6
 
7
- // extconf probes the running MRI before enabling these optional interfaces.
8
- #ifdef HAVE_RB_CALLABLE_METHOD_ENTRY
9
- struct rb_callable_method_entry_struct;
10
- RUBY_EXTERN const struct rb_callable_method_entry_struct *rb_callable_method_entry(VALUE klass, ID method);
11
-
12
- static inline VALUE liquid_method_entry(VALUE klass, ID method)
13
- {
14
- return (VALUE)rb_callable_method_entry(klass, method);
15
- }
16
- #endif
17
-
18
- #ifdef HAVE_RUBY_VM_EVENT_FLAGS
19
- RUBY_EXTERN rb_event_flag_t ruby_vm_event_flags;
20
-
21
- static inline bool liquid_vm_tracing(void)
22
- {
23
- return ruby_vm_event_flags != 0;
24
- }
25
- #endif
26
-
7
+ // Only functions that extconf found exported by the running MRI are declared.
27
8
  #ifdef HAVE_RB_HASH_COMPARE_BY_ID_P
28
9
  RUBY_EXTERN VALUE rb_hash_compare_by_id_p(VALUE hash);
29
10
  #endif
30
11
 
31
- #if defined(HAVE_RB_CALLABLE_METHOD_ENTRY) && defined(HAVE_RUBY_VM_EVENT_FLAGS)
32
- #define LIQUID_HAVE_GUARDED_COMPILER 1
33
- #endif
34
-
35
12
  #endif