Ada DLL导致system.secondary_stack.ss_mark中的Seg Fault

时间:2015-03-13 15:16:11

标签: ada

如何在我的DLL中修复此Seg Fault?

我正在生成一个Windows DLL(在Ada中)并使用来自Ada的DLL 程序。我正在为这两个DLL使用AdaCore的GNAT GPS v6.0.1 IDE 和一个测试DLL的Ada程序,在Windows 7机器上运行。 使用两个单独的项目文件,一个用于DLL,另一个用于 测试驱动程序。 DLL没有任何DLLMain也没有初始化 或最终确定程序。

作为第一步(因为我之前从未创建过DLL或使用过GPS,但确实知道一些Ada),我为DLL编写了两个非常简单的函数。一个函数返回一个指向字符串的指针,另一个函数返回一个固定长度的字符串。

测试程序成功调用返回固定长度的DLL函数 但是,当调用返回字符串指针的函数时,a 发生分段错误。这是gcc调试输出:

Program received signal SIGSEGV, Segmentation fault.
0x6b81dd2c in system.secondary_stack.ss_mark () from C:\GNAT\2014\bin\libgnat-2014.dll
(gdb) quit

以下是代码:

DLL Spec

with Ada.Strings.Fixed;            use Ada.Strings.Fixed;

package String_Utils is
   type String_Ptr_T is access String;
   type Spec_Str is new String (1..7);

   function Int_Trim_Left( IntToTrim : Integer) return String_Ptr_T;
   pragma Export(DLL, Int_Trim_Left, "String_Utils__Int_Trim_Left");

   function Spec( Input_Int : Integer) return Spec_Str;
   pragma Export(DLL, Spec, "String_Utils__Spec");

end String_Utils;

DLL Body

package body String_Utils is

   function Int_Trim_Left( IntToTrim : Integer) return String_Ptr_T is
      String_Ptr   : String_Ptr_T;
   begin
      Text_IO.Put_Line("About to call new String in DLL.");
      String_Ptr := new String'(
                               Ada.Strings.Fixed.Trim(Integer'Image(IntToTrim),
                                  Ada.Strings.Left));
      return String_Ptr;
   end;
   --
   function Spec( Input_Int : Integer) return Spec_Str
   is
      Result_Spec : String := "ABC-UNK";
   begin
      case Input_Int is
         when 1 => return "ABC-STD"; -- Standard
         when 2 => return "ABC-PRF"; -- Performance
         when 3 => return "DEF-DTL"; -- Detailed
         when Others => return "ABC-UNK";
      end case;
   end;

DLL项目文件

project HAGUtils is

   for Library_Name use "HAGUtils";
   for Library_Dir use "libdir";
   for Library_Version use "0.01";
   for Library_Kind use "dynamic";
   for Object_Dir use "obj";
   for Source_Dirs use ("src");
   for Source_Files use ("string_utils.adb", "string_utils.ads");

end HAGUtils;

测试驱动程序

-- Driver for DLL
with Text_IO;                       use Text_IO;

procedure test_ada_dll is

   type String_Ptr_T is access String;
   subtype String7 is String(1..7);

   input_val      : Integer := 0;
   Spec_Str       : String7 := (Others => ' ');
   Int_String_Ptr : String_Ptr_T:= null;

   -- Import
   function Int_Trim_Left ( IntToTrim : Integer) return String_Ptr_T
   is
      function Inner_Int_Trim_Left ( IntToTrim : Integer) return String_Ptr_T;
      pragma Import (DLL, Inner_Int_Trim_Left, "String_Utils__Int_Trim_Left");

   begin
      return Inner_Int_Trim_Left (IntToTrim);
   end Int_Trim_Left;

   -- Import
   function Spec ( Input_Int : Integer) return String7
   is
      function Inner_Spec ( Input_Int : Integer) return String7;
      pragma Import (DLL, Inner_Spec, "String_Utils__Spec");

   begin
      return Inner_Spec (Input_Int);
   end Spec;

begin
   input_val := 3;
   Spec_Str := Spec(input_val);
   Text_IO.Put_Line("The Spec is -- " & Spec_Str);

   Text_IO.Put_Line("Calling Int_Trim_Left with --" & Integer'Image(input_val));
   Int_String_Ptr :=  Int_Trim_Left(input_val);
   Text_IO.Put_Line("After call  --" & Int_String_Ptr.all);
end;

1 个答案:

答案 0 :(得分:4)

认为发生了SEGV,因为您的DLL未初始化。 Ada运行时系统需要初始化,在没有DLL的情况下将在GNAT绑定过程中调用(您可能已经看到调用gnatbindgprbind闪烁屏幕)。

但是,你有一个DLL需要初始化RTS(处理辅助堆栈的部分,这是GNAT构造临时不受约束的对象,如字符串);但由于您链接程序的方式,绑定器不知道这一点(您没有说,但我怀疑您已通过-lHAGutils指定了DLL?)。

让GNAT为你处理这个问题的方法是为测试程序编写一个项目文件并让它with你的DLL项目:

with "HAGutils";
project Test_Ada_Dll is
for Main use ("test_ada_dll.adb");
  for Exec_Dir use ".";
  for Source_Files use ("test_ada_dll.adb");
  for Object_Dir use ".build";
end Test_Ada_Dll;

这会使HAGlib的界面对test_ada_dll可见,因此您可以将其更改为

with Text_IO;                       use Text_IO;
with String_Utils;

procedure test_ada_dll is

   input_val      : Integer := 0;
   Spec_Str       : String_Utils.Spec_Str := (Others => ' ');
   Int_String_Ptr : String_Utils.String_Ptr_T:= null;

begin
   input_val := 3;
   Spec_Str := String_Utils.Spec(input_val);
   Text_IO.Put_Line("The Spec is -- " & String (Spec_Str));

   Text_IO.Put_Line("Calling Int_Trim_Left with --" & Integer'Image(input_val));
   Int_String_Ptr :=  String_Utils.Int_Trim_Left(input_val);
   Text_IO.Put_Line("After call  --" & Int_String_Ptr.all);
end;

(请注意,Text_IO.Put_Line("The Spec is -- " & String (Spec_Str));中的转换是因为Spec_Str是派生类型;我认为在这种情况下使其成为子类型更为正常。)

此外,您不再需要在pragma Export的规范中使用String_Utils


结果是绑定器知道HAGutils DLL的属性,并可以安排必要的初始化。


您可以通过某种方式使原始代码正常工作,即使用Library_Auto_Init中的GPR属性HAGutils.gpr

for Library_Auto_Init use “true”;

但我认为你必须让HAGlib成为合适的standalone library。这是非常复杂的正确,而不是让图书馆工作开始。