Skip to content

Commit 20af2d4

Browse files
committed
[Comgr] Failure to build driver results in undefined-behaviour
Change-Id: I4373dd9f51b724f83d11864a8fa5e561a3858548
1 parent 9ed0b0c commit 20af2d4

File tree

4 files changed

+107
-5
lines changed

4 files changed

+107
-5
lines changed

amd/comgr/src/comgr-compiler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,10 @@ AMDGPUCompiler::executeInProcessDriver(ArrayRef<const char *> Args) {
686686
}
687687

688688
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
689-
if (!C) {
690-
return C->containsError() ? AMD_COMGR_STATUS_ERROR
691-
: AMD_COMGR_STATUS_SUCCESS;
689+
if (!C || C->containsError()) {
690+
return AMD_COMGR_STATUS_ERROR;
692691
}
692+
693693
for (auto &Job : C->getJobs()) {
694694
auto Arguments = Job.getArguments();
695695
SmallVector<const char *, 128> Argv;

amd/comgr/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ add_comgr_test(get_data_isa_name_test c)
224224
add_comgr_test(include_subdirectory_test c)
225225
add_comgr_test(options_test c)
226226
add_comgr_test(demangle_test c)
227+
add_comgr_test(fail_to_build_driver c)
227228
add_comgr_test(file_map c)
228229
add_comgr_test(lookup_code_object_test c)
229230
add_comgr_test(symbolize_test c)

amd/comgr/test/common.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ int setBuf(const char *infile, char **buf) {
9292
return size;
9393
}
9494

95-
void checkError(amd_comgr_status_t status, const char *str) {
96-
if (status != AMD_COMGR_STATUS_SUCCESS) {
95+
void checkStatus(amd_comgr_status_t status, amd_comgr_status_t expected,
96+
const char *str) {
97+
if (status != expected) {
9798
const char *statusStr;
9899
printf("FAILED: %s\n", str);
99100
status = amd_comgr_status_string(status, &statusStr);
@@ -103,6 +104,10 @@ void checkError(amd_comgr_status_t status, const char *str) {
103104
}
104105
}
105106

107+
void checkError(amd_comgr_status_t status, const char *str) {
108+
checkStatus(status, AMD_COMGR_STATUS_SUCCESS, str);
109+
}
110+
106111
void dumpData(amd_comgr_data_t Data, const char *OutFile) {
107112
size_t size;
108113
char *bytes = NULL;

amd/comgr/test/fail_to_build_driver.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*******************************************************************************
2+
*
3+
* University of Illinois/NCSA
4+
* Open Source License
5+
*
6+
* Copyright (c) 2018 Advanced Micro Devices, Inc. All Rights Reserved.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* with the Software without restriction, including without limitation the
11+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12+
* sell copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* * Redistributions of source code must retain the above copyright notice,
16+
* this list of conditions and the following disclaimers.
17+
*
18+
* * Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimers in the
20+
* documentation and/or other materials provided with the distribution.
21+
*
22+
* * Neither the names of Advanced Micro Devices, Inc. nor the names of its
23+
* contributors may be used to endorse or promote products derived from
24+
* this Software without specific prior written permission.
25+
*
26+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
32+
* THE SOFTWARE.
33+
*
34+
******************************************************************************/
35+
36+
#include "amd_comgr.h"
37+
#include "common.h"
38+
#include <stdio.h>
39+
#include <stdlib.h>
40+
#include <string.h>
41+
42+
int main(int argc, char *argv[]) {
43+
char *BufSource1;
44+
size_t SizeSource1, SizeSource2, SizeInclude;
45+
amd_comgr_data_t DataSource1, DataSource2, DataInclude;
46+
amd_comgr_data_set_t DataSetIn, DataSetBc;
47+
amd_comgr_action_info_t DataAction;
48+
amd_comgr_status_t Status;
49+
size_t Count;
50+
const char *CodeGenOptions[] = {"-this-is-a-non-existent-flag"};
51+
size_t CodeGenOptionsCount =
52+
sizeof(CodeGenOptions) / sizeof(CodeGenOptions[0]);
53+
54+
SizeSource1 = setBuf(TEST_OBJ_DIR "/source1.cl", &BufSource1);
55+
56+
Status = amd_comgr_create_data_set(&DataSetIn);
57+
checkError(Status, "amd_comgr_create_data_set");
58+
59+
Status = amd_comgr_create_data(AMD_COMGR_DATA_KIND_SOURCE, &DataSource1);
60+
checkError(Status, "amd_comgr_create_data");
61+
Status = amd_comgr_set_data(DataSource1, SizeSource1, BufSource1);
62+
checkError(Status, "amd_comgr_set_data");
63+
Status = amd_comgr_set_data_name(DataSource1, "source1.cl");
64+
checkError(Status, "amd_comgr_set_data_name");
65+
Status = amd_comgr_data_set_add(DataSetIn, DataSource1);
66+
checkError(Status, "amd_comgr_data_set_add");
67+
68+
Status = amd_comgr_create_action_info(&DataAction);
69+
checkError(Status, "amd_comgr_create_action_info");
70+
Status = amd_comgr_action_info_set_language(DataAction,
71+
AMD_COMGR_LANGUAGE_OPENCL_1_2);
72+
checkError(Status, "amd_comgr_action_info_set_language");
73+
Status = amd_comgr_action_info_set_isa_name(DataAction,
74+
"amdgcn-amd-amdhsa--gfx900");
75+
checkError(Status, "amd_comgr_action_info_set_isa_name");
76+
Status = amd_comgr_action_info_set_option_list(DataAction, CodeGenOptions,
77+
CodeGenOptionsCount);
78+
checkError(Status, "amd_comgr_action_info_set_option_list");
79+
80+
Status = amd_comgr_create_data_set(&DataSetBc);
81+
checkError(Status, "amd_comgr_create_data_set");
82+
83+
Status = amd_comgr_do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_TO_BC,
84+
DataAction, DataSetIn, DataSetBc);
85+
checkStatus(Status, AMD_COMGR_STATUS_ERROR, "amd_comgr_do_action");
86+
87+
Status = amd_comgr_release_data(DataSource1);
88+
checkError(Status, "amd_comgr_release_data");
89+
Status = amd_comgr_destroy_data_set(DataSetIn);
90+
checkError(Status, "amd_comgr_destroy_data_set");
91+
Status = amd_comgr_destroy_data_set(DataSetBc);
92+
checkError(Status, "amd_comgr_destroy_data_set");
93+
Status = amd_comgr_destroy_action_info(DataAction);
94+
checkError(Status, "amd_comgr_destroy_action_info");
95+
free(BufSource1);
96+
}

0 commit comments

Comments
 (0)