From 9d70470cb6169baa14f9e64ca19e81a073eae85a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Mon, 6 Oct 2014 17:01:23 +0200
Subject: [PATCH] Nettle: Reference the programs being inherited.

Make sure that there's a local symbol in the parent for the
programs that we inherit. Otherwise the parent_identifier in
the generated inherit may point back to some later override.

This happens frequently when inheriting the State class.

This fixes infinite recursion in eg Program.all_inherits()
when called with several of the Crypto and Nettle classes.
---
 src/post_modules/Nettle/aead.H      |  6 ++--
 src/post_modules/Nettle/aead.cmod   |  6 ++--
 src/post_modules/Nettle/cipher.H    |  3 ++
 src/post_modules/Nettle/cipher.cmod | 45 ++++++++++++++++++++++++++++-
 src/post_modules/Nettle/hash.H      |  6 ++--
 src/post_modules/Nettle/hash.cmod   |  6 ++--
 src/post_modules/Nettle/mac.H       |  6 ++--
 src/post_modules/Nettle/mac.cmod    |  6 ++--
 8 files changed, 71 insertions(+), 13 deletions(-)

diff --git a/src/post_modules/Nettle/aead.H b/src/post_modules/Nettle/aead.H
index 9e78de2c56..9747f12951 100644
--- a/src/post_modules/Nettle/aead.H
+++ b/src/post_modules/Nettle/aead.H
@@ -51,9 +51,11 @@ PIKECLASS PIKE_NAME
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
-		      parent_State_fun_num +
-		      parent_prog->inherits[1].identifier_level,
+		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
 	}
       }
diff --git a/src/post_modules/Nettle/aead.cmod b/src/post_modules/Nettle/aead.cmod
index a44c41f76f..c51ca4ecf6 100644
--- a/src/post_modules/Nettle/aead.cmod
+++ b/src/post_modules/Nettle/aead.cmod
@@ -204,9 +204,11 @@ PIKECLASS AEAD
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
-		      parent_State_fun_num +
-		      parent_prog->inherits[1].identifier_level,
+		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
 	}
       }
diff --git a/src/post_modules/Nettle/cipher.H b/src/post_modules/Nettle/cipher.H
index 809cc8b60b..117c5ac502 100644
--- a/src/post_modules/Nettle/cipher.H
+++ b/src/post_modules/Nettle/cipher.H
@@ -77,6 +77,9 @@ PIKECLASS PIKE_NAME
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0, parent_State_fun_num,
 		      1 + 42, 0, NULL);
 	}
diff --git a/src/post_modules/Nettle/cipher.cmod b/src/post_modules/Nettle/cipher.cmod
index bdd92c2365..2f77000283 100644
--- a/src/post_modules/Nettle/cipher.cmod
+++ b/src/post_modules/Nettle/cipher.cmod
@@ -205,6 +205,9 @@ PIKECLASS Cipher
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
 		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
@@ -1119,6 +1122,9 @@ PIKECLASS BlockCipher
 	  struct program *parent_State_prog =
 	    low_program_from_function(parent_obj, parent_State_fun_num);
 	  if (parent_State_prog) {
+	    parent_State_fun_num =
+	      really_low_reference_inherited_identifier(Pike_compiler->previous,
+							0, parent_State_fun_num);
 	    low_inherit(parent_State_prog, 0,
 			parent_State_fun_num,
 			1 + 42, 0, NULL);
@@ -1625,6 +1631,9 @@ PIKECLASS BlockCipher
 	  struct program *parent_State_prog =
 	    low_program_from_function(parent_obj, parent_State_fun_num);
 	  if (parent_State_prog) {
+	    parent_State_fun_num =
+	      really_low_reference_inherited_identifier(Pike_compiler->previous,
+							0, parent_State_fun_num);
 	    low_inherit(parent_State_prog, 0,
 			parent_State_fun_num,
 			1 + 42, 0, NULL);
@@ -2140,6 +2149,9 @@ PIKECLASS BlockCipher
 	  struct program *parent_State_prog =
 	    low_program_from_function(parent_obj, parent_State_fun_num);
 	  if (parent_State_prog) {
+	    parent_State_fun_num =
+	      really_low_reference_inherited_identifier(Pike_compiler->previous,
+							0, parent_State_fun_num);
 	    low_inherit(parent_State_prog, 0,
 			parent_State_fun_num,
 			1 + 42, 0, NULL);
@@ -2532,6 +2544,9 @@ PIKECLASS BlockCipher
 	  struct program *parent_State_prog =
 	    low_program_from_function(parent_obj, parent_State_fun_num);
 	  if (parent_State_prog) {
+	    parent_State_fun_num =
+	      really_low_reference_inherited_identifier(Pike_compiler->previous,
+							0, parent_State_fun_num);
 	    low_inherit(parent_State_prog, 0,
 			parent_State_fun_num,
 			1 + 42, 0, NULL);
@@ -2980,6 +2995,9 @@ PIKECLASS BlockCipher16
 	struct program *parent__CTR_prog =
 	  low_program_from_function(parent_obj, parent__CTR_fun_num);
 	if (parent__CTR_prog) {
+	  parent__CTR_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent__CTR_fun_num);
 	  low_inherit(parent__CTR_prog, 0,
 		      parent__CTR_fun_num,
 		      1 + 42, 0, NULL);
@@ -3057,6 +3075,9 @@ PIKECLASS BlockCipher16
 	  struct program *parent_State_prog =
 	    low_program_from_function(parent_obj, parent_State_fun_num);
 	  if (parent_State_prog) {
+	    parent_State_fun_num =
+	      really_low_reference_inherited_identifier(Pike_compiler->previous,
+							0, parent_State_fun_num);
 	    low_inherit(parent_State_prog, 0,
 			parent_State_fun_num,
 			1 + 42, 0, NULL);
@@ -3492,8 +3513,9 @@ PIKECLASS BlockCipher16
      */
     EXTRA
     {
-      /* Perform an inherit if the _CCM class that our parent contains.
+      /* Perform an inherit of the _CCM class that our parent contains.
        */
+      /* FIXME: */
       low_inherit(Nettle_BlockCipher16_cq__CCM_program, 0,
 		  Nettle_BlockCipher16_cq__CCM_program_fun_num,
 		  1 + 42, 0, NULL);
@@ -3628,6 +3650,9 @@ PIKECLASS BlockCipher16
 	  struct program *parent_State_prog =
 	    low_program_from_function(parent_obj, parent_State_fun_num);
 	  if (parent_State_prog) {
+	    parent_State_fun_num =
+	      really_low_reference_inherited_identifier(Pike_compiler->previous,
+							0, parent_State_fun_num);
 	    low_inherit(parent_State_prog, 0,
 			parent_State_fun_num,
 			1 + 42, 0, NULL);
@@ -4081,6 +4106,9 @@ PIKECLASS BlockCipher16
 	  struct program *parent_State_prog =
 	    low_program_from_function(parent_obj, parent_State_fun_num);
 	  if (parent_State_prog) {
+	    parent_State_fun_num =
+	      really_low_reference_inherited_identifier(Pike_compiler->previous,
+							0, parent_State_fun_num);
 	    low_inherit(parent_State_prog, 0,
 			parent_State_fun_num,
 			1 + 42, 0, NULL);
@@ -4636,6 +4664,9 @@ PIKECLASS ARCTWO
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
 		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
@@ -5008,6 +5039,9 @@ PIKECLASS CHACHA
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
 		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
@@ -5218,6 +5252,9 @@ PIKECLASS DES
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
 		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
@@ -5455,6 +5492,9 @@ PIKECLASS DES3
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
 		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
@@ -5572,6 +5612,9 @@ PIKECLASS SALSA20
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
 		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
diff --git a/src/post_modules/Nettle/hash.H b/src/post_modules/Nettle/hash.H
index 53c605cc63..21f0ea043c 100644
--- a/src/post_modules/Nettle/hash.H
+++ b/src/post_modules/Nettle/hash.H
@@ -51,9 +51,11 @@ PIKECLASS PIKE_NAME
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
-		      parent_State_fun_num +
-		      parent_prog->inherits[1].identifier_level,
+		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
 	}
       }
diff --git a/src/post_modules/Nettle/hash.cmod b/src/post_modules/Nettle/hash.cmod
index 403c09a888..1b0aa920ee 100644
--- a/src/post_modules/Nettle/hash.cmod
+++ b/src/post_modules/Nettle/hash.cmod
@@ -472,9 +472,11 @@ PIKECLASS Hash
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
-		      parent_State_fun_num +
-		      parent_prog->inherits[1].identifier_level,
+		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
 	}
       }
diff --git a/src/post_modules/Nettle/mac.H b/src/post_modules/Nettle/mac.H
index d9179a4703..1fc9b46b8b 100644
--- a/src/post_modules/Nettle/mac.H
+++ b/src/post_modules/Nettle/mac.H
@@ -51,9 +51,11 @@ PIKECLASS PIKE_NAME
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
-		      parent_State_fun_num +
-		      parent_prog->inherits[1].identifier_level,
+		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
 	}
       }
diff --git a/src/post_modules/Nettle/mac.cmod b/src/post_modules/Nettle/mac.cmod
index 52faed91c4..b3ae84cc4a 100644
--- a/src/post_modules/Nettle/mac.cmod
+++ b/src/post_modules/Nettle/mac.cmod
@@ -184,9 +184,11 @@ PIKECLASS MAC
 	struct program *parent_State_prog =
 	  low_program_from_function(parent_obj, parent_State_fun_num);
 	if (parent_State_prog) {
+	  parent_State_fun_num =
+	    really_low_reference_inherited_identifier(Pike_compiler->previous,
+						      0, parent_State_fun_num);
 	  low_inherit(parent_State_prog, 0,
-		      parent_State_fun_num +
-		      parent_prog->inherits[1].identifier_level,
+		      parent_State_fun_num,
 		      1 + 42, 0, NULL);
 	}
       }
-- 
GitLab