From 5b195b1c90cc03310efc3eed394ebec99e486e9c Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Tue, 5 Aug 2008 20:15:09 +0200
Subject: [PATCH] Include: added symbol table to the binary for global
 variables.

---
 src/lib-sieve/plugins/include/cmd-import.c    |   2 +-
 .../plugins/include/ext-include-binary.c      |  36 +++-
 .../plugins/include/ext-include-binary.h      |   3 +-
 .../plugins/include/ext-include-common.c      |  36 ++--
 .../plugins/include/ext-include-common.h      |   2 +-
 .../plugins/include/ext-include-variables.c   | 204 +++++++++++++++---
 .../plugins/include/ext-include-variables.h   |  13 ++
 src/lib-sieve/plugins/include/ext-include.c   |   6 +-
 .../plugins/variables/ext-variables-common.c  |  77 ++++++-
 .../plugins/variables/sieve-ext-variables.h   |  32 ++-
 src/lib-sieve/sieve-binary-dumper.c           |  33 +--
 src/lib-sieve/sieve-binary-dumper.h           |   2 +-
 src/lib-sieve/sieve-extensions.h              |   2 +-
 src/lib-sieve/sieve-generator.c               |   2 +-
 src/testsuite/ext-testsuite.c                 |   6 +-
 tests/extensions/include/errors.svtest        |   2 +-
 .../include/errors/import-runtime.sieve       |  15 ++
 .../extensions/include/errors/variables.sieve |   8 +-
 18 files changed, 397 insertions(+), 84 deletions(-)
 create mode 100644 tests/extensions/include/errors/import-runtime.sieve

diff --git a/src/lib-sieve/plugins/include/cmd-import.c b/src/lib-sieve/plugins/include/cmd-import.c
index 9c2afe089..920c0ebbc 100644
--- a/src/lib-sieve/plugins/include/cmd-import.c
+++ b/src/lib-sieve/plugins/include/cmd-import.c
@@ -54,7 +54,7 @@ static bool cmd_import_validate
 	struct sieve_ast_argument *arg = cmd->first_positional;
 	struct sieve_command_context *prev_context = 
 		sieve_command_prev_context(cmd);
-		
+
 	/* Check valid command placement */
 	if ( !sieve_command_is_toplevel(cmd) ||
 		( !sieve_command_is_first(cmd) && prev_context != NULL &&
diff --git a/src/lib-sieve/plugins/include/ext-include-binary.c b/src/lib-sieve/plugins/include/ext-include-binary.c
index 0126b5207..202bdc209 100644
--- a/src/lib-sieve/plugins/include/ext-include-binary.c
+++ b/src/lib-sieve/plugins/include/ext-include-binary.c
@@ -9,7 +9,10 @@
 #include "sieve-interpreter.h"
 #include "sieve-dump.h"
 
+#include "sieve-ext-variables.h"
+
 #include "ext-include-common.h"
+#include "ext-include-variables.h"
 #include "ext-include-binary.h"
 
 /*
@@ -28,6 +31,7 @@ struct ext_include_binary_context {
 	unsigned int dependency_block;
 	
 	struct hash_table *included_scripts;
+	struct sieve_variable_scope *global_vars;
 };
 
 /*
@@ -90,8 +94,10 @@ static inline struct ext_include_binary_context *ext_include_binary_get_context
  */
  
 struct ext_include_binary_context *ext_include_binary_init
-(struct sieve_binary *sbin)
+(struct sieve_binary *sbin, struct sieve_ast *ast)
 {
+	struct ext_include_ast_context *ast_ctx =
+		ext_include_get_ast_context(ast);
 	struct ext_include_binary_context *ctx;
 	
 	/* Get/create our context from the binary we are working on */
@@ -101,6 +107,11 @@ struct ext_include_binary_context *ext_include_binary_init
 	if ( ctx->dependency_block == 0 )
 		ctx->dependency_block = 
 			sieve_binary_extension_create_block(sbin, &include_extension);
+
+	if ( ctx->global_vars == NULL ) {
+		ctx->global_vars = ast_ctx->global_vars;
+		sieve_variable_scope_ref(ctx->global_vars);
+	}
 			
 	return ctx;
 }
@@ -145,6 +156,7 @@ static bool ext_include_binary_save(struct sieve_binary *sbin)
 		hash_iterate_init(binctx->included_scripts);
 	void *key, *value;
 	unsigned int prvblk;
+	bool result = TRUE;
 	
 	sieve_binary_block_clear(sbin, binctx->dependency_block);
 	if ( !sieve_binary_block_set_active(sbin, binctx->dependency_block, &prvblk) )	
@@ -158,12 +170,14 @@ static bool ext_include_binary_save(struct sieve_binary *sbin)
 		sieve_binary_emit_byte(sbin, incscript->location);
 		sieve_binary_emit_cstring(sbin, sieve_script_name(incscript->script));
 	}
+
+	result = ext_include_variables_save(sbin, binctx->global_vars);
 	
 	(void) sieve_binary_block_set_active(sbin, prvblk, NULL);
 
 	hash_iterate_deinit(&hctx);
 	
-	return TRUE;
+	return result;
 }
 
 static bool ext_include_binary_open(struct sieve_binary *sbin)
@@ -226,6 +240,9 @@ static bool ext_include_binary_open(struct sieve_binary *sbin)
 				
 		sieve_script_unref(&script);
 	}
+
+	if ( !ext_include_variables_load(sbin, &offset, block, &binctx->global_vars) )
+		return FALSE;
 	
 	/* Restore previously active block */
 	(void)sieve_binary_block_set_active(sbin, prvblk, NULL);
@@ -273,6 +290,10 @@ static void ext_include_binary_free(struct sieve_binary *sbin)
 	hash_iterate_deinit(&hctx);
 
 	hash_destroy(&binctx->included_scripts);
+
+	if ( binctx->global_vars != NULL ) 
+		sieve_variable_scope_unref(&binctx->global_vars);
+
 }
 
 inline static const char *_script_location
@@ -295,11 +316,14 @@ bool ext_include_binary_dump(struct sieve_dumptime_env *denv)
 	struct sieve_binary *sbin = denv->sbin;
 	struct ext_include_binary_context *binctx = 
 		ext_include_binary_get_context(sbin);
-	struct hash_iterate_context *hctx = 
-		hash_iterate_init(binctx->included_scripts);
+	struct hash_iterate_context *hctx;
 	void *key, *value;
 	unsigned int prvblk = 0;
-				
+
+	if ( !ext_include_variables_dump(denv, binctx->global_vars) )
+		return FALSE;
+
+	hctx = hash_iterate_init(binctx->included_scripts);		
 	while ( hash_iterate(hctx, &key, &value) ) {
 		struct _included_script *incscript = (struct _included_script *) value;
 
@@ -325,6 +349,6 @@ bool ext_include_binary_dump(struct sieve_dumptime_env *denv)
 	
 	hash_iterate_deinit(&hctx);
 	
-	return TRUE;	
+	return TRUE;
 }
 
diff --git a/src/lib-sieve/plugins/include/ext-include-binary.h b/src/lib-sieve/plugins/include/ext-include-binary.h
index 35cf716ae..36c27306c 100644
--- a/src/lib-sieve/plugins/include/ext-include-binary.h
+++ b/src/lib-sieve/plugins/include/ext-include-binary.h
@@ -6,7 +6,8 @@
 struct ext_include_binary_context;
 
 struct ext_include_binary_context *ext_include_binary_init
-	(struct sieve_binary *sbin);
+	(struct sieve_binary *sbin, struct sieve_ast *ast);
+
 void ext_include_binary_script_include
 	(struct ext_include_binary_context *binctx, struct sieve_script *script,
 		enum ext_include_script_location location, unsigned int block_id);
diff --git a/src/lib-sieve/plugins/include/ext-include-common.c b/src/lib-sieve/plugins/include/ext-include-common.c
index bab9ffec9..86a676652 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.c
+++ b/src/lib-sieve/plugins/include/ext-include-common.c
@@ -113,18 +113,20 @@ struct ext_include_ast_context *ext_include_create_ast_context
 
     pool_t pool = sieve_ast_pool(ast);
     actx = p_new(pool, struct ext_include_ast_context, 1);
-   	actx->import_vars = sieve_variable_scope_create(pool, &include_extension);
+   	actx->import_vars = sieve_variable_scope_create(&include_extension);
 	p_array_init(&actx->included_scripts, pool, 32);
 
     if ( parent != NULL ) {
         struct ext_include_ast_context *parent_ctx =
             (struct ext_include_ast_context *)
             sieve_ast_extension_get_context(parent, &include_extension);
-        actx->global_vars = ( parent_ctx == NULL ? NULL : parent_ctx->global_vars );
+        actx->global_vars = parent_ctx->global_vars;
 
-		if ( actx->global_vars != NULL )
-	        sieve_variable_scope_ref(actx->global_vars);
-    }
+		i_assert( actx->global_vars != NULL );
+
+        sieve_variable_scope_ref(actx->global_vars);
+	} else
+		actx->global_vars = sieve_variable_scope_create(&include_extension);			
 
 	sieve_ast_extension_register(ast, &include_ast_extension, (void *) actx);
 
@@ -191,18 +193,23 @@ static inline void ext_include_initialize_generator_context
 }
 
 void ext_include_register_generator_context
-(struct sieve_generator *gentr)
+(const struct sieve_codegen_env *cgenv)
 {
 	struct ext_include_generator_context *ctx = 
-		ext_include_get_generator_context(gentr);
-	struct sieve_script *script = sieve_generator_script(gentr);
+		ext_include_get_generator_context(cgenv->gentr);
 	
+	/* Initialize generator context if necessary */
 	if ( ctx == NULL ) {
-		ctx = ext_include_create_generator_context(gentr, NULL, script);
+		ctx = ext_include_create_generator_context(
+			cgenv->gentr, NULL, cgenv->script);
 		
 		sieve_generator_extension_set_context
-			(gentr, &include_extension, (void *) ctx);		
+			(cgenv->gentr, &include_extension, (void *) ctx);		
 	}
+
+	/* Initialize ast context if necessary */
+	(void)ext_include_get_ast_context(cgenv->ast);
+	(void)ext_include_binary_init(cgenv->sbin, cgenv->ast);
 }
 
 
@@ -319,8 +326,8 @@ bool ext_include_generate_include
 		pctx = pctx->parent;
 	}	
 
-	/* Initialize binary context */
-	binctx = ext_include_binary_init(sbin);
+	/* Get binary context */
+	binctx = ext_include_binary_init(sbin, cgenv->ast);
 
 	/* Is the script already compiled into the current binary? */
 	if ( !ext_include_binary_script_is_included(binctx, script, &inc_block_id) )	
@@ -364,8 +371,9 @@ bool ext_include_generate_include
 					"failed to generate code for included script '%s'", script_name);
 		 		result = FALSE;
 			}
-					
-			(void) sieve_binary_block_set_active(sbin, this_block_id, NULL); 	
+			
+			if ( sbin != NULL )		
+				(void) sieve_binary_block_set_active(sbin, this_block_id, NULL); 	
 			sieve_generator_free(&subgentr);
 		} else result = FALSE;
 		
diff --git a/src/lib-sieve/plugins/include/ext-include-common.h b/src/lib-sieve/plugins/include/ext-include-common.h
index 28daa04eb..fa005e49f 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.h
+++ b/src/lib-sieve/plugins/include/ext-include-common.h
@@ -69,7 +69,7 @@ void ext_include_ast_link_included_script
  */
 
 void ext_include_register_generator_context
-	(struct sieve_generator *gentr);
+	(const struct sieve_codegen_env *cgenv);
 
 bool ext_include_generate_include
 	(const struct sieve_codegen_env *cgenv, struct sieve_command_context *cmd,
diff --git a/src/lib-sieve/plugins/include/ext-include-variables.c b/src/lib-sieve/plugins/include/ext-include-variables.c
index 0d8d91c73..992b44800 100644
--- a/src/lib-sieve/plugins/include/ext-include-variables.c
+++ b/src/lib-sieve/plugins/include/ext-include-variables.c
@@ -1,15 +1,33 @@
 #include "sieve-common.h"
 #include "sieve-error.h"
 #include "sieve-ast.h"
+#include "sieve-binary.h"
 #include "sieve-commands.h"
 #include "sieve-validator.h"
 #include "sieve-generator.h"
 #include "sieve-interpreter.h"
+#include "sieve-dump.h"
+
 #include "sieve-ext-variables.h"
 
 #include "ext-include-common.h"
 #include "ext-include-variables.h"
 
+/*
+ * Types
+ */
+
+enum ext_include_variable_type {
+	EXT_INCLUDE_VAR_IMPORTED,
+	EXT_INCLUDE_VAR_EXPORTED,
+	EXT_INCLUDE_VAR_INVALID
+};
+
+struct ext_include_variable {
+	enum ext_include_variable_type type;
+	unsigned int source_line;
+};
+
 /* 
  * Variable import-export
  */
@@ -21,43 +39,175 @@ bool ext_include_variable_import_global
 	struct sieve_ast *ast = cmd->ast_node->ast;
 	struct ext_include_ast_context *ctx = ext_include_get_ast_context(ast);
 	struct sieve_variable_scope *main_scope;
-	struct sieve_variable *var;
+	struct sieve_variable *var = NULL, *impvar = NULL;
 
-	if ( export ) {	
-		if ( sieve_variable_scope_get_variable(ctx->import_vars, variable, FALSE) 
-			!= NULL ) {
+	/* Check if the requested variable was imported already */
+	if ( export && 
+		(impvar=sieve_variable_scope_get_variable(ctx->import_vars, variable, FALSE))
+		!= NULL ) {
+		if ( export ) {
+			/* Yes, and now export is attempted. ERROR */
 			sieve_command_validate_error(valdtr, cmd, 
 				"cannot export imported variable '%s'", variable);
 			return FALSE;
+		} else {
+			/* Yes, and it is imported again. Warn the user */
+			if ( impvar->context != NULL ) {
+				struct ext_include_variable *varctx = 
+					(struct ext_include_variable *) impvar->context;
+				sieve_command_validate_warning(valdtr, cmd,
+	                "variable '%s' already imported earlier at line %d", variable, 
+					varctx->source_line);
+			}
 		}
-	
-		if ( ctx->global_vars == NULL ) {
-			pool_t pool = sieve_ast_pool(ast);
-			ctx->global_vars = sieve_variable_scope_create(pool, &include_extension);
-		}
-		
-		var = sieve_variable_scope_get_variable(ctx->global_vars, variable, TRUE);
-		
-	} else {
-		var = NULL;
-		
-		if ( ctx->global_vars != NULL ) {
-			var = sieve_variable_scope_get_variable
-				(ctx->global_vars, variable, FALSE);
-		}
-	
-		if ( var == NULL ) {
-			sieve_command_validate_error(valdtr, cmd, 
-				"importing unknown global variable '%s'", variable);
-			return FALSE;
-		}
-		
-		(void)sieve_variable_scope_declare(ctx->import_vars, variable);
 	}
 
+	/* Sanity safeguard */	
+	i_assert ( ctx->global_vars != NULL );
+		
+	/* Get/Declare the variable in the global scope */
+	var = sieve_variable_scope_get_variable(ctx->global_vars, variable, TRUE);
+
+	/* Assign context for creation of symbol block during code generation */
+	if ( var->context == NULL ) {
+		pool_t pool = sieve_variable_scope_pool(ctx->global_vars);
+		struct ext_include_variable *varctx;
+
+		/* We only record data from the first encounter */
+		varctx = p_new(pool, struct ext_include_variable, 1);
+		varctx->type = export ? 
+			EXT_INCLUDE_VAR_EXPORTED : EXT_INCLUDE_VAR_IMPORTED;
+		varctx->source_line = cmd->ast_node->source_line;
+		var->context = varctx;
+	}
+	
+	/* Import the global variable into the local script scope */	
 	main_scope = sieve_ext_variables_get_main_scope(valdtr);
 	(void)sieve_variable_scope_import(main_scope, var);
+
+	/* If this is an import it needs to be registered to detect duplicates */
+	if ( !export && impvar == NULL ) { 
+		pool_t pool = sieve_variable_scope_pool(ctx->import_vars);
+		struct ext_include_variable *varctx;
+
+		impvar = sieve_variable_scope_declare(ctx->import_vars, variable);		
+		varctx = p_new(pool, struct ext_include_variable, 1);
+        varctx->type = EXT_INCLUDE_VAR_IMPORTED;
+        varctx->source_line = cmd->ast_node->source_line;
+        impvar->context = varctx;
+	}
+
 	return TRUE;	
 }
 
 
+bool ext_include_variables_save
+	(struct sieve_binary *sbin, struct sieve_variable_scope *global_vars)
+{
+	unsigned int count = sieve_variable_scope_size(global_vars);
+
+	sieve_binary_emit_integer(sbin, count);
+
+	if ( count > 0 ) {
+		unsigned int size, i;
+		struct sieve_variable *const *vars = 
+			sieve_variable_scope_get_variables(global_vars, &size);
+
+		for ( i = 0; i < size; i++ ) {
+			struct ext_include_variable *varctx =
+				(struct ext_include_variable *) vars[i]->context;
+
+    	    sieve_binary_emit_byte(sbin, varctx->type);
+			sieve_binary_emit_integer(sbin, varctx->source_line);
+			sieve_binary_emit_cstring(sbin, vars[i]->identifier);
+		}
+    }
+
+	return TRUE;
+}
+
+bool ext_include_variables_load
+(struct sieve_binary *sbin, sieve_size_t *offset, unsigned int block,
+	struct sieve_variable_scope **global_vars_r)
+{
+	sieve_size_t count = 0;
+	unsigned int i;
+	pool_t pool;
+
+	/* Sanity assert */
+	i_assert( *global_vars_r == NULL );
+
+	if ( !sieve_binary_read_integer(sbin, offset, &count) ) {
+        sieve_sys_error("include: failed to read global variables count "
+            "from dependency block %d of binary %s", block, sieve_binary_path(sbin));
+        return FALSE;
+    }
+
+	*global_vars_r = sieve_variable_scope_create(&include_extension);
+	pool = sieve_variable_scope_pool(*global_vars_r);
+
+    /* Read global variable scope */
+    for ( i = 0; i < count; i++ ) {
+		struct sieve_variable *var;
+        struct ext_include_variable *varctx;
+		enum ext_include_variable_type type;
+		sieve_size_t source_line;
+		string_t *identifier;
+
+        if (
+            !sieve_binary_read_byte(sbin, offset, &type) ||
+            !sieve_binary_read_integer(sbin, offset, &source_line) ||
+            !sieve_binary_read_string(sbin, offset, &identifier) ) {
+            /* Binary is corrupt, recompile */
+            sieve_sys_error("include: failed to read global variable specification "
+                "from dependency block %d of binary %s", block, sieve_binary_path(sbin));
+            return FALSE;
+        }
+
+        if ( type >= EXT_INCLUDE_VAR_INVALID ) {
+            /* Binary is corrupt, recompile */
+            sieve_sys_error("include: dependency block %d of binary %s "
+                "reports invalid global variable type (id %d).",
+                block, sieve_binary_path(sbin), type);
+            return FALSE;
+        }
+
+        var = sieve_variable_scope_declare(*global_vars_r, str_c(identifier));
+        varctx = p_new(pool, struct ext_include_variable, 1);
+        varctx->type = type;
+        varctx->source_line = source_line;
+        var->context = varctx;
+
+		i_assert(var->index == i);
+    }
+	
+    return TRUE;
+}
+
+bool ext_include_variables_dump
+(struct sieve_dumptime_env *denv, struct sieve_variable_scope *global_vars)
+{
+	unsigned int size;
+	struct sieve_variable *const *vars;
+
+	i_assert(global_vars != NULL);
+
+	vars = sieve_variable_scope_get_variables(global_vars, &size);
+
+	if ( size > 0 ) {
+		unsigned int i;
+
+		sieve_binary_dump_sectionf(denv, "Global variables");
+	
+		for ( i = 0; i < size; i++ ) {
+			struct ext_include_variable *varctx =
+				(struct ext_include_variable *) vars[i]->context;
+
+			sieve_binary_dumpf(denv, "%3d: %s '%s'\n", i, 
+				varctx->type == EXT_INCLUDE_VAR_EXPORTED ? "export" : "import", 
+				vars[i]->identifier);
+		}	
+	}
+
+	return TRUE;
+}
diff --git a/src/lib-sieve/plugins/include/ext-include-variables.h b/src/lib-sieve/plugins/include/ext-include-variables.h
index 22f5d4853..764f63785 100644
--- a/src/lib-sieve/plugins/include/ext-include-variables.h
+++ b/src/lib-sieve/plugins/include/ext-include-variables.h
@@ -2,6 +2,7 @@
 #define __EXT_INCLUDE_VARIABLES_H
 
 #include "sieve-common.h"
+
 #include "sieve-ext-variables.h"
 
 #include "ext-include-common.h"
@@ -13,6 +14,18 @@
 bool ext_include_variable_import_global
 	(struct sieve_validator *valdtr, struct sieve_command_context *cmd, 
 		const char *variable, bool export);
+
+/*
+ * Binary symbol table
+ */
+
+bool ext_include_variables_save
+    (struct sieve_binary *sbin, struct sieve_variable_scope *global_vars);
+bool ext_include_variables_load
+	(struct sieve_binary *sbin, sieve_size_t *offset, unsigned int block,
+    	struct sieve_variable_scope **global_vars_r);
+bool ext_include_variables_dump
+	(struct sieve_dumptime_env *denv, struct sieve_variable_scope *global_vars);
 		
 #endif /* __EXT_INCLUDE_VARIABLES_H */
 
diff --git a/src/lib-sieve/plugins/include/ext-include.c b/src/lib-sieve/plugins/include/ext-include.c
index 1a51cb4f3..a7c444f0a 100644
--- a/src/lib-sieve/plugins/include/ext-include.c
+++ b/src/lib-sieve/plugins/include/ext-include.c
@@ -31,7 +31,7 @@
 
 static bool ext_include_load(int ext_id);
 static bool ext_include_validator_load(struct sieve_validator *validator);
-static bool ext_include_generator_load(struct sieve_generator *gentr);
+static bool ext_include_generator_load(const struct sieve_codegen_env *cgenv);
 static bool ext_include_binary_load(struct sieve_binary *sbin);
 static bool ext_include_interpreter_load(struct sieve_interpreter *interp);
 
@@ -85,9 +85,9 @@ static bool ext_include_validator_load(struct sieve_validator *validator)
 
 /* Load extension into generator */
 
-static bool ext_include_generator_load(struct sieve_generator *gentr)
+static bool ext_include_generator_load(const struct sieve_codegen_env *cgenv)
 {
-	ext_include_register_generator_context(gentr);
+	ext_include_register_generator_context(cgenv);
 
 	return TRUE;
 }
diff --git a/src/lib-sieve/plugins/variables/ext-variables-common.c b/src/lib-sieve/plugins/variables/ext-variables-common.c
index 70aef5dd7..a26c21144 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-common.c
+++ b/src/lib-sieve/plugins/variables/ext-variables-common.c
@@ -46,15 +46,22 @@ struct sieve_variable_scope {
 
 	const struct sieve_extension *ext;
 
-	unsigned int next_index;
 	struct hash_table *variables;
+	ARRAY_DEFINE(variable_index, struct sieve_variable *);
+};
+
+struct sieve_variable_scope_iter {
+	struct sieve_variable_scope *scope;
+	struct hash_iterate_context *hctx;
 };
 
 struct sieve_variable_scope *sieve_variable_scope_create
-	(pool_t pool, const struct sieve_extension *ext) 
+	(const struct sieve_extension *ext) 
 {
 	struct sieve_variable_scope *scope;
-	
+	pool_t pool;
+
+	pool = pool_alloconly_create("sieve_variable_scope", 4096);
 	scope = p_new(pool, struct sieve_variable_scope, 1);
 	scope->pool = pool;
 	scope->refcount = 1;
@@ -62,6 +69,7 @@ struct sieve_variable_scope *sieve_variable_scope_create
 	scope->ext = ext;
 	scope->variables = hash_create
 		(default_pool, pool, 0, strcase_hash, (hash_cmp_callback_t *)strcasecmp);
+	p_array_init(&scope->variable_index, pool, 128);
 		
 	return scope;
 }
@@ -79,18 +87,26 @@ void sieve_variable_scope_unref(struct sieve_variable_scope **scope)
         return;
 
 	hash_destroy(&(*scope)->variables);
+
+	pool_unref(&(*scope)->pool);
     *scope = NULL;
 }
 
+pool_t sieve_variable_scope_pool(struct sieve_variable_scope *scope)
+{
+	return scope->pool;
+}
+
 struct sieve_variable *sieve_variable_scope_declare
 (struct sieve_variable_scope *scope, const char *identifier)
 {
 	struct sieve_variable *new_var = p_new(scope->pool, struct sieve_variable, 1);
 	new_var->identifier = p_strdup(scope->pool, identifier);
-	new_var->index = scope->next_index++;
+	new_var->index = array_count(&scope->variable_index);
 	new_var->ext = scope->ext;
 
 	hash_insert(scope->variables, (void *) new_var->identifier, (void *) new_var);
+	array_append(&scope->variable_index, &new_var, 1);
 	
 	return new_var;
 }
@@ -117,9 +133,59 @@ struct sieve_variable *sieve_variable_scope_import
 		
 	hash_insert(scope->variables, (void *) new_var->identifier, (void *) new_var);
 	
+	/* Not entered into the index because it is an external variable */
+
 	return new_var;
 }
 
+struct sieve_variable_scope_iter *sieve_variable_scope_iterate_init
+(struct sieve_variable_scope *scope)
+{
+	struct sieve_variable_scope_iter *iter = t_new(struct sieve_variable_scope_iter, 1);
+	iter->scope = scope;
+	iter->hctx = hash_iterate_init(scope->variables);
+
+	return iter;
+}
+
+bool sieve_variable_scope_iterate
+(struct sieve_variable_scope_iter *iter, struct sieve_variable **var_r)
+{
+	void *key, *value;
+
+	if ( !hash_iterate(iter->hctx, &key, &value) )
+		return FALSE; 
+	
+	*var_r = (struct sieve_variable *) value;
+	return TRUE;
+}
+
+void sieve_variable_scope_iterate_deinit
+(struct sieve_variable_scope_iter **iter)
+{
+	hash_iterate_deinit(&(*iter)->hctx);
+	*iter = NULL;
+}
+
+unsigned int sieve_variable_scope_declarations
+(struct sieve_variable_scope *scope)
+{
+	return hash_count(scope->variables);
+}
+
+unsigned int sieve_variable_scope_size
+(struct sieve_variable_scope *scope)
+{
+    return array_count(&scope->variable_index);
+}
+
+struct sieve_variable * const *sieve_variable_scope_get_variables
+(struct sieve_variable_scope *scope, unsigned int *size_r)
+{
+	return array_get(&scope->variable_index, size_r);
+}
+
+
 /* Variable storage */
 
 struct sieve_variable_storage {
@@ -204,9 +270,8 @@ static struct sieve_variable_scope *ext_variables_create_main_scope
 (struct sieve_ast *ast)
 {
     struct sieve_variable_scope *scope;
-    pool_t pool = sieve_ast_pool(ast);
 
-	scope = sieve_variable_scope_create(pool, NULL);
+	scope = sieve_variable_scope_create(NULL);
 
     sieve_ast_extension_register(ast, &variables_ast_extension, (void *) scope);
 
diff --git a/src/lib-sieve/plugins/variables/sieve-ext-variables.h b/src/lib-sieve/plugins/variables/sieve-ext-variables.h
index 65068725b..be41057b8 100644
--- a/src/lib-sieve/plugins/variables/sieve-ext-variables.h
+++ b/src/lib-sieve/plugins/variables/sieve-ext-variables.h
@@ -16,17 +16,21 @@
 struct sieve_variable {
 	const char *identifier;
 	unsigned int index;
+
 	const struct sieve_extension *ext;
+	void *context;
 };
 
 struct sieve_variable_scope;
 
 struct sieve_variable_scope *sieve_variable_scope_create
-	(pool_t pool, const struct sieve_extension *ext);
+	(const struct sieve_extension *ext);
 void sieve_variable_scope_ref
 	(struct sieve_variable_scope *scope);
 void sieve_variable_scope_unref
 	(struct sieve_variable_scope **scope);
+pool_t sieve_variable_scope_pool
+	(struct sieve_variable_scope *scope);
 
 struct sieve_variable *sieve_variable_scope_declare
 	(struct sieve_variable_scope *scope, const char *identifier);
@@ -34,7 +38,31 @@ struct sieve_variable *sieve_variable_scope_import
 	(struct sieve_variable_scope *scope, struct sieve_variable *var);
 struct sieve_variable *sieve_variable_scope_get_variable
 	(struct sieve_variable_scope *scope, const char *identifier, bool create);
-	
+
+/* Iteration over all declared variables */
+
+struct sieve_variable_scope_iter;
+
+struct sieve_variable_scope_iter *sieve_variable_scope_iterate_init
+	(struct sieve_variable_scope *scope);
+bool sieve_variable_scope_iterate
+	(struct sieve_variable_scope_iter *iter, struct sieve_variable **var_r);
+void sieve_variable_scope_iterate_deinit
+	(struct sieve_variable_scope_iter **iter);
+
+/* Statistics */
+
+unsigned int sieve_variable_scope_declarations
+	(struct sieve_variable_scope *scope);
+unsigned int sieve_variable_scope_size
+	(struct sieve_variable_scope *scope);
+
+/* Get all native variables */
+
+struct sieve_variable * const *sieve_variable_scope_get_variables
+	(struct sieve_variable_scope *scope, unsigned int *size_r);
+
+
 /* 
  * Variable storage
  */	
diff --git a/src/lib-sieve/sieve-binary-dumper.c b/src/lib-sieve/sieve-binary-dumper.c
index d0cf208c7..593483b2d 100644
--- a/src/lib-sieve/sieve-binary-dumper.c
+++ b/src/lib-sieve/sieve-binary-dumper.c
@@ -77,7 +77,7 @@ void sieve_binary_dump_sectionf
 
 /* Dumper execution */
 
-void sieve_binary_dumper_run
+bool sieve_binary_dumper_run
 	(struct sieve_binary_dumper *dumper, struct ostream *stream) 
 {	
 	struct sieve_binary *sbin = dumper->dumpenv.sbin;
@@ -95,20 +95,10 @@ void sieve_binary_dumper_run
 		for ( i = 0; i < count; i++ ) {
 			const struct sieve_extension *ext = sieve_binary_extension_get_by_index
 				(sbin, i);
-			sieve_binary_dumpf(denv, "%d: %s (%d)\n", i, ext->name, *ext->id);
+			sieve_binary_dumpf(denv, "%3d: %s (%d)\n", i, ext->name, *ext->id);
 		}
 	}
-	
-	/* Dump main program */
-	
-	sieve_binary_dump_sectionf(denv, "Main program");
-	
-	dumper->dumpenv.cdumper = sieve_code_dumper_create(&(dumper->dumpenv));
 
-	sieve_code_dumper_run(dumper->dumpenv.cdumper);
-		
-	sieve_code_dumper_free(&dumper->dumpenv.cdumper);	
-	
 	/* Dump extension-specific elements of the binary */
 	
 	count = sieve_binary_extensions_count(sbin);
@@ -118,11 +108,28 @@ void sieve_binary_dumper_run
 				(sbin, i);
 	
 			if ( ext->binary_dump != NULL ) {	
-				if ( !ext->binary_dump(denv) ) break;
+				if ( !ext->binary_dump(denv) )
+					return FALSE;
 			}
 		}
 	}
 	
+	/* Dump main program */
+	
+	sieve_binary_dump_sectionf(denv, "Main program");
+
+	if ( !sieve_binary_block_set_active(sbin, SBIN_SYSBLOCK_MAIN_PROGRAM, NULL) ) {
+        return FALSE;
+	}
+
+	dumper->dumpenv.cdumper = sieve_code_dumper_create(&(dumper->dumpenv));
+
+	sieve_code_dumper_run(dumper->dumpenv.cdumper);
+		
+	sieve_code_dumper_free(&dumper->dumpenv.cdumper);	
+	
 	/* Finish with empty line */
 	sieve_binary_dumpf(denv, "\n");
+
+	return TRUE;
 }
diff --git a/src/lib-sieve/sieve-binary-dumper.h b/src/lib-sieve/sieve-binary-dumper.h
index 1ce0b0d9f..62f30290b 100644
--- a/src/lib-sieve/sieve-binary-dumper.h
+++ b/src/lib-sieve/sieve-binary-dumper.h
@@ -22,7 +22,7 @@ void sieve_binary_dump_sectionf
 
 /* Dumper execution */
 
-void sieve_binary_dumper_run
+bool sieve_binary_dumper_run
 	(struct sieve_binary_dumper *dumper, struct ostream *stream);
 
 
diff --git a/src/lib-sieve/sieve-extensions.h b/src/lib-sieve/sieve-extensions.h
index df13517ec..3160f902f 100644
--- a/src/lib-sieve/sieve-extensions.h
+++ b/src/lib-sieve/sieve-extensions.h
@@ -20,7 +20,7 @@ struct sieve_extension {
 	bool (*load)(int ext_id);
 
 	bool (*validator_load)(struct sieve_validator *validator);	
-	bool (*generator_load)(struct sieve_generator *generator);
+	bool (*generator_load)(const struct sieve_codegen_env *cgenv);
 	bool (*interpreter_load)(struct sieve_interpreter *interpreter);
 	
 	bool (*runtime_load)(const struct sieve_runtime_env *renv);
diff --git a/src/lib-sieve/sieve-generator.c b/src/lib-sieve/sieve-generator.c
index 994110b1f..5e992b2aa 100644
--- a/src/lib-sieve/sieve-generator.c
+++ b/src/lib-sieve/sieve-generator.c
@@ -160,7 +160,7 @@ bool sieve_generator_link_extension
 	(void)sieve_binary_extension_link(gentr->genenv.sbin, ext);
 	
 	if ( ext->generator_load != NULL )
-		return ext->generator_load(gentr);
+		return ext->generator_load(&gentr->genenv);
 	
 	return TRUE;
 }
diff --git a/src/testsuite/ext-testsuite.c b/src/testsuite/ext-testsuite.c
index dc8fb5a7a..b1acf786e 100644
--- a/src/testsuite/ext-testsuite.c
+++ b/src/testsuite/ext-testsuite.c
@@ -47,7 +47,7 @@
 
 static bool ext_testsuite_load(int ext_id);
 static bool ext_testsuite_validator_load(struct sieve_validator *valdtr);
-static bool ext_testsuite_generator_load(struct sieve_generator *gentr);
+static bool ext_testsuite_generator_load(const struct sieve_codegen_env *cgenv);
 static bool ext_testsuite_binary_load(struct sieve_binary *sbin);
 
 /* Commands */
@@ -117,9 +117,9 @@ static bool ext_testsuite_validator_load(struct sieve_validator *valdtr)
 
 /* Load extension into generator */
 
-static bool ext_testsuite_generator_load(struct sieve_generator *gentr)
+static bool ext_testsuite_generator_load(const struct sieve_codegen_env *cgenv)
 {
-	return testsuite_generator_context_initialize(gentr);
+	return testsuite_generator_context_initialize(cgenv->gentr);
 }
 
 /* Load extension into binary */
diff --git a/tests/extensions/include/errors.svtest b/tests/extensions/include/errors.svtest
index dc0c9814b..de8d22b7e 100644
--- a/tests/extensions/include/errors.svtest
+++ b/tests/extensions/include/errors.svtest
@@ -70,7 +70,7 @@ test "Variables" {
 		test_fail "compile should have failed";
 	}
 
-	if not test_error :count "eq" :comparator "i;ascii-numeric" "5" {
+	if not test_error :count "eq" :comparator "i;ascii-numeric" "4" {
 		test_fail "wrong number of errors reported";
 	}
 }
diff --git a/tests/extensions/include/errors/import-runtime.sieve b/tests/extensions/include/errors/import-runtime.sieve
new file mode 100644
index 000000000..3bca51501
--- /dev/null
+++ b/tests/extensions/include/errors/import-runtime.sieve
@@ -0,0 +1,15 @@
+/*
+ * Import runtime test
+ *
+ * Tests whether the import directive fails when importing variables that were
+ * never exported by a parent script or one of its sibblings. 
+ */
+require "include";
+require "variables";
+
+# This fails at runtime
+import "global";
+
+export "local";
+
+keep;
diff --git a/tests/extensions/include/errors/variables.sieve b/tests/extensions/include/errors/variables.sieve
index ac6e9a6bd..51997fc4b 100644
--- a/tests/extensions/include/errors/variables.sieve
+++ b/tests/extensions/include/errors/variables.sieve
@@ -1,18 +1,20 @@
 require "include";
 require "variables";
 
-# Importing unknown variable
+# Importing unknown variable, but not a compile-time error
 import "frop";
 
-# Importing unknown variable
+# Importing unknown variable, but not a compile-time error
 import ["friep", "frml"];
 
-# Not an error
+# Cannot export imported variable
 export ["friep"];
 
+# Import after export
 import "friep";
 
 keep;
 
+# Export after command not being require, import or export
 export "friep";
 
-- 
GitLab