summaryrefslogtreecommitdiff
path: root/tool
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-11 09:48:58 (GMT)
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-11 09:48:58 (GMT)
commitc2bfb4e93c674347b7eb09a30856e3d75f74cf4d (patch)
treeb898add4e9f9ab117aebcad189d47e79984c2f16 /tool
parentecda2d23d593eea81a4daf97ec0d74945fa96baf (diff)
add new instruction attribute called leaf
An instruction is leaf if it has no rb_funcall inside. In order to check this property, we introduce stack canary which is a random number collected at runtime. Stack top is always filled with this number and checked for stack smashing operations, when VM_CHECK_MODE. [GH-1947] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64677 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'tool')
-rwxr-xr-xtool/ruby_vm/models/bare_instructions.rb8
-rw-r--r--tool/ruby_vm/views/_insn_entry.erb21
-rw-r--r--tool/ruby_vm/views/_leaf_helpers.erb105
-rw-r--r--tool/ruby_vm/views/insns_info.inc.erb1
4 files changed, 135 insertions, 0 deletions
diff --git a/tool/ruby_vm/models/bare_instructions.rb b/tool/ruby_vm/models/bare_instructions.rb
index d2ff134..5c27af3 100755
--- a/tool/ruby_vm/models/bare_instructions.rb
+++ b/tool/ruby_vm/models/bare_instructions.rb
@@ -105,6 +105,10 @@ class RubyVM::BareInstructions
/\b(false|0)\b/ !~ @attrs['handles_sp'].expr.expr
end
+ def complicated_return_values?
+ @sig[:ret].any? {|i| i == '...' }
+ end
+
def inspect
sprintf "#<%s %s@%s:%d>", self.class.name, @name, @loc[0], @loc[1]
end
@@ -130,6 +134,10 @@ class RubyVM::BareInstructions
generate_attribute 'rb_num_t', 'width', width
generate_attribute 'rb_snum_t', 'sp_inc', rets.size - pops.size
generate_attribute 'bool', 'handles_sp', false
+ generate_attribute 'bool', 'leaf', opes.all? {|o|
+ # Insn with ISEQ should yield it; can never be a leaf.
+ o[:type] != 'ISEQ'
+ }
end
def typesplit a
diff --git a/tool/ruby_vm/views/_insn_entry.erb b/tool/ruby_vm/views/_insn_entry.erb
index e3e3ea4..90a489f 100644
--- a/tool/ruby_vm/views/_insn_entry.erb
+++ b/tool/ruby_vm/views/_insn_entry.erb
@@ -13,6 +13,12 @@ INSN_ENTRY(<%= insn.name %>)
%# NAME_OF_CURRENT_INSN is used in vm_exec.h
# define NAME_OF_CURRENT_INSN <%= insn.name %>
# define INSN_ATTR(x) <%= insn.call_attribute(' ## x ## ') %>
+% unless insn.complicated_return_values?
+# if VM_CHECK_MODE > 0
+ bool leaf;
+ VALUE *canary;
+# endif
+% end
% unless insn.declarations.empty?
<%= insn.declarations.join(";\n ") %>;
@@ -33,12 +39,27 @@ INSN_ENTRY(<%= insn.name %>)
% if insn.handles_sp?
POPN(INSN_ATTR(popn));
% end
+% unless insn.complicated_return_values?
+#if VM_CHECK_MODE > 0
+ if ((leaf = INSN_ATTR(leaf))) {
+ canary = STACK_ADDR_FROM_TOP(-1);
+ *canary = vm_stack_canary;
+ }
+#endif
+% end
COLLECT_USAGE_INSN(INSN_ATTR(bin));
% insn.opes.each_with_index do |ope, i|
COLLECT_USAGE_OPERAND(INSN_ATTR(bin), <%= i %>, <%= ope[:name] %>);
% end
<%= render_c_expr insn.expr -%>
CHECK_VM_STACK_OVERFLOW_FOR_INSN(VM_REG_CFP, INSN_ATTR(retn));
+% unless insn.complicated_return_values?
+#if VM_CHECK_MODE > 0
+ if (leaf && (*canary != vm_stack_canary)) {
+ vm_canary_is_found_dead(INSN_ATTR(bin), *canary);
+ }
+#endif
+% end
% if insn.handles_sp?
% insn.rets.reverse_each do |ret|
PUSH(<%= insn.cast_to_VALUE ret %>);
diff --git a/tool/ruby_vm/views/_leaf_helpers.erb b/tool/ruby_vm/views/_leaf_helpers.erb
new file mode 100644
index 0000000..00c2e38
--- /dev/null
+++ b/tool/ruby_vm/views/_leaf_helpers.erb
@@ -0,0 +1,105 @@
+%# -*- mode:c; style:ruby; coding: utf-8; indent-tabs-mode: nil -*-
+%# Copyright (c) 2018 Urabe, Shyouhei. All rights reserved.
+%#
+%# This file is a part of the programming language Ruby. Permission is hereby
+%# granted, to either redistribute and/or modify this file, provided that the
+%# conditions mentioned in the file COPYING are met. Consult the file for
+%# details.
+%;
+
+static bool
+leafness_of_getglobal(VALUE gentry)
+{
+ const struct rb_global_entry *e = (void *)gentry;
+
+ if (UNLIKELY(rb_gvar_is_traced(e))) {
+ return false;
+ }
+ else {
+ /* We cannot write this function using a switch() because a
+ * case label cannot be a function pointer. */
+ static rb_gvar_getter_t *const whitelist[] = {
+ rb_gvar_val_getter,
+ rb_gvar_var_getter,
+ rb_gvar_undef_getter,
+ };
+ rb_gvar_getter_t *f = rb_gvar_getter_function_of(e);
+ int i;
+
+ for (i = 0; i < numberof(whitelist); i++) {
+ if (f == whitelist[i]) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
+
+static bool
+leafness_of_setglobal(VALUE gentry)
+{
+ const struct rb_global_entry *e = (void *)gentry;
+
+ if (UNLIKELY(rb_gvar_is_traced(e))) {
+ return false;
+ }
+ else {
+ /* We cannot write this function using a switch() because a
+ * case label cannot be a function pointer. */
+ static rb_gvar_setter_t *const whitelist[] = {
+ rb_gvar_val_setter,
+ rb_gvar_readonly_setter,
+ rb_gvar_var_setter,
+ rb_gvar_undef_setter,
+ };
+ rb_gvar_setter_t *f = rb_gvar_setter_function_of(e);
+ int i;
+
+ for (i = 0; i < numberof(whitelist); i++) {
+ if (f == whitelist[i]) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
+
+#include "iseq.h"
+
+static bool
+leafness_of_defined(rb_num_t op_type)
+{
+ /* see also: vm_insnhelper.c:vm_defined() */
+ switch (op_type) {
+ case DEFINED_IVAR:
+ case DEFINED_IVAR2:
+ case DEFINED_GVAR:
+ case DEFINED_CVAR:
+ case DEFINED_YIELD:
+ case DEFINED_REF:
+ case DEFINED_ZSUPER:
+ return false;
+ case DEFINED_CONST:
+ /* has rb_autoload_load(); */
+ return false;
+ case DEFINED_FUNC:
+ case DEFINED_METHOD:
+ /* calls #respond_to_missing? */
+ return false;
+ default:
+ rb_bug("unknown operand %ld: blame @shyouhei.", op_type);
+ }
+}
+
+static bool
+leafness_of_checkmatch(rb_num_t flag)
+{
+ /* see also: vm_insnhelper.c:check_match() */
+ if (flag == VM_CHECKMATCH_TYPE_WHEN) {
+ return true;
+ }
+ else {
+ /* has rb_funcallv() */
+ return false;
+ }
+}
diff --git a/tool/ruby_vm/views/insns_info.inc.erb b/tool/ruby_vm/views/insns_info.inc.erb
index e08a15e..67fcebf 100644
--- a/tool/ruby_vm/views/insns_info.inc.erb
+++ b/tool/ruby_vm/views/insns_info.inc.erb
@@ -15,5 +15,6 @@
<%= render 'insn_name_info' %>
<%= render 'insn_len_info' %>
<%= render 'insn_operand_info' %>
+<%= render 'leaf_helpers' %>
<%= render 'attributes' %>
<%= render 'insn_stack_increase' %>