Skip to content

Commit

Permalink
Merge pull request #1202 from checkedc/merge-fork
Browse files Browse the repository at this point in the history
Microsoft transferred ownership of the Checked C clang repo to the Checked C GitHub organization on August 19th, 2024. This PR merges our fork of Checked C clang into the original Checked C clang repo.

Our fork is currently in https://github.com/checkedc/checkedc-llvm-project.  This brings all the changes in from that repo.

The big changes in this merge include
- Add the latest version of the 3C tool for converting C code to Checked C.  This includes all the changes from [CorrectComputation)](https://github.com/correctcomputation).
- Compiler support for [eraseable syntax](https://github.com/checkedc/checkedc/wiki/Proposed-extension-changes-to-improve-backward-compatibility).  With this syntax, Checked C can be compiled by compilers that don't understand the Checked C extension.   Of course, with those compilers, you don't get the runtime checking from Checked C.    We need to [better document](checkedc/checkedc#499) the syntax.
- Compiler bug fixes to support a [research project](https://machiry.github.io/research/) at Purdue University investigating applying Checked C to UEFI.
  • Loading branch information
dtarditi authored Sep 1, 2024
2 parents a620b39 + ef9ecb9 commit 74ec828
Show file tree
Hide file tree
Showing 286 changed files with 10,315 additions and 4,520 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main-branch-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
env:
LLVMBOT_TOKEN: ${{ secrets.LLVMBOT_MAIN_SYNC }}
run: |
git push https://[email protected]/${{ github.repository }} HEAD:master
git push https://[email protected]/${{ github.repository }} HEAD:master
94 changes: 28 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# The Checked C clang repo

This repo contains a version of the LLVM/Clang toolchain that is being modified
to support Checked C. Checked C extends C with checking to detect or prevent
common programming errors such as out-of-bounds memory accesses. The Checked
C specification is available at the
[Checked C repo](https://github.com/Microsoft/checkedc).
This repo contains a version of the LLVM/Clang toolchain that has been modified to support Checked C.
Checked C extends C with bounds checking and improved type-safety.
The Checked C specification is available at the
[Checked C repo release page](https://github.com/checkedc/checkedc/releases).

<!---
## Announcements
### Source code update
Expand All @@ -15,49 +15,19 @@ specifically [this](https://github.com/llvm/llvm-project/commit/2e10b7a39b930ef8
On Feb 18, 2020 we updated the checkedc-clang sources to upstream release_90,
specifically [this](https://github.com/llvm/llvm-project/commit/c89a3d78f43d81b9cff7b9248772ddf14d21b749) commit.

### Transition to monorepo

Early in 2019, the LLVM community
[transitioned](https://forums.swift.org/t/llvm-monorepo-transition/25689) to
"monorepo".

We moved Checked C to a monorepo on Oct 30, 2019. This has resulted in the following changes:

1. [checkedc-llvm](https://github.com/Microsoft/checkedc-llvm) and
[checkedc-clang](https://github.com/Microsoft/checkedc-clang) (as well as other
LLVM subprojects) are now tracked via a single git repo.

2. The [checkedc-llvm](https://github.com/Microsoft/checkedc-llvm) repo will
no longer be maintained. The
[checkedc-clang](https://github.com/Microsoft/checkedc-clang) repo will be the
new monorepo.

3. There will be no changes to the
[checkedc](https://github.com/Microsoft/checkedc) repo. It will continue to be
a separate git repo.

4. All future patches should be based off this new monorepo.

5. You can use
[this](https://github.com/microsoft/checkedc-clang/blob/master/clang/automation/UNIX/cherry-pick-to-monorepo.sh)
script to cherry-pick your existing patches to the new monorepo.

6. Make sure to set the following CMake flag to enable clang in your builds:
`-DLLVM_ENABLE_PROJECTS=clang`
--->

## Trying out Checked C

Programmers are welcome to use Checked C as it is being implemented. We have
pre-built compiler installers for Windows available for download on the
[release page](https://github.com/Microsoft/checkedc-clang/releases). For
other platforms, you will have to build your own copy of the compiler. For
directions on how to do this, see the [Checked C clang
wiki](https://github.com/Microsoft/checkedc-clang/wiki). The compiler user
manual is
[here](https://github.com/Microsoft/checkedc-clang/wiki/Checked-C-clang-user-manual).
For more information on Checked C and pointers to example code, see our
[Wiki](https://github.com/Microsoft/checkedc/wiki).
You can install the Checked C compiler and the 3C tool
from the [Checked C LLVM Project releases page](https://github.com/checkedc/checkedc-llvm-project/releases).
There are versions available for Ubuntu 22.04, Windows 10/11, and MacOS.
The compiler user
manual is [here](https://github.com/checkedc/checkedc-llvm-project/wiki/Checked-C-clang-user-manual).
For more information on Checked C and pointers to example code, see the
[Checked C wiki](https://github.com/checkedc/checkedc/wiki).
If you want to build your own copy of the compiler, see the directions on the
[Checked C LLVM Project wiki](https://github.com/checkedc/checkedc-llvm-project/wiki).

You can use `clangd` built from this repository to get similar IDE support for
editing Checked C code as upstream `clangd` provides for C code. For example,
Expand All @@ -79,37 +49,29 @@ conversion of C code to Checked C. Quick documentation links:

## More information

For more information on the Checked C clang compiler, see the [Checked C clang
wiki](https://github.com/Microsoft/checkedc-clang/wiki).
For more information on the Checked C clang compiler, see the [Checked C LLVM Project
wiki](https://github.com/checkedc/checkedc-llvm-project/wiki).

## Automated testing status

[![Checked C Clang CI [Linux]](https://github.com/checkedc/workflows/actions/workflows/check-clang-linux.yml/badge.svg)](https://github.com/checkedc/workflows/actions/workflows/check-clang-linux.yml)

## Build Status
[![Checked C Clang CI [MacOS]](https://github.com/checkedc/workflows/actions/workflows/checkedc-clang-macos.yml/badge.svg)](https://github.com/checkedc/workflows/actions/workflows/checkedc-clang-macos.yml)

| Configuration | Testing | Status |
| ----------------- | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| Debug X86 Windows | Checked C and clang regression tests | ![Debug X86 Windows status](https://msresearch.visualstudio.com/_apis/public/build/definitions/f6454e27-a46c-49d9-8453-29d89d53d2f9/211/badge) |
| Debug X64 Windows | Checked C and clang regression tests | ![Debug X64 Windows status](https://msresearch.visualstudio.com/_apis/public/build/definitions/f6454e27-a46c-49d9-8453-29d89d53d2f9/205/badge) |
| Debug X64 Linux | Checked C and clang regression tests | ![Debug X64 Linux status](https://msresearch.visualstudio.com/_apis/public/build/definitions/f6454e27-a46c-49d9-8453-29d89d53d2f9/217/badge) |
| Debug X64 Linux | 3C (Checked-C-Convert tool) nightly tests | ![Nightly Sanity Tests](https://github.com/correctcomputation/checkedc-clang/workflows/Nightly%20Sanity%20Tests/badge.svg?branch=master) |
| Release X64 Linux | Checked C, clang, and LLVM nightly tests | ![Release X64 Linux status](https://msresearch.visualstudio.com/_apis/public/build/definitions/f6454e27-a46c-49d9-8453-29d89d53d2f9/238/badge) |
[![Checked C Clang CI [WINDOWS]](https://github.com/checkedc/workflows/actions/workflows/check-clang-windows.yml/badge.svg)](https://github.com/checkedc/workflows/actions/workflows/check-clang-windows.yml)

## Contributing

We welcome contributions to the Checked C project. To get involved in the
project, see [Contributing to Checked
C](https://github.com/Microsoft/checkedc/blob/master/CONTRIBUTING.md). We
have a wish list of possible projects there.
C](https://github.com/checkedc/checkedc/blob/main/CONTRIBUTING.md).

For code contributions, we follow the standard [Github
workflow](https://guides.github.com/introduction/flow/). See [Contributing to
Checked C](https://github.com/Microsoft/checkedc/blob/master/CONTRIBUTING.md)
for more detail. You will need to sign a contributor license agreement before
contributing code.
Checked C](https://github.com/checkedc/checkedc/blob/main/CONTRIBUTING.md)
for more detail.

## Code of conduct

This project has adopted the [Microsoft Open Source Code of
Conduct](https://opensource.microsoft.com/codeofconduct/). For more
information see the [Code of Conduct
FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact
[[email protected]](mailto:[email protected]) with any additional
questions or comments.
This project has adopted a
[code of conduct](https://github.com/checkedc/checkedc/blob/main/CODE_OF_CONDUCT.md).
76 changes: 76 additions & 0 deletions clang-tools-extra/clangd/3CCommands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//=--3CCommands.cpp-----------------------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Implementation of 3C command helper methods.
//===----------------------------------------------------------------------===//

#ifdef LSP3C
#include "3CCommands.h"
#include "support/Logger.h"

namespace clang {
namespace clangd {

#define _3CSOURCE "3C_RealWild"

static bool GetPtrIDFromDiagMessage(const Diagnostic &DiagMsg,
unsigned long &PtrId) {
if (DiagMsg.source==_3CSOURCE) {
PtrId = atoi(DiagMsg.code.c_str());
return true;
}
return false;
}

void AsCCCommands(const Diagnostic &D, std::vector<Command> &OutCommands) {
unsigned long PtrId;
if (GetPtrIDFromDiagMessage(D, PtrId)) {
Command AllPtrsCmd;
_3CManualFix PtrFix;
PtrFix.ptrID = PtrId;
AllPtrsCmd._3CFix = PtrFix;
Command SinglePtrCmd = AllPtrsCmd;

AllPtrsCmd.command = std::string(Command::_3C_APPLY_FOR_ALL);
AllPtrsCmd.title = "Make this pointer non-WILD and apply the "
"same observation to all the pointers.";

OutCommands.push_back(AllPtrsCmd);

SinglePtrCmd.command = std::string(Command::_3C_APPLY_ONLY_FOR_THIS);
SinglePtrCmd.title = "Make ONLY this pointer non-WILD.";

OutCommands.push_back(SinglePtrCmd);
}
}

bool Is3CCommand(const ExecuteCommandParams &Params) {
return (Params.command == Command::_3C_APPLY_ONLY_FOR_THIS ||
Params.command == Command::_3C_APPLY_FOR_ALL);
}

bool ExecuteCCCommand(const ExecuteCommandParams &Params,
std::string &ReplyMessage,
_3CInterface &CcInterface) {
ReplyMessage = "Checked C Pointer Modified.";
if (Params.command == Command::_3C_APPLY_ONLY_FOR_THIS) {
int PtrId = Params._3CFix->ptrID;
CcInterface.makeSinglePtrNonWild(PtrId);
log("Single Pointer Wild.");
return true;
}
if (Params.command == Command::_3C_APPLY_FOR_ALL){
int PtrId = Params._3CFix->ptrID;
CcInterface.invalidateWildReasonGlobally(PtrId);
log("Global Pointer Wild.");
return true;
}
return false;
}
}
}
#endif
33 changes: 33 additions & 0 deletions clang-tools-extra/clangd/3CCommands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//=--3CCommands.h-------------------------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Helper methods used to handle 3C commands.
//===----------------------------------------------------------------------===//

#ifdef LSP3C
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CCOMMANDS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CCOMMANDS_H

#include "Protocol.h"
#include "clang/3C/3C.h"

namespace clang {
namespace clangd {
// Convert the provided Diagnostic into Commands
void AsCCCommands(const Diagnostic &D, std::vector<Command> &OutCommands);
// Check if the execute command request from the client is a 3C command.
bool Is3CCommand(const ExecuteCommandParams &Params);

// Interpret the provided execute command request as 3C command
// and execute them.
bool ExecuteCCCommand(const ExecuteCommandParams &Params,
std::string &ReplyMessage,
_3CInterface &CcInterface);
}
}
#endif //LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CCOMMANDS_H
#endif
87 changes: 87 additions & 0 deletions clang-tools-extra/clangd/3CDiagnostics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//===--- 3CDiagnostics.cpp - 3C Diagnostics Functions -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifdef LSP3C
#include "3CDiagnostics.h"
#include "support/Logger.h"
#define DEF_PTR_SIZE 5

namespace clang {
namespace clangd {

void _3CDiagnostics::ClearAllDiags() {
std::lock_guard<std::mutex> Lock(DiagMutex);
AllFileDiagnostics.clear();
}

static bool IsValidSourceFile(ConstraintsInfo &CCRes, std::string &FilePath) {
return CCRes.ValidSourceFiles.find(FilePath) != CCRes.ValidSourceFiles.end();
}

bool _3CDiagnostics::PopulateDiagsFromConstraintsInfo(ConstraintsInfo &Line) {
std::lock_guard<std::mutex> Lock(DiagMutex);
std::set<ConstraintKey> ProcessedCKeys;
ProcessedCKeys.clear();
int i=0;
auto GetLocRange = [](uint32_t Line, uint32_t ColNoS,
uint32_t ColNoE) -> Range {
Range nRange;
Line--;
nRange.start.line = Line;
nRange.end.line = Line;
nRange.start.character = ColNoS;
if (ColNoE > 0) {
nRange.end.character = ColNoE;
} else {
nRange.end.character = ColNoS + DEF_PTR_SIZE;
}
return nRange;
};


for (auto &WReason : Line.RootWildAtomsWithReason) {
if (Line.AtomSourceMap.find(WReason.first) != Line.AtomSourceMap.end()) {
auto PsInfo = Line.AtomSourceMap[WReason.first];
std::string FilePath = PsInfo.getFileName();
// If this is not a file in a project? Then ignore.
if (!IsValidSourceFile(Line, FilePath))
continue;

ProcessedCKeys.insert(WReason.first);

Diag NewDiag;
NewDiag.Range = GetLocRange(PsInfo.getLineNo(), PsInfo.getColSNo()-1,
PsInfo.getColENo());
NewDiag.Source = Diag::Main3C;
NewDiag.Severity = DiagnosticsEngine::Level::Error;
NewDiag.code = std::to_string(WReason.first);
NewDiag.Message =
"Pointer is wild because of :" + WReason.second.getReason();

// Create notes for the information about root cause.
PersistentSourceLoc SL = WReason.second.getLocation();
if (SL.valid()) {
Note DiagNote;
DiagNote.AbsFile = SL.getFileName();
DiagNote.Range =
GetLocRange(SL.getLineNo(), SL.getColSNo(), SL.getColENo());
DiagNote.Message = WReason.second.getReason();
NewDiag.Notes.push_back(DiagNote);
}
AllFileDiagnostics[FilePath].push_back(NewDiag);
i++;

}
}
log(std::to_string(i).c_str());

return true;
}
}
}
#endif
48 changes: 48 additions & 0 deletions clang-tools-extra/clangd/3CDiagnostics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//=--3CDiagnostics.h----------------------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Class that handles 3C Diagnostic messages.
//===----------------------------------------------------------------------===//

#ifdef LSP3C
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CDIAGNOSTICS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_3CDIAGNOSTICS_H

#include <set>
#include "Diagnostics.h"
#include "clang/3C/3C.h"

namespace clang {
namespace clangd {

// Class that represents diagnostics messages specific to 3C.
class _3CDiagnostics {
public:
std::mutex DiagMutex;

// GUARDED by DiagMutex
// Populate diagnostics from the given disjoint set.
bool PopulateDiagsFromConstraintsInfo(ConstraintsInfo &Line);
// GUARDED by DiagMutex
// Clear diagnostics of all files.
void ClearAllDiags();
std::map<std::string, std::vector<Diag>> &GetAllFilesDiagnostics() {
return AllFileDiagnostics;
}
std::vector<Diag> &Get3CDiagsForThisFile(std::string FileName){
return AllFileDiagnostics[FileName];
}
private:
// Diagnostics of all files.
std::map<std::string, std::vector<Diag>> AllFileDiagnostics;


};
}
}
#endif //LLVM_CLANG_TOOLS_EXTRA_CLANGD_
#endif
Loading

0 comments on commit 74ec828

Please sign in to comment.