MATLAB 任意代码执行

发布于 2024-08-25 11:12:14 字数 757 浏览 6 评论 0原文

我正在linux下写一个自动评分器程序。有几个用 MATLAB 编写的评分器,所以我想将它们全部捆绑在一起,让学生运行一个程序来完成作业,并让他们选择作业。我正在使用 C++ 主程序,然后将 mcc 编译的 MATLAB 库链接到它。

具体来说,我的程序读取一个配置文件,其中包含各种 matlab 程序的名称和其他信息。然后它使用该信息向学生提供选择。因此,如果分配发生更改、添加或删除,那么您所要做的就是更改配置文件。

这个想法是,接下来,程序调用已使用 mcc 编译的正确 matlab 库。但是,这意味着如果评分器发生变化,则必须重新编译库。更糟糕的是,如果添加或删除评分器,则必须重新编译整个程序。因此,我想要一个简单的、不变的 matlab 库函数来直接调用分级机 m 文件。我目前有这样一个库,它对从主程序传递给它的字符串使用 eval 。

问题是,当我这样做时,显然,mcc 将分级机 m 代码吸收到自身中;编译后更改 Grader m 代码没有任何效果。我希望这种事不要发生。我注意到 Mathworks 可能不希望我能够这样做,因为它可以完全绕过 matlab。这不是我的意图,我会很高兴有一个需要完整安装 matlab 的解决方案。

我可能的解决方案是为主程序使用 mex 文件,或者让主程序调用 mcc 库,然后调用 mex 文件,然后调用正确的评分器。我对第一个解决方案犹豫不决的原因是我不确定需要对代码进行多少更改才能使其工作;我的代码是 C++,而不是 C,我认为这让事情变得更加复杂。然而,第二个解决方案可能更复杂,并且最终会出现同样的问题。

那么,对于这种情况有什么想法吗?我该怎么做?

I am writing an automatic grader program under linux. There are several graders written in MATLAB, so I want to tie them all together and let students run a program to do an assignment, and have them choose the assignment. I am using a C++ main program, which then has mcc-compiled MATLAB libraries linked to it.

Specifically, my program reads a config file for the names of the various matlab programs, and other information. It then uses that information to present choices to the student. So, If an assignment changes, is added or removed, then all you have to do is change the config file.

The idea is that next, the program invokes the correct matlab library that has been compiled with mcc. But, that means that the libraries have to be recompiled if a grader gets changed. Worse, the whole program must be recompiled if a grader is added or removed. So, I would like one, simple, unchanging matlab library function to call the grader m-files directly. I currently have such a library, that uses eval on a string passed to it from the main program.

The problem is that when I do this, apparently, mcc absorbs the grader m-code into itself; changing the grader m code after compilation has no effect. I would like for this not to happen. It was brought to my attention that Mathworks may not want me to be able to do this, since it could bypass matlab entirely. That is not my intention, and I would be happy with a solution that requires a full matlab install.

My possible solutions are to use a mex file for the main program, or have the main program call a mcc library, which then calls a mex file, which then calls the proper grader. The reason I am hesitant about the first solution is that I'm not sure how many changes I would have to make to my code to make it work; my code is C++, not C, which I think makes things more complicated. The 2nd solution, though, may just be more complicated and ultimately have the same problem.

So, any thoughts on this situation? How should I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朮生 2024-09-01 11:12:14

您似乎选择了最复杂的方法来解决问题。以下是一些替代方案:

  1. 根本不使用 C/C++ -- 编写 MATLAB 程序来显示选择菜单(MATLAB 命令窗口中的简单文本菜单的 GUI),然后调用适当的 MATLAB完全

  2. 用 C/C++ 编写菜单程序,但使用 -r 参数调用 MATLAB 来运行特定的评分程序(要加快启动时间,请根据需要使用 -nodesktop、-nojvm 或 -nodisplay 选项)。但是,请注意,MATLAB 将在每次菜单选择时重新启动。

  3. 用 C/C++ 编写菜单程序并使用 popen< /a> 命令(这会在 C++ 程序和 MATLAB 进程之间建立一条管道)。用户选择菜单后:

更新:上面的选项#3实际上是MATLAB 引擎 可以工作,因此您最好直接使用它。

You seem to have picked the most complicated way of solving the problem. Here are some alternatives:

  1. Don't use C/C++ at all -- Write a MATLAB program to display the menu of choices (either a GUI for a simple text menu in the MATLAB command window) and then invoke the appropriate MATLAB grading programs.

  2. Write your menu program in C/C++, but invoke MATLAB using a -r argument to run a specific grading program (to speed up the startup times, use the -nodesktop, -nojvm or -nodisplay options as appropriate). However, note that MATLAB will be started anew on each menu selection.

  3. Write your menu program in C/C++ and start MATLAB using the popen command (this sets up a pipe between your C++ program and the MATLAB process). After a menu selection by the user:

    • your C++ program writes the name of the MATLAB program (and any parameters) to the pipe.
    • On the MATLAB side, write a MATLAB program to a blocking read on that pipe. When it reads a command, it invokes the appropriate MATLAB function.
    • You could also use named pipes. See this MATLAB newsgroup thread for more information.

Update: Option #3 above is effectively how the MATLAB engine works, so you are probably better off using that directly.

如何视而不见 2024-09-01 11:12:14

不要将此设为 mex 函数。

使用必须在 matlab 中执行的常规 m 文件。如果您不想先启动 matlab,请编写一个 bat 文件。我相信 -r 或 -m 运行给定的命令(在运行 ml 函数之前,您必须 cd 到正确的目录)。

要使用 mex 编译 C++ 代码,首先安装 Visual Studio。然后运行(在 matlab 中)mex -setup。选择“找到已安装的编译器”或类似选项,然后从列表中选择您的编译器。现在mex将编译c++代码。

Don't make this a mex function.

Use a regular m-file that has to be executed in matlab. If you don't want to launch matlab first, write a bat file. I believe -r or -m runs a given command (you will have to cd to the correct directory before running you ml function).

To compile c++ code using mex first install visual studio. Then run (in matlab) mex -setup. Select "locate installed compilers" or some such, and then select your compiler from the list. Now mex will compile c++ code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文